@@ -115,37 +115,46 @@ func GetToken(kubeClient kubernetes.Interface) (string, TokenType, error) {
115115 return token , ServiceAccountToken , nil
116116}
117117
118- //GetBootstrapToken returns the service-account token in kube-system
119- func GetBootstrapToken (kubeClient kubernetes.Interface ) (string , error ) {
118+ //GetBootstrapSecret returns the secret in kube-system
119+ func GetBootstrapSecret (kubeClient kubernetes.Interface ) (* corev1. Secret , error ) {
120120 var bootstrapSecret * corev1.Secret
121121 l , err := kubeClient .CoreV1 ().
122122 Secrets ("kube-system" ).
123123 List (context .TODO (), metav1.ListOptions {LabelSelector : fmt .Sprintf ("%v = %v" , config .LabelApp , config .ClusterManagerName )})
124124 if err != nil {
125- return "" , err
125+ return nil , err
126126 }
127127 for _ , s := range l .Items {
128128 if strings .HasPrefix (s .Name , config .BootstrapSecretPrefix ) {
129129 bootstrapSecret = & s
130130 }
131131 }
132- if bootstrapSecret != nil {
133- return fmt .Sprintf ("%s.%s" , string (bootstrapSecret .Data ["token-id" ]), string (bootstrapSecret .Data ["token-secret" ])), nil
132+ if bootstrapSecret == nil {
133+ return nil , errors .NewNotFound (schema.GroupResource {
134+ Group : corev1 .GroupName ,
135+ Resource : "secrets" },
136+ fmt .Sprintf ("%s*" , config .BootstrapSecretPrefix ))
137+
134138 }
135- return "" , errors .NewNotFound (schema.GroupResource {
136- Group : corev1 .GroupName ,
137- Resource : "secrets" },
138- fmt .Sprintf ("%s*" , config .BootstrapSecretPrefix ))
139+ return bootstrapSecret , err
139140}
140141
141- //GetBootstrapSecretFromSA retrieves the service-account token secret
142- func GetBootstrapTokenFromSA (
143- kubeClient kubernetes.Interface ) (string , error ) {
142+ //GetBootstrapToken returns the token in kube-system
143+ func GetBootstrapToken (kubeClient kubernetes.Interface ) (string , error ) {
144+ bootstrapSecret , err := GetBootstrapSecret (kubeClient )
145+ if err != nil {
146+ return "" , err
147+ }
148+ return fmt .Sprintf ("%s.%s" , string (bootstrapSecret .Data ["token-id" ]), string (bootstrapSecret .Data ["token-secret" ])), nil
149+ }
150+
151+ func GetBootstrapSecretFromSA (
152+ kubeClient kubernetes.Interface ) (* corev1.Secret , error ) {
144153 sa , err := kubeClient .CoreV1 ().
145154 ServiceAccounts (config .OpenClusterManagementNamespace ).
146155 Get (context .TODO (), config .BootstrapSAName , metav1.GetOptions {})
147156 if err != nil {
148- return "" , err
157+ return nil , err
149158 }
150159 var secret * corev1.Secret
151160 for _ , objectRef := range sa .Secrets {
@@ -169,12 +178,22 @@ func GetBootstrapTokenFromSA(
169178 }
170179 }
171180 if secret == nil {
172- return "" , fmt .Errorf ("secret with prefix %s and type %s not found in service account %s/%s" ,
181+ return nil , fmt .Errorf ("secret with prefix %s and type %s not found in service account %s/%s" ,
173182 config .BootstrapSAName ,
174183 corev1 .SecretTypeServiceAccountToken ,
175184 config .OpenClusterManagementNamespace ,
176185 config .BootstrapSAName )
177186 }
187+ return secret , nil
188+ }
189+
190+ //GetBootstrapSecretFromSA retrieves the service-account token secret
191+ func GetBootstrapTokenFromSA (
192+ kubeClient kubernetes.Interface ) (string , error ) {
193+ secret , err := GetBootstrapSecretFromSA (kubeClient )
194+ if err != nil {
195+ return "" , err
196+ }
178197 return string (secret .Data ["token" ]), nil
179198}
180199
0 commit comments