Skip to content

Commit 1687026

Browse files
authored
feat: add --managed-cluster-arn flag to clusteradm join command (#486)
Signed-off-by: Ahmad Ibrahim <[email protected]>
1 parent 8cc04b6 commit 1687026

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pkg/cmd/join/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
8383
cmd.Flags().Int32Var(&o.clientCertExpirationSeconds, "client-cert-expiration-seconds", 31536000, "clientCertExpirationSeconds represents the seconds of a client certificate to expire.")
8484
cmd.Flags().StringVar(&o.registrationAuth, "registration-auth", "", "The type of authentication to use for registering and authenticating with hub")
8585
cmd.Flags().StringVar(&o.hubClusterArn, "hub-cluster-arn", "", "The arn of the hub cluster(i.e. EKS cluster) to which managed-cluster will join")
86+
cmd.Flags().StringVar(&o.managedClusterArn, "managed-cluster-arn", "", "The arn of the managed cluster(i.e. EKS cluster) which will be joining the hub")
8687
cmd.Flags().StringArrayVar(&o.klusterletAnnotations, "klusterlet-annotation", []string{}, fmt.Sprintf("Annotations to set on the ManagedCluster, in key=value format. Note: each key will be automatically prefixed with '%s/', if not set.", operatorv1.ClusterAnnotationsKeyPrefix))
8788
return cmd
8889
}

pkg/cmd/join/exec.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,15 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
156156

157157
// set registration auth type
158158
if o.registrationAuth == AwsIrsaAuthentication {
159-
rawConfig, err := o.ClusteradmFlags.KubectlFactory.ToRawKubeConfigLoader().RawConfig()
159+
managedClusterArn, err := getManagedClusterArn(o)
160160
if err != nil {
161-
klog.Errorf("unable to load managedcluster kubeconfig: %v", err)
162161
return err
163162
}
164-
165163
o.klusterletChartConfig.Klusterlet.RegistrationConfiguration.RegistrationDriver = operatorv1.RegistrationDriver{
166164
AuthType: o.registrationAuth,
167165
AwsIrsa: &operatorv1.AwsIrsa{
168166
HubClusterArn: o.hubClusterArn,
169-
ManagedClusterArn: rawConfig.Contexts[rawConfig.CurrentContext].Cluster,
167+
ManagedClusterArn: managedClusterArn,
170168
},
171169
}
172170
}
@@ -774,3 +772,21 @@ func (o *Options) setKlusterletRegistrationAnnotations() {
774772
o.klusterletChartConfig.Klusterlet.RegistrationConfiguration.ClusterAnnotations[k] = v
775773
}
776774
}
775+
776+
func getManagedClusterArn(o *Options) (string, error) {
777+
if o.managedClusterArn != "" {
778+
return o.managedClusterArn, nil
779+
}
780+
781+
rawConfig, err := o.ClusteradmFlags.KubectlFactory.ToRawKubeConfigLoader().RawConfig()
782+
if err != nil {
783+
klog.Errorf("unable to load managedcluster kubeconfig: %v", err)
784+
return "", err
785+
}
786+
managedClusterArn := rawConfig.Contexts[rawConfig.CurrentContext].Cluster
787+
if managedClusterArn == "" {
788+
klog.Errorf("managedClusterArn has empty value in kubeconfig")
789+
return "", fmt.Errorf("unable to retrieve managedClusterArn from kubeconfig")
790+
}
791+
return managedClusterArn, nil
792+
}

pkg/cmd/join/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ type Options struct {
9393
// The arn of hub cluster(i.e. EKS) to which managed-cluster will join
9494
hubClusterArn string
9595

96+
// The arn of managed cluster(i.e. EKS) which will be joining the hub
97+
managedClusterArn string
98+
9699
// Annotations for registration controller to set on the ManagedCluster
97100
klusterletAnnotations []string
98101
}

0 commit comments

Comments
 (0)