Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing creationTimestamp under Template object #50

Merged

Conversation

dperaza4dustbit
Copy link
Contributor

The creationTimestamp field not only pops up under TemplateTier metadata but also under template itself

Copy link
Contributor

@alexeykazakov alexeykazakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me but it looks like there is a room for improvements in the unit tests.

@dperaza4dustbit
Copy link
Contributor Author

Will try to figure out if I spot the issue with unit test

Copy link
Contributor

@fbm3307 fbm3307 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from splitting the functions, if possible as a part of this PR we can also have a unit test to check these, they are currently not being tested, i am unsure of the reason for it

pkg/cmd/generate/util.go Outdated Show resolved Hide resolved
@dperaza4dustbit dperaza4dustbit force-pushed the fix-create-timestamp-null branch 2 times, most recently from 8636ee0 to 013d2c4 Compare July 18, 2024 17:36
@@ -159,6 +159,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", " {}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to set to empty object to fix issue with ArgoCD synch loop created by TierTemplates do to spec.template.matadata see the fix in infra deployment here: redhat-appstudio/infra-deployments#4120. The requirement is that all TierTemplates in source will start with empty object {} and then we need to remove the creationTimeStap set to null. If there is a better way to do this let me know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's fix this empty metadata as a separate PR as this won't work if the spec.template.metadata.name is set (for example in our e2e tests).
Let's focus on the empty creationTimestamp for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as for the empty metadata - this is tricky one. Not sure if there is a better (generic) way to deal with this problem, but re-marshalling the object (after removing empty creationTimestamp field) and removing metadata: null should work:

rawObject := runtime.RawExtension{}
err := yaml.Unmarshal([]byte(contentString), &rawObject)
if err != nil {
	return err
}
rawContent, err := yaml.Marshal(rawObject)
if err != nil {
	return err
}
emptyMetadata := regexp.MustCompile(`\n *metadata: null`)
contentString = emptyMetadata.ReplaceAllString(string(rawContent), "")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my new approach in utils.go, seems simpler to me

@dperaza4dustbit
Copy link
Contributor Author

Working on fixing the testcases

Copy link
Contributor

@fbm3307 fbm3307 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good,
just a minor suggestion
Either one of it can be used, whichever works

pkg/cmd/generate/util.go Outdated Show resolved Hide resolved
pkg/cmd/generate/util.go Outdated Show resolved Hide resolved
Comment on lines 161 to 162
contentString := strings.ReplaceAll(string(content), "\n creationTimestamp: null", "")
contentString = strings.ReplaceAll(contentString, "\n creationTimestamp: null", " {}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather make it more generic so it replaces any empty creationTimestamp no matter at which level

Suggested change
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), "")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took your suggestion here

@MatousJobanek
Copy link
Contributor

as for unit-testing - let's either improve the util_test.go so it verifies objects at any level, or add this check:

assert.True(t, strings.HasPrefix(string(file), header), "every file generated by cli should container a header")
assert.NotContains(t, string(file), "creationTimestamp")
assert.NotContains(t, string(file), "user: {}")

into
objFile, err := os.ReadFile(path)
require.NoError(a.t, err)

because the assertObject function is not called in the nstemplatetiers_test.go but listObjects is, so it would be verified automatically.

Copy link
Contributor Author

@dperaza4dustbit dperaza4dustbit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments added, working on improving test to check all timestamps

@@ -158,8 +159,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", "")
emptyCreationTimestamp := regexp.MustCompile(`\n *creationTimestamp: null`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works well as suggest by @MatousJobanek

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about declaring this as a top level var so that the regex doesn't have to be compiled with each invocation of this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that yeah

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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:")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the metadata problem with Konflux as tiers in src already start with spec.template.metadata as {}. See here: https://github.com/redhat-appstudio/infra-deployments/blob/main/components/sandbox/tiers/src/appstudio/spacerole_admin.yaml#L3
But this won't affect our e2e nor unitest as those start with spec.template.metadata.name having a string value. Would this work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, this should work fine 👍 🚀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another option would be removing the metadata field completely. But I'll leave it up to you ;-)

Suggested change
contentString = strings.ReplaceAll(contentString, "metadata:\n objects:", "metadata: {}\n objects:")
contentString = strings.ReplaceAll(contentString, "metadata:\n objects:", "objects:")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well tried that but ArgoCD expects metadata: {}. Will prefer to match exactly what ArgoCD expects there to avoid re-synching situation

@@ -159,6 +159,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", " {}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my new approach in utils.go, seems simpler to me

Comment on lines 161 to 162
contentString := strings.ReplaceAll(string(content), "\n creationTimestamp: null", "")
contentString = strings.ReplaceAll(contentString, "\n creationTimestamp: null", " {}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took your suggestion here

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks good overall, just waiting for the updates of the unit tests #50 (comment)

Signed-off-by: David Peraza <dperaza@redhat.com>
Also changed the replacement of creationTimestamp at any
level with regex as suggested in reviews by  @MatousJobanek

Signed-off-by: David Peraza <dperaza@redhat.com>
Signed-off-by: David Peraza <dperaza@redhat.com>
@dperaza4dustbit dperaza4dustbit force-pushed the fix-create-timestamp-null branch from f28d487 to 156189f Compare October 9, 2024 12:57
@dperaza4dustbit
Copy link
Contributor Author

it looks good overall, just waiting for the updates of the unit tests #50 (comment)

Got it

Signed-off-by: David Peraza <dperaza@redhat.com>
@dperaza4dustbit
Copy link
Contributor Author

as for unit-testing - let's either improve the util_test.go so it verifies objects at any level, or add this check:

assert.True(t, strings.HasPrefix(string(file), header), "every file generated by cli should container a header")
assert.NotContains(t, string(file), "creationTimestamp")
assert.NotContains(t, string(file), "user: {}")

into

objFile, err := os.ReadFile(path)
require.NoError(a.t, err)

because the assertObject function is not called in the nstemplatetiers_test.go but listObjects is, so it would be verified automatically.

added assertions to listObject method, thanks for suggestion

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for addressing our comments

@MatousJobanek MatousJobanek merged commit 9e9b2f4 into kubesaw:master Oct 11, 2024
7 checks passed
Copy link

codecov bot commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.59%. Comparing base (ac3aaca) to head (fd122c3).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #50      +/-   ##
==========================================
+ Coverage   69.57%   69.59%   +0.01%     
==========================================
  Files          43       43              
  Lines        2564     2565       +1     
==========================================
+ Hits         1784     1785       +1     
  Misses        589      589              
  Partials      191      191              
Files with missing lines Coverage Δ
pkg/cmd/generate/util.go 78.16% <100.00%> (+0.15%) ⬆️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants