From 062b49662aa40bf7a8df1b6f54afeb76f62f483f Mon Sep 17 00:00:00 2001 From: David Peraza Date: Mon, 15 Jul 2024 18:56:41 -0400 Subject: [PATCH 1/4] Removing creationTimestamp under Template object Signed-off-by: David Peraza --- pkg/cmd/generate/util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/generate/util.go b/pkg/cmd/generate/util.go index 947f0c6..e14b493 100644 --- a/pkg/cmd/generate/util.go +++ b/pkg/cmd/generate/util.go @@ -163,6 +163,7 @@ 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 = strings.ReplaceAll(contentString, "\n creationTimestamp: null", " {}") contentString = strings.ReplaceAll(contentString, "\nuser: {}", "") contentString = fmt.Sprintf("%s%s", header, contentString) return os.WriteFile(filePath, []byte(contentString), 0600) From a495af3ab806f6aca03f18ad2d6d03cbdb54bc10 Mon Sep 17 00:00:00 2001 From: David Peraza Date: Fri, 9 Aug 2024 12:30:15 -0400 Subject: [PATCH 2/4] When template.metadata is empty string switch to empty object Also changed the replacement of creationTimestamp at any level with regex as suggested in reviews by @MatousJobanek Signed-off-by: David Peraza --- pkg/cmd/generate/util.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/generate/util.go b/pkg/cmd/generate/util.go index e14b493..9847625 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" @@ -162,9 +163,11 @@ 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 = strings.ReplaceAll(contentString, "\n creationTimestamp: null", " {}") + emptyCreationTimestamp := regexp.MustCompile(`\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) } From 156189f303a762ce7c366562bdad26448e7daad8 Mon Sep 17 00:00:00 2001 From: David Peraza Date: Wed, 9 Oct 2024 08:50:14 -0400 Subject: [PATCH 3/4] Moving creationTimestamp regex as a top level var Signed-off-by: David Peraza --- pkg/cmd/generate/util.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/generate/util.go b/pkg/cmd/generate/util.go index 9847625..d28035d 100644 --- a/pkg/cmd/generate/util.go +++ b/pkg/cmd/generate/util.go @@ -20,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 { @@ -163,7 +165,6 @@ const header = `# -------------------------------------------------------------- func writeFile(filePath string, content []byte) error { // https://github.com/kubernetes/kubernetes/issues/67610 - emptyCreationTimestamp := regexp.MustCompile(`\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 = {} From fd122c30d9657b29ce763f57ecd2c401ffa24590 Mon Sep 17 00:00:00 2001 From: David Peraza Date: Thu, 10 Oct 2024 12:25:45 -0400 Subject: [PATCH 4/4] Added asserts to listObjects for more test coverage on creationTimestamp Signed-off-by: David Peraza --- pkg/cmd/generate/assertion_test.go | 3 +++ 1 file changed, 3 insertions(+) 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 {