Skip to content

Commit

Permalink
Simplify cluster-name query using Get (kubernetes-sigs#744)
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri authored and k8s-ci-robot committed Feb 12, 2019
1 parent 4b4ec3b commit 0f315e9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
1 change: 0 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/controller/machine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 6 additions & 20 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0f315e9

Please sign in to comment.