Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cluster_check to use interface if available #353

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions controller/cluster_check.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
package controller

import (
"os"
"reflect"
"strings"

"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)

type ObjectClusterName interface {
ObjClusterName() string
}

func ObjectInCluster(cluster string, obj interface{}) bool {
// Check if the object implements the interface, this is best case and
// what objects should strive to be
if o, ok := obj.(ObjectClusterName); ok {
return o.ObjClusterName() == cluster
}

// For types outside of rancher, attempt to check the anno, then use the namespace
// This is much better than using the reflect hole below
switch v := obj.(type) {
case *corev1.Secret:
if c, ok := v.Annotations["field.cattle.io/projectId"]; ok {
if parts := strings.SplitN(c, ":", 2); len(parts) == 2 {
return cluster == parts[0]
}
}
return v.Namespace == cluster
case *corev1.Namespace:
if c, ok := v.Annotations["field.cattle.io/projectId"]; ok {
if parts := strings.SplitN(c, ":", 2); len(parts) == 2 {
return cluster == parts[0]
}
}
return v.Namespace == cluster
case *corev1.Node:
if c, ok := v.Annotations["field.cattle.io/projectId"]; ok {
if parts := strings.SplitN(c, ":", 2); len(parts) == 2 {
return cluster == parts[0]
}
}
return v.Namespace == cluster
}

// Seeing this message means something needs to be done with the type, see comments above
if dm := os.Getenv("CATTLE_DEV_MODE"); dm != "" {
logrus.Errorf("ObjectClusterName not implemented by type %T", obj)
}

var clusterName string

if c := getValue(obj, "ClusterName"); c.IsValid() {
clusterName = c.String()
}
Expand Down