Skip to content

Commit

Permalink
Adding optional hubClusterArn incase it is missing in EKS kubeconfig
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Jaswal <[email protected]>
  • Loading branch information
jaswalkiranavtar committed Jan 20, 2025
1 parent 4842271 commit f5fa2b6
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 23 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module open-cluster-management.io/clusteradm

go 1.22.6

replace open-cluster-management.io/ocm => /Users/gjaswal/Documents/codebase/guidewire-oss/ocm

require (
github.com/briandowns/spinner v1.23.0
github.com/disiqueira/gotree v1.0.0
Expand Down Expand Up @@ -33,7 +31,7 @@ require (
open-cluster-management.io/api v0.15.1-0.20250116010516-3a595d6a4e40
open-cluster-management.io/cluster-proxy v0.4.0
open-cluster-management.io/managed-serviceaccount v0.6.0
open-cluster-management.io/ocm v0.15.1-0.20250116085531-34275ef1eac8
open-cluster-management.io/ocm v0.15.1-0.20250120013556-eeb4ab31d5ab
open-cluster-management.io/sdk-go v0.15.1-0.20241125015855-1536c3970f8f
sigs.k8s.io/apiserver-network-proxy v0.29.0
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ open-cluster-management.io/cluster-proxy v0.4.0 h1:rm0UDaDWe3/P3xLzwqdHtqNksKwSz
open-cluster-management.io/cluster-proxy v0.4.0/go.mod h1:gTvfDHAhGezhdg4BD3ECBn6jbg2Y5PbHhV2ceW5nrB0=
open-cluster-management.io/managed-serviceaccount v0.6.0 h1:qIi5T9WQJBuoGqnYGIktXbtqfQoiN2H9XU2P/6lAQiw=
open-cluster-management.io/managed-serviceaccount v0.6.0/go.mod h1:G4LUTbZiyrB8c0+rqi/xnDmGlsg7Rdr4T7MPLCWhyQI=
open-cluster-management.io/ocm v0.15.1-0.20250120013556-eeb4ab31d5ab h1:DY4DSDQUEoVQ6fCda7nSYetJRhvkyoiHPLyMppL/a8w=
open-cluster-management.io/ocm v0.15.1-0.20250120013556-eeb4ab31d5ab/go.mod h1:Mfg6rf0CylcnY5y8zJB99ClbMUMpAAUa22Rv+3ct5Lg=
open-cluster-management.io/sdk-go v0.15.1-0.20241125015855-1536c3970f8f h1:zeC7QrFNarfK2zY6jGtd+mX+yDrQQmnH/J8A7n5Nh38=
open-cluster-management.io/sdk-go v0.15.1-0.20241125015855-1536c3970f8f/go.mod h1:fi5WBsbC5K3txKb8eRLuP0Sim/Oqz/PHX18skAEyjiA=
oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo=
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/init/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var example = `
%[1]s init
# Initialize the hub cluster with the type of authentication. Either or both of csr,awsirsa
%[1]s init --registration-auth awsirsa --registration-auth csr
%[1]s init --registration-auth awsirsa --registration-auth csr --hubClusterArn arn:aws:eks:us-west-2:123456789012:cluster/hub-cluster1
`

// NewCmd ...
Expand Down Expand Up @@ -82,6 +82,8 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
o.Helm.AddFlags(singletonSet)
cmd.Flags().AddFlagSet(singletonSet)
cmd.Flags().StringArrayVar(&o.registrationAuth, "registration-auth", []string{}, "The type of authentication to use for registering and authenticating with hub, this flag can be repeated to specify multiple authentication types.")
cmd.Flags().StringVar(&o.hubClusterArn, "hub-cluster-arn", "",
"The hubCluster ARN to be passed if awsirsa is one of the registrationAuths and the cluster name in EKS kubeconfig doesn't contain hubClusterArn")

return cmd
}
19 changes: 11 additions & 8 deletions pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,18 @@ func getRegistrationDrivers(o *Options) ([]operatorv1.RegistrationDriverHub, err
registrationDrivers = append(registrationDrivers, operatorv1.RegistrationDriverHub{AuthType: "csr"})
}
if slices.Contains(o.registrationAuth, "awsirsa") {
rawConfig, err := o.ClusteradmFlags.KubectlFactory.ToRawKubeConfigLoader().RawConfig()
if err != nil {
klog.Errorf("unable to load hub cluster kubeconfig: %v", err)
return nil, err
}
hubClusterArn := rawConfig.Contexts[rawConfig.CurrentContext].Cluster
hubClusterArn := o.hubClusterArn
if hubClusterArn == "" {
klog.Errorf("hubClusterArn has empty value in kubeconfig")
return nil, fmt.Errorf("unable to retrieve hubClusterArn from kubeconfig")
rawConfig, err := o.ClusteradmFlags.KubectlFactory.ToRawKubeConfigLoader().RawConfig()
if err != nil {
klog.Errorf("unable to load hub cluster kubeconfig: %v", err)
return nil, err
}
hubClusterArn = rawConfig.Contexts[rawConfig.CurrentContext].Cluster
if hubClusterArn == "" {
klog.Errorf("hubClusterArn has empty value in kubeconfig")
return nil, fmt.Errorf("unable to retrieve hubClusterArn from kubeconfig")
}
}
registrationDrivers = append(registrationDrivers, operatorv1.RegistrationDriverHub{AuthType: "awsirsa", HubClusterArn: hubClusterArn})
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/init/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type Options struct {

// The type of authentication to use for initializing the hub cluster
registrationAuth []string
// The optional ARN to pass if awsirsa is one of the registrationAuths
// and the cluster name in EKS kubeconfig doesn't contain hubClusterArn
hubClusterArn string
}

func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericiooptions.IOStreams) *Options {
Expand Down
29 changes: 29 additions & 0 deletions test/e2e/clusteradm/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ var _ = ginkgo.Describe("test clusteradm with bootstrap token in singleton mode"
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(len(cm.Spec.RegistrationConfiguration.FeatureGates)).Should(gomega.Equal(1))

err = e2e.Clusteradm().Init(
"--use-bootstrap-token",
"--context", e2e.Cluster().Hub().Context(),
"--bundle-version=latest",
"--registration-auth awsirsa",
"--hub-cluster-arn arn:aws:eks:us-west-2:123456789012:cluster/hub-cluster1",
)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")

cm, err = operatorClient.OperatorV1().ClusterManagers().Get(context.TODO(), "cluster-manager", metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Ensure that when only awsirsa is passed as registration-auth only awsirsa driver is available
gomega.Expect(len(cm.Spec.RegistrationConfiguration.RegistrationDrivers)).Should(gomega.Equal(1))

err = e2e.Clusteradm().Init(
"--use-bootstrap-token",
"--context", e2e.Cluster().Hub().Context(),
"--bundle-version=latest",
"--registration-auth awsirsa",
"--registration-auth csr",
"--hub-cluster-arn arn:aws:eks:us-west-2:123456789012:cluster/hub-cluster1",
)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")

cm, err = operatorClient.OperatorV1().ClusterManagers().Get(context.TODO(), "cluster-manager", metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Ensure that awsirsa and csr is passed as registration-auth both the values are set.
gomega.Expect(len(cm.Spec.RegistrationConfiguration.RegistrationDrivers)).Should(gomega.Equal(2))

err = e2e.Clusteradm().Init(
"--use-bootstrap-token",
"--context", e2e.Cluster().Hub().Context(),
Expand Down
3 changes: 1 addition & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ open-cluster-management.io/managed-serviceaccount/pkg/generated/clientset/versio
open-cluster-management.io/managed-serviceaccount/pkg/generated/clientset/versioned/scheme
open-cluster-management.io/managed-serviceaccount/pkg/generated/clientset/versioned/typed/authentication/v1alpha1
open-cluster-management.io/managed-serviceaccount/pkg/generated/clientset/versioned/typed/authentication/v1beta1
# open-cluster-management.io/ocm v0.15.1-0.20250116085531-34275ef1eac8 => /Users/gjaswal/Documents/codebase/guidewire-oss/ocm
# open-cluster-management.io/ocm v0.15.1-0.20250120013556-eeb4ab31d5ab
## explicit; go 1.22.5
open-cluster-management.io/ocm/deploy/cluster-manager/chart
open-cluster-management.io/ocm/deploy/klusterlet/chart
Expand Down Expand Up @@ -1432,4 +1432,3 @@ sigs.k8s.io/structured-merge-diff/v4/value
sigs.k8s.io/yaml
sigs.k8s.io/yaml/goyaml.v2
sigs.k8s.io/yaml/goyaml.v3
# open-cluster-management.io/ocm => /Users/gjaswal/Documents/codebase/guidewire-oss/ocm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5fa2b6

Please sign in to comment.