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

refactor: server side apply over create / update #3075

Open
wants to merge 40 commits into
base: main
Choose a base branch
from

Conversation

AustinAbro321
Copy link
Contributor

Description

There are several situations where we create, check if the resource already existed, and if it does then we update. It is simpler in this situation to just use server side apply

Related Issue

Fixes #2626

Checklist before merging

Copy link

netlify bot commented Oct 7, 2024

Deploy Preview for zarf-docs ready!

Name Link
🔨 Latest commit e6d25d3
🔍 Latest deploy log https://app.netlify.com/sites/zarf-docs/deploys/672513aae416a7000719b35c
😎 Deploy Preview https://deploy-preview-3075--zarf-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 77.89474% with 42 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pkg/cluster/zarf.go 0.00% 19 Missing ⚠️
src/pkg/packager/remove.go 0.00% 9 Missing ⚠️
src/internal/packager/helm/post-render.go 0.00% 8 Missing ⚠️
src/pkg/packager/deploy.go 0.00% 4 Missing ⚠️
src/pkg/cluster/state.go 85.71% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/pkg/cluster/cluster.go 0.00% <ø> (ø)
src/pkg/cluster/injector.go 65.49% <100.00%> (-3.83%) ⬇️
src/pkg/cluster/namespace.go 16.66% <100.00%> (-22.47%) ⬇️
src/pkg/cluster/secrets.go 63.06% <100.00%> (-3.61%) ⬇️
src/pkg/cluster/state.go 60.52% <85.71%> (-0.66%) ⬇️
src/pkg/packager/deploy.go 5.43% <0.00%> (+0.10%) ⬆️
src/internal/packager/helm/post-render.go 12.85% <0.00%> (+1.56%) ⬆️
src/pkg/packager/remove.go 0.00% <0.00%> (ø)
src/pkg/cluster/zarf.go 14.83% <0.00%> (+2.46%) ⬆️

Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

expanded this so it'd be easier to read / edit. The only differences is that creationTimestamp and status no longer show up in the json (because the apply objects use pointers instead of values)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Expanded for clarity, but content is the same

Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
@AustinAbro321 AustinAbro321 marked this pull request as ready for review October 24, 2024 17:07
@AustinAbro321 AustinAbro321 requested review from a team as code owners October 24, 2024 17:07
@AustinAbro321 AustinAbro321 changed the title refactor: change create or update if not exists operations to use server side apply refactor: server side apply over create / update Oct 24, 2024
Copy link
Member

@phillebaba phillebaba left a comment

Choose a reason for hiding this comment

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

Small comment. Additionally have we looked into if it is possible to set a global field manager?

src/pkg/cluster/state.go Show resolved Hide resolved
@AustinAbro321
Copy link
Contributor Author

@phillebaba As far as I can tell it's not possible to set a global field manager. I thought about making a function like cluster.DefaultApplyOpts(), lmk if you'd prefer that

@phillebaba
Copy link
Member

For now just make a constant called something like FieldManagerName so that we can track where it is being used.

// TODO: Refactor as error is not checked instead of checking for not found error.
currentRegistrySecret, _ := c.Clientset.CoreV1().Secrets(name).Get(ctx, config.ZarfImagePullSecretName, metav1.GetOptions{})
sameSecretData := maps.EqualFunc(currentRegistrySecret.Data, validRegistrySecret.Data, func(v1, v2 []byte) bool { return bytes.Equal(v1, v2) })
if currentRegistrySecret.Name != config.ZarfImagePullSecretName || !sameSecretData {
Copy link
Member

Choose a reason for hiding this comment

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

What is the effect of removing this logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current code only updates the secret if the name or the data is different. Maybe it was there to preserve labels and annotations? I'm not sure, but that's no longer a worry with apply at least.

Additionally, the current code updates the git server secret if the registry secret has a different name or different data, this seems like it was a bug with indentation, but I'm not 100% sure. I don't see any reason for that logic.

Still, I should have made a unit test to verify the behavior, I will do that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I am realizing the existing logic is flawed. I don't think there's much value in testing it. I could separate the new logic and write a test for it, but since it's just two apply calls, I'd pretty much just be testing that the fake client works.

Additionally, after evaluating further, there is no reason to dog sled errors here. We always want both of these secrets created

AustinAbro321 and others added 5 commits November 1, 2024 16:40
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
@@ -138,11 +117,8 @@ func (c *Cluster) UpdateZarfManagedImageSecrets(ctx context.Context, state *type
if err != nil {
return err
}
if maps.EqualFunc(currentRegistrySecret.Data, newRegistrySecret.Data, func(v1, v2 []byte) bool { return bytes.Equal(v1, v2) }) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that apply operations do not cost much, even if the data is the same, IMO it's better to simplify the code and let the k8s server figure out what to change / keep the same.

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

Successfully merging this pull request may close these issues.

Use Server Side Apply when creating Kubernetes resources
2 participants