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 .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ linters:
- goconst
- gocritic
- gocyclo
- gomodguard
- gomodguard_v2
- goprintffuncname
- gosec
- govet
Expand Down Expand Up @@ -72,12 +72,10 @@ linters:
- style
gocyclo:
min-complexity: 20
gomodguard:
gomodguard_v2:
blocked:
modules:
- k8s.io/kubernetes:
reason: There is no good, avoidable reason to use this package and often leads to issues such as https://bit.ly/3dlKScY. However, if you disagree please include @ravchama in code review highlighting the reason.
local-replace-directives: true
- module: k8s.io/kubernetes
local-replace-directives: true
govet:
settings:
printf:
Expand Down Expand Up @@ -132,6 +130,7 @@ linters:
- third_party$
- builtin$
- examples$
- internal/test/helpers
issues:
max-issues-per-linter: 0
max-same-issues: 0
Expand All @@ -149,3 +148,4 @@ formatters:
- third_party$
- builtin$
- examples$
- internal/test/helpers
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ KIND := $(TOOLS_BIN_DIR)/kind
KUBECTL := $(TOOLS_BIN_DIR)/kubectl
CLUSTERCTL := $(TOOLS_BIN_DIR)/clusterctl

GOLANGCI_LINT_VERSION := "v2.11.4"
GOLANGCI_LINT_VERSION := "v2.12.1"
CLUSTERCTL_VERSION := v1.13.2

KUSTOMIZE_VER := v5.8.0
Expand Down Expand Up @@ -170,7 +170,7 @@ endif
# K8S_VERSION for the Kind cluster can be set as environment variable. If not defined,
# this default value is used
ifndef K8S_VERSION
K8S_VERSION := v1.35.0
K8S_VERSION := v1.35.1
endif

KIND_CONFIG ?= kind-cluster.yaml
Expand Down
10 changes: 5 additions & 5 deletions controllers/classifier_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ var _ = Describe("Classifier: Reconciler", func() {
Client: c,
Logger: logger,
Classifier: classifier,
ControllerName: "classifier",
ControllerName: classifierControllerName,
})
Expect(err).To(BeNil())

Expand Down Expand Up @@ -334,7 +334,7 @@ var _ = Describe("Classifier: Reconciler", func() {
Client: c,
Logger: logger,
Classifier: classifier,
ControllerName: "classifier",
ControllerName: classifierControllerName,
})
Expect(err).To(BeNil())

Expand Down Expand Up @@ -409,7 +409,7 @@ var _ = Describe("Classifier: Reconciler", func() {
Client: c,
Logger: logger,
Classifier: classifier,
ControllerName: "classifier",
ControllerName: classifierControllerName,
})
Expect(err).To(BeNil())

Expand Down Expand Up @@ -493,7 +493,7 @@ var _ = Describe("Classifier: Reconciler", func() {
Client: c,
Logger: logger,
Classifier: classifier,
ControllerName: "classifier",
ControllerName: classifierControllerName,
})
Expect(err).To(BeNil())

Expand Down Expand Up @@ -659,7 +659,7 @@ var _ = Describe("Classifier: Reconciler", func() {
Namespace: clusterNamespace,
Name: clusterName,
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Kind: clusterKind,
}

managed, unManaged, err := controllers.ClassifyLabels(reconciler, context.TODO(), classifier,
Expand Down
32 changes: 20 additions & 12 deletions controllers/classifier_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ const (
const (
namespaceKind = "Namespace"
clusterRoleBindingKind = "ClusterRoleBinding"
deploymentKind = "Deployment"
appsGroup = "apps"
)

const (
accessRequestClassifierLabelValue = "ok"
applyPatchFieldManager = "application/apply-patch"
deployedBySveltos = "true"
)

const (
Expand Down Expand Up @@ -321,7 +329,7 @@ func createAccessRequest(ctx context.Context, c client.Client,
accessRequest.Namespace = clusterNamespace
accessRequest.Name = getAccessRequestName(clusterName, clusterType)
accessRequest.Labels = map[string]string{
accessRequestClassifierLabel: "ok",
accessRequestClassifierLabel: accessRequestClassifierLabelValue,
}
accessRequest.Spec = libsveltosv1beta1.AccessRequestSpec{
Namespace: clusterNamespace,
Expand Down Expand Up @@ -1301,7 +1309,7 @@ func applyCRD(ctx context.Context, clusterNamespace, clusterName string, u *unst
}

options := metav1.ApplyOptions{
FieldManager: "application/apply-patch",
FieldManager: applyPatchFieldManager,
Force: true,
}
_, err = dr.Apply(ctx, u.GetName(), u, options)
Expand Down Expand Up @@ -1420,7 +1428,7 @@ func getClassifierToDeploy(classifier *libsveltosv1beta1.Classifier) *libsveltos
ObjectMeta: metav1.ObjectMeta{
Name: classifier.Name,
Annotations: map[string]string{
libsveltosv1beta1.DeployedBySveltosAnnotation: "true",
libsveltosv1beta1.DeployedBySveltosAnnotation: deployedBySveltos,
},
},
Spec: classifier.Spec,
Expand Down Expand Up @@ -1456,7 +1464,7 @@ func deployClassifierInstance(ctx context.Context, remoteClient client.Client,

currentClassifier.Spec = classifier.Spec
currentClassifier.Annotations = map[string]string{
libsveltosv1beta1.DeployedBySveltosAnnotation: "true",
libsveltosv1beta1.DeployedBySveltosAnnotation: deployedBySveltos,
}
return remoteClient.Update(ctx, currentClassifier)
}
Expand Down Expand Up @@ -1770,7 +1778,7 @@ func deploySveltosAgentResources(ctx context.Context, clusterNamespace, clusterN
}
policy.SetLabels(currentLabels)

if policy.GetKind() == "Deployment" {
if policy.GetKind() == deploymentKind {
policy, err = addTemplateSpecLabels(policy, lbls)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to set deployment spec.template.labels")
Expand Down Expand Up @@ -1878,7 +1886,7 @@ func deploySveltosAgentPatchedResources(ctx context.Context, restConfig *rest.Co
}

options := metav1.ApplyOptions{
FieldManager: "application/apply-patch",
FieldManager: applyPatchFieldManager,
Force: true,
}

Expand Down Expand Up @@ -2057,8 +2065,8 @@ func getPatchesFromConfigMap(configMap *corev1.ConfigMap, logger logr.Logger,

if patch.Target == nil {
patch.Target = &libsveltosv1beta1.PatchSelector{
Kind: "Deployment",
Group: "apps",
Kind: deploymentKind,
Group: appsGroup,
}
}

Expand Down Expand Up @@ -2128,8 +2136,8 @@ func getSveltosAgentPatchesOld(ctx context.Context, c client.Client,
patch := libsveltosv1beta1.Patch{
Patch: configMap.Data[k],
Target: &libsveltosv1beta1.PatchSelector{
Kind: "Deployment",
Group: "apps",
Kind: deploymentKind,
Group: appsGroup,
},
}
patches = append(patches, patch)
Expand Down Expand Up @@ -2160,8 +2168,8 @@ func getSveltosApplierPatchesOld(ctx context.Context, c client.Client,
patch := libsveltosv1beta1.Patch{
Patch: configMap.Data[k],
Target: &libsveltosv1beta1.PatchSelector{
Kind: "Deployment",
Group: "apps",
Kind: deploymentKind,
Group: appsGroup,
},
}
patches = append(patches, patch)
Expand Down
10 changes: 7 additions & 3 deletions controllers/classifier_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import (
fakedeployer "github.com/projectsveltos/libsveltos/lib/deployer/fake"
)

const (
classifierCRDName = "classifiers.lib.projectsveltos.io"
)

var _ = Describe("Classifier Deployer", func() {
var logger logr.Logger

Expand Down Expand Up @@ -106,7 +110,7 @@ var _ = Describe("Classifier Deployer", func() {
Eventually(func() error {
classifierCRD := &apiextensionsv1.CustomResourceDefinition{}
return testEnv.Get(context.TODO(),
types.NamespacedName{Name: "classifiers.lib.projectsveltos.io"}, classifierCRD)
types.NamespacedName{Name: classifierCRDName}, classifierCRD)
}, timeout, pollingInterval).Should(BeNil())
})

Expand Down Expand Up @@ -279,7 +283,7 @@ var _ = Describe("Classifier Deployer", func() {
Eventually(func() error {
classifierCRD := &apiextensionsv1.CustomResourceDefinition{}
return testEnv.Get(context.TODO(),
types.NamespacedName{Name: "classifiers.lib.projectsveltos.io"}, classifierCRD)
types.NamespacedName{Name: classifierCRDName}, classifierCRD)
}, timeout, pollingInterval).Should(BeNil())
})

Expand Down Expand Up @@ -1101,7 +1105,7 @@ func prepareCluster() *clusterv1.Cluster {
Name: "sveltos-agent-version",
},
Data: map[string]string{
"version": version,
classifierLabelVersion: version,
},
}
err := testEnv.Create(context.TODO(), cm)
Expand Down
3 changes: 2 additions & 1 deletion controllers/classifier_report_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func removeAccessRequest(ctx context.Context, c client.Client, logger logr.Logge

listOptions := []client.ListOption{
client.MatchingLabels{
accessRequestClassifierLabel: "ok",
accessRequestClassifierLabel: accessRequestClassifierLabelValue,
},
}

Expand Down Expand Up @@ -483,6 +483,7 @@ func updateClassifierReport(ctx context.Context, c client.Client, cluster *corev
if apierrors.IsNotFound(err) {
return nil
}
return err
}
if !currentClassifier.DeletionTimestamp.IsZero() {
return nil
Expand Down
23 changes: 14 additions & 9 deletions controllers/classifier_report_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ import (
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
)

const (
testNsOne = "ns-one"
testSveltosCluster = "sveltos-cluster"
)

var _ = Describe("groupClassifierReportsByCluster", func() {
var (
sveltosCluster = corev1.ObjectReference{
Namespace: "ns-one",
Name: "sveltos-cluster",
Namespace: testNsOne,
Name: testSveltosCluster,
Kind: libsveltosv1beta1.SveltosClusterKind,
APIVersion: libsveltosv1beta1.GroupVersion.String(),
}
capiCluster = corev1.ObjectReference{
Namespace: "ns-two",
Name: "capi-cluster",
Kind: "Cluster",
Kind: clusterKind,
APIVersion: clusterv1.GroupVersion.String(),
}
)
Expand All @@ -68,24 +73,24 @@ var _ = Describe("groupClassifierReportsByCluster", func() {
It("groups reports by cluster and filters out non-shard and malformed reports", func() {
reports := []libsveltosv1beta1.ClassifierReport{
// two reports for sveltosCluster
makeReport("ns-one", "sveltos-cluster", "sveltos"),
makeReport("ns-one", "sveltos-cluster", "sveltos"),
makeReport(testNsOne, testSveltosCluster, "sveltos"),
makeReport(testNsOne, testSveltosCluster, "sveltos"),
// one report for capiCluster
makeReport("ns-two", "capi-cluster", "capi"),
// nil labels — excluded
{ObjectMeta: metav1.ObjectMeta{Namespace: "ns-one", Name: "no-labels"}},
{ObjectMeta: metav1.ObjectMeta{Namespace: testNsOne, Name: "no-labels"}},
// missing cluster-name label — excluded
{ObjectMeta: metav1.ObjectMeta{
Namespace: "ns-one", Name: "missing-name",
Namespace: testNsOne, Name: "missing-name",
Labels: map[string]string{
libsveltosv1beta1.ClassifierReportClusterTypeLabel: "sveltos",
},
}},
// missing cluster-type label — excluded
{ObjectMeta: metav1.ObjectMeta{
Namespace: "ns-one", Name: "missing-type",
Namespace: testNsOne, Name: "missing-type",
Labels: map[string]string{
libsveltosv1beta1.ClassifierReportClusterNameLabel: "sveltos-cluster",
libsveltosv1beta1.ClassifierReportClusterNameLabel: testSveltosCluster,
},
}},
// cluster not in shard — excluded
Expand Down
8 changes: 6 additions & 2 deletions controllers/classifier_transformations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import (
libsveltosset "github.com/projectsveltos/libsveltos/lib/set"
)

const (
testKubeVersion124 = "1.24.0"
)

var _ = Describe("ClassifierTransformations map functions", func() {
It("requeueClassifierForCluster returns all existing Classifiers", func() {
cluster := &clusterv1.Cluster{
Expand All @@ -55,7 +59,7 @@ var _ = Describe("ClassifierTransformations map functions", func() {
Spec: libsveltosv1beta1.ClassifierSpec{
KubernetesVersionConstraints: []libsveltosv1beta1.KubernetesVersionConstraint{
{
Version: "1.24.0",
Version: testKubeVersion124,
Comparison: string(libsveltosv1beta1.ComparisonEqual),
},
},
Expand All @@ -72,7 +76,7 @@ var _ = Describe("ClassifierTransformations map functions", func() {
Spec: libsveltosv1beta1.ClassifierSpec{
KubernetesVersionConstraints: []libsveltosv1beta1.KubernetesVersionConstraint{
{
Version: "1.24.0",
Version: testKubeVersion124,
Comparison: string(libsveltosv1beta1.ComparisonEqual),
},
},
Expand Down
6 changes: 4 additions & 2 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const (
upstreamClusterNamePrefix = "upstream-cluster"
upstreamMachineNamePrefix = "upstream-machine"
clusterKind = "Cluster"
classifierControllerName = "classifier"
classifierLabelVersion = "version"
)

const (
Expand Down Expand Up @@ -187,7 +189,7 @@ func getClassifierReport(classifierName, clusterNamespace, clusterName string) *
}

func getClassifierInstance(name string) *libsveltosv1beta1.Classifier {
classifierLabels := []libsveltosv1beta1.ClassifierLabel{{Key: "version", Value: "v1.26.3"}}
classifierLabels := []libsveltosv1beta1.ClassifierLabel{{Key: classifierLabelVersion, Value: "v1.26.3"}}
return &libsveltosv1beta1.Classifier{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -224,7 +226,7 @@ func getClassifierScope(c client.Client, logger logr.Logger,
Client: c,
Logger: logger,
Classifier: classifier,
ControllerName: "classifier",
ControllerName: classifierControllerName,
})
Expect(err).To(BeNil())
return classifierScope
Expand Down
Loading