Skip to content

Commit

Permalink
cherry pick 2.11
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros committed Dec 17, 2024
1 parent 10cfb4a commit 571cd55
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
mcov1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
"github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/pkg/config"
mchv1 "github.com/stolostron/multiclusterhub-operator/api/v1"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -139,7 +140,7 @@ func GetMCHPredicateFunc(c client.Client) predicate.Funcs {
e.Object.(*mchv1.MultiClusterHub).Status.DesiredVersion == e.Object.(*mchv1.MultiClusterHub).Status.CurrentVersion {
// only read the image manifests configmap and enqueue the request when the MCH is
// installed/upgraded successfully
ok, err := config.ReadImageManifestConfigMap(
_, ok, err := config.ReadImageManifestConfigMap(
c,
e.Object.(*mchv1.MultiClusterHub).Status.CurrentVersion,
)
Expand All @@ -151,20 +152,32 @@ func GetMCHPredicateFunc(c client.Client) predicate.Funcs {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
// Ensure the event pertains to the target namespace and object type
if e.ObjectNew.GetNamespace() == config.GetMCONamespace() &&
e.ObjectNew.GetResourceVersion() != e.ObjectOld.GetResourceVersion() &&
e.ObjectNew.(*mchv1.MultiClusterHub).Status.CurrentVersion != "" &&
e.ObjectNew.(*mchv1.MultiClusterHub).Status.DesiredVersion ==
e.ObjectNew.(*mchv1.MultiClusterHub).Status.CurrentVersion {
// only read the image manifests configmap and enqueue the request when the MCH is
// installed/upgraded successfully
ok, err := config.ReadImageManifestConfigMap(

currentData, _, err := config.ReadImageManifestConfigMap(
c,
e.ObjectNew.(*mchv1.MultiClusterHub).Status.CurrentVersion,
)
if err != nil {
log.Error(err, "Failed to read image manifest ConfigMap")
return false
}
return ok

previousData, exists := config.GetCachedImageManifestData()
if !exists {
config.SetCachedImageManifestData(currentData)
return true
}
if !reflect.DeepEqual(currentData, previousData) {
config.SetCachedImageManifestData(currentData)
return true
}
return false
}
return false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func getMchPred(c client.Client) predicate.Funcs {
e.Object.(*mchv1.MultiClusterHub).Status.DesiredVersion == e.Object.(*mchv1.MultiClusterHub).Status.CurrentVersion {
// only read the image manifests configmap and enqueue the request when the MCH is
// installed/upgraded successfully
ok, err := config.ReadImageManifestConfigMap(
_, ok, err := config.ReadImageManifestConfigMap(
c,
e.Object.(*mchv1.MultiClusterHub).Status.CurrentVersion,
)
Expand All @@ -121,21 +121,32 @@ func getMchPred(c client.Client) predicate.Funcs {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
// Ensure the event pertains to the target namespace and object type
if e.ObjectNew.GetNamespace() == config.GetMCONamespace() &&
e.ObjectNew.GetResourceVersion() != e.ObjectOld.GetResourceVersion() &&
e.ObjectNew.(*mchv1.MultiClusterHub).Status.CurrentVersion != "" &&
e.ObjectNew.(*mchv1.MultiClusterHub).Status.DesiredVersion ==
e.ObjectNew.(*mchv1.MultiClusterHub).Status.CurrentVersion {
// / only read the image manifests configmap and enqueue the request when the MCH is
// installed/upgraded successfully
ok, err := config.ReadImageManifestConfigMap(

currentData, _, err := config.ReadImageManifestConfigMap(
c,
e.ObjectNew.(*mchv1.MultiClusterHub).Status.CurrentVersion,
)
if err != nil {
log.Error(err, "Failed to read image manifest ConfigMap")
return false
}
return ok

previousData, exists := config.GetCachedImageManifestData()
if !exists {
config.SetCachedImageManifestData(currentData)
return true
}
if !reflect.DeepEqual(currentData, previousData) {
config.SetCachedImageManifestData(currentData)
return true
}
return false
}
return false
},
Expand Down
24 changes: 19 additions & 5 deletions operators/multiclusterobservability/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"

ocinfrav1 "github.com/openshift/api/config/v1"
Expand Down Expand Up @@ -250,6 +251,7 @@ var (
}

multicloudConsoleRouteHost = ""
imageManifestCache sync.Map
)

// GetCrLabelKey returns the key for the CR label injected into the resources created by the operator.
Expand All @@ -267,7 +269,7 @@ func GetImageManifestConfigMapName() string {
}

// ReadImageManifestConfigMap reads configmap with the label ocm-configmap-type=image-manifest.
func ReadImageManifestConfigMap(c client.Client, version string) (bool, error) {
func ReadImageManifestConfigMap(c client.Client, version string) (map[string]string, bool, error) {
mcoNamespace := GetMCONamespace()
// List image manifest configmap with label ocm-configmap-type=image-manifest and ocm-release-version
matchLabels := map[string]string{
Expand All @@ -282,17 +284,16 @@ func ReadImageManifestConfigMap(c client.Client, version string) (bool, error) {
imageCMList := &corev1.ConfigMapList{}
err := c.List(context.TODO(), imageCMList, listOpts...)
if err != nil {
return false, fmt.Errorf("failed to list mch-image-manifest configmaps: %w", err)
return nil, false, fmt.Errorf("failed to list mch-image-manifest configmaps: %w", err)
}

if len(imageCMList.Items) != 1 {
// there should be only one matched image manifest configmap found
return false, nil
return nil, false, nil
}

imageManifests = imageCMList.Items[0].Data
log.V(1).Info("the length of mch-image-manifest configmap", "imageManifests", len(imageManifests))
return true, nil
return imageManifests, true, nil
}

// GetImageManifests...
Expand Down Expand Up @@ -819,3 +820,16 @@ func IsAlertingDisabledInSpec(mco *observabilityv1beta2.MultiClusterObservabilit
annotations := mco.GetAnnotations()
return annotations != nil && annotations[AnnotationDisableMCOAlerting] == "true"
}

func SetCachedImageManifestData(data map[string]string) {
imageManifestCache.Store("mch-image-manifest", data)
}

func GetCachedImageManifestData() (map[string]string, bool) {
if value, ok := imageManifestCache.Load("mch-image-manifest"); ok {
if cachedData, valid := value.(map[string]string); valid {
return cachedData, true
}
}
return nil, false
}

0 comments on commit 571cd55

Please sign in to comment.