Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 17d21fa

Browse files
committed
refactor: More clearly define secret names in host and joining clusters
1 parent 02afe88 commit 17d21fa

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

pkg/kubefedctl/join.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ type joinFederation struct {
9494
}
9595

9696
type joinFederationOptions struct {
97-
secretName string
98-
scope apiextv1.ResourceScope
99-
errorOnExisting bool
97+
hostClusterSecretName string
98+
scope apiextv1.ResourceScope
99+
errorOnExisting bool
100100
}
101101

102102
// Bind adds the join specific arguments to the flagset passed in as an
103103
// argument.
104104
func (o *joinFederationOptions) Bind(flags *pflag.FlagSet) {
105-
flags.StringVar(&o.secretName, "secret-name", "",
105+
flags.StringVar(&o.hostClusterSecretName, "secret-name", "",
106106
"Name of the secret where the cluster's credentials will be stored in the host cluster. This name should be a valid RFC 1035 label. If unspecified, defaults to a generated name containing the cluster name.")
107107
flags.BoolVar(&o.errorOnExisting, "error-on-existing", true,
108108
"Whether the join operation will throw an error if it encounters existing artifacts with the same name as those it's trying to create. If false, the join operation will update existing artifacts to match its own specification.")
@@ -161,7 +161,7 @@ func (j *joinFederation) Complete(args []string) error {
161161

162162
klog.V(2).Infof("Args and flags: name %s, host: %s, host-system-namespace: %s, kubeconfig: %s, cluster-context: %s, secret-name: %s, dry-run: %v",
163163
j.ClusterName, j.HostClusterContext, j.KubeFedNamespace, j.Kubeconfig, j.ClusterContext,
164-
j.secretName, j.DryRun)
164+
j.hostClusterSecretName, j.DryRun)
165165

166166
return nil
167167
}
@@ -198,7 +198,7 @@ func (j *joinFederation) Run(cmdOut io.Writer, config util.FedConfig) error {
198198
}
199199

200200
_, err = JoinCluster(hostConfig, clusterConfig, j.KubeFedNamespace,
201-
hostClusterName, j.ClusterName, j.secretName, j.joinFederationOptions.scope, j.DryRun, j.errorOnExisting)
201+
hostClusterName, j.ClusterName, j.hostClusterSecretName, j.joinFederationOptions.scope, j.DryRun, j.errorOnExisting)
202202

203203
return err
204204
}
@@ -207,18 +207,18 @@ func (j *joinFederation) Run(cmdOut io.Writer, config util.FedConfig) error {
207207
// KubeFed namespace in the joining cluster will be the same as in the
208208
// host cluster.
209209
func JoinCluster(hostConfig, clusterConfig *rest.Config, kubefedNamespace,
210-
hostClusterName, joiningClusterName, secretName string,
210+
hostClusterName, joiningClusterName, hostClusterSecretName string,
211211
scope apiextv1.ResourceScope, dryRun, errorOnExisting bool) (*fedv1b1.KubeFedCluster, error) {
212212
return joinClusterForNamespace(hostConfig, clusterConfig, kubefedNamespace,
213-
kubefedNamespace, hostClusterName, joiningClusterName, secretName,
213+
kubefedNamespace, hostClusterName, joiningClusterName, hostClusterSecretName,
214214
scope, dryRun, errorOnExisting)
215215
}
216216

217217
// joinClusterForNamespace registers a cluster with a KubeFed control
218218
// plane. The KubeFed namespace in the joining cluster is provided by
219219
// the joiningNamespace parameter.
220220
func joinClusterForNamespace(hostConfig, clusterConfig *rest.Config, kubefedNamespace,
221-
joiningNamespace, hostClusterName, joiningClusterName, secretName string,
221+
joiningNamespace, hostClusterName, joiningClusterName, hostClusterSecretName string,
222222
scope apiextv1.ResourceScope, dryRun, errorOnExisting bool) (*fedv1b1.KubeFedCluster, error) {
223223
start := time.Now()
224224

@@ -255,15 +255,16 @@ func joinClusterForNamespace(hostConfig, clusterConfig *rest.Config, kubefedName
255255
}
256256
klog.V(2).Infof("Created %s namespace in joining cluster", joiningNamespace)
257257

258-
saName, err := createAuthorizedServiceAccount(clusterClientset,
258+
joiningClusterSATokenSecretName, err := createAuthorizedServiceAccount(clusterClientset,
259259
joiningNamespace, joiningClusterName, hostClusterName,
260260
scope, dryRun, errorOnExisting)
261261
if err != nil {
262262
return nil, err
263263
}
264264

265265
secret, caBundle, err := populateSecretInHostCluster(clusterClientset, hostClientset,
266-
saName, kubefedNamespace, joiningNamespace, joiningClusterName, secretName, dryRun, errorOnExisting)
266+
joiningClusterSATokenSecretName, kubefedNamespace, joiningNamespace, joiningClusterName,
267+
hostClusterSecretName, dryRun, errorOnExisting)
267268
if err != nil {
268269
klog.V(2).Infof("Error creating secret in host cluster: %s due to: %v", hostClusterName, err)
269270
return nil, err
@@ -424,7 +425,7 @@ func createKubeFedNamespace(clusterClientset kubeclient.Interface, kubefedNamesp
424425
// account is returned on success.
425426
func createAuthorizedServiceAccount(joiningClusterClientset kubeclient.Interface,
426427
namespace, joiningClusterName, hostClusterName string,
427-
scope apiextv1.ResourceScope, dryRun, errorOnExisting bool) (string, error) {
428+
scope apiextv1.ResourceScope, dryRun, errorOnExisting bool) (saTokenSecretName string, err error) {
428429
klog.V(2).Infof("Creating service account in joining cluster: %s", joiningClusterName)
429430

430431
saName, err := createServiceAccount(joiningClusterClientset, namespace,
@@ -437,15 +438,15 @@ func createAuthorizedServiceAccount(joiningClusterClientset kubeclient.Interface
437438

438439
klog.V(2).Infof("Created service account: %s in joining cluster: %s", saName, joiningClusterName)
439440

440-
secretName, err := createServiceAccountTokenSecret(saName, joiningClusterClientset, namespace,
441+
saTokenSecretName, err = createServiceAccountTokenSecret(saName, joiningClusterClientset, namespace,
441442
joiningClusterName, hostClusterName, dryRun, errorOnExisting)
442443
if err != nil {
443-
klog.V(2).Infof("Error creating service account: %s in joining cluster: %s due to: %v",
444+
klog.V(2).Infof("Error creating service account token secret: %s in joining cluster: %s due to: %v",
444445
saName, joiningClusterName, err)
445446
return "", err
446447
}
447448

448-
klog.V(2).Infof("Created service account token secret: %s in joining cluster: %s", secretName, joiningClusterName)
449+
klog.V(2).Infof("Created service account token secret: %s in joining cluster: %s", saTokenSecretName, joiningClusterName)
449450

450451
if scope == apiextv1.NamespaceScoped {
451452
klog.V(2).Infof("Creating role and binding for service account: %s in joining cluster: %s", saName, joiningClusterName)
@@ -485,7 +486,7 @@ func createAuthorizedServiceAccount(joiningClusterClientset kubeclient.Interface
485486
saName, joiningClusterName)
486487
}
487488

488-
return saName, nil
489+
return saTokenSecretName, nil
489490
}
490491

491492
// createServiceAccount creates a service account in the cluster associated
@@ -523,7 +524,9 @@ func createServiceAccount(clusterClientset kubeclient.Interface, namespace,
523524
default:
524525
return saName, nil
525526
}
526-
} // createServiceAccount creates a service account in the cluster associated
527+
}
528+
529+
// createServiceAccount creates a service account in the cluster associated
527530
// with clusterClientset with credentials that will be used by the host cluster
528531
// to access its API server.
529532
func createServiceAccountTokenSecret(saName string, clusterClientset kubeclient.Interface, namespace,
@@ -880,7 +883,7 @@ func createHealthCheckClusterRoleAndBinding(clientset kubeclient.Interface, saNa
880883
// hostClientset, putting it in a secret named secretName in the provided
881884
// namespace.
882885
func populateSecretInHostCluster(clusterClientset, hostClientset kubeclient.Interface,
883-
saName, hostNamespace, joiningNamespace, joiningClusterName, secretName string,
886+
saTokenSecretName, hostNamespace, joiningNamespace, joiningClusterName, secretName string,
884887
dryRun bool, errorOnExisting bool) (*corev1.Secret, []byte, error) {
885888
klog.V(2).Infof("Creating cluster credentials secret in host cluster")
886889

@@ -892,9 +895,10 @@ func populateSecretInHostCluster(clusterClientset, hostClientset kubeclient.Inte
892895

893896
// Get the secret from the joining cluster.
894897
var secret *corev1.Secret
898+
895899
err := wait.PollImmediate(1*time.Second, serviceAccountSecretTimeout, func() (bool, error) {
896900
joiningClusterSASecret, err := clusterClientset.CoreV1().Secrets(joiningNamespace).Get(
897-
context.Background(), saName, metav1.GetOptions{},
901+
context.Background(), saTokenSecretName, metav1.GetOptions{},
898902
)
899903
if err != nil {
900904
return false, nil

pkg/kubefedctl/util/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func ClusterServiceAccountName(joiningClusterName, hostClusterName string) strin
107107
return fmt.Sprintf("%s-%s", joiningClusterName, hostClusterName)
108108
}
109109

110-
// ClusterServiceAccountTokenName returns the name of a service account token secret whose
110+
// ClusterServiceAccountTokenSecretName returns the name of a service account token secret whose
111111
// credentials are used by the host cluster to access the client cluster.
112112
func ClusterServiceAccountTokenSecretName(joiningClusterName, hostClusterName string) string {
113113
return fmt.Sprintf("%s-%s", joiningClusterName, hostClusterName)

0 commit comments

Comments
 (0)