@@ -5,9 +5,12 @@ import (
55 "context"
66 "fmt"
77 "strings"
8+ "time"
89
910 corev1 "k8s.io/api/core/v1"
11+ "k8s.io/apimachinery/pkg/api/errors"
1012 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+ "k8s.io/apimachinery/pkg/util/wait"
1114 "k8s.io/client-go/kubernetes"
1215 "open-cluster-management.io/clusteradm/pkg/helpers"
1316
@@ -63,93 +66,122 @@ func (o *Options) run() error {
6366
6467func (o * Options ) runWithClient (kubeClient * kubernetes.Clientset , clusterClient * clusterclientset.Clientset ) error {
6568 for _ , clusterName := range o .values .clusters {
66-
67- csrs , err := kubeClient .CertificatesV1 ().CertificateSigningRequests ().List (context .TODO (),
68- metav1.ListOptions {
69- LabelSelector : fmt .Sprintf ("%v = %v" , clusterLabel , clusterName ),
70- })
69+ err := wait .PollImmediate (1 * time .Second , time .Duration (o .timeout )* time .Second , func () (bool , error ) {
70+ csrApproved , err := o .approveCSR (kubeClient , clusterName )
71+ if err != nil {
72+ return false , err
73+ }
74+ mcUpdated , err := o .updateManagedCluster (clusterClient , clusterName )
75+ if err != nil {
76+ return false , err
77+ }
78+ if csrApproved && mcUpdated {
79+ return true , nil
80+ }
81+ return false , nil
82+ })
7183 if err != nil {
7284 return err
7385 }
74- var csr * certificatesv1.CertificateSigningRequest
75- for _ , item := range csrs .Items {
76- //Does not have the correct name prefix
77- if ! strings .HasPrefix (item .Spec .Username , userNameSignaturePrefix ) {
78- continue
79- }
80- //Check groups
81- var group string
82- for _ , g := range item .Spec .Groups {
83- if g == groupName {
84- group = g
85- break
86- }
87- }
88- //Does not contain the correct group
89- if len (group ) == 0 {
90- continue
86+ }
87+ return nil
88+ }
89+
90+ func (o * Options ) approveCSR (kubeClient * kubernetes.Clientset , clusterName string ) (bool , error ) {
91+ csrs , err := kubeClient .CertificatesV1 ().CertificateSigningRequests ().List (context .TODO (),
92+ metav1.ListOptions {
93+ LabelSelector : fmt .Sprintf ("%v = %v" , clusterLabel , clusterName ),
94+ })
95+ if err != nil {
96+ if errors .IsNotFound (err ) {
97+ return false , nil
98+ }
99+ return false , err
100+ }
101+ var csr * certificatesv1.CertificateSigningRequest
102+ for _ , item := range csrs .Items {
103+ //Does not have the correct name prefix
104+ if ! strings .HasPrefix (item .Spec .Username , userNameSignaturePrefix ) {
105+ continue
106+ }
107+ //Check groups
108+ var group string
109+ for _ , g := range item .Spec .Groups {
110+ if g == groupName {
111+ group = g
112+ break
91113 }
92- //Check if already approved or denied
93- done := false
94- for _ , c := range item .Status .Conditions {
95- if c .Type == certificatesv1 .CertificateApproved || c .Type == certificatesv1 .CertificateDenied {
96- done = true
97- break
98- }
114+ }
115+ //Does not contain the correct group
116+ if len (group ) == 0 {
117+ continue
118+ }
119+ //Check if already approved or denied
120+ for _ , c := range item .Status .Conditions {
121+ if c .Type == certificatesv1 .CertificateApproved {
122+ fmt .Printf ("CSR %s already approved\n " , item .Name )
123+ return true , nil
99124 }
100- if done {
101- continue
125+ if c .Type == certificatesv1 .CertificateDenied {
126+ fmt .Printf ("CSR %s already denied\n " , item .Name )
127+ return true , nil
102128 }
103- csr = & item
104- break
105129 }
130+ csr = & item
131+ break
132+ }
106133
107- if csr != nil {
108- if ! o .ClusteradmFlags .DryRun {
109- if csr .Status .Conditions == nil {
110- csr .Status .Conditions = make ([]certificatesv1.CertificateSigningRequestCondition , 0 )
111- }
134+ if csr != nil {
135+ if ! o .ClusteradmFlags .DryRun {
136+ if csr .Status .Conditions == nil {
137+ csr .Status .Conditions = make ([]certificatesv1.CertificateSigningRequestCondition , 0 )
138+ }
112139
113- csr .Status .Conditions = append (csr .Status .Conditions , certificatesv1.CertificateSigningRequestCondition {
114- Status : corev1 .ConditionTrue ,
115- Type : certificatesv1 .CertificateApproved ,
116- Reason : fmt .Sprintf ("%sApprove" , helpers .GetExampleHeader ()),
117- Message : fmt .Sprintf ("This CSR was approved by %s certificate approve." , helpers .GetExampleHeader ()),
118- LastUpdateTime : metav1 .Now (),
119- })
140+ csr .Status .Conditions = append (csr .Status .Conditions , certificatesv1.CertificateSigningRequestCondition {
141+ Status : corev1 .ConditionTrue ,
142+ Type : certificatesv1 .CertificateApproved ,
143+ Reason : fmt .Sprintf ("%sApprove" , helpers .GetExampleHeader ()),
144+ Message : fmt .Sprintf ("This CSR was approved by %s certificate approve." , helpers .GetExampleHeader ()),
145+ LastUpdateTime : metav1 .Now (),
146+ })
120147
121- kubeClient , err := o .ClusteradmFlags .KubectlFactory .KubernetesClientSet ()
122- if err != nil {
123- return err
124- }
125- signingRequest := kubeClient .CertificatesV1 ().CertificateSigningRequests ()
126- if _ , err := signingRequest .UpdateApproval (context .TODO (), csr .Name , csr , metav1.UpdateOptions {}); err != nil {
127- return err
128- }
148+ kubeClient , err := o .ClusteradmFlags .KubectlFactory .KubernetesClientSet ()
149+ if err != nil {
150+ return false , err
151+ }
152+ signingRequest := kubeClient .CertificatesV1 ().CertificateSigningRequests ()
153+ if _ , err := signingRequest .UpdateApproval (context .TODO (), csr .Name , csr , metav1.UpdateOptions {}); err != nil {
154+ return false , err
129155 }
130- fmt .Printf ("CSR %s approved\n " , csr .Name )
131- } else {
132- fmt .Printf ("no CSR to approve for cluster %s\n " , clusterName )
133156 }
157+ fmt .Printf ("CSR %s approved\n " , csr .Name )
158+ return true , nil
159+ }
160+ fmt .Printf ("no CSR to approve for cluster %s\n " , clusterName )
161+ return false , nil
162+ }
134163
135- mc , err := clusterClient .ClusterV1 ().ManagedClusters ().Get (context .TODO (),
136- clusterName ,
137- metav1.GetOptions {})
138- if err != nil {
139- return err
164+ func (o * Options ) updateManagedCluster (clusterClient * clusterclientset.Clientset , clusterName string ) (bool , error ) {
165+ mc , err := clusterClient .ClusterV1 ().ManagedClusters ().Get (context .TODO (),
166+ clusterName ,
167+ metav1.GetOptions {})
168+ if err != nil {
169+ if errors .IsNotFound (err ) {
170+ return false , nil
140171 }
141- if ! mc .Spec .HubAcceptsClient {
142- if ! o .ClusteradmFlags .DryRun {
143- mc .Spec .HubAcceptsClient = true
144- _ , err = clusterClient .ClusterV1 ().ManagedClusters ().Update (context .TODO (), mc , metav1.UpdateOptions {})
145- if err != nil {
146- return err
147- }
172+ return false , err
173+ }
174+ if ! mc .Spec .HubAcceptsClient {
175+ if ! o .ClusteradmFlags .DryRun {
176+ mc .Spec .HubAcceptsClient = true
177+ _ , err = clusterClient .ClusterV1 ().ManagedClusters ().Update (context .TODO (), mc , metav1.UpdateOptions {})
178+ if err != nil {
179+ return false , err
148180 }
149- fmt .Printf ("set httpAcceptsClient to true for cluster %s\n " , clusterName )
150- } else {
151- fmt .Printf ("httpAcceptsClient already set for cluster %s\n " , clusterName )
152181 }
182+ fmt .Printf ("set httpAcceptsClient to true for cluster %s\n " , clusterName )
183+ } else {
184+ fmt .Printf ("httpAcceptsClient already set for cluster %s\n " , clusterName )
153185 }
154- return nil
186+ return true , nil
155187}
0 commit comments