Skip to content

Commit

Permalink
removed addition promrules and servicemonitors from openshift-monitor…
Browse files Browse the repository at this point in the history
…ing ns

Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn committed Oct 4, 2023
1 parent 4d41ef7 commit d405580
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 42 deletions.
115 changes: 99 additions & 16 deletions api/v1/multiclusterengine_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,76 @@ limitations under the License.

package v1

import "fmt"

const (
ManagedServiceAccount = "managedserviceaccount"
ManagedServiceAccountPreview = "managedserviceaccount-preview"
ConsoleMCE = "console-mce"
Discovery = "discovery"
Hive = "hive"
AssistedService = "assisted-service"
ClusterLifecycle = "cluster-lifecycle"
ClusterManager = "cluster-manager"
ServerFoundation = "server-foundation"
HyperShift = "hypershift"
HyperShiftPreview = "hypershift-preview"
ClusterProxyAddon = "cluster-proxy-addon"
ConsoleMCE = "console-mce"
Discovery = "discovery"
Hive = "hive"
HyperShift = "hypershift"
HypershiftLocalHosting = "hypershift-local-hosting"
HyperShiftPreview = "hypershift-preview"
LocalCluster = "local-cluster"
ManagedServiceAccount = "managedserviceaccount"
ManagedServiceAccountPreview = "managedserviceaccount-preview"
ServerFoundation = "server-foundation"
)

var allComponents = []string{
AssistedService,
ClusterLifecycle,
ClusterManager,
ClusterProxyAddon,
ConsoleMCE,
Discovery,
Hive,
ServerFoundation,
ConsoleMCE,
HyperShift,
HypershiftLocalHosting,
HyperShiftPreview,
LocalCluster,
ManagedServiceAccount,
ManagedServiceAccountPreview,
ServerFoundation,
}

// MCEComponents is a slice containing component names specific to the "MCE" category.
var MCEComponents = []string{
AssistedService,
ClusterLifecycle,
ClusterManager,
ClusterProxyAddon,
ConsoleMCE,
Discovery,
Hive,
HyperShift,
HyperShiftPreview,
HypershiftLocalHosting,
ClusterProxyAddon,
LocalCluster,
ManagedServiceAccount,
ServerFoundation,
}

var LegacyPrometheusKind = []string{"PrometheusRule", "ServiceMonitor"}

// MCEPrometheusRules is a map that associates certain component names with their corresponding prometheus rules.
var MCEPrometheusRules = map[string]string{
ConsoleMCE: "acm-console-prometheus-rules",
// Add other components here when PrometheusRules is required.
}

// MCEServiceMonitors is a map that associates certain component names with their corresponding service monitors.
var MCEServiceMonitors = map[string]string{
ClusterLifecycle: "clusterlifecycle-state-metrics-v2",
ConsoleMCE: "console-mce-monitor",
// Add other components here when ServiceMonitors is required.
}

/*
ComponentPresent checks if a component with the given name is present in the MultiClusterEngine's Overrides.
Returns true if the component is present, otherwise false.
*/
func (mce *MultiClusterEngine) ComponentPresent(s string) bool {
if mce.Spec.Overrides == nil {
return false
Expand All @@ -64,6 +100,10 @@ func (mce *MultiClusterEngine) ComponentPresent(s string) bool {
return false
}

/*
Enabled checks if a component with the given name is enabled in the MultiClusterEngine's Overrides.
Returns true if the component is enabled, otherwise false.
*/
func (mce *MultiClusterEngine) Enabled(s string) bool {
if mce.Spec.Overrides == nil {
return false
Expand All @@ -73,10 +113,13 @@ func (mce *MultiClusterEngine) Enabled(s string) bool {
return c.Enabled
}
}

return false
}

/*
Enable enables a component with the given name in the MultiClusterEngine's Overrides.
If the component is not present, it adds it and sets it as enabled.
*/
func (mce *MultiClusterEngine) Enable(s string) {
if mce.Spec.Overrides == nil {
mce.Spec.Overrides = &Overrides{}
Expand All @@ -93,7 +136,10 @@ func (mce *MultiClusterEngine) Enable(s string) {
})
}

// Prune removes the component from the component list. Returns true if changes are made
/*
Prune removes a component with the given name from the MultiClusterEngine's Overrides.
Returns true if the component is pruned, indicating changes were made.
*/
func (mce *MultiClusterEngine) Prune(s string) bool {
if mce.Spec.Overrides == nil {
return false
Expand All @@ -115,6 +161,10 @@ func (mce *MultiClusterEngine) Prune(s string) bool {
return false
}

/*
Disable disables a component with the given name in the MultiClusterEngine's Overrides.
If the component is not present, it adds it and sets it as disabled.
*/
func (mce *MultiClusterEngine) Disable(s string) {
if mce.Spec.Overrides == nil {
mce.Spec.Overrides = &Overrides{}
Expand All @@ -131,7 +181,10 @@ func (mce *MultiClusterEngine) Disable(s string) {
})
}

// a component is valid if its name matches a known component
/*
validComponent checks if a ComponentConfig is valid by comparing its name to a list of known component names.
Returns true if the component is valid, otherwise false.
*/
func validComponent(c ComponentConfig) bool {
for _, name := range allComponents {
if c.Name == name {
Expand All @@ -141,6 +194,10 @@ func validComponent(c ComponentConfig) bool {
return false
}

/*
IsInHostedMode checks if the MultiClusterEngine has an annotation indicating it is in hosted mode.
Returns true if the annotation is present and its value is "ModeHosted," otherwise false.
*/
func IsInHostedMode(mce *MultiClusterEngine) bool {
a := mce.GetAnnotations()
if a == nil {
Expand All @@ -151,3 +208,29 @@ func IsInHostedMode(mce *MultiClusterEngine) bool {
}
return false
}

/*
GetLegacyPrometheusKind returns a list of legacy kind resources that are required to be removed before updating to
ACM 2.9 and later.
*/
func GetLegacyPrometheusKind() []string {
return LegacyPrometheusKind
}

// GetPrometheusRulesName returns the name of the PrometheusRules based on the provided component name.
func GetPrometheusRulesName(component string) (string, error) {
if val, ok := MCEPrometheusRules[component]; !ok {
return val, fmt.Errorf("failed to find PrometheusRules name for: %s component", component)
} else {
return val, nil
}
}

// GetServiceMonitorName returns the name of the ServiceMonitors based on the provided component name.
func GetServiceMonitorName(component string) (string, error) {
if val, ok := MCEServiceMonitors[component]; !ok {
return val, fmt.Errorf("failed to find ServiceMonitors name for: %s component", component)
} else {
return val, nil
}
}
5 changes: 2 additions & 3 deletions controllers/backplaneconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,8 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
return result, err
}

err = r.removeLegacyCLCPrometheusConfig(ctx)
if err != nil {
return ctrl.Result{}, err
for _, kind := range backplanev1.GetLegacyPrometheusKind() {
err = r.removeLegacyPrometheusConfigurations(ctx, "openshift-monitoring", kind)
}

result, err = r.ensureToggleableComponents(ctx, backplaneConfig)
Expand Down
16 changes: 12 additions & 4 deletions controllers/backplaneconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

configv1 "github.com/openshift/api/config/v1"

backplanev1 "github.com/stolostron/backplane-operator/api/v1"
v1 "github.com/stolostron/backplane-operator/api/v1"
"github.com/stolostron/backplane-operator/pkg/utils"

Expand Down Expand Up @@ -1097,17 +1098,24 @@ var _ = Describe("BackplaneConfig controller", func() {
err := k8sClient.Create(context.TODO(), sm)
Expect(err).To(BeNil())

legacyResourceKind := backplanev1.GetLegacyPrometheusKind()
ns := "openshift-monitoring"

By("Running the cleanup of the legacy Prometheus configuration")
err = reconciler.removeLegacyCLCPrometheusConfig(context.TODO())
Expect(err).To(BeNil())
for _, kind := range legacyResourceKind {
err = reconciler.removeLegacyPrometheusConfigurations(context.TODO(), ns, kind)
Expect(err).To(BeNil())
}

By("Verifying that the legacy CLC ServiceMonitor is deleted")
err = k8sClient.Get(context.TODO(), client.ObjectKeyFromObject(sm), sm)
Expect(errors.IsNotFound(err)).To(BeTrue())

By("Running the cleanup of the legacy Prometheus configuration again should do nothing")
err = reconciler.removeLegacyCLCPrometheusConfig(context.TODO())
Expect(err).To(BeNil())
for _, kind := range legacyResourceKind {
err = reconciler.removeLegacyPrometheusConfigurations(context.TODO(), ns, kind)
Expect(err).To(BeNil())
}
})
})
})
Expand Down
66 changes: 49 additions & 17 deletions controllers/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package controllers

import (
"context"
"fmt"

backplanev1 "github.com/stolostron/backplane-operator/api/v1"
"github.com/stolostron/backplane-operator/pkg/toggle"
Expand Down Expand Up @@ -135,32 +136,63 @@ func (r *MultiClusterEngineReconciler) uninstall(backplaneConfig *backplanev1.Mu
return false, nil
}

// removeLegacyCLCPrometheusConfig will remove the CLC PrometheusRule and ServiceMonitor in the openshift-monitoring
// namespace. This configuration should be in the controller namespace instead.
func (r *MultiClusterEngineReconciler) removeLegacyCLCPrometheusConfig(ctx context.Context) error {
/*
removeLegacyPrometheusConfigurations will remove the specified kind of configuration
(PrometheusRule or ServiceMonitor) in the target namespace. This configuration should be in the controller namespace
instead.
*/
func (r *MultiClusterEngineReconciler) removeLegacyPrometheusConfigurations(ctx context.Context,
targetNamespace string, kind string) error {
log := log.FromContext(ctx)

obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(schema.GroupVersionKind{
Group: "monitoring.coreos.com",
Kind: "ServiceMonitor",
Kind: kind,
Version: "v1",
})
obj.SetName("clusterlifecycle-state-metrics-v2")
obj.SetNamespace("openshift-monitoring")

err := r.Client.Delete(ctx, obj)
if err != nil {
if !errors.IsNotFound(err) && !apimeta.IsNoMatchError(err) {
log.Error(
err,
"Error while deleting ServiceMonitor: clusterlifecycle-state-metrics-v2",
)
var configType string
switch kind {
case "PrometheusRule":
configType = "PrometheusRule"

return err
}
} else {
log.Info("Deleted the legacy CLC Prometheus configuration", "kind", "ServiceMonitor", "name", obj.GetName())
case "ServiceMonitor":
configType = "ServiceMonitor"

default:
return fmt.Errorf("Unsupported kind detected when trying to remove legacy configuration: %s", kind)
}

for _, c := range backplanev1.MCEComponents {
res, err := func() (string, error) {
if configType == "PrometheusRule" {
return backplanev1.GetPrometheusRulesName(c)
}
return backplanev1.GetServiceMonitorName(c)
}()

if err != nil {
continue
}

obj.SetName(res)
obj.SetNamespace(targetNamespace)

err = r.Client.Delete(ctx, obj)
if err != nil {
if !errors.IsNotFound(err) && !apimeta.IsNoMatchError(err) {
log.Error(
err,
fmt.Sprintf("Error while deleting the legacy %s configuration", configType),
"kind", kind,
"name", obj.GetName(),
)
return err
}
} else {
log.Info(fmt.Sprintf("Deleted the legacy %s configuration: %s", configType, obj.GetName()))
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: console-mce-monitor
namespace: openshift-monitoring
namespace: {{ .Values.global.namespace }}
spec:
endpoints:
- bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
Expand All @@ -17,7 +17,7 @@ spec:
jobLabel: console-mce-console
namespaceSelector:
matchNames:
- multicluster-engine
- {{ .Values.global.namespace }}
selector:
matchLabels:
app: console-mce

0 comments on commit d405580

Please sign in to comment.