diff --git a/pkg/controller/instance/instance_controller_test.go b/pkg/controller/instance/instance_controller_test.go index f376d58a2..244e45731 100644 --- a/pkg/controller/instance/instance_controller_test.go +++ b/pkg/controller/instance/instance_controller_test.go @@ -272,17 +272,15 @@ func TestEventFilterForDelete(t *testing.T) { e event.DeleteEvent }{ {"A Pod without annotations", true, event.DeleteEvent{ - Meta: &v1.Pod{}, - Object: nil, + Object: &v1.Pod{}, DeleteStateUnknown: false, }}, {"A Pod with pipePod annotation", false, event.DeleteEvent{ - Meta: &v1.Pod{ + Object: &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{task.PipePodAnnotation: "true"}, }, }, - Object: nil, DeleteStateUnknown: false, }}, } diff --git a/pkg/engine/renderer/dependencies_test.go b/pkg/engine/renderer/dependencies_test.go index 61a350bd0..99fe4a198 100644 --- a/pkg/engine/renderer/dependencies_test.go +++ b/pkg/engine/renderer/dependencies_test.go @@ -45,9 +45,9 @@ func TestGetResources(t *testing.T) { taskObjects []*unstructured.Unstructured client client.Client }{ - {name: "from api server", taskObjects: []*unstructured.Unstructured{}, client: fake.NewFakeClientWithScheme(scheme.Scheme, &cm)}, - {name: "from task objects", taskObjects: []*unstructured.Unstructured{&cmUnstructured}, client: fake.NewFakeClientWithScheme(scheme.Scheme)}, - {name: "from api server without annotation", taskObjects: []*unstructured.Unstructured{}, client: fake.NewFakeClientWithScheme(scheme.Scheme, cmWithoutAnnotation)}, + {name: "from api server", taskObjects: []*unstructured.Unstructured{}, client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(&cm).Build()}, + {name: "from task objects", taskObjects: []*unstructured.Unstructured{&cmUnstructured}, client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()}, + {name: "from api server without annotation", taskObjects: []*unstructured.Unstructured{}, client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(cmWithoutAnnotation).Build()}, } for _, tt := range tests { diff --git a/pkg/engine/renderer/enhancer_test.go b/pkg/engine/renderer/enhancer_test.go index dc3cada7f..ebdb2d1fd 100644 --- a/pkg/engine/renderer/enhancer_test.go +++ b/pkg/engine/renderer/enhancer_test.go @@ -16,6 +16,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/uuid" "k8s.io/kubectl/pkg/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake" "github.com/kudobuilder/kudo/pkg/engine" @@ -25,7 +26,7 @@ import ( func TestEnhancerApply_embeddedMetadataStatefulSet(t *testing.T) { - tpls := []runtime.Object{ + tpls := []client.Object{ statefulSet("sfs1", "default"), } @@ -70,7 +71,7 @@ func TestEnhancerApply_embeddedMetadataStatefulSet(t *testing.T) { func TestEnhancerApply_embeddedMetadataCronjob(t *testing.T) { - tpls := []runtime.Object{ + tpls := []client.Object{ cronjob("cronjob", "default"), } @@ -111,7 +112,7 @@ func TestEnhancerApply_embeddedMetadataCronjob(t *testing.T) { func TestEnhancerApply_noAdditionalMetadata(t *testing.T) { - tpls := []runtime.Object{ + tpls := []client.Object{ pod("pod", "default"), unstructuredCrd("crd", "default"), } @@ -155,7 +156,7 @@ func TestEnhancerApply_noAdditionalMetadata(t *testing.T) { func TestEnhancerApply_dependencyHash_noDependencies(t *testing.T) { ss := statefulSet("statefulset", "default") - tpls := []runtime.Object{ss} + tpls := []client.Object{ss} meta := metadata() meta.PlanUID = uuid.NewUUID() @@ -170,7 +171,7 @@ func TestEnhancerApply_dependencyHash_noDependencies(t *testing.T) { t.Errorf("failed to apply template %s", err) } - ssApplied := funk.Find(objs, func(o runtime.Object) bool { + ssApplied := funk.Find(objs, func(o client.Object) bool { return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"} }) @@ -198,13 +199,13 @@ func TestEnhancerApply_dependencyHash_unavailableResource(t *testing.T) { }, }) - tpls := []runtime.Object{ss} + tpls := []client.Object{ss} meta := metadata() meta.PlanUID = uuid.NewUUID() e := &DefaultEnhancer{ - Client: clientfake.NewFakeClientWithScheme(scheme.Scheme), + Client: clientfake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Scheme: utils.Scheme(), Discovery: fake.CachedDiscoveryClient(), } @@ -214,7 +215,7 @@ func TestEnhancerApply_dependencyHash_unavailableResource(t *testing.T) { t.Errorf("failed to apply template %s", err) } - ssApplied := funk.Find(objs, func(o runtime.Object) bool { + ssApplied := funk.Find(objs, func(o client.Object) bool { return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"} }) @@ -244,7 +245,7 @@ func TestEnhancerApply_dependencyHash_calculatedOnResourceWithoutLastAppliedConf }, }) - tpls := []runtime.Object{ss} + tpls := []client.Object{ss} meta := metadata() meta.PlanUID = uuid.NewUUID() @@ -252,7 +253,7 @@ func TestEnhancerApply_dependencyHash_calculatedOnResourceWithoutLastAppliedConf e := &DefaultEnhancer{ Scheme: utils.Scheme(), Discovery: fake.CachedDiscoveryClient(), - Client: clientfake.NewFakeClientWithScheme(scheme.Scheme, cm), + Client: clientfake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(cm).Build(), } objs, err := e.Apply(tpls, meta) @@ -260,7 +261,7 @@ func TestEnhancerApply_dependencyHash_calculatedOnResourceWithoutLastAppliedConf t.Errorf("failed to apply template %s", err) } - ssApplied := funk.Find(objs, func(o runtime.Object) bool { + ssApplied := funk.Find(objs, func(o client.Object) bool { return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"} }) @@ -287,7 +288,7 @@ func TestEnhancerApply_dependencyHash_changes(t *testing.T) { }, }) - tpls := []runtime.Object{ss, cm} + tpls := []client.Object{ss, cm} meta := metadata() meta.PlanUID = uuid.NewUUID() @@ -302,7 +303,7 @@ func TestEnhancerApply_dependencyHash_changes(t *testing.T) { t.Errorf("failed to apply template %s", err) } - ssApplied := funk.Find(objs, func(o runtime.Object) bool { + ssApplied := funk.Find(objs, func(o client.Object) bool { return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"} }) @@ -316,11 +317,11 @@ func TestEnhancerApply_dependencyHash_changes(t *testing.T) { assert.NotNil(t, hash, "Pod template spec annotations contains no dependency hash field") cm.Data["newkey"] = "newvalue" - tpls = []runtime.Object{ss, cm} + tpls = []client.Object{ss, cm} objs, err = e.Apply(tpls, meta) assert.Nil(t, err) - ssApplied = funk.Find(objs, func(o runtime.Object) bool { + ssApplied = funk.Find(objs, func(o client.Object) bool { return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"} }) @@ -485,7 +486,7 @@ func pod(name string, namespace string) *corev1.Pod { return pod } -func unstructuredCrd(name string, namespace string) runtime.Object { +func unstructuredCrd(name string, namespace string) client.Object { data := `apiVersion: install.istio.io/v1alpha1 kind: IstioOperator metadata: diff --git a/pkg/engine/task/task_apply_test.go b/pkg/engine/task/task_apply_test.go index 02f47733c..3417947a9 100644 --- a/pkg/engine/task/task_apply_test.go +++ b/pkg/engine/task/task_apply_test.go @@ -9,8 +9,8 @@ import ( batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/yaml" @@ -51,7 +51,7 @@ func TestApplyTask_Run(t *testing.T) { done: true, wantErr: false, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: renderer.Metadata{}, @@ -67,7 +67,7 @@ func TestApplyTask_Run(t *testing.T) { wantErr: true, fatal: true, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -84,7 +84,7 @@ func TestApplyTask_Run(t *testing.T) { wantErr: true, fatal: true, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &fatalErrorEnhancer{}, Meta: meta, @@ -101,7 +101,7 @@ func TestApplyTask_Run(t *testing.T) { wantErr: true, fatal: false, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &transientErrorEnhancer{}, Meta: meta, @@ -117,7 +117,7 @@ func TestApplyTask_Run(t *testing.T) { done: true, wantErr: false, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -133,7 +133,7 @@ func TestApplyTask_Run(t *testing.T) { done: false, wantErr: false, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -201,18 +201,18 @@ func resourceAsString(resource metav1.Object) string { type testEnhancer struct{} -func (k *testEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) { +func (k *testEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) { return objs, nil } type fatalErrorEnhancer struct{} -func (k *fatalErrorEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) { +func (k *fatalErrorEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) { return nil, fmt.Errorf("%wsomething fatally bad happens every time", engine.ErrFatalExecution) } type transientErrorEnhancer struct{} -func (k *transientErrorEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) { +func (k *transientErrorEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) { return nil, fmt.Errorf("something transiently bad happens every time") } diff --git a/pkg/engine/task/task_delete_test.go b/pkg/engine/task/task_delete_test.go index b0d8f901c..312405905 100644 --- a/pkg/engine/task/task_delete_test.go +++ b/pkg/engine/task/task_delete_test.go @@ -46,7 +46,7 @@ func TestDeleteTask_Run(t *testing.T) { done: true, wantErr: false, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CustomCachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: renderer.Metadata{}, @@ -62,7 +62,7 @@ func TestDeleteTask_Run(t *testing.T) { wantErr: true, fatal: true, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CustomCachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -79,7 +79,7 @@ func TestDeleteTask_Run(t *testing.T) { wantErr: true, fatal: true, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CustomCachedDiscoveryClient(), Enhancer: &fatalErrorEnhancer{}, Meta: meta, @@ -95,7 +95,7 @@ func TestDeleteTask_Run(t *testing.T) { done: true, wantErr: false, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CustomCachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, diff --git a/pkg/engine/task/task_kudo_operator_test.go b/pkg/engine/task/task_kudo_operator_test.go index 2316c13c8..992267ce3 100644 --- a/pkg/engine/task/task_kudo_operator_test.go +++ b/pkg/engine/task/task_kudo_operator_test.go @@ -50,14 +50,16 @@ func Test_applyInstance(t *testing.T) { name: "creating a brand new instance is successful", new: instance, ns: namespace, - c: fake.NewFakeClientWithScheme(scheme, operatorVersion), + c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(operatorVersion).Build(), wantErr: false, }, { - name: "patching an existing instance with the same spec is successful", - new: instance, - ns: namespace, - c: fake.NewFakeClientWithScheme(scheme, operatorVersion, instance), + name: "patching an existing instance with the same spec is successful", + new: instance, + ns: namespace, + // After https://github.com/kubernetes-sigs/controller-runtime/pull/1306 + // fake client sets resourceVersion on objects passed into it. + c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(operatorVersion, instance.DeepCopy()).Build(), wantErr: false, }, { @@ -67,8 +69,10 @@ func Test_applyInstance(t *testing.T) { c.Spec.Parameters = map[string]string{"foo": "bar"} return c }(), - ns: namespace, - c: fake.NewFakeClientWithScheme(scheme, operatorVersion, instance), + ns: namespace, + // After https://github.com/kubernetes-sigs/controller-runtime/pull/1306 + // fake client sets resourceVersion on objects passed into it. + c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(operatorVersion, instance.DeepCopy()).Build(), wantErr: false, subset: map[string]interface{}{ "spec": map[string]interface{}{ @@ -87,8 +91,10 @@ func Test_applyInstance(t *testing.T) { } return c }(), - ns: namespace, - c: fake.NewFakeClientWithScheme(scheme, instance), + ns: namespace, + // After https://github.com/kubernetes-sigs/controller-runtime/pull/1306 + // fake client sets resourceVersion on objects passed into it. + c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance.DeepCopy()).Build(), wantErr: false, subset: map[string]interface{}{ "spec": map[string]interface{}{ diff --git a/pkg/engine/task/task_toggle_test.go b/pkg/engine/task/task_toggle_test.go index dc4292181..97de57509 100644 --- a/pkg/engine/task/task_toggle_test.go +++ b/pkg/engine/task/task_toggle_test.go @@ -49,7 +49,7 @@ func TestToggleTask_Run(t *testing.T) { wantErr: true, fatal: true, ctx: Context{ - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -69,7 +69,7 @@ func TestToggleTask_Run(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "true", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -89,7 +89,7 @@ func TestToggleTask_Run(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "true", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -145,7 +145,7 @@ func TestToggleTask_intermediateTask(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "true", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: renderer.Metadata{}, @@ -165,7 +165,7 @@ func TestToggleTask_intermediateTask(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "false", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: renderer.Metadata{}, @@ -185,7 +185,7 @@ func TestToggleTask_intermediateTask(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "notABooleanValue", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -205,7 +205,7 @@ func TestToggleTask_intermediateTask(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, @@ -224,7 +224,7 @@ func TestToggleTask_intermediateTask(t *testing.T) { Parameters: map[string]interface{}{ "feature-enabled": "someValue", }, - Client: fake.NewFakeClientWithScheme(scheme.Scheme), + Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(), Discovery: kudofake.CachedDiscoveryClient(), Enhancer: &testEnhancer{}, Meta: meta, diff --git a/pkg/engine/workflow/engine_test.go b/pkg/engine/workflow/engine_test.go index 74784f11f..08eda2b69 100644 --- a/pkg/engine/workflow/engine_test.go +++ b/pkg/engine/workflow/engine_test.go @@ -7,9 +7,10 @@ import ( corev1 "k8s.io/api/core/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/discovery/cached/memory" "k8s.io/client-go/kubernetes/scheme" + + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" kudoapi "github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" @@ -620,7 +621,7 @@ func TestExecutePlan(t *testing.T) { } testScheme := scheme.Scheme - testClient := fake.NewFakeClientWithScheme(scheme.Scheme) + testClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build() fakeDiscovery := kudofake.CachedDiscoveryClient() fakeCachedDiscovery := memory.NewMemCacheClient(fakeDiscovery) for _, tt := range tests { @@ -661,6 +662,6 @@ func instance() *kudoapi.Instance { type testEnhancer struct{} -func (k *testEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) { +func (k *testEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) { return objs, nil } diff --git a/pkg/kubernetes/status/readiness_test.go b/pkg/kubernetes/status/readiness_test.go index 83b670fe4..e2ead5d20 100644 --- a/pkg/kubernetes/status/readiness_test.go +++ b/pkg/kubernetes/status/readiness_test.go @@ -6,8 +6,8 @@ import ( apps "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" kudoapi "github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" @@ -24,15 +24,15 @@ func TestIsReady(t *testing.T) { tests := []struct { name string isReady bool - objs []runtime.Object + objs []client.Object }{ - {"no linked resources, ready", true, []runtime.Object{}}, - {"one ready deployment", true, []runtime.Object{readyDeployment()}}, - {"one not ready deployment", false, []runtime.Object{notReadyDeployment()}}, - {"one ready and one not ready deployment", false, []runtime.Object{readyDeployment(), notReadyDeployment()}}, + {"no linked resources, ready", true, []client.Object{}}, + {"one ready deployment", true, []client.Object{readyDeployment()}}, + {"one not ready deployment", false, []client.Object{notReadyDeployment()}}, + {"one ready and one not ready deployment", false, []client.Object{readyDeployment(), notReadyDeployment()}}, } for _, tt := range tests { - c := fake.NewFakeClientWithScheme(scheme.Scheme) + c := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build() for _, obj := range tt.objs { err := c.Create(context.TODO(), obj) if err != nil { @@ -46,7 +46,7 @@ func TestIsReady(t *testing.T) { } } -func readyDeployment() runtime.Object { +func readyDeployment() client.Object { var replicas int32 = 2 return &apps.Deployment{ ObjectMeta: metav1.ObjectMeta{ @@ -69,7 +69,7 @@ func readyDeployment() runtime.Object { } } -func notReadyDeployment() runtime.Object { +func notReadyDeployment() client.Object { var replicas int32 = 2 return &apps.Deployment{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/kudoctl/cmd/init_integration_test.go b/pkg/kudoctl/cmd/init_integration_test.go index c92e90b00..a37f19800 100644 --- a/pkg/kudoctl/cmd/init_integration_test.go +++ b/pkg/kudoctl/cmd/init_integration_test.go @@ -332,7 +332,8 @@ func TestInitWithServiceAccount(t *testing.T) { if rbNamespace == "" { rbNamespace = namespace } - crb := testutils.NewClusterRoleBinding("rbac.authorization.k8s.io/v1", "ClusterRoleBinding", "kudo-clusterrole-binding", rbNamespace, tt.serviceAccount, tt.roleBindingRole) + crb, converted := testutils.NewClusterRoleBinding("rbac.authorization.k8s.io/v1", "ClusterRoleBinding", "kudo-clusterrole-binding", rbNamespace, tt.serviceAccount, tt.roleBindingRole).(client.Object) + assert.True(t, converted) assert.NoError(t, testClient.Create(context.TODO(), crb)) defer func() { assert.NoError(t, testClient.Delete(context.TODO(), crb)) @@ -424,31 +425,33 @@ func TestReInitFails(t *testing.T) { assertStringContains(t, "CRD operators.kudo.dev is already installed. Did you mean to use --upgrade?", errBuf.String()) } -func deleteObjects(objs []runtime.Object, client *testutils.RetryClient) error { +func deleteObjects(objs []client.Object, client *testutils.RetryClient) error { + runtimeObjs := []runtime.Object{} for _, obj := range objs { if err := client.Delete(context.TODO(), obj); err != nil { return err } + runtimeObjs = append(runtimeObjs, obj) } - return testutils.WaitForDelete(client, objs) + return testutils.WaitForDelete(client, runtimeObjs) } -func deleteInitPrereqs(cmd *initCmd, client *testutils.RetryClient) error { +func deleteInitPrereqs(cmd *initCmd, cl *testutils.RetryClient) error { opts := kudoinit.NewOptions(cmd.version, cmd.ns, cmd.serviceAccount, cmd.upgrade, cmd.selfSignedWebhookCA) - objs := append([]runtime.Object{}, prereq.NewWebHookInitializer(opts).Resources()...) + objs := append([]client.Object{}, prereq.NewWebHookInitializer(opts).Resources()...) objs = append(objs, prereq.NewServiceAccountInitializer(opts).Resources()...) objs = append(objs, crd.NewInitializer().Resources()...) // Namespaced resources aren't waited on after deletion because they aren't GC'ed in this test environment. for _, ns := range prereq.NewNamespaceInitializer(opts).Resources() { - if err := client.Delete(context.TODO(), ns); err != nil { + if err := cl.Delete(context.TODO(), ns); err != nil { return err } } - return deleteObjects(objs, client) + return deleteObjects(objs, cl) } func getKubeClient(t *testing.T) *kube.Client { diff --git a/pkg/kudoctl/cmd/init_test.go b/pkg/kudoctl/cmd/init_test.go index 92317e42a..23dd4c1db 100644 --- a/pkg/kudoctl/cmd/init_test.go +++ b/pkg/kudoctl/cmd/init_test.go @@ -44,7 +44,7 @@ type fakeClient struct { func newFakeClient(objs ...runtime.Object) fakeClient { fc := fake.NewSimpleClientset(objs...) fc2 := apiextfake.NewSimpleClientset() - cc := fake2.NewFakeClientWithScheme(scheme.Scheme, objs...) + cc := fake2.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(objs...).Build() return fakeClient{ client: &kube.Client{ diff --git a/pkg/webhook/instance_admission_integration_test.go b/pkg/webhook/instance_admission_integration_test.go index 51b8b7017..3b90cbe2c 100644 --- a/pkg/webhook/instance_admission_integration_test.go +++ b/pkg/webhook/instance_admission_integration_test.go @@ -12,7 +12,6 @@ import ( . "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/uuid" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" @@ -43,7 +42,7 @@ var _ = BeforeSuite(func() { iaw := prereq.InstanceAdmissionWebhook(v1.NamespaceDefault) instanceAdmissionWebhookPath = *iaw.Webhooks[0].ClientConfig.Service.Path - env.WebhookInstallOptions = envtest.WebhookInstallOptions{MutatingWebhooks: []runtime.Object{&iaw}} + env.WebhookInstallOptions = envtest.WebhookInstallOptions{MutatingWebhooks: []client.Object{&iaw}} // 2. add KUDO CRDs log.Print("test.BeforeSuite: initializing CRDs") @@ -62,7 +61,7 @@ var _ = Describe("Test", func() { var defaultTimeout float64 = 5 // seconds var c client.Client - var stop chan struct{} + var stop context.CancelFunc // every test case gets it's own manager and client instances. not sure if that's the best // practice but it seems to be fast enough. @@ -94,15 +93,16 @@ var _ = Describe("Test", func() { server.Register(instanceAdmissionWebhookPath, &ctrhook.Admission{Handler: &InstanceAdmission{client: c}}) // 5. starting the manager - stop = make(chan struct{}) + var ctx context.Context + ctx, stop = context.WithCancel(context.Background()) go func() { - err = mgr.Start(stop) + err = mgr.Start(ctx) Expect(err).NotTo(HaveOccurred()) }() }) AfterEach(func() { - close(stop) + stop() }) deploy := kudoapi.DeployPlanName @@ -157,7 +157,7 @@ var _ = Describe("Test", func() { It("should schedule deploy plan when an instance is created", func() { // 1. admission controller should schedule 'deploy' plan and add cleanup finalizer for new instances - key = keyFrom(idle) + key = client.ObjectKeyFromObject(idle) i := instanceWith(c, key) Expect(i.Spec.PlanExecution.PlanName).Should(Equal(deploy)) @@ -289,14 +289,6 @@ var _ = Describe("Test", func() { }) }) -// keyFrom method is a small helper method to retrieve an ObjectKey from the given object. Meant to be used within this test class -// only as it will fail the test should an error occur. -func keyFrom(obj runtime.Object) client.ObjectKey { - key, err := client.ObjectKeyFromObject(obj) - Expect(err).NotTo(HaveOccurred()) - return key -} - // instanceWith method is a small helper method to retrieve an Instance with the give key. Meant to be used within this test class // only as it will fail the test should an error occur. func instanceWith(c client.Client, key client.ObjectKey) *kudoapi.Instance {