Skip to content

Commit 932f448

Browse files
committed
Refactor cluster management to use constant for local cluster name
Updated the ClusterManager to replace hardcoded "local" string with the new constant localClusterName. This change improves code readability and maintainability by centralizing the definition of the local cluster name. Adjustments made in methods related to cluster updates, status reporting, and ingress discovery to ensure consistent usage of the constant.
1 parent 1af8945 commit 932f448

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

internal/controller/cluster_manager.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (m *ClusterManager) UpdateClusters(ctx context.Context, dashboard *homerv1a
143143

144144
// Remove clusters that are no longer in the configuration
145145
for name := range m.clients {
146-
if name == "local" {
146+
if name == localClusterName {
147147
continue // Never remove local cluster
148148
}
149149
if !activeClusters[name] {
@@ -153,9 +153,9 @@ func (m *ClusterManager) UpdateClusters(ctx context.Context, dashboard *homerv1a
153153
}
154154

155155
// Ensure local cluster is always present
156-
if _, ok := m.clients["local"]; !ok {
157-
m.clients["local"] = &ClusterClient{
158-
Name: "local",
156+
if _, ok := m.clients[localClusterName]; !ok {
157+
m.clients[localClusterName] = &ClusterClient{
158+
Name: localClusterName,
159159
Client: m.localClient,
160160
Connected: true,
161161
LastCheck: time.Now(),
@@ -236,7 +236,7 @@ func (m *ClusterManager) GetClusterStatuses() []homerv1alpha1.ClusterConnectionS
236236

237237
statuses := []homerv1alpha1.ClusterConnectionStatus{}
238238
for name, cluster := range m.clients {
239-
if name == "local" {
239+
if name == localClusterName {
240240
continue // Don't report local cluster status
241241
}
242242

@@ -269,7 +269,7 @@ func (m *ClusterManager) DiscoverIngresses(ctx context.Context, dashboard *homer
269269
log := log.FromContext(ctx)
270270

271271
for name, cluster := range m.clients {
272-
if !cluster.Connected && name != "local" {
272+
if !cluster.Connected && name != localClusterName {
273273
log.V(1).Info("Skipping disconnected cluster", "cluster", name)
274274
continue
275275
}
@@ -278,7 +278,7 @@ func (m *ClusterManager) DiscoverIngresses(ctx context.Context, dashboard *homer
278278
if err != nil {
279279
log.Error(err, "Failed to discover ingresses", "cluster", name)
280280
// Mark cluster as disconnected on error
281-
if name != "local" {
281+
if name != localClusterName {
282282
cluster.Connected = false
283283
cluster.LastError = err
284284
cluster.LastCheck = time.Now()
@@ -287,7 +287,7 @@ func (m *ClusterManager) DiscoverIngresses(ctx context.Context, dashboard *homer
287287
}
288288

289289
// Update connection status on success
290-
if name != "local" {
290+
if name != localClusterName {
291291
cluster.Connected = true
292292
cluster.LastError = nil
293293
cluster.LastCheck = time.Now()
@@ -331,7 +331,7 @@ func (m *ClusterManager) discoverClusterIngresses(ctx context.Context, cluster *
331331
var selector *metav1.LabelSelector
332332
if cluster.ClusterCfg != nil && cluster.ClusterCfg.IngressSelector != nil {
333333
selector = cluster.ClusterCfg.IngressSelector
334-
} else if cluster.Name == "local" && dashboard.Spec.IngressSelector != nil {
334+
} else if cluster.Name == localClusterName && dashboard.Spec.IngressSelector != nil {
335335
selector = dashboard.Spec.IngressSelector
336336
}
337337

@@ -395,7 +395,7 @@ func (m *ClusterManager) DiscoverHTTPRoutes(ctx context.Context, dashboard *home
395395
log := log.FromContext(ctx)
396396

397397
for name, cluster := range m.clients {
398-
if !cluster.Connected && name != "local" {
398+
if !cluster.Connected && name != localClusterName {
399399
log.V(1).Info("Skipping disconnected cluster", "cluster", name)
400400
continue
401401
}
@@ -404,7 +404,7 @@ func (m *ClusterManager) DiscoverHTTPRoutes(ctx context.Context, dashboard *home
404404
if err != nil {
405405
log.Error(err, "Failed to discover HTTPRoutes", "cluster", name)
406406
// Mark cluster as disconnected on error
407-
if name != "local" {
407+
if name != localClusterName {
408408
cluster.Connected = false
409409
cluster.LastError = err
410410
cluster.LastCheck = time.Now()
@@ -413,7 +413,7 @@ func (m *ClusterManager) DiscoverHTTPRoutes(ctx context.Context, dashboard *home
413413
}
414414

415415
// Update connection status on success
416-
if name != "local" {
416+
if name != localClusterName {
417417
cluster.Connected = true
418418
cluster.LastError = nil
419419
cluster.LastCheck = time.Now()
@@ -490,7 +490,7 @@ func (m *ClusterManager) shouldIncludeHTTPRoute(ctx context.Context, cluster *Cl
490490
var httpRouteSelector *metav1.LabelSelector
491491
if cluster.ClusterCfg != nil && cluster.ClusterCfg.HTTPRouteSelector != nil {
492492
httpRouteSelector = cluster.ClusterCfg.HTTPRouteSelector
493-
} else if cluster.Name == "local" && dashboard.Spec.HTTPRouteSelector != nil {
493+
} else if cluster.Name == localClusterName && dashboard.Spec.HTTPRouteSelector != nil {
494494
httpRouteSelector = dashboard.Spec.HTTPRouteSelector
495495
}
496496

@@ -508,7 +508,7 @@ func (m *ClusterManager) shouldIncludeHTTPRoute(ctx context.Context, cluster *Cl
508508
var gatewaySelector *metav1.LabelSelector
509509
if cluster.ClusterCfg != nil && cluster.ClusterCfg.GatewaySelector != nil {
510510
gatewaySelector = cluster.ClusterCfg.GatewaySelector
511-
} else if cluster.Name == "local" && dashboard.Spec.GatewaySelector != nil {
511+
} else if cluster.Name == localClusterName && dashboard.Spec.GatewaySelector != nil {
512512
gatewaySelector = dashboard.Spec.GatewaySelector
513513
}
514514

@@ -556,7 +556,7 @@ func (m *ClusterManager) UpdateClusterStatuses(statuses []homerv1alpha1.ClusterC
556556

557557
// Update counts
558558
for clusterName, ingresses := range clusterIngresses {
559-
if clusterName == "local" {
559+
if clusterName == localClusterName {
560560
continue
561561
}
562562
if status, ok := statusMap[clusterName]; ok {
@@ -565,7 +565,7 @@ func (m *ClusterManager) UpdateClusterStatuses(statuses []homerv1alpha1.ClusterC
565565
}
566566

567567
for clusterName, httproutes := range clusterHTTPRoutes {
568-
if clusterName == "local" {
568+
if clusterName == localClusterName {
569569
continue
570570
}
571571
if status, ok := statusMap[clusterName]; ok {

internal/controller/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ package controller
1919
const (
2020
dashboardFinalizer = "homer.rajsingh.info/finalizer"
2121
gatewayKind = "Gateway"
22+
localClusterName = "local"
2223
)

0 commit comments

Comments
 (0)