Skip to content

Commit

Permalink
remove creationTimestamp under Template object and format metadata pr…
Browse files Browse the repository at this point in the history
…operly (#50)
  • Loading branch information
dperaza4dustbit authored Oct 11, 2024
1 parent ac3aaca commit 9e9b2f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/cmd/generate/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/generate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 9e9b2f4

Please sign in to comment.