Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
cs-operator refactor for no olm
Signed-off-by: Allen Li <[email protected]>
  • Loading branch information
qpdpQ committed Feb 4, 2025
commit e8218b41d0bb53d0a4e9e9712c16d6eb30342cd5
110 changes: 60 additions & 50 deletions controllers/bootstrap/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -97,39 +98,6 @@ type Resource struct {
Scope string
}

func NewNonOLMBootstrap(mgr manager.Manager) (bs *Bootstrap, err error) {
cpfsNs := util.GetCPFSNamespace(mgr.GetAPIReader())
servicesNs := util.GetServicesNamespace(mgr.GetAPIReader())
operatorNs, err := util.GetOperatorNamespace()
if err != nil {
return
}
csData := apiv3.CSData{
CPFSNs: cpfsNs,
ServicesNs: servicesNs,
OperatorNs: operatorNs,
CatalogSourceName: "",
CatalogSourceNs: "",
ApprovalMode: "",
WatchNamespaces: util.GetWatchNamespace(),
OnPremMultiEnable: strconv.FormatBool(util.CheckMultiInstances(mgr.GetAPIReader())),
ExcludedCatalog: constant.ExcludedCatalog,
StatusMonitoredServices: constant.StatusMonitoredServices,
}

bs = &Bootstrap{
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
Config: mgr.GetConfig(),
EventRecorder: mgr.GetEventRecorderFor("ibm-common-service-operator"),
Manager: deploy.NewDeployManager(mgr),
SaasEnable: util.CheckSaas(mgr.GetAPIReader()),
MultiInstancesEnable: util.CheckMultiInstances(mgr.GetAPIReader()),
CSData: csData,
}
return
}

// NewBootstrap is the way to create a NewBootstrap struct
func NewBootstrap(mgr manager.Manager) (bs *Bootstrap, err error) {
cpfsNs := util.GetCPFSNamespace(mgr.GetAPIReader())
Expand All @@ -140,10 +108,13 @@ func NewBootstrap(mgr manager.Manager) (bs *Bootstrap, err error) {
}

catalogSourceName, catalogSourceNs := util.GetCatalogSource(constant.IBMCSPackage, operatorNs, mgr.GetAPIReader())
if catalogSourceName == "" || catalogSourceNs == "" {
err = fmt.Errorf("failed to get catalogsource")
return
if os.Getenv("NO_OLM") != "true" {
if catalogSourceName == "" || catalogSourceNs == "" {
err = fmt.Errorf("failed to get catalogsource")
return
}
}

approvalMode, err := util.GetApprovalModeinNs(mgr.GetAPIReader(), operatorNs)
if err != nil {
return
Expand Down Expand Up @@ -196,13 +167,18 @@ func NewBootstrap(mgr manager.Manager) (bs *Bootstrap, err error) {

// InitResources initialize resources at the bootstrap of operator
func (b *Bootstrap) InitResources(instance *apiv3.CommonService, forceUpdateODLMCRs bool) error {
installPlanApproval := instance.Spec.InstallPlanApproval

if installPlanApproval != "" {
if installPlanApproval != olmv1alpha1.ApprovalAutomatic && installPlanApproval != olmv1alpha1.ApprovalManual {
return fmt.Errorf("invalid value for installPlanApproval %v", installPlanApproval)
installPlanApproval := instance.Spec.InstallPlanApproval
if os.Getenv("NO_OLM") != "true" {
if installPlanApproval != "" {
if installPlanApproval != olmv1alpha1.ApprovalAutomatic && installPlanApproval != olmv1alpha1.ApprovalManual {
return fmt.Errorf("invalid value for installPlanApproval %v", installPlanApproval)
}
b.CSData.ApprovalMode = string(installPlanApproval)
}
b.CSData.ApprovalMode = string(installPlanApproval)
} else {
// set installPlanApproval to empty in non olm environment
installPlanApproval = ""
}

// Clean v3 Namespace Scope Operator and CRs in the servicesNamespace
Expand Down Expand Up @@ -280,16 +256,18 @@ func (b *Bootstrap) InitResources(instance *apiv3.CommonService, forceUpdateODLM
}
}

klog.Info("Installing ODLM Operator")
if err := b.renderTemplate(constant.ODLMSubscription, b.CSData); err != nil {
return err
}
if os.Getenv("NO_OLM") != "true" {
klog.Info("Installing ODLM Operator")
if err := b.renderTemplate(constant.ODLMSubscription, b.CSData); err != nil {
return err
}

klog.Info("Waiting for ODLM Operator to be ready")
if isWaiting, err := b.waitOperatorCSV(constant.IBMODLMPackage, "ibm-odlm", b.CSData.CPFSNs); err != nil {
return err
} else if isWaiting {
forceUpdateODLMCRs = true
klog.Info("Waiting for ODLM Operator to be ready")
if isWaiting, err := b.waitOperatorCSV(constant.IBMODLMPackage, "ibm-odlm", b.CSData.CPFSNs); err != nil {
return err
} else if isWaiting {
forceUpdateODLMCRs = true
}
}

// wait ODLM OperandRegistry and OperandConfig CRD
Expand Down Expand Up @@ -532,6 +510,10 @@ func (b *Bootstrap) DeleteFromYaml(objectTemplate string, data interface{}) erro

// GetSubscription returns the subscription instance of "name" from "namespace" namespace
func (b *Bootstrap) GetSubscription(ctx context.Context, name, namespace string) (*unstructured.Unstructured, error) {
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip get subscription in no olm environment")
return nil, nil
}
klog.Infof("Fetch Subscription: %v/%v", namespace, name)
sub := &unstructured.Unstructured{}
sub.SetGroupVersionKind(olmv1alpha1.SchemeGroupVersion.WithKind("subscription"))
Expand All @@ -549,6 +531,10 @@ func (b *Bootstrap) GetSubscription(ctx context.Context, name, namespace string)

// GetSubscription returns the subscription instances from a namespace
func (b *Bootstrap) ListSubscriptions(ctx context.Context, namespace string, listOptions client.ListOptions) (*unstructured.UnstructuredList, error) {
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip list subscription in no olm environment")
return nil, nil
}
klog.Infof("List Subscriptions in namespace %v", namespace)
subs := &unstructured.UnstructuredList{}
subs.SetGroupVersionKind(olmv1alpha1.SchemeGroupVersion.WithKind("SubscriptionList"))
Expand Down Expand Up @@ -658,6 +644,11 @@ func (b *Bootstrap) ListIssuer(ctx context.Context, opts ...client.ListOption) *

func (b *Bootstrap) CheckOperatorCatalog(ns string) error {

if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip ckeck catalog in no olm environment")
return nil
}

err := utilwait.PollImmediate(time.Second*10, time.Minute*3, func() (done bool, err error) {
subList := &olmv1alpha1.SubscriptionList{}

Expand Down Expand Up @@ -1116,6 +1107,11 @@ func (b *Bootstrap) GetObjs(objectTemplate string, data interface{}, alwaysUpdat
// need this function because common service operator is not in operandRegistry
func (b *Bootstrap) UpdateCsOpApproval() error {
var commonserviceNS string
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip update common-service operator approval mode in no olm environment")
return nil
}

operatorNs, err := util.GetOperatorNamespace()
if err != nil {
klog.Errorf("Getting operator namespace failed: %v", err)
Expand Down Expand Up @@ -1173,6 +1169,11 @@ func (b *Bootstrap) UpdateCsOpApproval() error {
}

func (b *Bootstrap) updateApprovalMode() error {
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip update ApprovalMode in no olm environment")
return nil
}

opreg := &odlm.OperandRegistry{}
opregKey := types.NamespacedName{
Name: "common-service",
Expand Down Expand Up @@ -1431,6 +1432,11 @@ func (b *Bootstrap) DeployCertManagerCR() error {
// NamespaceScope resources include common-service, nss-managedby-odlm, nss-odlm-scope, and odlm-scope-managedby-odlm
func (b *Bootstrap) CleanNamespaceScopeResources() error {

if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip cleanup namespace scope resources in no olm environment")
return nil
}

// get namespace-scope ConfigMap in operatorNamespace
nssCmNs, err := util.GetNssCmNs(b.Reader, b.CSData.OperatorNs)
if err != nil {
Expand Down Expand Up @@ -2137,6 +2143,10 @@ func (b *Bootstrap) waitOperatorCSV(subName, packageManifest, operatorNs string)
}

func (b *Bootstrap) checkOperatorCSV(subName, packageManifest, operatorNs string) (bool, error) {
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip checking operator CSV in no olm environment")
return false, nil
}
// Get the subscription by name and namespace
sub := &olmv1alpha1.Subscription{}
if err := b.Reader.Get(context.TODO(), types.NamespacedName{Name: subName, Namespace: operatorNs}, sub); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions controllers/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,13 @@ func GetControlNs(r client.Reader) (controlNs string) {
return
}

// could have issue
func GetApprovalModeinNs(r client.Reader, ns string) (approvalMode string, err error) {
// set approval mode to empty in non-olm environment
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("set approval mode to empty in no olm environment")
return "", nil
}
approvalMode = string(olmv1alpha1.ApprovalAutomatic)
subList := &olmv1alpha1.SubscriptionList{}
if err := r.List(context.TODO(), subList, &client.ListOptions{Namespace: ns}); err != nil {
Expand All @@ -511,6 +517,10 @@ func GetApprovalModeinNs(r client.Reader, ns string) (approvalMode string, err e

// GetCatalogSource gets CatalogSource will be used by operators
func GetCatalogSource(packageName, ns string, r client.Reader) (CatalogSourceName, CatalogSourceNS string) {
if os.Getenv("NO_OLM") != "true" {
klog.V(2).Infof("set catalogsource name and namespace to empty in no olm environment")
return "", ""
}
subList := &olmv1alpha1.SubscriptionList{}
if err := r.List(context.TODO(), subList, &client.ListOptions{Namespace: ns}); err != nil {
klog.Info(err)
Expand Down
46 changes: 0 additions & 46 deletions controllers/commonservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,6 @@ func (r *CommonServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
klog.Error("Accept license by changing .spec.license.accept to true in the CommonService CR. Operator will not proceed until then")
}

if os.Getenv("NO_OLM") == "true" {
klog.Infof("Reconciling CommonService: %s in non OLM environment", req.NamespacedName)

// create ibm-cpp-config configmap
if err := configurationcollector.CreateUpdateConfig(r.Bootstrap); err != nil {
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}

// deploy Cert Manager CR
if err := r.Bootstrap.DeployCertManagerCR(); err != nil {
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}

klog.Infof("Start to Create ODLM CR in the namespace %s", r.Bootstrap.CSData.OperatorNs)
// Check if ODLM OperandRegistry and OperandConfig are created
klog.Info("Checking if OperandRegistry and OperandConfig CRD already exist")
existOpreg, _ := r.Bootstrap.CheckCRD(constant.OpregAPIGroupVersion, constant.OpregKind)
existOpcon, _ := r.Bootstrap.CheckCRD(constant.OpregAPIGroupVersion, constant.OpconKind)
// Install/update Opreg and Opcon resources before installing ODLM if CRDs exist
if existOpreg && existOpcon {
klog.Info("Installing/Updating OperandRegistry")
if err := r.Bootstrap.InstallOrUpdateOpreg(true, ""); err != nil {
klog.Errorf("Fail to Installing/Updating OperandConfig: %v", err)
return ctrl.Result{}, err
}

klog.Info("Installing/Updating OperandConfig")
if err := r.Bootstrap.InstallOrUpdateOpcon(true); err != nil {
klog.Errorf("Fail to Installing/Updating OperandConfig: %v", err)
return ctrl.Result{}, err
}

// Temporary solution for EDB image ConfigMap reference
if err := r.Bootstrap.CreateEDBImageMaps(); err != nil {
klog.Errorf("Failed to create EDB Image ConfigMap: %v", err)
return ctrl.Result{}, err
}
} else {
klog.Error("ODLM CRD not ready, waiting for it to be ready")
}

return ctrl.Result{}, nil
}

// If the CommonService CR is not paused, continue to reconcile
if !r.reconcilePauseRequest(instance) {
if r.checkNamespace(req.NamespacedName.String()) {
Expand Down
5 changes: 5 additions & 0 deletions controllers/deploy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package deploy
import (
"context"
"fmt"
"os"
"time"

olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
Expand Down Expand Up @@ -165,6 +166,10 @@ func (d *Manager) GetDeployment() (*appsv1.Deployment, error) {

// DeleteOperator delete operator's csv and subscription from specific namespace
func (d *Manager) DeleteOperator(name, namespace string) error {
if os.Getenv("NO_OLM") == "true" {
klog.V(2).Infof("skip delete operator in no-olm environment")
return nil
}
// Get existing operator's subscription
subName := name
subNs := namespace
Expand Down
Loading
Loading