-
Notifications
You must be signed in to change notification settings - Fork 374
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
Conversation
/test-all |
/test-windows-e2e |
1 similar comment
/test-windows-e2e |
fca53f1
to
7ab4db0
Compare
pkg/antctl/runtime/runtime.go
Outdated
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) | ||
} |
There was a problem hiding this comment.
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:
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, | |
) | |
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 was a problem hiding this comment.
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.
0f26511
to
480ed83
Compare
pkg/antctl/runtime/runtime.go
Outdated
} | ||
return nil, fmt.Errorf("failed to resolve kubeconfig: kubeconfig file does not exist at path '%s'", path) | ||
} | ||
config, err := clientcmd.BuildConfigFromFlags("", path) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, thanks!
pkg/antctl/runtime/runtime.go
Outdated
@@ -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) { |
There was a problem hiding this comment.
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
// ...
}
There was a problem hiding this comment.
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.
pkg/antctl/runtime/runtime.go
Outdated
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) | ||
} |
There was a problem hiding this comment.
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?
480ed83
to
ae05194
Compare
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]>
ae05194
to
e11daf1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/test-all |
@luolanzone any further comments? |
Fixes: #6687
by the absence of this file.
between kubeconfig file errors and InClusterConfig errors.