Skip to content

Commit 192ab0a

Browse files
jeffw17alex0chan
authored andcommitted
Add two flags for init to capture csr users and aws arn patterns to whitelist for auto approval
Signed-off-by: “Jeffrey <[email protected]> Signed-off-by: Alex <[email protected]>
1 parent 32f8ec8 commit 192ab0a

11 files changed

+84
-11
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
k8s.io/klog/v2 v2.130.1
2929
k8s.io/kubectl v0.31.1
3030
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6
31-
open-cluster-management.io/api v0.15.1-0.20250116010516-3a595d6a4e40
31+
open-cluster-management.io/api v0.15.1-0.20250219064651-4281b7684d9b
3232
open-cluster-management.io/cluster-proxy v0.4.0
3333
open-cluster-management.io/managed-serviceaccount v0.6.0
3434
open-cluster-management.io/ocm v0.15.1-0.20250120013556-eeb4ab31d5ab

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,8 @@ k8s.io/kubectl v0.31.1 h1:ih4JQJHxsEggFqDJEHSOdJ69ZxZftgeZvYo7M/cpp24=
557557
k8s.io/kubectl v0.31.1/go.mod h1:aNuQoR43W6MLAtXQ/Bu4GDmoHlbhHKuyD49lmTC8eJM=
558558
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 h1:MDF6h2H/h4tbzmtIKTuctcwZmY0tY9mD9fNT47QO6HI=
559559
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
560-
open-cluster-management.io/api v0.15.1-0.20250116010516-3a595d6a4e40 h1:LckTHZ68rcy3hDFu6wa7BVOJ9wbWItJLZXmi0bpMyh8=
561-
open-cluster-management.io/api v0.15.1-0.20250116010516-3a595d6a4e40/go.mod h1:9erZEWEn4bEqh0nIX2wA7f/s3KCuFycQdBrPrRzi0QM=
560+
open-cluster-management.io/api v0.15.1-0.20250219064651-4281b7684d9b h1:1ScdOKBMLbzA/k84P9Z64uSq3sxRclquej3tT1zhsqU=
561+
open-cluster-management.io/api v0.15.1-0.20250219064651-4281b7684d9b/go.mod h1:9erZEWEn4bEqh0nIX2wA7f/s3KCuFycQdBrPrRzi0QM=
562562
open-cluster-management.io/cluster-proxy v0.4.0 h1:rm0UDaDWe3/P3xLzwqdHtqNksKwSzsic02MkrEe6BnM=
563563
open-cluster-management.io/cluster-proxy v0.4.0/go.mod h1:gTvfDHAhGezhdg4BD3ECBn6jbg2Y5PbHhV2ceW5nrB0=
564564
open-cluster-management.io/managed-serviceaccount v0.6.0 h1:qIi5T9WQJBuoGqnYGIktXbtqfQoiN2H9XU2P/6lAQiw=

pkg/cmd/init/cmd.go

+4
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,9 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
8686
cmd.Flags().StringVar(&o.hubClusterArn, "hub-cluster-arn", "",
8787
"The hubCluster ARN to be passed if awsirsa is one of the registrationAuths and the cluster name in EKS kubeconfig doesn't contain hubClusterArn")
8888

89+
cmd.Flags().StringSliceVar(&o.autoApprovedCSRIdentities, "auto-approved-csr-identities", []string{},
90+
"The users or identities that can be auto approved for CSR and auto accepted to join with hub cluster")
91+
cmd.Flags().StringSliceVar(&o.autoApprovedARNPatterns, "auto-approved-arn-patterns", []string{},
92+
"List of AWS EKS ARN patterns so any EKS clusters with these patterns will be auto accepted to join with hub cluster")
8993
return cmd
9094
}

pkg/cmd/init/exec.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ package init
44
import (
55
"context"
66
"fmt"
7-
"k8s.io/apimachinery/pkg/util/sets"
87
"os"
98
"time"
109

10+
"k8s.io/apimachinery/pkg/util/sets"
11+
1112
"github.com/spf13/cobra"
1213
"github.com/spf13/pflag"
1314
corev1 "k8s.io/api/core/v1"
@@ -75,6 +76,7 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
7576
if err != nil {
7677
return err
7778
}
79+
7880
o.clusterManagerChartConfig.ClusterManager = chart.ClusterManagerConfig{
7981
RegistrationConfiguration: operatorv1.RegistrationHubConfiguration{
8082
FeatureGates: genericclioptionsclusteradm.ConvertToFeatureGateAPI(
@@ -157,6 +159,32 @@ func (o *Options) validate() error {
157159
}
158160
}
159161

162+
featureGates := o.clusterManagerChartConfig.ClusterManager.RegistrationConfiguration.FeatureGates
163+
managedClusterAutoApprove := false
164+
165+
for _, feature := range featureGates {
166+
if feature.Feature == "featuregate/ManagedClusterAutoApproval" {
167+
if feature.Mode == "Enabled" {
168+
managedClusterAutoApprove = true
169+
}
170+
}
171+
}
172+
173+
if managedClusterAutoApprove {
174+
// If hub registration does not accept awsirsa, we stop user if they also pass in a list of patterns for AWS EKS ARN.
175+
176+
if len(o.autoApprovedARNPatterns) > 0 && !sets.New[string](o.registrationAuth...).Has("awsirsa") {
177+
return fmt.Errorf("should not provide list of patterns for aws eks arn if not initializing hub with awsirsa registration")
178+
}
179+
180+
// If hub registration does not accept csr, we stop user if they also pass in a list of users for CSR auto approval.
181+
if len(o.autoApprovedCSRIdentities) > 0 && !sets.New[string](o.registrationAuth...).Has("csr") {
182+
return fmt.Errorf("should not provide list of users for csr to auto approve if not initializing hub with csr registration")
183+
}
184+
} else if len(o.autoApprovedARNPatterns) > 0 || len(o.autoApprovedCSRIdentities) > 0 {
185+
return fmt.Errorf("should enable feature gate ManagedClusterAutoApproval before passing list of identities")
186+
}
187+
160188
// If --wait is set, some information during initialize process will print to output, the output would not keep
161189
// machine readable, so this behavior should be disabled
162190
if o.wait && o.output != "text" {
@@ -373,17 +401,16 @@ func getRegistrationDrivers(o *Options) ([]operatorv1.RegistrationDriverHub, err
373401

374402
for _, driver := range o.registrationAuth {
375403
if driver == "csr" {
376-
registrationDriver = operatorv1.RegistrationDriverHub{AuthType: driver}
404+
registrationDriver = operatorv1.RegistrationDriverHub{AuthType: driver, AutoApprovedIdentities: o.autoApprovedCSRIdentities}
377405
} else if driver == "awsirsa" {
378406
hubClusterArn, err := getHubClusterArn(o)
379407
if err != nil {
380408
return registrationDrivers, err
381409
}
382-
registrationDriver = operatorv1.RegistrationDriverHub{AuthType: driver, HubClusterArn: hubClusterArn}
410+
registrationDriver = operatorv1.RegistrationDriverHub{AuthType: driver, HubClusterArn: hubClusterArn, AutoApprovedIdentities: o.autoApprovedARNPatterns}
383411
}
384412
registrationDrivers = append(registrationDrivers, registrationDriver)
385413
}
386-
387414
return registrationDrivers, nil
388415
}
389416

pkg/cmd/init/options.go

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ type Options struct {
5656
// The optional ARN to pass if awsirsa is one of the registrationAuths
5757
// and the cluster name in EKS kubeconfig doesn't contain hubClusterArn
5858
hubClusterArn string
59+
60+
// A list of users that can be auto approve csr and auto accept to join hub cluster
61+
autoApprovedCSRIdentities []string
62+
// A list of AWS EKS ARN patterns that are accepted and whatever matches can be auto accepted to join hub cluster
63+
autoApprovedARNPatterns []string
5964
}
6065

6166
func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericiooptions.IOStreams) *Options {

test/e2e/clusteradm/init_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ var _ = ginkgo.Describe("test clusteradm with bootstrap token in singleton mode"
6969
//gomega.Expect(cm.Spec.RegistrationConfiguration.RegistrationDrivers[1].HubClusterArn).
7070
// Should(gomega.Equal("arn:aws:eks:us-west-2:123456789012:cluster/hub-cluster1"))
7171

72+
err = e2e.Clusteradm().Init(
73+
"--use-bootstrap-token",
74+
"--context", e2e.Cluster().Hub().Context(),
75+
"--bundle-version=latest",
76+
"--registration-auth awsirsa,csr",
77+
"--auto-approved-csr-identities csr1",
78+
"--auto-approved-arn-patterns arn:aws:eks:us-west-2:123456789012:cluster/*",
79+
)
80+
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")
81+
cm, err = operatorClient.OperatorV1().ClusterManagers().Get(context.TODO(), "cluster-manager", metav1.GetOptions{})
82+
gomega.Expect(err).NotTo(gomega.HaveOccurred())
83+
// Ensure that the auto approval identities contain user for CSR and pattern for AWS
84+
gomega.Expect(cm.Spec.RegistrationConfiguration.RegistrationDrivers[0].AuthType).Should(gomega.Equal("csr"))
85+
gomega.Expect(cm.Spec.RegistrationConfiguration.RegistrationDrivers[1].AuthType).Should(gomega.Equal("awsirsa"))
86+
gomega.Expect(cm.Spec.RegistrationConfiguration.RegistrationDrivers[0].AutoApprovedIdentities[0]).Should(gomega.Equal("csr1"))
87+
gomega.Expect(cm.Spec.RegistrationConfiguration.RegistrationDrivers[1].AutoApprovedIdentities[0]).Should(gomega.Equal("arn:aws:eks:us-west-2:123456789012:cluster/*"))
88+
7289
err = e2e.Clusteradm().Init(
7390
"--use-bootstrap-token",
7491
"--context", e2e.Cluster().Hub().Context(),

vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ k8s.io/utils/pointer
12391239
k8s.io/utils/ptr
12401240
k8s.io/utils/strings/slices
12411241
k8s.io/utils/trace
1242-
# open-cluster-management.io/api v0.15.1-0.20250116010516-3a595d6a4e40
1242+
# open-cluster-management.io/api v0.15.1-0.20250219064651-4281b7684d9b
12431243
## explicit; go 1.22.0
12441244
open-cluster-management.io/api/addon/v1alpha1
12451245
open-cluster-management.io/api/client/addon/clientset/versioned

vendor/open-cluster-management.io/api/operator/v1/0000_01_operator.open-cluster-management.io_clustermanagers.crd.yaml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/operator/v1/types_clustermanager.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/operator/v1/zz_generated.deepcopy.go

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/open-cluster-management.io/api/operator/v1/zz_generated.swagger_doc_generated.go

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)