diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index aeb396611..3ab4f1c15 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -23,6 +23,7 @@ rules: - apiGroups: - config.openshift.io resources: + - clusterversions - infrastructures verbs: - get diff --git a/controllers/performanceprofile_controller.go b/controllers/performanceprofile_controller.go index 3c1138ef3..d06bef234 100644 --- a/controllers/performanceprofile_controller.go +++ b/controllers/performanceprofile_controller.go @@ -224,7 +224,7 @@ func validateUpdateEvent(e *event.UpdateEvent) bool { // +kubebuilder:rbac:groups=machineconfiguration.openshift.io,resources=machineconfigs;machineconfigpools;kubeletconfigs,verbs=* // +kubebuilder:rbac:groups=tuned.openshift.io,resources=tuneds;profiles,verbs=* // +kubebuilder:rbac:groups=node.k8s.io,resources=runtimeclasses,verbs=* -// +kubebuilder:rbac:groups=config.openshift.io,resources=infrastructures,verbs=get;list;watch +// +kubebuilder:rbac:groups=config.openshift.io,resources=infrastructures;clusterversions,verbs=get;list;watch // +kubebuilder:rbac:namespace="openshift-performance-addon-operator",groups=core,resources=pods;services;services/finalizers;configmaps,verbs=* // +kubebuilder:rbac:namespace="openshift-performance-addon-operator",groups=coordination.k8s.io,resources=leases,verbs=create;get;list;update // +kubebuilder:rbac:namespace="openshift-performance-addon-operator",groups=apps,resourceNames=performance-operator,resources=deployments/finalizers,verbs=update diff --git a/deploy/olm-catalog/performance-addon-operator/4.10.0/performance-addon-operator.v4.10.0.clusterserviceversion.yaml b/deploy/olm-catalog/performance-addon-operator/4.10.0/performance-addon-operator.v4.10.0.clusterserviceversion.yaml index 0d204a1d1..e17c2b3e3 100644 --- a/deploy/olm-catalog/performance-addon-operator/4.10.0/performance-addon-operator.v4.10.0.clusterserviceversion.yaml +++ b/deploy/olm-catalog/performance-addon-operator/4.10.0/performance-addon-operator.v4.10.0.clusterserviceversion.yaml @@ -180,6 +180,7 @@ spec: - apiGroups: - config.openshift.io resources: + - clusterversions - infrastructures verbs: - get diff --git a/main.go b/main.go index d53677991..6bc9a9ae8 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,14 @@ limitations under the License. package main import ( + "context" "flag" "fmt" + "os" + "os/signal" "runtime" + "strings" + "syscall" performancev1 "github.com/openshift-kni/performance-addon-operators/api/v1" performancev1alpha1 "github.com/openshift-kni/performance-addon-operators/api/v1alpha1" @@ -28,10 +33,12 @@ import ( "github.com/openshift-kni/performance-addon-operators/pkg/controller/performanceprofile/components" "github.com/openshift-kni/performance-addon-operators/pkg/utils/leaderelection" "github.com/openshift-kni/performance-addon-operators/version" - "github.com/spf13/cobra" - + v1 "github.com/openshift/api/config/v1" tunedv1 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/tuned/v1" mcov1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" + "github.com/spf13/cobra" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog" @@ -109,6 +116,53 @@ func newRootCommand() *cobra.Command { return cmd } +func sleepUntilTERM() { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) + + for { + if sig := <-sigs; sig == syscall.SIGTERM { + signal.Stop(sigs) + klog.Info("SIGTERM caught") + break + } + } +} + +func detectOCP411(cfg *rest.Config) { + // Detect cluster version + // and cease operation (do not die to avoid respawn) when on 4.11 + // as the PAO logic is provided by NTO there + k8sclient, err := client.New(cfg, client.Options{}) + if err != nil { + klog.Fatalf("could not detect cluster version: failed to create client: %v", err) + } + + // Register OCP types to the client + if err := v1.Install(k8sclient.Scheme()); err != nil { + klog.Fatalf("could not detect cluster version: failed to register OCP types: %v", err) + } + + // Get OCP version objects (there should be just one) + clusterversionlist := v1.ClusterVersionList{} + ctx := context.Background() + if err := k8sclient.List(ctx, &clusterversionlist); err != nil { + klog.Fatalf("could not detect cluster version: %v", err) + } + if len(clusterversionlist.Items) == 0 { + klog.Fatalf("could not detect cluster version: no version objects found") + } + + // Detect 4.11 + // The operator will not be installable on 4.12 so this is + // the only exception we need to check for + if strings.HasPrefix(clusterversionlist.Items[0].Status.Desired.Version, "4.11") { + klog.Error("This operator's functionality is provided by the Node Tuning Operator from now on. Hibernating.") + sleepUntilTERM() + os.Exit(0) + } +} + func runPAO() { var metricsAddr string var enableLeaderElection bool @@ -136,6 +190,8 @@ func runPAO() { restConfig := ctrl.GetConfigOrDie() le := leaderelection.GetLeaderElectionConfig(restConfig, enableLeaderElection) + detectOCP411(restConfig) + mgr, err := ctrl.NewManager(restConfig, ctrl.Options{ NewCache: cache.MultiNamespacedCacheBuilder(namespaces), Scheme: scheme,