Skip to content

Commit

Permalink
[ACM-11731] Enhanced status for available components (#740)
Browse files Browse the repository at this point in the history
* enhanced status for available components

Signed-off-by: Disaiah Bennett <[email protected]>

* reverted hosted.go deletion

Signed-off-by: Disaiah Bennett <[email protected]>

---------

Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn authored Jun 10, 2024
1 parent 550ed54 commit 1c9fc28
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 109 deletions.
69 changes: 6 additions & 63 deletions controllers/backplaneconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strings"
"time"

"github.com/go-co-op/gocron"
"github.com/go-logr/logr"
backplanev1 "github.com/stolostron/backplane-operator/api/v1"
"github.com/stolostron/backplane-operator/pkg/foundation"
Expand Down Expand Up @@ -58,7 +57,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -91,9 +90,7 @@ const (
)

var (
scheduler *gocron.Scheduler
cronResyncTag = "multiclusterengine-operator-resync"
reconciliationInterval = 10 // minutes
log = logf.Log.WithName("reconcile")
)

//+kubebuilder:rbac:groups=multicluster.openshift.io,resources=multiclusterengines,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -158,15 +155,9 @@ var (
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (retRes ctrl.Result, retErr error) {
r.Log = log.Log.WithName("reconcile")
r.Log = log
r.Log.Info("Reconciling MultiClusterEngine")

// Initalize sceduler instance for operator resync cronjob.
if scheduler == nil {
r.Log.Info("Setting up scheduler for operator resync")
r.InitScheduler()
}

// Fetch the BackplaneConfig instance
backplaneConfig, err := r.getBackplaneConfig(ctx, req)
if err != nil && !apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -376,10 +367,6 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
// Do not reconcile objects if this instance of mce is labeled "paused"
if utils.IsPaused(backplaneConfig) {
r.Log.Info("MultiClusterEngine reconciliation is paused. Nothing more to do.")
if ok := scheduler.IsRunning(); ok {
r.Log.Info("Pausing MultiClusterEngine operator controller resync job.")
go r.StopScheduleOperatorControllerResync()
}

cond := status.NewCondition(
backplanev1.MultiClusterEngineProgressing,
Expand All @@ -389,8 +376,6 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
)
r.StatusManager.AddCondition(cond)
return ctrl.Result{}, nil
} else if ok := scheduler.IsRunning(); !ok {
defer r.ScheduleOperatorControllerResync(ctx, req)
}

var crdsDir string
Expand Down Expand Up @@ -473,7 +458,8 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
r.StatusManager.AddCondition(status.NewCondition(backplanev1.MultiClusterEngineProgressing, metav1.ConditionTrue,
status.DeploySuccessReason, "All components deployed"))

return ctrl.Result{}, nil
r.Log.Info("Reconcile completed. Requeuing after " + utils.ShortRefreshInterval.String())
return ctrl.Result{RequeueAfter: utils.ShortRefreshInterval}, nil
}

// This function set the operator condition created by OLM to either allow or disallow upgrade based on whether X.Y desired version matches current version
Expand Down Expand Up @@ -585,7 +571,6 @@ func (r *MultiClusterEngineReconciler) SetupWithManager(mgr ctrl.Manager) error
// trusted CA bundle for use with the OCP cluster wide proxy
func (r *MultiClusterEngineReconciler) createTrustBundleConfigmap(ctx context.Context,
mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

// Get Trusted Bundle configmap name
trustBundleName := defaultTrustBundleName
Expand Down Expand Up @@ -640,7 +625,6 @@ func (r *MultiClusterEngineReconciler) createTrustBundleConfigmap(ctx context.Co

func (r *MultiClusterEngineReconciler) createMetricsService(ctx context.Context,
mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

const Port = 8080

Expand Down Expand Up @@ -705,7 +689,6 @@ func (r *MultiClusterEngineReconciler) createMetricsService(ctx context.Context,

func (r *MultiClusterEngineReconciler) createMetricsServiceMonitor(ctx context.Context,
mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

smName := utils.MCEOperatorMetricsServiceMonitorName
smNamespace := mce.Spec.TargetNamespace
Expand Down Expand Up @@ -776,7 +759,6 @@ func (r *MultiClusterEngineReconciler) createMetricsServiceMonitor(ctx context.C
// DeployAlwaysSubcomponents ensures all subcomponents exist
func (r *MultiClusterEngineReconciler) DeployAlwaysSubcomponents(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

chartsDir := renderer.AlwaysChartsDir
// Renders all templates from charts
Expand Down Expand Up @@ -813,7 +795,6 @@ func (r *MultiClusterEngineReconciler) DeployAlwaysSubcomponents(ctx context.Con

func (r *MultiClusterEngineReconciler) ensureToggleableComponents(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
r.Log = log.Log.WithName("reconcile")

errs := map[string]error{}
requeue := false
Expand Down Expand Up @@ -1111,7 +1092,6 @@ func (r *MultiClusterEngineReconciler) applyTemplate(ctx context.Context,
// means the resource is in the process of deleting.
func (r *MultiClusterEngineReconciler) deleteTemplate(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine, template *unstructured.Unstructured) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

err := r.Client.Get(ctx, types.NamespacedName{Name: template.GetName(), Namespace: template.GetNamespace()}, template)
if err != nil && (apierrors.IsNotFound(err) || apimeta.IsNoMatchError(err)) {
Expand All @@ -1136,7 +1116,6 @@ func (r *MultiClusterEngineReconciler) deleteTemplate(ctx context.Context,

func (r *MultiClusterEngineReconciler) ensureCustomResources(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

if foundation.CanInstallAddons(ctx, r.Client) {
addonTemplates, err := foundation.GetAddons()
Expand Down Expand Up @@ -1167,7 +1146,6 @@ func (r *MultiClusterEngineReconciler) ensureCustomResources(ctx context.Context
func (r *MultiClusterEngineReconciler) ensureOpenShiftNamespaceLabel(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) {

log := log.Log.WithName("reconcile")
existingNs := &corev1.Namespace{}

err := r.Client.Get(ctx, types.NamespacedName{Name: backplaneConfig.Spec.TargetNamespace}, existingNs)
Expand Down Expand Up @@ -1199,7 +1177,6 @@ func (r *MultiClusterEngineReconciler) ensureOpenShiftNamespaceLabel(ctx context

func (r *MultiClusterEngineReconciler) finalizeBackplaneConfig(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine) error {
log := log.Log.WithName("reconcile")

ocpConsole, err := r.CheckConsole(ctx)
if err != nil {
Expand Down Expand Up @@ -1366,7 +1343,6 @@ func (r *MultiClusterEngineReconciler) finalizeBackplaneConfig(ctx context.Conte

func (r *MultiClusterEngineReconciler) getBackplaneConfig(ctx context.Context, req ctrl.Request) (
*backplanev1.MultiClusterEngine, error) {
r.Log = log.Log.WithName("reconcile")
backplaneConfig := &backplanev1.MultiClusterEngine{}
err := r.Client.Get(ctx, req.NamespacedName, backplaneConfig)
if err != nil {
Expand All @@ -1386,7 +1362,6 @@ func (r *MultiClusterEngineReconciler) getBackplaneConfig(ctx context.Context, r
// ensureUnstructuredResource ensures that the unstructured resource is applied in the cluster properly
func (r *MultiClusterEngineReconciler) ensureUnstructuredResource(ctx context.Context,
bpc *backplanev1.MultiClusterEngine, u *unstructured.Unstructured) (ctrl.Result, error) {
r.Log = log.Log.WithName("reconcile")

found := &unstructured.Unstructured{}
found.SetGroupVersionKind(u.GroupVersionKind())
Expand Down Expand Up @@ -1422,7 +1397,6 @@ func (r *MultiClusterEngineReconciler) ensureUnstructuredResource(ctx context.Co
}

func (r *MultiClusterEngineReconciler) setDefaults(ctx context.Context, m *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

updateNecessary := false
if !utils.AvailabilityConfigIsValid(m.Spec.AvailabilityConfig) {
Expand Down Expand Up @@ -1524,7 +1498,7 @@ func (r *MultiClusterEngineReconciler) setDefaults(ctx context.Context, m *backp

func (r *MultiClusterEngineReconciler) validateNamespace(ctx context.Context, m *backplanev1.MultiClusterEngine) (
ctrl.Result, error) {
log := log.Log.WithName("reconcile")

newNs := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: m.Spec.TargetNamespace,
Expand Down Expand Up @@ -1584,7 +1558,6 @@ func (r *MultiClusterEngineReconciler) validateImagePullSecret(ctx context.Conte
}

func (r *MultiClusterEngineReconciler) getClusterVersion(ctx context.Context) (string, error) {
r.Log = log.Log.WithName("reconcile")
// If Unit test
if val, ok := os.LookupEnv("UNIT_TEST"); ok && val == "true" {
if _, exists := os.LookupEnv("ACM_HUB_OCP_VERSION"); exists {
Expand All @@ -1610,7 +1583,6 @@ func (r *MultiClusterEngineReconciler) getClusterVersion(ctx context.Context) (s
//+kubebuilder:rbac:groups="config.openshift.io",resources="ingresses",verbs=get;list;watch

func (r *MultiClusterEngineReconciler) getClusterIngressDomain(ctx context.Context, mce *backplanev1.MultiClusterEngine) (string, error) {
r.Log = log.Log.WithName("reconcile")
// If Unit test
if val, ok := os.LookupEnv("UNIT_TEST"); ok && val == "true" {
return "apps.installer-test-cluster.dev00.red-chesterfield.com", nil
Expand All @@ -1630,35 +1602,6 @@ func (r *MultiClusterEngineReconciler) getClusterIngressDomain(ctx context.Conte
return clusterIngress.Spec.Domain, nil
}

func (r *MultiClusterEngineReconciler) InitScheduler() {
scheduler = gocron.NewScheduler(time.UTC)
}

func (r *MultiClusterEngineReconciler) ScheduleOperatorControllerResync(ctx context.Context, req ctrl.Request) {
r.Log = log.Log.WithName("reconcile")

if ok := scheduler.IsRunning(); !ok {
_, err := scheduler.Tag(cronResyncTag).Every(reconciliationInterval).Minutes().Do(r.Reconcile, ctx, req)

if err != nil {
r.Log.Error(err, "failed to schedule scheduler job for operator controller resync")
} else {
r.Log.Info(fmt.Sprintf("Starting scheduler job for operator controller. Reconciling every %v minutes",
reconciliationInterval))
scheduler.StartAsync()
}
}
}

// StopScheduleOperatorControllerResync ...
func (r *MultiClusterEngineReconciler) StopScheduleOperatorControllerResync() {
scheduler.Stop()

if ok := scheduler.IsRunning(); !ok {
r.InitScheduler()
}
}

func (r *MultiClusterEngineReconciler) CheckDeprecatedFieldUsage(m *backplanev1.MultiClusterEngine) {
a := m.GetAnnotations()
df := []struct {
Expand Down
3 changes: 1 addition & 2 deletions controllers/backplaneconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,7 @@ var _ = Describe("BackplaneConfig controller", func() {
backplaneConfig.Annotations = annotations
_ = k8sClient.Update(ctx, backplaneConfig)

reconciler.StopScheduleOperatorControllerResync()
return utils.IsPaused(backplaneConfig) && !scheduler.IsRunning()
return utils.IsPaused(backplaneConfig)
}, timeout, interval).Should(BeTrue())
})
})
Expand Down
12 changes: 3 additions & 9 deletions controllers/hosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

var ErrBadFormat = errors.New("bad format")

func (r *MultiClusterEngineReconciler) HostedReconcile(ctx context.Context, mce *backplanev1.MultiClusterEngine) (
retRes ctrl.Result, retErr error) {
r.Log = log.Log.WithName("reconcile")
r.Log = log

defer func() {
mce.Status = r.StatusManager.ReportStatus(*mce)
Expand Down Expand Up @@ -293,8 +292,6 @@ func parseKubeCreds(secret *corev1.Secret) ([]byte, error) {

func (r *MultiClusterEngineReconciler) ensureHostedImport(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine, hostedClient client.Client) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

r.StatusManager.AddComponent(toggle.EnabledStatus(types.NamespacedName{Name: "managedcluster-import-controller-v2",
Namespace: backplaneConfig.Spec.TargetNamespace}))

Expand Down Expand Up @@ -343,8 +340,6 @@ func (r *MultiClusterEngineReconciler) ensureHostedImport(ctx context.Context,

func (r *MultiClusterEngineReconciler) ensureNoHostedImport(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine, hostedClient client.Client) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")

r.StatusManager.RemoveComponent(toggle.EnabledStatus(
types.NamespacedName{Name: "managedcluster-import-controller-v2",
Namespace: backplaneConfig.Spec.TargetNamespace}))
Expand Down Expand Up @@ -394,7 +389,6 @@ func (r *MultiClusterEngineReconciler) ensureNoHostedImport(ctx context.Context,

func (r *MultiClusterEngineReconciler) ensureHostedClusterManager(ctx context.Context,
mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) {
log := log.Log.WithName("reconcile")
cmName := fmt.Sprintf("%s-cluster-manager", mce.Name)

r.StatusManager.AddComponent(status.ClusterManagerStatus{
Expand Down Expand Up @@ -520,7 +514,7 @@ func (r *MultiClusterEngineReconciler) ensureNoHostedClusterManager(ctx context.
// setHostedDefaults configures the MCE with default values and updates
func (r *MultiClusterEngineReconciler) setHostedDefaults(ctx context.Context, m *backplanev1.MultiClusterEngine) (
ctrl.Result, error) {
r.Log = log.Log.WithName("reconcile")
r.Log = log

updateNecessary := false
if !utils.AvailabilityConfigIsValid(m.Spec.AvailabilityConfig) {
Expand Down Expand Up @@ -606,7 +600,7 @@ A false response without error means the resource is in the process of deleting.
func (r *MultiClusterEngineReconciler) hostedDeleteTemplate(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine, template *unstructured.Unstructured, hostedClient client.Client) (
ctrl.Result, error) {
r.Log = log.Log.WithName("reconcile")
r.Log = log

err := hostedClient.Get(ctx, types.NamespacedName{Name: template.GetName(), Namespace: template.GetNamespace()},
template)
Expand Down
Loading

0 comments on commit 1c9fc28

Please sign in to comment.