diff --git a/pkg/cmd/generate/assertion_test.go b/pkg/cmd/generate/assertion_test.go index 7a394cd..5d8093e 100644 --- a/pkg/cmd/generate/assertion_test.go +++ b/pkg/cmd/generate/assertion_test.go @@ -141,6 +141,9 @@ func (a *kStructureAssertion) listObjects(dirName, kind string, object runtimecl obj := object.DeepCopyObject() objFile, err := os.ReadFile(path) require.NoError(a.t, err) + assert.True(a.t, strings.HasPrefix(string(objFile), header), "every file generated by cli should container a header") + assert.NotContains(a.t, string(objFile), "creationTimestamp") + assert.NotContains(a.t, string(objFile), "user: {}") err = yaml.Unmarshal(objFile, obj) require.NoError(a.t, err) if obj.GetObjectKind().GroupVersionKind().Kind == kind { diff --git a/pkg/cmd/generate/util.go b/pkg/cmd/generate/util.go index 947f0c6..d28035d 100644 --- a/pkg/cmd/generate/util.go +++ b/pkg/cmd/generate/util.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "sort" "strings" @@ -19,6 +20,8 @@ import ( runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) +var emptyCreationTimestamp = regexp.MustCompile(`\n *creationTimestamp: null`) + type objectsCache map[string]runtimeclient.Object func (c objectsCache) storeObject(ctx *clusterContext, obj runtimeclient.Object) error { @@ -162,8 +165,10 @@ const header = `# -------------------------------------------------------------- func writeFile(filePath string, content []byte) error { // https://github.com/kubernetes/kubernetes/issues/67610 - contentString := strings.ReplaceAll(string(content), "\n creationTimestamp: null", "") + contentString := emptyCreationTimestamp.ReplaceAllString(string(content), "") contentString = strings.ReplaceAll(contentString, "\nuser: {}", "") + // This will only apply to konflux tier template that start with metadata = {} + contentString = strings.ReplaceAll(contentString, "metadata:\n objects:", "metadata: {}\n objects:") contentString = fmt.Sprintf("%s%s", header, contentString) return os.WriteFile(filePath, []byte(contentString), 0600) }