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

Fix spec comparison for observatorium #1652

Merged
merged 17 commits into from
Nov 21, 2024
2 changes: 1 addition & 1 deletion COMPONENT_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.12.0
2.13.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
package multiclusterobservability

import (
"bytes"
"context"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"

// The import of crypto/md5 below is not for cryptographic use. It is used to hash the contents of files to track
// changes and thus it's not a security issue.
// nolint:gosec
Expand Down Expand Up @@ -170,9 +172,7 @@ func GenerateObservatoriumCR(
}
}

oldSpecBytes, _ := yaml.Marshal(oldSpec)
newSpecBytes, _ := yaml.Marshal(newSpec)
if bytes.Equal(newSpecBytes, oldSpecBytes) &&
if equality.Semantic.DeepEqual(newSpec, oldSpec) &&
labels[obsCRConfigHashLabelName] == observatoriumCRFound.Labels[obsCRConfigHashLabelName] {
return nil, nil
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func newDefaultObservatoriumSpec(cl client.Client, mco *mcov1beta2.MultiClusterO
return obs, err
}
obs.API = obsApi
obs.Thanos = newThanosSpec(mco, scSelected)
obs.Thanos = newThanosSpec(cl, mco, scSelected)
if util.ProxyEnvVarsAreSet() {
obs.EnvVars = newEnvVars()
}
Expand Down Expand Up @@ -647,7 +647,7 @@ func newReceiversSpec(
return receSpec
}

func newRuleSpec(mco *mcov1beta2.MultiClusterObservability, scSelected string) obsv1alpha1.RuleSpec {
func newRuleSpec(cl client.Client, mco *mcov1beta2.MultiClusterObservability, scSelected string) obsv1alpha1.RuleSpec {
ruleSpec := obsv1alpha1.RuleSpec{}
if mco.Spec.AdvancedConfig != nil && mco.Spec.AdvancedConfig.RetentionConfig != nil &&
mco.Spec.AdvancedConfig.RetentionConfig.BlockDuration != "" {
Expand Down Expand Up @@ -721,21 +721,23 @@ func newRuleSpec(mco *mcov1beta2.MultiClusterObservability, scSelected string) o
},
}

if mcoconfig.HasCustomRuleConfigMap() {
if err := cl.Get(context.TODO(), types.NamespacedName{Name: mcoconfig.AlertRuleCustomConfigMapName, Namespace: mcoconfig.GetDefaultNamespace()}, &corev1.ConfigMap{}); err != nil {
if k8serrors.IsNotFound(err) {
ruleSpec.RulesConfig = []obsv1alpha1.RuleConfig{
{
Name: mcoconfig.AlertRuleDefaultConfigMapName,
Key: mcoconfig.AlertRuleDefaultFileKey,
},
}
}
} else {
customRuleConfig := []obsv1alpha1.RuleConfig{
{
Name: mcoconfig.AlertRuleCustomConfigMapName,
Key: mcoconfig.AlertRuleCustomFileKey,
},
}
ruleSpec.RulesConfig = append(ruleSpec.RulesConfig, customRuleConfig...)
} else {
ruleSpec.RulesConfig = []obsv1alpha1.RuleConfig{
{
Name: mcoconfig.AlertRuleDefaultConfigMapName,
Key: mcoconfig.AlertRuleDefaultFileKey,
},
}
}

if mco.Spec.AdvancedConfig != nil && mco.Spec.AdvancedConfig.Rule != nil &&
Expand Down Expand Up @@ -832,14 +834,14 @@ func newMemCacheSpec(component string, mco *mcov1beta2.MultiClusterObservability
return memCacheSpec
}

func newThanosSpec(mco *mcov1beta2.MultiClusterObservability, scSelected string) obsv1alpha1.ThanosSpec {
func newThanosSpec(cl client.Client, mco *mcov1beta2.MultiClusterObservability, scSelected string) obsv1alpha1.ThanosSpec {
thanosSpec := obsv1alpha1.ThanosSpec{}
thanosSpec.Image = mcoconfig.DefaultImgRepository + "/" + mcoconfig.ThanosImgName +
":" + mcoconfig.DefaultImgTagSuffix

thanosSpec.Compact = newCompactSpec(mco, scSelected)
thanosSpec.Receivers = newReceiversSpec(mco, scSelected)
thanosSpec.Rule = newRuleSpec(mco, scSelected)
thanosSpec.Rule = newRuleSpec(cl, mco, scSelected)
thanosSpec.Store = newStoreSpec(mco, scSelected)
thanosSpec.ReceiveController = newReceiverControllerSpec(mco)
thanosSpec.Query = newQuerySpec(mco)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"reflect"
"testing"

"k8s.io/apimachinery/pkg/api/equality"

routev1 "github.com/openshift/api/route/v1"

mcoshared "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/shared"
Expand Down Expand Up @@ -318,10 +320,24 @@ func TestUpdateObservatoriumCR(t *testing.T) {
t.Errorf("config-hash label contains unexpected hash. Want: '%s', got '%s'", expectedConfigHash, updatedHash)
}

createdSpecBytes, _ := yaml.Marshal(createdObservatoriumCR.Spec)
updatedSpecBytes, _ := yaml.Marshal(updatedObservatorium.Spec)
if res := bytes.Compare(updatedSpecBytes, createdSpecBytes); res != 0 {
t.Errorf("%v should be equal to %v", string(createdSpecBytes), string(updatedSpecBytes))
if !equality.Semantic.DeepDerivative(updatedObservatorium.Spec, createdObservatoriumCR.Spec) {
t.Errorf("updated observatorium CR spec is not equal to the created one")
}

// Test Observaotrium CR gets updated with new update from MCO
mco.Spec.StorageConfig.CompactStorageSize = "2Gi"
_, err = GenerateObservatoriumCR(cl, s, mco)
if err != nil {
t.Errorf("Failed to update observatorium due to %v", err)
}
updatedObservatorium = &observatoriumv1alpha1.Observatorium{}
cl.Get(context.TODO(), types.NamespacedName{
Name: mcoconfig.GetDefaultCRName(),
Namespace: namespace,
}, updatedObservatorium)

if updatedObservatorium.Spec.Thanos.Compact.VolumeClaimTemplate.Spec.Resources.Requests.Storage().String() != "2Gi" {
t.Errorf("Failed to update observatorium CR with new compact storage size")
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func GetConfigMapPredicateFunc() predicate.Funcs {
CreateFunc: func(e event.CreateEvent) bool {
if e.Object.GetNamespace() == config.GetDefaultNamespace() {
if e.Object.GetName() == config.AlertRuleCustomConfigMapName {
config.SetCustomRuleConfigMap(true)
return true
} else if _, ok := e.Object.GetLabels()[config.BackupLabelName]; ok {
// resource already has backup label
Expand Down Expand Up @@ -78,7 +77,6 @@ func GetConfigMapPredicateFunc() predicate.Funcs {
DeleteFunc: func(e event.DeleteEvent) bool {
if e.Object.GetName() == config.AlertRuleCustomConfigMapName &&
e.Object.GetNamespace() == config.GetDefaultNamespace() {
config.SetCustomRuleConfigMap(false)
return true
}
return false
Expand Down
11 changes: 0 additions & 11 deletions operators/multiclusterobservability/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ var (
tenantUID = ""
imageManifests = map[string]string{}
imageManifestConfigMapName = ""
hasCustomRuleConfigMap = false
certDuration = time.Hour * 24 * 365
isAlertingDisabled = false

Expand Down Expand Up @@ -647,16 +646,6 @@ func GetObsAPISvc(instanceName string) string {
return instanceName + "-observatorium-api." + defaultNamespace + ".svc.cluster.local"
}

// SetCustomRuleConfigMap set true if there is custom rule configmap.
func SetCustomRuleConfigMap(hasConfigMap bool) {
hasCustomRuleConfigMap = hasConfigMap
}

// HasCustomRuleConfigMap returns true if there is custom rule configmap.
func HasCustomRuleConfigMap() bool {
return hasCustomRuleConfigMap
}

func GetCertDuration() time.Duration {
return certDuration
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pkg/tests/observability_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func installMCO() {
Expect(string(pod.Status.Phase)).To(Equal("Running"))
}

By("Checking Required CRDs are created")
By("Checking Required CRDs are created test")
Eventually(func() error {
return utils.HaveCRDs(testOptions.HubCluster, testOptions.KubeConfig,
[]string{
Expand Down
Loading