Skip to content

Commit 226a352

Browse files
authored
Merge pull request #658 from gianlucam76/connection-status-and-sets
(feat) ClusterSet/Set: look at SveltosCluster ConnectionStatus
2 parents 44434a6 + 7ff3f6e commit 226a352

16 files changed

+218
-30
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ARCH ?= $(shell go env GOARCH)
2525
OS ?= $(shell uname -s | tr A-Z a-z)
2626
K8S_LATEST_VER ?= $(shell curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)
2727
export CONTROLLER_IMG ?= $(REGISTRY)/$(IMAGE_NAME)
28-
TAG ?= main
28+
TAG ?= dev
2929

3030
.PHONY: all
3131
all: build

config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,8 @@ spec:
16231623
https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
16241624
type: string
16251625
type: object
1626+
required:
1627+
- patch
16261628
type: object
16271629
type: array
16281630
policyRefs:

config/crd/bases/config.projectsveltos.io_clustersummaries.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,8 @@ spec:
15961596
https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
15971597
type: string
15981598
type: object
1599+
required:
1600+
- patch
15991601
type: object
16001602
type: array
16011603
policyRefs:

config/crd/bases/config.projectsveltos.io_profiles.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,8 @@ spec:
16231623
https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
16241624
type: string
16251625
type: object
1626+
required:
1627+
- patch
16261628
type: object
16271629
type: array
16281630
policyRefs:

config/default/manager_auth_proxy_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ spec:
1515
- "--report-mode=0"
1616
- --shard-key=
1717
- "--v=5"
18-
- "--version=main"
18+
- "--version=dev"

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ spec:
88
spec:
99
containers:
1010
# Change the value of image field below to your controller image URL
11-
- image: docker.io/projectsveltos/addon-controller:main
11+
- image: docker.io/projectsveltos/addon-controller:dev
1212
name: controller

controllers/clusterset_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ func (r *ClusterSetReconciler) reconcileNormal(
143143

144144
setScope.SetMatchingClusterRefs(matchingCluster)
145145

146-
selectClusters(setScope)
146+
err = selectClusters(ctx, r.Client, setScope, logger)
147+
if err != nil {
148+
return reconcile.Result{Requeue: true, RequeueAfter: normalRequeueAfter}
149+
}
147150

148151
r.updateMaps(setScope)
149152

controllers/export_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ var (
194194
)
195195

196196
var (
197-
SelectClusters = selectClusters
198-
SelectMoreClusters = selectMoreClusters
197+
SelectClusters = selectClusters
198+
SelectMoreClusters = selectMoreClusters
199+
PruneConnectionDownClusters = pruneConnectionDownClusters
199200
)
200201

201202
var (

controllers/set_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ func (r *SetReconciler) reconcileNormal(
152152

153153
setScope.SetMatchingClusterRefs(matchingCluster)
154154

155-
selectClusters(setScope)
155+
err = selectClusters(ctx, r.Client, setScope, logger)
156+
if err != nil {
157+
return reconcile.Result{Requeue: true, RequeueAfter: normalRequeueAfter}
158+
}
156159

157160
r.updateMaps(setScope)
158161

controllers/set_utils.go

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,46 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
2021
"fmt"
2122

2223
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/types"
2325
ctrl "sigs.k8s.io/controller-runtime"
2426
"sigs.k8s.io/controller-runtime/pkg/client"
2527
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2628

2729
"github.com/go-logr/logr"
2830

2931
"github.com/projectsveltos/addon-controller/pkg/scope"
32+
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
33+
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
3034
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
3135
libsveltosset "github.com/projectsveltos/libsveltos/lib/set"
3236
)
3337

34-
func selectClusters(setScope *scope.SetScope) {
38+
func selectClusters(ctx context.Context, c client.Client, setScope *scope.SetScope, logger logr.Logger) error {
3539
status := setScope.GetStatus()
3640
spec := setScope.GetSpec()
3741

3842
// Verify all currently selected are still ready
3943
// Status.MatchingClusterRef only contains ready cluster
40-
currentMatchingCluster := make(map[corev1.ObjectReference]bool)
41-
for i := range status.MatchingClusterRefs {
42-
currentMatchingCluster[status.MatchingClusterRefs[i]] = true
44+
// Set must consider only cluster with connectionStatus set to Healthy. Prune matching cluster whose
45+
// connection is down
46+
healthyMatchingClusters, err := pruneConnectionDownClusters(ctx, c, status.MatchingClusterRefs, logger)
47+
if err != nil {
48+
return err
49+
}
50+
51+
currentMatchingHealthyCluster := make(map[corev1.ObjectReference]bool)
52+
for i := range healthyMatchingClusters {
53+
currentMatchingHealthyCluster[healthyMatchingClusters[i]] = true
4354
}
4455

4556
currentSelectedClusters := make([]corev1.ObjectReference, 0)
4657
for i := range status.SelectedClusterRefs {
4758
cluster := &status.SelectedClusterRefs[i]
48-
if _, ok := currentMatchingCluster[*cluster]; ok {
59+
if _, ok := currentMatchingHealthyCluster[*cluster]; ok {
4960
currentSelectedClusters = append(currentSelectedClusters, *cluster)
5061
}
5162
}
@@ -57,7 +68,7 @@ func selectClusters(setScope *scope.SetScope) {
5768
if len(currentSelectedClusters) == spec.MaxReplicas {
5869
// Number of selected cluster matches the MaxReplicas, so there
5970
// is nothing else to do
60-
return
71+
return nil
6172
}
6273

6374
if spec.MaxReplicas == 0 {
@@ -67,11 +78,13 @@ func selectClusters(setScope *scope.SetScope) {
6778
status.SelectedClusterRefs = currentSelectedClusters[:spec.MaxReplicas-1]
6879
} else if len(currentSelectedClusters) < spec.MaxReplicas {
6980
// select more clusters
70-
selectMoreClusters(setScope)
81+
selectMoreClusters(setScope, healthyMatchingClusters)
7182
}
83+
84+
return nil
7285
}
7386

74-
func selectMoreClusters(setScope *scope.SetScope) {
87+
func selectMoreClusters(setScope *scope.SetScope, healthyMatchingClusters []corev1.ObjectReference) {
7588
status := setScope.GetStatus()
7689
spec := setScope.GetSpec()
7790

@@ -86,8 +99,8 @@ func selectMoreClusters(setScope *scope.SetScope) {
8699
currentSelectedCluster[status.SelectedClusterRefs[i]] = true
87100
}
88101

89-
for i := range status.MatchingClusterRefs {
90-
cluster := &status.MatchingClusterRefs[i]
102+
for i := range healthyMatchingClusters {
103+
cluster := &healthyMatchingClusters[i]
91104
if _, ok := currentSelectedCluster[*cluster]; !ok {
92105
status.SelectedClusterRefs = append(status.SelectedClusterRefs, *cluster)
93106
if len(status.SelectedClusterRefs) == spec.MaxReplicas {
@@ -128,3 +141,44 @@ func requeueForSet(set client.Object,
128141

129142
return requests
130143
}
144+
145+
func pruneConnectionDownClusters(ctx context.Context, c client.Client, matchingClusters []corev1.ObjectReference,
146+
logger logr.Logger) ([]corev1.ObjectReference, error) {
147+
148+
result := make([]corev1.ObjectReference, 0)
149+
for i := range matchingClusters {
150+
cluster := &matchingClusters[i]
151+
clusterType := clusterproxy.GetClusterType(cluster)
152+
153+
if clusterType == libsveltosv1beta1.ClusterTypeSveltos {
154+
sveltosCluster, err := getSveltosCluster(ctx, c, cluster.Namespace, cluster.Name)
155+
if err != nil {
156+
return nil, err
157+
}
158+
if sveltosCluster.Status.ConnectionStatus == libsveltosv1beta1.ConnectionDown {
159+
logger.V(logs.LogDebug).Info(fmt.Sprintf("connection to sveltosCluster %s/%s is down. Ignore cluster",
160+
cluster.Namespace, cluster.Name))
161+
continue
162+
}
163+
}
164+
165+
result = append(result, *cluster)
166+
}
167+
168+
return result, nil
169+
}
170+
171+
func getSveltosCluster(ctx context.Context, c client.Client,
172+
clusterNamespace, clusterName string) (*libsveltosv1beta1.SveltosCluster, error) {
173+
174+
clusterNamespacedName := types.NamespacedName{
175+
Namespace: clusterNamespace,
176+
Name: clusterName,
177+
}
178+
179+
cluster := &libsveltosv1beta1.SveltosCluster{}
180+
if err := c.Get(ctx, clusterNamespacedName, cluster); err != nil {
181+
return nil, err
182+
}
183+
return cluster, nil
184+
}

0 commit comments

Comments
 (0)