-
Notifications
You must be signed in to change notification settings - Fork 916
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
Support to run karmadactl init within a pod #3338
Conversation
Hi @hzxuzhonghu , Could you please help test it in your env? |
1873a17
to
4dcfea8
Compare
@@ -43,6 +44,13 @@ func RestConfig(context, kubeconfigPath string) (*rest.Config, error) { | |||
kubeconfigPath = env.GetString("KUBECONFIG", defaultKubeConfig) | |||
} | |||
if !Exists(kubeconfigPath) { | |||
klog.Warning("Neither --kubeconfig nor KUBECONFIG was specified and '~/.kube/config' doesn't exist. Using the inClusterConfig. This might not work.") | |||
// if the kubeconfig path is not valid, try to load InClusterConfig | |||
inClusterRestConfig, err := rest.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.
great, will test it
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
It can connect to kube-apiserver successfully, we just need to grant some permission to the service account.
Great, I will update the klog message to match our scenario. |
4dcfea8
to
9654407
Compare
If there isn't kubeconfig, the message is that
What do you think? /cc @RainbowMango |
/assign |
@@ -43,6 +44,13 @@ func RestConfig(context, kubeconfigPath string) (*rest.Config, error) { | |||
kubeconfigPath = env.GetString("KUBECONFIG", defaultKubeConfig) | |||
} | |||
if !Exists(kubeconfigPath) { | |||
klog.Warningf("Kubeconfig '%s' doesn't exist. Using the inClusterConfig. This might not work.", kubeconfigPath) |
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.
Since we are going to officially support the in-cluster mode, we can think of this(build rest config by InClusterConfig) as a normal way, so, this log should not be a warning
.
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.
How about this?
if !Exists(kubeconfigPath) {
// Given no kubeconfig is provided, give it a try to load the config by
// in-cluster mode if the client running inside a pod running on kubernetes.
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) > 0 || len(port) > 0 { // in-cluster mode
inClusterRestConfig, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to load rest config by in-cluster mode: %v", err)
}
return inClusterRestConfig, nil
}
return nil, ErrEmptyConfig
}
If no kubeconfig is provided and it works inside a Pod, we assume that it is intended to be running inside, so we just return the in-cluster error.
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 think it is great.
Signed-off-by: lonelyCZ <chengzhe@zju.edu.cn>
9654407
to
59ef627
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
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hzxuzhonghu, RainbowMango 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 |
What type of PR is this?
/kind feature
What this PR does / why we need it:
Support to run karmadactl init within a pod by InClusterConfig.
Which issue(s) this PR fixes:
Fixes #3335
Special notes for your reviewer:
Does this PR introduce a user-facing change?: