Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: 1.25.6
go-version: 1.26.1
- name: Build
run: make build
- name: FMT
Expand All @@ -37,7 +37,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: 1.25.6
go-version: 1.26.1
- name: ut
run: make test
env:
Expand All @@ -50,7 +50,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: 1.25.6
go-version: 1.26.1
- name: fv
run: make create-cluster fv
env:
Expand All @@ -63,7 +63,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: 1.25.6
go-version: 1.26.1
- name: fv-sharding
run: make create-cluster fv-sharding
env:
Expand All @@ -76,7 +76,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: 1.25.6
go-version: 1.26.1
- name: fv-agentless
run: make create-cluster fv-agentless
env:
Expand All @@ -89,7 +89,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: 1.25.6
go-version: 1.26.1
- name: fv-pullmode
run: make create-cluster-pullmode fv-pullmode
env:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.25.6 AS builder
FROM golang:1.26.1 AS builder

ARG BUILDOS
ARG TARGETARCH
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ KIND := $(TOOLS_BIN_DIR)/kind
KUBECTL := $(TOOLS_BIN_DIR)/kubectl
CLUSTERCTL := $(TOOLS_BIN_DIR)/clusterctl

GOLANGCI_LINT_VERSION := "v2.8.0"
CLUSTERCTL_VERSION := v1.12.3
GOLANGCI_LINT_VERSION := "v2.11.3"
CLUSTERCTL_VERSION := v1.12.4

KUSTOMIZE_VER := v5.8.0
KUSTOMIZE_BIN := kustomize
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Classifier currently supports the following classification criteria:
1. Kubernetes version
2. Kubernetes resources

For instance, this Classifier instance will match any cluster whose Kubernetes version is greater than or equal to "v1.24.0" and strictly less than "v1.25.6"
For instance, this Classifier instance will match any cluster whose Kubernetes version is greater than or equal to "v1.24.0" and strictly less than "v1.26.1"

```
apiVersion: lib.projectsveltos.io/v1beta1
Expand All @@ -31,7 +31,7 @@ spec:
- comparison: GreaterThanOrEqualTo
version: 1.24.0
- comparison: LessThan
version: 1.25.6
version: 1.26.1
```

When a cluster is a match for a Classifier instances, all classifierLabels will be automatically added to the Cluster instance.
Expand All @@ -45,8 +45,8 @@ Refer to [examples](./examples/) for more complex examples.
## A simple use case: upgrade helm charts automatically when Kubernetes cluster is upgraded
Suppose you are managing several Kubernetes clusters with different versions.
And you want to deploy:
1. OPA Gatekeeper version 3.10.0 in any Kubernetes cluster whose version is >= v1.25.6
2. OPA Gatekeeper version 3.9.0 in any Kubernetes cluster whose version is >= v1.24.0 && < v1.25.6
1. OPA Gatekeeper version 3.10.0 in any Kubernetes cluster whose version is >= v1.26.1
2. OPA Gatekeeper version 3.9.0 in any Kubernetes cluster whose version is >= v1.24.0 && < v1.26.1

You can create following ClusterProfiles and Classifiers in the management cluster:
```
Expand Down Expand Up @@ -102,7 +102,7 @@ spec:
value: v3-10
kubernetesVersionConstraints:
- comparison: GreaterThanOrEqualTo
version: 1.25.6
version: 1.26.1
```

```
Expand All @@ -118,7 +118,7 @@ spec:
- comparison: GreaterThanOrEqualTo
version: 1.24.0
- comparison: LessThan
version: 1.25.6
version: 1.26.1
```

With the above configuration:
Expand Down
2 changes: 1 addition & 1 deletion controllers/classifier_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (r *ClassifierReconciler) SetupWithManager(ctx context.Context,
// Later on, in main, we detect that and if CAPI is present WatchForCAPI will be invoked.

if r.ClassifierReportMode == CollectFromManagementCluster {
go collectClassifierReports(mgr.GetClient(), r.ShardKey, r.CapiOnboardAnnotation, getVersion(), mgr.GetLogger())
go collectClassifierReports(ctx, mgr.GetClient(), r.ShardKey, r.CapiOnboardAnnotation, getVersion(), mgr.GetLogger())
}

if getAgentInMgmtCluster() {
Expand Down
8 changes: 6 additions & 2 deletions controllers/classifier_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,11 @@ func prepareSveltosAgentYAML(agentYAML, clusterNamespace, clusterName, mode stri
agentYAML = strings.ReplaceAll(agentYAML, "cluster-type=", fmt.Sprintf("cluster-type=%s", clusterType))
agentYAML = strings.ReplaceAll(agentYAML, "v=5", "v=0")

registry := GetSveltosAgentRegistry()
if getSveltosAgentEnableNATS() {
agentYAML = strings.ReplaceAll(agentYAML, "enable-nats-watcher=false", "enable-nats-watcher=true")
}

registry := getSveltosAgentRegistry()
if registry != "" {
agentYAML = replaceRegistry(agentYAML, registry)
}
Expand All @@ -1526,7 +1530,7 @@ func prepareSveltosApplierYAML(agentYAML, clusterNamespace, clusterName string,
agentYAML = strings.ReplaceAll(agentYAML, "cluster-type=", fmt.Sprintf("cluster-type=%s", clusterType))
agentYAML = strings.ReplaceAll(agentYAML, "secret-with-kubeconfig=", fmt.Sprintf("secret-with-kubeconfig=%s-sveltos-kubeconfig", clusterName))

registry := GetSveltosAgentRegistry()
registry := getSveltosAgentRegistry()
if registry != "" {
agentYAML = replaceRegistry(agentYAML, registry)
}
Expand Down
43 changes: 25 additions & 18 deletions controllers/classifier_report_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,42 @@ func shouldIgnore(cr *libsveltosv1beta1.ClassifierReport, isPullMode bool) bool

// Periodically collects ClassifierReports from each cluster.
// If sharding is used, it will collect only from clusters matching shard.
func collectClassifierReports(c client.Client, shardKey, capiOnboardAnnotation, version string, logger logr.Logger) {
func collectClassifierReports(ctx context.Context, c client.Client, shardKey, capiOnboardAnnotation, version string,
logger logr.Logger) {

interval := 10 * time.Second
if shardKey != "" {
// This controller will only fetch ClassifierReport instances
// so it can be more aggressive
interval = 5 * time.Second
}
ticker := time.NewTicker(interval)
defer ticker.Stop()

ctx := context.TODO()
for {
logger.V(logs.LogDebug).Info("collecting ClassifierReports")
// Get a selectors that matches everything
clusterList, err := clusterproxy.GetListOfClustersForShardKey(ctx, c, "", capiOnboardAnnotation, shardKey, logger)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get clusters: %v", err))
}

for i := range clusterList {
cluster := &clusterList[i]
l := logger.WithValues("cluster", fmt.Sprintf("%s:%s/%s", cluster.Kind, cluster.Namespace, cluster.Name))
err = collectClassifierReportsFromCluster(ctx, c, cluster, version, l)
select {
case <-ctx.Done():
logger.Info("stopping collecting ClassifierReports")
return
case <-ticker.C:
logger.V(logs.LogDebug).Info("collecting ClassifierReports")
// Get a selectors that matches everything
clusterList, err := clusterproxy.GetListOfClustersForShardKey(ctx, c, "", capiOnboardAnnotation, shardKey, logger)
if err != nil {
l.V(logs.LogInfo).Info(fmt.Sprintf("failed to collect ClassifierReports from cluster: %s/%s %v",
cluster.Namespace, cluster.Name, err))
continue
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get clusters: %v", err))
}
}

time.Sleep(interval)
for i := range clusterList {
cluster := &clusterList[i]
l := logger.WithValues("cluster", fmt.Sprintf("%s:%s/%s", cluster.Kind, cluster.Namespace, cluster.Name))
err = collectClassifierReportsFromCluster(ctx, c, cluster, version, l)
if err != nil {
l.V(logs.LogInfo).Info(fmt.Sprintf("failed to collect ClassifierReports from cluster: %s/%s %v",
cluster.Namespace, cluster.Name, err))
continue
}
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ func getClassifierReport(classifierName, clusterNamespace, clusterName string) *
}

func getClassifierInstance(name string) *libsveltosv1beta1.Classifier {
classifierLabels := []libsveltosv1beta1.ClassifierLabel{{Key: "version", Value: "v1.25.6"}}
classifierLabels := []libsveltosv1beta1.ClassifierLabel{{Key: "version", Value: "v1.26.1"}}
return &libsveltosv1beta1.Classifier{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: libsveltosv1beta1.ClassifierSpec{
KubernetesVersionConstraints: []libsveltosv1beta1.KubernetesVersionConstraint{
{
Version: "1.25.6",
Version: "1.26.1",
Comparison: string(libsveltosv1beta1.ComparisonEqual),
},
},
Expand Down
11 changes: 10 additions & 1 deletion controllers/management_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
sveltosApplierConfigMap string
registry string
agentInMgmtCluster bool
enableNatsWatcher bool
)

func SetManagementClusterAccess(config *rest.Config, c client.Client) {
Expand All @@ -52,6 +53,10 @@ func SetAgentInMgmtCluster(isInMgmtCluster bool) {
agentInMgmtCluster = isInMgmtCluster
}

func SetSveltosAgentEnableNATS(enable bool) {
enableNatsWatcher = enable
}

func getManagementClusterConfig() *rest.Config {
return managementClusterConfig
}
Expand All @@ -72,10 +77,14 @@ func getSveltosApplierConfigMap() string {
return sveltosApplierConfigMap
}

func GetSveltosAgentRegistry() string {
func getSveltosAgentRegistry() string {
return registry
}

func getAgentInMgmtCluster() bool {
return agentInMgmtCluster
}

func getSveltosAgentEnableNATS() bool {
return enableNatsWatcher
}
43 changes: 25 additions & 18 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,34 @@ func removeStaleClassifierResources(ctx context.Context, logger logr.Logger) {
},
}

const interval = 5 * time.Minute
ticker := time.NewTicker(interval)
defer ticker.Stop()

for {
const five = 5
time.Sleep(five * time.Minute)

c := getManagementClusterClient()
sveltosAgentDeployments := &appsv1.DeploymentList{}
err := c.List(ctx, sveltosAgentDeployments, listOptions...)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to collect sveltos-agent deployment: %v", err))
continue
}
select {
case <-ctx.Done():
logger.Info("stopping detection and removal of stale classifier resources")
return
case <-ticker.C:
c := getManagementClusterClient()
sveltosAgentDeployments := &appsv1.DeploymentList{}
err := c.List(ctx, sveltosAgentDeployments, listOptions...)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to collect sveltos-agent deployment: %v", err))
continue
}

for i := range sveltosAgentDeployments.Items {
depl := &sveltosAgentDeployments.Items[i]
for i := range sveltosAgentDeployments.Items {
depl := &sveltosAgentDeployments.Items[i]

exist, clusterNs, clusterName, clusterType := deplAssociatedClusterExist(ctx, c, depl, logger)
if !exist {
_, err = cleanClusterStaleResources(ctx, c, clusterNs, clusterName, clusterType, logger)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to remove sveltos-agent resources: %v", err))
continue
exist, clusterNs, clusterName, clusterType := deplAssociatedClusterExist(ctx, c, depl, logger)
if !exist {
_, err = cleanClusterStaleResources(ctx, c, clusterNs, clusterName, clusterType, logger)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to remove sveltos-agent resources: %v", err))
continue
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/kubernetes_version.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Following Classifier will match any Cluster whose
# Kubernetes version is >= v1.24.0 and < v1.25.6
# Kubernetes version is >= v1.24.0 and < v1.26.1
apiVersion: lib.projectsveltos.io/v1beta1
kind: Classifier
metadata:
Expand All @@ -12,4 +12,4 @@ spec:
- comparison: GreaterThanOrEqualTo
version: 1.24.0
- comparison: LessThan
version: 1.25.6
version: 1.26.1
4 changes: 2 additions & 2 deletions examples/multiple_constraints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Following Classifier will match any Cluster whose
# Kubernetes version is >= v1.24.0 and < v1.25.6
# Kubernetes version is >= v1.24.0 and < v1.26.1
apiVersion: lib.projectsveltos.io/v1beta1
kind: Classifier
metadata:
Expand All @@ -10,7 +10,7 @@ spec:
value: multiple
kubernetesVersionConstraints:
- comparison: GreaterThanOrEqualTo
version: 1.25.6
version: 1.26.1
deployedResourceConstraint:
- group: ""
version: v1
Expand Down
Loading