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

crdutil: Add feature to also apply single files #58

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

Conversation

tobiasgiese
Copy link
Member

@tobiasgiese tobiasgiese commented Nov 25, 2024

This PR adds a feature to apply single yaml files, not only complete directories.

pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Nov 25, 2024

Pull Request Test Coverage Report for Build 12053812919

Details

  • 10 of 45 (22.22%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.08%) to 62.059%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/crdutil/crdutil.go 10 45 22.22%
Files with Coverage Reduction New Missed Lines %
pkg/crdutil/crdutil.go 2 37.72%
Totals Coverage Status
Change from base Build 11928826672: -0.08%
Covered Lines: 1091
Relevant Lines: 1758

💛 - Coveralls

@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch 2 times, most recently from 56fdeb6 to e8a0806 Compare November 25, 2024 09:28
@tobiasgiese tobiasgiese changed the title crdutil: Add feature to also apply also single files crdutil: Add feature to also apply single files Nov 25, 2024
@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch from e8a0806 to 7b4a23b Compare November 26, 2024 20:31
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch 2 times, most recently from 43b027c to 776bbdb Compare November 27, 2024 00:10
@tobiasgiese
Copy link
Member Author

tobiasgiese commented Nov 27, 2024

Tanks for the extensive review @shivamerla. I should have implemented all findings in this diff.

❯ go run cmd/apply-crds/main.go -f /tmp/foo/
2024/11/27 01:25:19 Apply CRDs from file: /tmp/foo/1.yaml
2024/11/27 01:25:19 Update CRD issuers.cert-manager.io

❯ go run cmd/apply-crds/main.go -f /tmp/foo/ -R
2024/11/27 01:25:22 Apply CRDs from file: /tmp/foo/1.yaml
2024/11/27 01:25:22 Update CRD issuers.cert-manager.io
2024/11/27 01:25:22 Apply CRDs from file: /tmp/foo/recursive/2.yaml
2024/11/27 01:25:22 Update CRD orders.acme.cert-manager.io

❯ go run cmd/apply-crds/main.go -f /tmp/foo/1.yaml
2024/11/27 01:25:28 Apply CRDs from file: /tmp/foo/1.yaml
2024/11/27 01:25:28 Update CRD issuers.cert-manager.io

❯ go run cmd/apply-crds/main.go -f /tmp/foo/1.yaml -f /tmp/foo/recursive/2.yaml
2024/11/27 01:25:35 Apply CRDs from file: /tmp/foo/1.yaml
2024/11/27 01:25:35 Update CRD issuers.cert-manager.io
2024/11/27 01:25:35 Apply CRDs from file: /tmp/foo/recursive/2.yaml
2024/11/27 01:25:35 Update CRD orders.acme.cert-manager.io

@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch 4 times, most recently from 4cd95ea to e72a10f Compare November 27, 2024 00:36
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch from e72a10f to 92553f3 Compare November 27, 2024 00:39
Copy link
Member

@vasrem vasrem left a comment

Choose a reason for hiding this comment

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

LGTM overall, just one comment on the linter stuff.

pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
return nil
}
// If not recursive we want to only apply the CRDs in the top-level directory.
if !recursive && parentDir.IsDir() && filepath.Dir(path) != strings.TrimRight(crdDir, "/") {
Copy link
Member

Choose a reason for hiding this comment

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

should we add a test case for this new branch or should these all be included in the "TODO add unit tests" comment you have added?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me take another look at the tests, I think I can easily test the walkCRDs func

@@ -49,26 +51,32 @@ func (s *StringList) Set(value string) error {
}

var (
crdsDir StringList
files []string
recursive bool
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to separate the handling of multiple non-recursive paths (including files) from the addition of recursive handling?

Copy link
Member Author

Choose a reason for hiding this comment

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

AFAIK kubectl is also not separating it, so it is totally fine to keep it pretty open IMO. But also fine to me to split it somehow (you mean like: if multiple files are passed the recursive flag is not possible?)

Copy link
Member

Choose a reason for hiding this comment

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

I actually just meant as separate PRs. From NVIDIA/gpu-operator#1137 it seems that we don't use recursive mode. Is it used in the network operator?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it is not used by the network-operator. Maybe it makes sense to remove this new feature from the PR, you are right. The request adding it to this PR came through #58 (comment) (at least that's how I understood it)

pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
)

func initFlags() {
flag.Var(&crdsDir, "crds-dir", "Path to the directory containing the CRD manifests")
Copy link
Member

Choose a reason for hiding this comment

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

Are we concerned about this as a breaking change since the command line arguments are changed?

Copy link
Member Author

@tobiasgiese tobiasgiese Nov 27, 2024

Choose a reason for hiding this comment

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

I was also thinking about this but this pkg is pretty new and we are only using it for the network-operator currently. Do you think we should deprecate it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If the network-operator is not released with this change, better to just update as it will be first use of this package.

Copy link
Member Author

@tobiasgiese tobiasgiese Nov 28, 2024

Choose a reason for hiding this comment

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

The network-operator 24.10.0 has been released 2 days ago with the change in it, see https://github.com/Mellanox/network-operator/blob/v24.10.0/deployment/network-operator/templates/upgrade-crd-hook.yaml#L84.

But IMO it is totally fine to change it, because it is a Helm pre-install hook. If it fails, it fails the installation/upgrade and the user has to use the latest Helm chart. There is no dependency to the runtime which can cause the operator to fail.

WDYT @e0ne @rollandf

tl;dr: this PR changes the flag --crds-dir to --filename/-f

Copy link
Member

Choose a reason for hiding this comment

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

If there is a concern, we could support both -- marking crds-dir as deprecated -- for a release. Happy with the change since this is effectively an internal API.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can also revert it to --crds-dir. Both crds-dir and filename does not contain both files and dirs, so it is not 100% explicit anyway :)
Request came through #58 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

@shivamerla WDYT? Should we remove the flag rename and recursive flag (see #58 (comment)) out of this PR and change it in a follow-up? I'd say this will reduce the size of this PR.

pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
func walkCrdsDir(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface) error {
for _, crdDir := range crdsDir {
// walkCRDs walks the CRDs directory and applies each YAML file.
func walkCRDs(ctx context.Context, crdClient v1.CustomResourceDefinitionInterface) error {
Copy link
Member

Choose a reason for hiding this comment

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

As a question: Does it makes sense to separate filename collection from applying the CRDs? That is to say that we would have logic that accepts the flags and returns a slice of YAML file paths. The logic to apply the CRDs would then accept a slice to YAML file paths. This should make things simpler to test and also allow us to properly de-duplicate if a user specifies a file multiple times, for example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think that makes totally sense. Let me change the code accordingly

@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch from 92553f3 to 4f36d86 Compare November 27, 2024 15:49
Copy link
Collaborator

@shivamerla shivamerla left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks

pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
Comment on lines 94 to 96
// walkCRDs walks the CRDs directory and applies each YAML file.
// TODO: add unit test for this function.
func walkCRDs(recursive bool, crdDirs []string) ([]string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// walkCRDs walks the CRDs directory and applies each YAML file.
// TODO: add unit test for this function.
func walkCRDs(recursive bool, crdDirs []string) ([]string, error) {
// collectYAMLPaths processes a list of paths and returns all YAML files.
// If `recursive` is specified directories in the list are searched recursively for YAML files.
// TODO: add unit test for this function.
func collectYAMLPaths(recursive bool, paths []string) ([]string, error) {

Copy link
Member

Choose a reason for hiding this comment

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

Note that we also need to rename some local variables to better indicate what we are collecting.

pkg/crdutil/crdutil.go Outdated Show resolved Hide resolved
@tobiasgiese
Copy link
Member Author

tobiasgiese commented Jan 10, 2025

just as a note/reminder: will work on it next week again

@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch from 949680d to ff2335e Compare January 13, 2025 11:08
@tobiasgiese
Copy link
Member Author

@elezar @shivamerla I implemented all findings and also removed the --force flag and reverted the flag rename from --filename back to --crds-dir. We can think of deprecating the flag and use --filename in a follow-up PR. Additionally, the force feature can/will be also implemented in a follow-up PR - if needed.

If you both accept the changes I will squash both commits.

@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch 2 times, most recently from 3ea1753 to 6692fae Compare January 13, 2025 12:53
@tobiasgiese tobiasgiese force-pushed the crdutil-add-single-files branch from 6692fae to 100c62f Compare January 13, 2025 13:13
type: object
properties:
spec:
type: object
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we add EOL here?

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.

6 participants