Skip to content

Commit 8d2dcf7

Browse files
committed
do not list and iterate secrets
1 parent 217d1f2 commit 8d2dcf7

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

controllers/microk8sconfig_controller.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -626,17 +626,19 @@ func (r *MicroK8sConfigReconciler) storeBootstrapData(ctx context.Context, scope
626626

627627
func (r *MicroK8sConfigReconciler) getJoinToken(ctx context.Context, scope *Scope) (string, error) {
628628
// See if the token exists. If not create it.
629-
secrets := &corev1.SecretList{}
630-
err := r.Client.List(ctx, secrets)
631-
if err != nil {
632-
return "", err
633-
}
629+
secret := &corev1.Secret{}
634630

635-
found := false
636-
for _, s := range secrets.Items {
637-
if s.Name == scope.Cluster.Name+"-jointoken" {
638-
found = true
639-
}
631+
var found bool
632+
err := r.Client.Get(ctx, types.NamespacedName{
633+
Namespace: scope.Cluster.Namespace,
634+
Name: fmt.Sprintf("%s-jointoken", scope.Cluster.Name),
635+
}, secret)
636+
switch {
637+
case err == nil:
638+
found = true
639+
case apierrors.IsNotFound(err):
640+
default:
641+
return "", err
640642
}
641643

642644
if !found {
@@ -678,17 +680,19 @@ func (r *MicroK8sConfigReconciler) getJoinToken(ctx context.Context, scope *Scop
678680

679681
func (r *MicroK8sConfigReconciler) getCA(ctx context.Context, scope *Scope) (cert *string, key *string, err error) {
680682
// See if the CA cert exists. If not create it.
681-
secrets := &corev1.SecretList{}
682-
err = r.Client.List(ctx, secrets)
683-
if err != nil {
684-
return nil, nil, err
685-
}
683+
caSecret := &corev1.Secret{}
686684

687-
found := false
688-
for _, s := range secrets.Items {
689-
if s.Name == scope.Cluster.Name+"-ca" {
690-
found = true
691-
}
685+
var found bool
686+
err = r.Client.Get(ctx, types.NamespacedName{
687+
Namespace: scope.Cluster.Namespace,
688+
Name: fmt.Sprintf("%s-ca", scope.Cluster.Name),
689+
}, caSecret)
690+
switch {
691+
case err == nil:
692+
found = true
693+
case apierrors.IsNotFound(err):
694+
default:
695+
return nil, nil, err
692696
}
693697

694698
if !found {

0 commit comments

Comments
 (0)