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

Fix potential --kubeconfig redefined error #2855

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

dsimansk
Copy link
Contributor

@dsimansk dsimansk commented Oct 9, 2023

Changes

  • Fix potential redefined error with kubeconfig flag

The --kubeconfing flag is a popular one. I've run into a case with sigs.k8s.io/controller-runtime & /pkg when vendoring together there's flag redefined error thrown. Depending on the order of package inits.

This change should make /pkg safer to use alongside other K8s libs.

@knative-prow knative-prow bot requested review from kauana and ReToCode October 9, 2023 09:09
@knative-prow knative-prow bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 9, 2023
@dsimansk dsimansk force-pushed the pr/kubeconfig-flag-lazy-init branch from b8ebbc8 to 5e094c0 Compare October 9, 2023 09:32
@codecov
Copy link

codecov bot commented Oct 9, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (d0a82f9) 81.86% compared to head (5e094c0) 81.80%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2855      +/-   ##
==========================================
- Coverage   81.86%   81.80%   -0.07%     
==========================================
  Files         167      167              
  Lines       10224    10227       +3     
==========================================
- Hits         8370     8366       -4     
- Misses       1608     1614       +6     
- Partials      246      247       +1     
Files Coverage Δ
environment/client_config.go 36.53% <100.00%> (+3.88%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dsimansk
Copy link
Contributor Author

dsimansk commented Oct 9, 2023

Failed downstream test is unrelated. The eventing-rabbitmq repo needs to be fixed because of the k8s.io version bump.

Downstream Knative / Unit Test (knative-extensions/eventing-rabbitmq) (pull_request)

Copy link
Member

@pierDipi pierDipi left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Oct 9, 2023
@knative-prow
Copy link

knative-prow bot commented Oct 9, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dsimansk, pierDipi

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot merged commit bae23eb into knative:main Oct 9, 2023
41 of 42 checks passed
fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"),
"Path to a kubeconfig. Only required if out-of-cluster.")
if f := fs.Lookup("kubeconfig"); f != nil {
c.Kubeconfig = f.Value.String()
Copy link
Member

Choose a reason for hiding this comment

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

usually we call InitFlags method then a flags.Parse gets called

Given we're setting c.Kubeconfig here - i don't think it will update after flags.Parse is called.

So this config will have a different value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, indeed c.Kubeconfig will set as default value of first defined --kubeconfig. Not reflecting values provided as args and flags.Parse() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you prefer a revert here? There are several places in knative/pkg that call flags.Parse() and could be refactored to isolated flatset, but that's probably not worth it.

flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)

Copy link
Member

Choose a reason for hiding this comment

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

Would you prefer a revert here?

I think so - otherwise it's a subtle bug that I will be difficult to discover.

There are several places in knative/pkg that call flags.Parse() and could be refactored to isolated flatset, but that's probably not worth it.

Yeah a lot of the test code had that - hence why I pulled out this resource into a separate package that doesn't call Parse

I don't have a good solution for you to mix this with kubebuilder. You may want to initialize your own flagset and configure each configuration yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants