Skip to content

Commit

Permalink
Merge pull request #2502 from sedefsavas/2459
Browse files Browse the repository at this point in the history
🏃 Use controller-runtime's client ObjectKey instead of types.NamespacedName
  • Loading branch information
k8s-ci-robot authored Mar 4, 2020
2 parents aa289e0 + 18b03b9 commit 25a933a
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 174 deletions.
7 changes: 3 additions & 4 deletions bootstrap/kubeadm/controllers/kubeadmconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
Expand Down Expand Up @@ -335,7 +334,7 @@ func (r *KubeadmConfigReconciler) handleClusterNotInitialized(ctx context.Contex
err = certificates.LookupOrGenerate(
ctx,
r.Client,
types.NamespacedName{Name: scope.Cluster.Name, Namespace: scope.Cluster.Namespace},
util.ObjectKey(scope.Cluster),
*metav1.NewControllerRef(scope.Config, bootstrapv1.GroupVersion.WithKind("KubeadmConfig")),
)
if err != nil {
Expand Down Expand Up @@ -379,7 +378,7 @@ func (r *KubeadmConfigReconciler) joinWorker(ctx context.Context, scope *Scope)
err := certificates.Lookup(
ctx,
r.Client,
types.NamespacedName{Name: scope.Cluster.Name, Namespace: scope.Cluster.Namespace},
util.ObjectKey(scope.Cluster),
)
if err != nil {
scope.Error(err, "unable to lookup cluster certificates")
Expand Down Expand Up @@ -452,7 +451,7 @@ func (r *KubeadmConfigReconciler) joinControlplane(ctx context.Context, scope *S
err := certificates.Lookup(
ctx,
r.Client,
types.NamespacedName{Name: scope.Cluster.Name, Namespace: scope.Cluster.Namespace},
util.ObjectKey(scope.Cluster),
)
if err != nil {
scope.Error(err, "unable to lookup cluster certificates")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -50,7 +49,7 @@ var _ = Describe("KubeadmConfigReconciler", func() {
}
By("Calling reconcile should requeue")
result, err := reconciler.Reconcile(ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "my-machine-config",
},
Expand Down
63 changes: 32 additions & 31 deletions bootstrap/kubeadm/controllers/kubeadmconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
kubeadmv1beta1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
fakeremote "sigs.k8s.io/cluster-api/controllers/remote/fake"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/secret"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -115,9 +116,9 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: "default",
Name: "cfg",
NamespacedName: client.ObjectKey{
Name: "default",
Namespace: "cfg",
},
}
result, err := k.Reconcile(request)
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnErrorIfReferencedMachineIsNotFo
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasDataSecretName
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestKubeadmConfigReconciler_Reconcile_MigrateToSecret(t *testing.T) {
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand All @@ -233,7 +234,7 @@ func TestKubeadmConfigReconciler_Reconcile_MigrateToSecret(t *testing.T) {
t.Fatal("did not expect to requeue after")
}

if err := k.Client.Get(context.Background(), types.NamespacedName{Name: config.Name, Namespace: config.Namespace}, config); err != nil {
if err := k.Client.Get(context.Background(), client.ObjectKey{Name: config.Name, Namespace: config.Namespace}, config); err != nil {
t.Fatalf("failed to get KubeadmConfig: %v", err)
}

Expand All @@ -242,7 +243,7 @@ func TestKubeadmConfigReconciler_Reconcile_MigrateToSecret(t *testing.T) {
}

secret := &corev1.Secret{}
if err := k.Client.Get(context.Background(), types.NamespacedName{Namespace: config.Namespace, Name: *config.Status.DataSecretName}, secret); err != nil {
if err := k.Client.Get(context.Background(), client.ObjectKey{Namespace: config.Namespace, Name: *config.Status.DataSecretName}, secret); err != nil {
t.Fatalf("failed to get Secret bootstrap data for KubeadmConfig: %v", err)
}

Expand Down Expand Up @@ -282,7 +283,7 @@ func TestKubeadmConfigReconciler_ReturnEarlyIfClusterInfraNotReady(t *testing.T)
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand Down Expand Up @@ -317,7 +318,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasNoCluster(t *t
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand Down Expand Up @@ -345,7 +346,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfMachineDoesNotHaveAssociat
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand Down Expand Up @@ -375,7 +376,7 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfAssociatedClusterIsNotFoun
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "cfg",
},
Expand Down Expand Up @@ -405,7 +406,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
{
name: "requeue worker when control plane is not yet initialiezd",
request: ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: workerJoinConfig.Namespace,
Name: workerJoinConfig.Name,
},
Expand All @@ -419,7 +420,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueJoiningNodesIfControlPlaneNotI
{
name: "requeue a secondary control plane when the control plane is not yet initialized",
request: ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: controlPlaneJoinConfig.Namespace,
Name: controlPlaneJoinConfig.Name,
},
Expand Down Expand Up @@ -479,7 +480,7 @@ func TestKubeadmConfigReconciler_Reconcile_GenerateCloudConfigData(t *testing.T)
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-init-cfg",
},
Expand Down Expand Up @@ -543,7 +544,7 @@ func TestKubeadmConfigReconciler_Reconcile_ErrorIfJoiningControlPlaneHasInvalidC
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-join-cfg",
},
Expand Down Expand Up @@ -581,7 +582,7 @@ func TestKubeadmConfigReconciler_Reconcile_RequeueIfControlPlaneIsMissingAPIEndp
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "worker-join-cfg",
},
Expand Down Expand Up @@ -658,7 +659,7 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: config.GetNamespace(),
Name: rt.configName,
},
Expand Down Expand Up @@ -748,7 +749,7 @@ func TestReconcileIfJoinNodePoolsAndControlPlaneIsReady(t *testing.T) {
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: config.GetNamespace(),
Name: rt.configName,
},
Expand Down Expand Up @@ -819,7 +820,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
remoteClientGetter: fakeremote.NewClusterClient,
}
request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "worker-join-cfg",
},
Expand All @@ -845,7 +846,7 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
t.Fatal("Expected bootstrap data secret")
}
request = ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-join-cfg",
},
Expand Down Expand Up @@ -891,13 +892,13 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {

for _, req := range []ctrl.Request{
{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "worker-join-cfg",
},
},
{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-join-cfg",
},
Expand Down Expand Up @@ -946,13 +947,13 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {

for _, req := range []ctrl.Request{
{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "worker-join-cfg",
},
},
{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-join-cfg",
},
Expand Down Expand Up @@ -1355,7 +1356,7 @@ func TestKubeadmConfigReconciler_Reconcile_AlwaysCheckCAVerificationUnlessReques

wc := newWorkerJoinKubeadmConfig(workerMachine)
wc.Spec.JoinConfiguration.Discovery.BootstrapToken = tc.discovery
key := types.NamespacedName{Namespace: wc.Namespace, Name: wc.Name}
key := client.ObjectKey{Namespace: wc.Namespace, Name: wc.Name}
if err := myclient.Create(context.Background(), wc); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1438,7 +1439,7 @@ func TestKubeadmConfigReconciler_Reconcile_DoesNotFailIfCASecretsAlreadyExist(t
KubeadmInitLock: &myInitLocker{},
}
req := ctrl.Request{
NamespacedName: types.NamespacedName{Namespace: "default", Name: configName},
NamespacedName: client.ObjectKey{Namespace: "default", Name: configName},
}
if _, err := reconciler.Reconcile(req); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1471,7 +1472,7 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-init-cfg-first",
},
Expand All @@ -1488,7 +1489,7 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
}

request = ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-init-cfg-second",
},
Expand Down Expand Up @@ -1537,7 +1538,7 @@ func TestKubeadmConfigReconciler_Reconcile_DoNotPatchWhenErrorOccurred(t *testin
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "control-plane-init-cfg",
},
Expand Down Expand Up @@ -1753,7 +1754,7 @@ func createSecrets(t *testing.T, cluster *clusterv1.Cluster, config *bootstrapv1
t.Fatal(err)
}
for _, certificate := range certificates {
out = append(out, certificate.AsSecret(types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, *metav1.NewControllerRef(config, bootstrapv1.GroupVersion.WithKind("KubeadmConfig"))))
out = append(out, certificate.AsSecret(util.ObjectKey(cluster), *metav1.NewControllerRef(config, bootstrapv1.GroupVersion.WithKind("KubeadmConfig"))))
}
return out
}
Expand Down
6 changes: 1 addition & 5 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
Expand Down Expand Up @@ -480,9 +479,6 @@ func (r *ClusterReconciler) controlPlaneMachineToCluster(o handler.MapObject) []
}

return []ctrl.Request{{
NamespacedName: types.NamespacedName{
Namespace: cluster.Namespace,
Name: cluster.Name,
},
NamespacedName: util.ObjectKey(cluster),
}}
}
6 changes: 2 additions & 4 deletions controllers/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
"sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -569,10 +570,7 @@ func TestClusterReconciler(t *testing.T) {
},
want: []ctrl.Request{
{
NamespacedName: client.ObjectKey{
Name: cluster.Name,
Namespace: cluster.Namespace,
},
NamespacedName: util.ObjectKey(cluster),
},
},
},
Expand Down
Loading

0 comments on commit 25a933a

Please sign in to comment.