@@ -10,6 +10,7 @@ import (
1010 corev1 "k8s.io/api/core/v1"
1111 "k8s.io/apimachinery/pkg/api/errors"
1212 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+ "k8s.io/apimachinery/pkg/util/sets"
1314 "k8s.io/apimachinery/pkg/util/wait"
1415 "k8s.io/client-go/kubernetes"
1516 "open-cluster-management.io/clusteradm/pkg/helpers"
@@ -93,7 +94,6 @@ func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *cluste
9394 return true , nil
9495 }
9596 return false , nil
96-
9797}
9898
9999func (o * Options ) approveCSR (kubeClient * kubernetes.Clientset , clusterName string ) (bool , error ) {
@@ -114,60 +114,58 @@ func (o *Options) approveCSR(kubeClient *kubernetes.Clientset, clusterName strin
114114 continue
115115 }
116116 //Check groups
117- var group string
118- for _ , g := range item .Spec .Groups {
119- if g == groupName {
120- group = g
121- break
122- }
123- }
124- //Does not contain the correct group
125- if len (group ) == 0 {
117+ groups := sets .NewString (item .Spec .Groups ... )
118+ if ! groups .Has (groupName ) {
126119 continue
127120 }
128121 //Check if already approved or denied
129- for _ , c := range item .Status .Conditions {
130- if c .Type == certificatesv1 .CertificateApproved {
131- fmt .Printf ("CSR %s already approved\n " , item .Name )
132- return true , nil
133- }
134- if c .Type == certificatesv1 .CertificateDenied {
135- fmt .Printf ("CSR %s already denied\n " , item .Name )
136- return true , nil
137- }
122+ approved , denied := GetCertApprovalCondition (& item .Status )
123+ if approved {
124+ fmt .Printf ("CSR %s already approved\n " , item .Name )
125+ }
126+ if denied {
127+ fmt .Printf ("CSR %s already denied\n " , item .Name )
128+ }
129+ //if alreaady approved or denied, then nothing to do
130+ if approved || denied {
131+ return true , nil
138132 }
139133 csr = & item
140134 break
141135 }
142136
143- if csr != nil {
144- if ! o .ClusteradmFlags .DryRun {
145- if csr .Status .Conditions == nil {
146- csr .Status .Conditions = make ([]certificatesv1.CertificateSigningRequestCondition , 0 )
147- }
137+ //no csr found
138+ if csr == nil {
139+ fmt .Printf ("no CSR to approve for cluster %s\n " , clusterName )
140+ return false , nil
141+ }
142+ //if dry-run don't approve
143+ if o .ClusteradmFlags .DryRun {
144+ return true , nil
145+ }
146+ if csr .Status .Conditions == nil {
147+ csr .Status .Conditions = make ([]certificatesv1.CertificateSigningRequestCondition , 0 )
148+ }
148149
149- csr .Status .Conditions = append (csr .Status .Conditions , certificatesv1.CertificateSigningRequestCondition {
150- Status : corev1 .ConditionTrue ,
151- Type : certificatesv1 .CertificateApproved ,
152- Reason : fmt .Sprintf ("%sApprove" , helpers .GetExampleHeader ()),
153- Message : fmt .Sprintf ("This CSR was approved by %s certificate approve." , helpers .GetExampleHeader ()),
154- LastUpdateTime : metav1 .Now (),
155- })
150+ csr .Status .Conditions = append (csr .Status .Conditions , certificatesv1.CertificateSigningRequestCondition {
151+ Status : corev1 .ConditionTrue ,
152+ Type : certificatesv1 .CertificateApproved ,
153+ Reason : fmt .Sprintf ("%sApprove" , helpers .GetExampleHeader ()),
154+ Message : fmt .Sprintf ("This CSR was approved by %s certificate approve." , helpers .GetExampleHeader ()),
155+ LastUpdateTime : metav1 .Now (),
156+ })
156157
157- kubeClient , err := o .ClusteradmFlags .KubectlFactory .KubernetesClientSet ()
158- if err != nil {
159- return false , err
160- }
161- signingRequest := kubeClient .CertificatesV1 ().CertificateSigningRequests ()
162- if _ , err := signingRequest .UpdateApproval (context .TODO (), csr .Name , csr , metav1.UpdateOptions {}); err != nil {
163- return false , err
164- }
165- }
166- fmt .Printf ("CSR %s approved\n " , csr .Name )
167- return true , nil
158+ kubeClient , err = o .ClusteradmFlags .KubectlFactory .KubernetesClientSet ()
159+ if err != nil {
160+ return false , err
168161 }
169- fmt .Printf ("no CSR to approve for cluster %s\n " , clusterName )
170- return false , nil
162+ signingRequest := kubeClient .CertificatesV1 ().CertificateSigningRequests ()
163+ if _ , err := signingRequest .UpdateApproval (context .TODO (), csr .Name , csr , metav1.UpdateOptions {}); err != nil {
164+ return false , err
165+ }
166+
167+ fmt .Printf ("CSR %s approved\n " , csr .Name )
168+ return true , nil
171169}
172170
173171func (o * Options ) updateManagedCluster (clusterClient * clusterclientset.Clientset , clusterName string ) (bool , error ) {
@@ -187,10 +185,36 @@ func (o *Options) updateManagedCluster(clusterClient *clusterclientset.Clientset
187185 if err != nil {
188186 return false , err
189187 }
188+ fmt .Printf ("set httpAcceptsClient to true for cluster %s\n " , clusterName )
190189 }
191- fmt .Printf ("set httpAcceptsClient to true for cluster %s\n " , clusterName )
192190 } else {
193191 fmt .Printf ("httpAcceptsClient already set for cluster %s\n " , clusterName )
194192 }
195193 return true , nil
196194}
195+
196+ // func isCSRApprovedOrDenied(csr certificatesv1.CertificateSigningRequest) bool {
197+ // for _, c := range csr.Status.Conditions {
198+ // if c.Type == certificatesv1.CertificateApproved {
199+ // fmt.Printf("CSR %s already approved\n", csr.Name)
200+ // return true
201+ // }
202+ // if c.Type == certificatesv1.CertificateDenied {
203+ // fmt.Printf("CSR %s already denied\n", csr.Name)
204+ // return true
205+ // }
206+ // }
207+ // return false
208+ // }
209+
210+ func GetCertApprovalCondition (status * certificatesv1.CertificateSigningRequestStatus ) (approved bool , denied bool ) {
211+ for _ , c := range status .Conditions {
212+ if c .Type == certificatesv1 .CertificateApproved {
213+ approved = true
214+ }
215+ if c .Type == certificatesv1 .CertificateDenied {
216+ denied = true
217+ }
218+ }
219+ return
220+ }
0 commit comments