From 0f315e945d5fabe0c805ccf30fa6cf02fac6c0b5 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Tue, 12 Feb 2019 09:46:09 -0800 Subject: [PATCH] Simplify cluster-name query using Get (#744) Signed-off-by: Vince Prignano --- Gopkg.lock | 1 - pkg/controller/machine/BUILD.bazel | 1 - pkg/controller/machine/controller.go | 26 ++++++-------------------- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 770b13d52c35..08029cbaece2 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -985,7 +985,6 @@ "k8s.io/apimachinery/pkg/apis/meta/v1", "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured", "k8s.io/apimachinery/pkg/apis/meta/v1/validation", - "k8s.io/apimachinery/pkg/fields", "k8s.io/apimachinery/pkg/labels", "k8s.io/apimachinery/pkg/runtime", "k8s.io/apimachinery/pkg/runtime/schema", diff --git a/pkg/controller/machine/BUILD.bazel b/pkg/controller/machine/BUILD.bazel index 8984f0c53a0d..2266261c0d27 100644 --- a/pkg/controller/machine/BUILD.bazel +++ b/pkg/controller/machine/BUILD.bazel @@ -15,7 +15,6 @@ go_library( "//pkg/util:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", - "//vendor/k8s.io/apimachinery/pkg/fields:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", "//vendor/k8s.io/klog:go_default_library", "//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library", diff --git a/pkg/controller/machine/controller.go b/pkg/controller/machine/controller.go index eb408c8413a7..4355a3264f11 100644 --- a/pkg/controller/machine/controller.go +++ b/pkg/controller/machine/controller.go @@ -18,11 +18,8 @@ package machine import ( "context" - "errors" "os" - "k8s.io/apimachinery/pkg/fields" - corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -58,10 +55,6 @@ func newReconciler(mgr manager.Manager, actuator Actuator) reconcile.Reconciler actuator: actuator, } - mgr.GetFieldIndexer().IndexField(&clusterv1.Cluster{}, "metadata.name", func(obj runtime.Object) []string { - return []string{obj.(*clusterv1.Cluster).Name} - }) - if r.nodeName == "" { klog.Warningf("environment variable %v is not set, this controller will not protect against deleting its own machine", NodeNameEnvVar) } @@ -206,24 +199,17 @@ func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Ma return nil, nil } - clusterList := clusterv1.ClusterList{} - listOptions := &client.ListOptions{ - FieldSelector: fields.OneTermEqualSelector("metadata.name", machine.Labels[MachineClusterLabelName]), - Namespace: machine.Namespace, + cluster := &clusterv1.Cluster{} + key := client.ObjectKey{ + Namespace: machine.Namespace, + Name: machine.Labels[MachineClusterLabelName], } - if err := r.Client.List(ctx, listOptions, &clusterList); err != nil { + if err := r.Client.Get(ctx, key, cluster); err != nil { return nil, err } - switch len(clusterList.Items) { - case 0: - return nil, errors.New("no clusters defined") - case 1: - return &clusterList.Items[0], nil - default: - return nil, errors.New("multiple clusters defined") - } + return cluster, nil } func (r *ReconcileMachine) isDeleteAllowed(machine *clusterv1.Machine) bool {