Skip to content

Commit f5b13c2

Browse files
committed
refactor: reuse deleteStatusAndTidyMetadata in cleanMetadataForPatch
- Refactor cleanMetadataForPatch to call deleteStatusAndTidyMetadata to avoid code duplication and ensure consistent metadata handling - Add TestCreatePatchForCRD test case to cover the CRD type branch (apiextv1.CustomResourceDefinition path) Signed-off-by: yxxhero <aiopsclub@163.com>
1 parent 5d14d97 commit f5b13c2

2 files changed

Lines changed: 102 additions & 25 deletions

File tree

manifest/generate.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -244,33 +244,10 @@ func isPatchEmpty(patch []byte) bool {
244244
}
245245

246246
func cleanMetadataForPatch(data []byte) ([]byte, error) {
247-
var objMap map[string]interface{}
248-
if err := json.Unmarshal(data, &objMap); err != nil {
247+
objMap, err := deleteStatusAndTidyMetadata(data)
248+
if err != nil {
249249
return nil, err
250250
}
251-
252-
delete(objMap, "status")
253-
254-
if metadata, ok := objMap["metadata"].(map[string]interface{}); ok {
255-
delete(metadata, "managedFields")
256-
delete(metadata, "generation")
257-
delete(metadata, "creationTimestamp")
258-
delete(metadata, "resourceVersion")
259-
delete(metadata, "uid")
260-
261-
// Remove helm-related and k8s annotations that shouldn't be compared
262-
if a := metadata["annotations"]; a != nil {
263-
annotations := a.(map[string]interface{})
264-
delete(annotations, "meta.helm.sh/release-name")
265-
delete(annotations, "meta.helm.sh/release-namespace")
266-
delete(annotations, "deployment.kubernetes.io/revision")
267-
268-
if len(annotations) == 0 {
269-
delete(metadata, "annotations")
270-
}
271-
}
272-
}
273-
274251
return json.Marshal(objMap)
275252
}
276253

manifest/generate_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7+
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
78
"k8s.io/apimachinery/pkg/api/meta"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
911
"k8s.io/apimachinery/pkg/runtime"
1012
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -318,3 +320,101 @@ func TestCreatePatchForUnstructured(t *testing.T) {
318320
})
319321
}
320322
}
323+
324+
// TestCreatePatchForCRD tests the three-way merge implementation for CRD objects.
325+
// This ensures the isCRD branch in createPatch is properly covered.
326+
func TestCreatePatchForCRD(t *testing.T) {
327+
originalCRD := &apiextv1.CustomResourceDefinition{
328+
TypeMeta: metav1.TypeMeta{
329+
APIVersion: "apiextensions.k8s.io/v1",
330+
Kind: "CustomResourceDefinition",
331+
},
332+
ObjectMeta: metav1.ObjectMeta{
333+
Name: "crds.example.com",
334+
},
335+
Spec: apiextv1.CustomResourceDefinitionSpec{
336+
Group: "example.com",
337+
Names: apiextv1.CustomResourceDefinitionNames{
338+
Plural: "crds",
339+
Singular: "crd",
340+
Kind: "Crd",
341+
},
342+
Versions: []apiextv1.CustomResourceDefinitionVersion{
343+
{
344+
Name: "v1",
345+
Served: true,
346+
Storage: true,
347+
},
348+
},
349+
},
350+
}
351+
352+
currentCRD := &apiextv1.CustomResourceDefinition{
353+
TypeMeta: metav1.TypeMeta{
354+
APIVersion: "apiextensions.k8s.io/v1",
355+
Kind: "CustomResourceDefinition",
356+
},
357+
ObjectMeta: metav1.ObjectMeta{
358+
Name: "crds.example.com",
359+
},
360+
Spec: apiextv1.CustomResourceDefinitionSpec{
361+
Group: "example.com",
362+
Names: apiextv1.CustomResourceDefinitionNames{
363+
Plural: "crds",
364+
Singular: "crd",
365+
Kind: "Crd",
366+
},
367+
Versions: []apiextv1.CustomResourceDefinitionVersion{
368+
{
369+
Name: "v1",
370+
Served: true,
371+
Storage: true,
372+
},
373+
},
374+
},
375+
}
376+
377+
targetCRD := &apiextv1.CustomResourceDefinition{
378+
TypeMeta: metav1.TypeMeta{
379+
APIVersion: "apiextensions.k8s.io/v1",
380+
Kind: "CustomResourceDefinition",
381+
},
382+
ObjectMeta: metav1.ObjectMeta{
383+
Name: "crds.example.com",
384+
},
385+
Spec: apiextv1.CustomResourceDefinitionSpec{
386+
Group: "example.com",
387+
Names: apiextv1.CustomResourceDefinitionNames{
388+
Plural: "crds",
389+
Singular: "crd",
390+
Kind: "Crd",
391+
},
392+
Versions: []apiextv1.CustomResourceDefinitionVersion{
393+
{
394+
Name: "v1",
395+
Served: true,
396+
Storage: true,
397+
},
398+
},
399+
},
400+
}
401+
402+
target := &resource.Info{
403+
Mapping: &meta.RESTMapping{
404+
GroupVersionKind: schema.GroupVersionKind{
405+
Group: "apiextensions.k8s.io",
406+
Version: "v1",
407+
Kind: "CustomResourceDefinition",
408+
},
409+
},
410+
Object: targetCRD,
411+
}
412+
413+
patch, patchType, err := createPatch(originalCRD, currentCRD, target)
414+
require.NoError(t, err, "createPatch should not return an error for CRD")
415+
require.Equal(t, types.MergePatchType, patchType, "patch type should be MergePatchType for CRD objects")
416+
417+
t.Logf("CRD Patch result: %s", string(patch))
418+
419+
require.Equal(t, "{}", string(patch), "CRD with no changes should result in empty patch")
420+
}

0 commit comments

Comments
 (0)