diff --git a/go.mod b/go.mod index e742c93a..aa0dc4d6 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( k8s.io/cli-runtime v0.29.2 k8s.io/client-go v0.29.2 k8s.io/utils v0.0.0-20240102154912-e7106e64919e - sigs.k8s.io/controller-runtime v0.17.1 + sigs.k8s.io/controller-runtime v0.17.2 sigs.k8s.io/yaml v1.4.0 ) diff --git a/go.sum b/go.sum index 6449a4c4..4c570b31 100644 --- a/go.sum +++ b/go.sum @@ -963,8 +963,8 @@ oras.land/oras-go v1.2.4/go.mod h1:DYcGfb3YF1nKjcezfX2SNlDAeQFKSXmf+qrFmrh4324= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= -sigs.k8s.io/controller-runtime v0.17.1 h1:V1dQELMGVk46YVXXQUbTFujU7u4DQj6YUj9Rb6cuzz8= -sigs.k8s.io/controller-runtime v0.17.1/go.mod h1:+MngTvIQQQhfXtwfdGw/UOQ/aIaqsYywfCINOtwMO/s= +sigs.k8s.io/controller-runtime v0.17.2 h1:FwHwD1CTUemg0pW2otk7/U5/i5m2ymzvOXdbeGOUvw0= +sigs.k8s.io/controller-runtime v0.17.2/go.mod h1:+MngTvIQQQhfXtwfdGw/UOQ/aIaqsYywfCINOtwMO/s= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 h1:XX3Ajgzov2RKUdc5jW3t5jwY7Bo7dcRm+tFxT+NfgY0= diff --git a/pkg/k8s/client_test.go b/pkg/k8s/client_test.go index 7ee0538e..ac21af23 100644 --- a/pkg/k8s/client_test.go +++ b/pkg/k8s/client_test.go @@ -2,6 +2,7 @@ package k8s import ( "context" + "errors" "testing" "github.com/kyma-project/nats-manager/testutils" @@ -18,6 +19,8 @@ import ( const testFieldManager = "nats-manager" +var errPatchNotAllowed = errors.New("apply patches are not supported in the fake client") + func Test_GetStatefulSet(t *testing.T) { t.Parallel() @@ -180,20 +183,11 @@ func Test_Delete(t *testing.T) { func Test_PatchApply(t *testing.T) { t.Parallel() - // NOTE: In real k8s client, the kubeClient.PatchApply creates the resource - // if it does not exist on the cluster. But in the fake client the behaviour - // is not properly replicated. As mentioned: "ObjectMeta's `Generation` and - // `ResourceVersion` don't behave properly, Patch or Update operations that - // rely on these fields will fail, or give false positives." in docs - // https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client/fake - // This scenario will be tested in integration tests with envTest pkg. - // define test cases testCases := []struct { name string givenStatefulSet *unstructured.Unstructured givenUpdateStatefulSet *unstructured.Unstructured - wantReplicas int }{ { name: "should update resource when exists in k8s", @@ -203,7 +197,6 @@ func Test_PatchApply(t *testing.T) { givenUpdateStatefulSet: testutils.NewNATSStatefulSetUnStruct( testutils.WithSpecReplicas(3), ), - wantReplicas: 3, }, } @@ -226,14 +219,12 @@ func Test_PatchApply(t *testing.T) { err := kubeClient.PatchApply(context.Background(), tc.givenUpdateStatefulSet) // then - require.NoError(t, err) - // check that it should exist on k8s. - gotSTS, err := kubeClient.GetStatefulSet(context.Background(), - tc.givenStatefulSet.GetName(), tc.givenStatefulSet.GetNamespace()) - require.NoError(t, err) - require.Equal(t, tc.givenUpdateStatefulSet.GetName(), gotSTS.Name) - require.Equal(t, tc.givenUpdateStatefulSet.GetNamespace(), gotSTS.Namespace) - require.Equal(t, int32(tc.wantReplicas), *gotSTS.Spec.Replicas) + // NOTE: The kubeClient.PatchApply is not supported in the fake client. + // (https://github.com/kubernetes/kubernetes/issues/115598) + // So in unit test we only check that the client.Patch with client.Apply + // is called or not. + // The real behaviour will be tested in integration tests with envTest pkg. + require.ErrorContains(t, err, errPatchNotAllowed.Error()) }) } }