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

Create new kubeconfig for SupportBundleClient #6840

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

XinShuYang
Copy link
Contributor

@XinShuYang XinShuYang commented Nov 29, 2024

Fixes: #6687

  • Create a new kubeconfig for local support bundle requests to avoid errors caused
    by the absence of this file.
  • For ResolveKubeconfig, detailed error messages are returned, differentiating
    between kubeconfig file errors and InClusterConfig errors.

@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang
Copy link
Contributor Author

/test-windows-e2e

1 similar comment
@XinShuYang
Copy link
Contributor Author

/test-windows-e2e

@XinShuYang XinShuYang marked this pull request as ready for review December 6, 2024 02:44
Comment on lines 51 to 65
if _, err := os.Stat(path); os.IsNotExist(err) {
if path == clientcmd.RecommendedHomeFile {
config, inClusterErr := rest.InClusterConfig()
if inClusterErr == nil {
return config, nil
}
return nil, fmt.Errorf(
"failed to resolve kubeconfig: neither a valid kubeconfig file was found at '%s', nor could InClusterConfig be used: %w",
path, inClusterErr,
)
}
return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the incluster way should be tried only when the path is the default one:

Suggested change
if _, err := os.Stat(path); os.IsNotExist(err) {
if path == clientcmd.RecommendedHomeFile {
config, inClusterErr := rest.InClusterConfig()
if inClusterErr == nil {
return config, nil
}
return nil, fmt.Errorf(
"failed to resolve kubeconfig: neither a valid kubeconfig file was found at '%s', nor could InClusterConfig be used: %w",
path, inClusterErr,
)
}
return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path)
}
if _, err := os.Stat(path); path == clientcmd.RecommendedHomeFile && os.IsNotExist(err) {
config, inClusterErr := rest.InClusterConfig()
if inClusterErr == nil {
return config, nil
}
return nil, fmt.Errorf(
"failed to resolve kubeconfig: no valid kubeconfig file was found at the default path '%s', and InClusterConfig can't be used: %w",
path, inClusterErr,
)
}

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 believe my original code has the same behavior and can handle an additional case where the file's absence at a non-default path results in a different error message.

Copy link
Contributor

Choose a reason for hiding this comment

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

so my understanding is that there is no change in behavior, just better error messages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so my understanding is that there is no change in behavior, just better error messages?

There is only one case where a non-existent non-default path can lead to an error message and stop further processing, which is not covered in the original code. Other changes indeed aim to improve the error messaging.

@XinShuYang XinShuYang force-pushed the antctllog branch 2 times, most recently from 0f26511 to 480ed83 Compare December 10, 2024 13:45
@XinShuYang XinShuYang changed the title Improve kubeconfig resolution with detailed error Create new kubeconfig for SupportBundleClient Dec 10, 2024
pkg/antctl/raw/supportbundle/command.go Show resolved Hide resolved
}
return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path)
}
config, err := clientcmd.BuildConfigFromFlags("", path)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we should use clientcmd.NewNonInteractiveDeferredLoadingClientConfig directly (like we do in other parts of the code). it is a bit weird to use BuildConfigFromFlags, which itself has a fallback to inClusterConfig (even though it should not be used in our case).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks!

@@ -47,10 +48,24 @@ func ResolveKubeconfig(path string) (*rest.Config, error) {
path = clientcmd.RecommendedHomeFile
}
}
if _, err = os.Stat(path); path == clientcmd.RecommendedHomeFile && os.IsNotExist(err) {
return rest.InClusterConfig()
if _, err := os.Stat(path); os.IsNotExist(err) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like the function could be cleaned up further. Maybe something like:

withExplicitPath := path != ""
if !withExplicitPath {
        path = strings.TrimSpace(os.Getenv("KUBECONFIG"))
        if path == "" {
                path = clientcmd.RecommendedHomeFile // default path
        }
}
if _, err := os.Stat(path); err != nil {
        if !os.IsNotExist(err) {
                return nil, err // unexpected error
        }
        if withExplicitPath {
                // user-provided path is not valid, return error
                // ...
        }
        // no file found at default path, fallback to InClusterConfig
        // ...
}

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, I feel this is better, updated.

Comment on lines 51 to 65
if _, err := os.Stat(path); os.IsNotExist(err) {
if path == clientcmd.RecommendedHomeFile {
config, inClusterErr := rest.InClusterConfig()
if inClusterErr == nil {
return config, nil
}
return nil, fmt.Errorf(
"failed to resolve kubeconfig: neither a valid kubeconfig file was found at '%s', nor could InClusterConfig be used: %w",
path, inClusterErr,
)
}
return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

so my understanding is that there is no change in behavior, just better error messages?

Fixes: antrea-io#6687

* Create a new kubeconfig for local support bundle requests to avoid errors caused
by the absence of this file.
* Refactored ResolveKubeconfig, detailed error messages are returned, differentiating
between kubeconfig file errors and InClusterConfig errors.

Signed-off-by: Shuyang Xin <[email protected]>
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

LGTM

@antoninbas
Copy link
Contributor

/test-all

@antoninbas
Copy link
Contributor

@luolanzone any further comments?

@luolanzone luolanzone merged commit 11e176f into antrea-io:main Dec 13, 2024
56 of 59 checks passed
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.

antctl.exe fails to run on Windows VM due to missing in-cluster configuration
3 participants