Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): drift-detection compatibility check utils #358

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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ generate-go-conversions: $(CONVERSION_GEN) ## Generate conversions go code for c
--output-file=zz_generated.conversion.go \
--go-header-file=./hack/boilerplate.generatego.txt \
./api/v1alpha1
$(MAKE) fmt

.PHONY: clean-generated-conversions
clean-generated-conversions: ## Remove files generated by conversion-gen from the mentioned dirs. Example SRC_DIRS="./api/v1alpha4"
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/zz_generated.conversion.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/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))
})
})