Skip to content

Commit

Permalink
(feat): drift-detection compatibility check utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mgianluc committed Sep 4, 2024
1 parent 23de304 commit ee15c77
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ endif
##@ TESTING

.PHONY: test
test: generate manifests fmt vet $(SETUP_ENVTEST) check-manifests ## Run tests.
test: generate generate-go-conversions manifests fmt vet $(SETUP_ENVTEST) check-manifests ## Run tests.
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test $(shell go list ./... | grep -v internal/test | grep -v lib/deployer/fake ) $(TEST_ARGS) -coverprofile cover.out

##@ Build
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions lib/sveltos_upgrade/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package sveltos_upgrade

const (
ConfigMapNamespace = configMapNamespace
ConfigMapName = configMapName
ConfigMapKey = configMapKey
ConfigMapNamespace = configMapNamespace
SveltosAgentConfigMapName = sveltosAgentConfigMapName
DriftDetectionConfigMapName = driftDetectionConfigMapName
ConfigMapKey = configMapKey
)
67 changes: 57 additions & 10 deletions lib/sveltos_upgrade/sveltos_agent_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,47 @@ import (
// - verify versions are compatible

const (
configMapNamespace = "projectsveltos"
configMapName = "sveltos-agent-version"
configMapKey = "sveltos-agent-version"
configMapNamespace = "projectsveltos"
sveltosAgentConfigMapName = "sveltos-agent-version"
driftDetectionConfigMapName = "drift-detection-version"
configMapKey = "version"
)

// IsVersionCompatible returns true if Sveltos-agent running in a managed cluster is compatible
// IsSveltosAgentVersionCompatible returns true if Sveltos-agent running in a managed cluster is compatible
// with the provided version.

// It takes three arguments:
// - ctx (context.Context): Context for the function call
// - c (client.Client): Kubernetes client used to interact with the API server
// - version (string): Version to compare against the sveltos-agent version

func IsVersionCompatible(ctx context.Context, c client.Client, version string) bool {
func IsSveltosAgentVersionCompatible(ctx context.Context, c client.Client, version string) bool {
cm := &corev1.ConfigMap{}

err := c.Get(ctx, types.NamespacedName{Namespace: configMapNamespace, Name: configMapName}, cm)
err := c.Get(ctx, types.NamespacedName{Namespace: configMapNamespace, Name: sveltosAgentConfigMapName}, cm)
if err != nil {
return false
}

if cm.Data == nil {
return false
}

return cm.Data[configMapKey] == version
}

// IsDriftDetectionVersionCompatible returns true if drift-detection-manager running in a managed cluster
// is compatible with the provided version.

// It takes three arguments:
// - ctx (context.Context): Context for the function call
// - c (client.Client): Kubernetes client used to interact with the API server
// - version (string): Version to compare against the drift-detection-manager version

func IsDriftDetectionVersionCompatible(ctx context.Context, c client.Client, version string) bool {
cm := &corev1.ConfigMap{}

err := c.Get(ctx, types.NamespacedName{Namespace: configMapNamespace, Name: driftDetectionConfigMapName}, cm)
if err != nil {
return false
}
Expand All @@ -82,10 +106,33 @@ func IsVersionCompatible(ctx context.Context, c client.Client, version string) b
func StoreSveltosAgentVersion(ctx context.Context, c client.Client, version string) error {
cm := &corev1.ConfigMap{}

err := c.Get(ctx, types.NamespacedName{Namespace: configMapNamespace, Name: configMapName}, cm)
err := c.Get(ctx, types.NamespacedName{Namespace: configMapNamespace, Name: sveltosAgentConfigMapName}, cm)
if err != nil {
if apierrors.IsNotFound(err) {
return createConfigMap(ctx, c, sveltosAgentConfigMapName, version)
}
return err
}

if cm.Data == nil {
cm.Data = map[string]string{}
}
cm.Data[configMapKey] = version
return c.Update(ctx, cm)
}

// StoreDriftDetectionVersion stores the provided drift-detection-manager version in a ConfigMap.
// It takes three arguments:
// - ctx (context.Context): Context for the function call
// - c (client.Client): Kubernetes client used to interact with the API server
// - version (string): Version of the drift-detection-manager to be stored
func StoreDriftDetectionVersion(ctx context.Context, c client.Client, version string) error {
cm := &corev1.ConfigMap{}

err := c.Get(ctx, types.NamespacedName{Namespace: configMapNamespace, Name: driftDetectionConfigMapName}, cm)
if err != nil {
if apierrors.IsNotFound(err) {
return createConfigMap(ctx, c, version)
return createConfigMap(ctx, c, driftDetectionConfigMapName, version)
}
return err
}
Expand All @@ -97,11 +144,11 @@ func StoreSveltosAgentVersion(ctx context.Context, c client.Client, version stri
return c.Update(ctx, cm)
}

func createConfigMap(ctx context.Context, c client.Client, version string) error {
func createConfigMap(ctx context.Context, c client.Client, name, version string) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: configMapNamespace,
Name: configMapName,
Name: name,
},
Data: map[string]string{
configMapKey: version,
Expand Down
82 changes: 71 additions & 11 deletions lib/sveltos_upgrade/sveltos_agent_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var _ = Describe("SveltosAgent compatibility checks", func() {
const (
version = "v1.31.0"
)
const (
version = "v1.31.0"
)

var _ = Describe("SveltosAgent compatibility checks", func() {
It("Create ConfigMap with Sveltos-agent version", func() {
c := fake.NewClientBuilder().WithScheme(scheme).Build()

Expect(sveltos_upgrade.StoreSveltosAgentVersion(context.TODO(), c, version)).To(Succeed())

cm := &corev1.ConfigMap{}
Expect(c.Get(context.TODO(),
types.NamespacedName{Namespace: sveltos_upgrade.ConfigMapNamespace, Name: sveltos_upgrade.ConfigMapName},
types.NamespacedName{Namespace: sveltos_upgrade.ConfigMapNamespace, Name: sveltos_upgrade.SveltosAgentConfigMapName},
cm)).To(Succeed())
Expect(cm.Data).ToNot(BeNil())
Expect(cm.Data[sveltos_upgrade.ConfigMapKey]).To(Equal(version))
Expand All @@ -53,7 +53,7 @@ var _ = Describe("SveltosAgent compatibility checks", func() {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: sveltos_upgrade.ConfigMapNamespace,
Name: sveltos_upgrade.ConfigMapName,
Name: sveltos_upgrade.SveltosAgentConfigMapName,
},
Data: map[string]string{
sveltos_upgrade.ConfigMapKey: randomString(),
Expand All @@ -64,17 +64,19 @@ var _ = Describe("SveltosAgent compatibility checks", func() {
Expect(sveltos_upgrade.StoreSveltosAgentVersion(context.TODO(), c, version)).To(Succeed())

Expect(c.Get(context.TODO(),
types.NamespacedName{Namespace: sveltos_upgrade.ConfigMapNamespace, Name: sveltos_upgrade.ConfigMapName},
types.NamespacedName{
Namespace: sveltos_upgrade.ConfigMapNamespace,
Name: sveltos_upgrade.SveltosAgentConfigMapName},
cm)).To(Succeed())
Expect(cm.Data).ToNot(BeNil())
Expect(cm.Data[sveltos_upgrade.ConfigMapKey]).To(Equal(version))
})

It("IsVersionCompatible returns true Sveltos-agent version is compatible", func() {
It("IsSveltosAgentVersionCompatible returns true Sveltos-agent version is compatible", func() {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: sveltos_upgrade.ConfigMapNamespace,
Name: sveltos_upgrade.ConfigMapName,
Name: sveltos_upgrade.SveltosAgentConfigMapName,
},
Data: map[string]string{
sveltos_upgrade.ConfigMapKey: version,
Expand All @@ -86,7 +88,65 @@ var _ = Describe("SveltosAgent compatibility checks", func() {

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

Expect(sveltos_upgrade.IsVersionCompatible(context.TODO(), c, version)).To(BeTrue())
Expect(sveltos_upgrade.IsVersionCompatible(context.TODO(), c, randomString())).To(BeFalse())
Expect(sveltos_upgrade.IsSveltosAgentVersionCompatible(context.TODO(), c, version)).To(BeTrue())
Expect(sveltos_upgrade.IsSveltosAgentVersionCompatible(context.TODO(), c, randomString())).To(BeFalse())
})
})

var _ = Describe("DriftDetection compatibility checks", func() {
It("IsDriftDetectionVersionCompatible returns true when drift-detection version is compatible", func() {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: sveltos_upgrade.ConfigMapNamespace,
Name: sveltos_upgrade.DriftDetectionConfigMapName,
},
Data: map[string]string{
sveltos_upgrade.ConfigMapKey: version,
},
}
initObjects := []client.Object{
cm,
}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

Expect(sveltos_upgrade.IsDriftDetectionVersionCompatible(context.TODO(), c, version)).To(BeTrue())
Expect(sveltos_upgrade.IsDriftDetectionVersionCompatible(context.TODO(), c, randomString())).To(BeFalse())
})

It("Create ConfigMap with drift-detection-manager version", func() {
c := fake.NewClientBuilder().WithScheme(scheme).Build()

Expect(sveltos_upgrade.StoreDriftDetectionVersion(context.TODO(), c, version)).To(Succeed())

cm := &corev1.ConfigMap{}
Expect(c.Get(context.TODO(),
types.NamespacedName{Namespace: sveltos_upgrade.ConfigMapNamespace, Name: sveltos_upgrade.DriftDetectionConfigMapName},
cm)).To(Succeed())
Expect(cm.Data).ToNot(BeNil())
Expect(cm.Data[sveltos_upgrade.ConfigMapKey]).To(Equal(version))
})

It("Update ConfigMap with drift-detection-manager version", func() {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: sveltos_upgrade.ConfigMapNamespace,
Name: sveltos_upgrade.DriftDetectionConfigMapName,
},
Data: map[string]string{
sveltos_upgrade.ConfigMapKey: randomString(),
},
}
c := fake.NewClientBuilder().WithScheme(scheme).Build()

Expect(sveltos_upgrade.StoreDriftDetectionVersion(context.TODO(), c, version)).To(Succeed())

Expect(c.Get(context.TODO(),
types.NamespacedName{
Namespace: sveltos_upgrade.ConfigMapNamespace,
Name: sveltos_upgrade.DriftDetectionConfigMapName},
cm)).To(Succeed())
Expect(cm.Data).ToNot(BeNil())
Expect(cm.Data[sveltos_upgrade.ConfigMapKey]).To(Equal(version))
})
})

0 comments on commit ee15c77

Please sign in to comment.