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

Bug 2293611: [release-4.16] Backport cleanup maintenance addon #227

Open
wants to merge 1 commit into
base: release-4.16
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions controllers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package controllers
import (
"context"
"flag"
"fmt"
"os"

"github.com/go-logr/logr"
consolev1alpha1 "github.com/openshift/api/console/v1alpha1"
"github.com/openshift/library-go/pkg/operator/events"
ramenv1alpha1 "github.com/ramendr/ramen/api/v1alpha1"
Expand All @@ -13,7 +15,11 @@ import (
"github.com/red-hat-storage/odf-multicluster-orchestrator/console"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -22,6 +28,7 @@ import (
clusterv1 "open-cluster-management.io/api/cluster/v1"
workv1 "open-cluster-management.io/api/work/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -93,10 +100,62 @@ func NewManagerCommand() *cobra.Command {
return cmd
}

// TODO: Remove/update this function in a later release when cleanup is not required.
func preRunGarbageCollection(cl client.Client, logger logr.Logger) error {
ctx := context.TODO()
req, err := labels.NewRequirement("openshiftVersion-major-minor", selection.In, []string{"4.16", "4.17", "4.18"})
if err != nil {
return fmt.Errorf("couldn't create ManagedCluster selector requirement. %w", err)
}

var managedClusters clusterv1.ManagedClusterList
err = cl.List(ctx, &managedClusters, &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*req),
})
if err != nil {
return fmt.Errorf("couldn't list ManagedClusters. %w", err)
}
logger.Info(fmt.Sprintf("Found %v ManagedClusters matching given requirements", len(managedClusters.Items)), "Requirements", req)

for idx := range managedClusters.Items {
maintenanceAddon := addonapiv1alpha1.ManagedClusterAddOn{
ObjectMeta: metav1.ObjectMeta{
Name: "maintenance",
Namespace: managedClusters.Items[idx].Name,
},
}
err = cl.Delete(ctx, &maintenanceAddon, &client.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to delete maintenance addon from cluster %q. %w", managedClusters.Items[idx].Name, err)
}
if err == nil || errors.IsNotFound(err) {
logger.Info("Deleted maintenance addon from ManagedCluster", "ManagedCluster", managedClusters.Items[idx].Name)
}
}
return nil
}

func (o *ManagerOptions) runManager() {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&o.ZapOpts)))
setupLog := ctrl.Log.WithName("setup")

preRunClient, err := client.New(ctrl.GetConfigOrDie(), client.Options{
Scheme: mgrScheme,
Cache: nil,
})
if err != nil {
setupLog.Error(err, "Failed to create a client for pre-run garbage collection")
os.Exit(1)
}

setupLog.Info("Starting garbage collection before starting manager")
err = preRunGarbageCollection(preRunClient, setupLog)
if err != nil {
setupLog.Error(err, "Pre-run garbage collection failed")
os.Exit(1)
}
setupLog.Info("Finished garbage collection")

srv := webhook.NewServer(webhook.Options{
CertDir: WebhookCertDir,
CertName: WebhookCertName,
Expand Down
Loading