Skip to content

Commit 03b6dd6

Browse files
committed
fix test
1 parent f12860e commit 03b6dd6

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

controllers/clustercache/cluster_cache.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,20 +448,8 @@ func (cc *clusterCache) Reconcile(ctx context.Context, req reconcile.Request) (r
448448

449449
requeueAfterDurations := []time.Duration{}
450450

451-
kubeconfigUpdated, err := accessor.KubeConfigUpdated(ctx)
452-
if err != nil {
453-
log.Error(err, "error checking if kubeconfig was updated")
454-
return ctrl.Result{RequeueAfter: defaultRequeueAfter}, nil
455-
}
456-
457451
// Try to connect, if not connected.
458452
connected := accessor.Connected(ctx)
459-
if connected && kubeconfigUpdated {
460-
log.Info("Kubeconfig was updated, disconnecting to re-connect with the new kubeconfig")
461-
accessor.Disconnect(ctx)
462-
didDisconnect = true
463-
connected = false
464-
}
465453

466454
if !connected {
467455
lastConnectionCreationErrorTimestamp := accessor.GetLastConnectionCreationErrorTimestamp(ctx)
@@ -530,6 +518,20 @@ func (cc *clusterCache) Reconcile(ctx context.Context, req reconcile.Request) (r
530518
lastProbeSuccessTime := accessor.GetLastProbeSuccessTimestamp(ctx)
531519
cc.sendEventsToClusterSources(ctx, cluster, time.Now(), lastProbeSuccessTime, didConnect, didDisconnect)
532520

521+
// Check if kubeconfig was updated (only when connected).
522+
if connected {
523+
kubeconfigUpdated, err := accessor.KubeConfigUpdated(ctx)
524+
if err != nil {
525+
log.Error(err, "error checking if kubeconfig was updated")
526+
// Don't return error here, just log it and continue
527+
} else if kubeconfigUpdated {
528+
log.Info("Kubeconfig was updated, disconnecting to re-connect with the new kubeconfig")
529+
accessor.Disconnect(ctx)
530+
didDisconnect = true
531+
connected = false
532+
}
533+
}
534+
533535
// Requeue based on requeueAfterDurations (fallback to defaultRequeueAfter).
534536
return reconcile.Result{RequeueAfter: minDurationOrDefault(requeueAfterDurations, defaultRequeueAfter)}, nil
535537
}

controllers/clustercache/cluster_cache_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,3 +781,62 @@ func getCounterMetric(metricFamilyName, controllerName string) (float64, error)
781781

782782
return 0, fmt.Errorf("failed to find %q metric", metricFamilyName)
783783
}
784+
785+
func TestReconcile_KubeconfigSecretResourceVersionChange(t *testing.T) {
786+
g := NewWithT(t)
787+
ctx := context.Background()
788+
789+
testCluster := &clusterv1.Cluster{
790+
ObjectMeta: metav1.ObjectMeta{
791+
Name: "test-kubeconfig-rv",
792+
Namespace: metav1.NamespaceDefault,
793+
},
794+
}
795+
clusterKey := client.ObjectKeyFromObject(testCluster)
796+
g.Expect(env.CreateAndWait(ctx, testCluster)).To(Succeed())
797+
defer func() { g.Expect(client.IgnoreNotFound(env.CleanupAndWait(ctx, testCluster))).To(Succeed()) }()
798+
799+
opts := Options{
800+
SecretClient: env.GetClient(),
801+
Client: ClientOptions{
802+
UserAgent: remote.DefaultClusterAPIUserAgent("test-controller-manager"),
803+
Timeout: 10 * time.Second,
804+
},
805+
Cache: CacheOptions{
806+
Indexes: []CacheOptionsIndex{NodeProviderIDIndex},
807+
},
808+
}
809+
accessorConfig := buildClusterAccessorConfig(env.GetScheme(), opts, nil)
810+
cc := &clusterCache{
811+
client: env.GetAPIReader(),
812+
clusterAccessorConfig: accessorConfig,
813+
clusterAccessors: make(map[client.ObjectKey]*clusterAccessor),
814+
cacheCtx: context.Background(),
815+
}
816+
817+
// Set Cluster.Status.InfrastructureReady == true
818+
patch := client.MergeFrom(testCluster.DeepCopy())
819+
testCluster.Status.Initialization = &clusterv1.ClusterInitializationStatus{InfrastructureProvisioned: true}
820+
g.Expect(env.Status().Patch(ctx, testCluster, patch)).To(Succeed())
821+
822+
// Create kubeconfig Secret
823+
kubeconfigSecret := kubeconfig.GenerateSecret(testCluster, kubeconfig.FromEnvTestConfig(env.Config, testCluster))
824+
g.Expect(env.CreateAndWait(ctx, kubeconfigSecret)).To(Succeed())
825+
defer func() { g.Expect(env.CleanupAndWait(ctx, kubeconfigSecret)).To(Succeed()) }()
826+
827+
// Initial reconcile to connect
828+
res, err := cc.Reconcile(ctx, reconcile.Request{NamespacedName: clusterKey})
829+
g.Expect(err).ToNot(HaveOccurred())
830+
g.Expect(res.RequeueAfter).To(BeNumerically(">=", accessorConfig.HealthProbe.Interval-2*time.Second))
831+
g.Expect(cc.getClusterAccessor(clusterKey).Connected(ctx)).To(BeTrue())
832+
833+
// Simulate kubeconfig Secret update (resourceVersion change)
834+
kubeconfigSecret.Data["dummy"] = []byte("changed")
835+
g.Expect(env.Update(ctx, kubeconfigSecret)).To(Succeed())
836+
837+
// Reconcile again, should detect update and disconnect
838+
_ = cc.getClusterAccessor(clusterKey) // ensure accessor is present
839+
res, err = cc.Reconcile(ctx, reconcile.Request{NamespacedName: clusterKey})
840+
g.Expect(err).ToNot(HaveOccurred())
841+
g.Expect(cc.getClusterAccessor(clusterKey).Connected(ctx)).To(BeFalse())
842+
}

0 commit comments

Comments
 (0)