Skip to content

Commit

Permalink
🐛 fix the wrong apiserver endpoint in hosted mode (#390)
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Le <[email protected]>
  • Loading branch information
elgnay authored Nov 8, 2023
1 parent c43d888 commit 205e896
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/cmd/join/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
cmd.Flags().BoolVar(&o.forceHubInClusterEndpointLookup, "force-internal-endpoint-lookup", false,
"If true, the installed klusterlet agent will be starting the cluster registration process by "+
"looking for the internal endpoint from the public cluster-info in the hub cluster instead of from --hub-apiserver.")
cmd.Flags().BoolVar(&o.forceManagedInClusterEndpointLookup, "force-internal-endpoint-lookup-managed", false,
"If true, the klusterlet accesses the managed cluster by using the internal endpoint from the public cluster-info"+
" in the managed cluster instead of from --managed-cluster-kubeconfig directly.")
cmd.Flags().BoolVar(&o.wait, "wait", false, "If true, running the cluster registration in foreground.")
cmd.Flags().StringVarP(&o.mode, "mode", "m", "default", "mode to deploy klusterlet, can be default or hosted")
cmd.Flags().StringVar(&o.managedKubeconfigFile, "managed-cluster-kubeconfig", "", "To specify the directory to external managed cluster kubeconfig in hosted mode")
Expand Down
49 changes: 45 additions & 4 deletions pkg/cmd/join/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapiv1 "k8s.io/client-go/tools/clientcmd/api/v1"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/cmd/util"
Expand Down Expand Up @@ -174,11 +175,25 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
}

// get managed cluster externalServerURL
kubeClient, err := o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
klog.Errorf("Failed building kube client: %v", err)
return err
var kubeClient *kubernetes.Clientset
switch o.mode {
case string(operatorv1.InstallModeHosted):
restConfig, err := clientcmd.BuildConfigFromFlags("", o.managedKubeconfigFile)
if err != nil {
return err
}
kubeClient, err = kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
default:
kubeClient, err = o.ClusteradmFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
klog.Errorf("Failed building kube client: %v", err)
return err
}
}

klusterletApiserver, err := helpers.GetAPIServer(kubeClient)
if err != nil {
klog.Warningf("Failed looking for cluster endpoint for the registering klusterlet: %v", err)
Expand Down Expand Up @@ -230,6 +245,32 @@ func (o *Options) validate() error {
if err != nil {
return err
}

// replace the server address with the internal endpoint
if o.forceManagedInClusterEndpointLookup {
config := &clientcmdapiv1.Config{}
err = yaml.Unmarshal(managedConfig, config)
if err != nil {
return err
}
restConfig, err := clientcmd.BuildConfigFromFlags("", o.managedKubeconfigFile)
if err != nil {
return err
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
inClusterEndpoint, err := helpers.GetAPIServer(kubeClient)
if err != nil {
return err
}
config.Clusters[0].Cluster.Server = inClusterEndpoint
managedConfig, err = yaml.Marshal(config)
if err != nil {
return err
}
}
o.values.ManagedKubeconfig = base64.StdEncoding.EncodeToString(managedConfig)
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/join/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ type Options struct {
// the external endpoint from --hub-apiserver instead of looking for the internal
// endpoint from the public cluster-info.
forceHubInClusterEndpointLookup bool
hubInClusterEndpoint string
// By default, the klusterlet running in the hosting cluster will access the managed
// cluster registered in the hosted mode by using the external endpoint from
// --managed-cluster-kubeconfig instead of looking for the internal endpoint from the
// public cluster-info.
forceManagedInClusterEndpointLookup bool
hubInClusterEndpoint string

//Values below are tempoary data
//HubCADate: data in hub ca file
Expand Down

0 comments on commit 205e896

Please sign in to comment.