Skip to content

Commit 6e340eb

Browse files
authored
Add support for operator aliasing via ConfigName (#1141)
* add configname type into operator registry Signed-off-by: YuChen <[email protected]> * enhance sharing config logic for uninstallation process Signed-off-by: YuChen <[email protected]> * polish code Signed-off-by: YuChen <[email protected]> * add configname and fallback channel into e2e crd Signed-off-by: YuChen <[email protected]> --------- Signed-off-by: YuChen <[email protected]>
1 parent 6bd71aa commit 6e340eb

File tree

11 files changed

+70
-17
lines changed

11 files changed

+70
-17
lines changed

api/v1alpha1/operandconfig_types.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,23 @@ const (
237237
)
238238

239239
// GetService obtains the service definition with the operand name.
240-
func (r *OperandConfig) GetService(operandName string) *ConfigService {
240+
func (r *OperandConfig) GetService(operandName string, configName string) *ConfigService { // First try to find by configName if it's provided and not empty
241+
// First try to find by configName if it's provided
242+
if configName != "" {
243+
for _, s := range r.Spec.Services {
244+
if s.Name == configName {
245+
return &s
246+
}
247+
}
248+
}
249+
250+
// Fall back to operandName if configName is not provided or not found
241251
for _, s := range r.Spec.Services {
242252
if s.Name == operandName {
243253
return &s
244254
}
245255
}
256+
246257
return nil
247258
}
248259

api/v1alpha1/operandregistry_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ type Operator struct {
5656
TargetNamespaces []string `json:"targetNamespaces,omitempty"`
5757
// Name of the package that defines the applications.
5858
PackageName string `json:"packageName"`
59+
// Name of service listed in OperandConfig
60+
ConfigName string `json:"configName,omitempty"`
5961
// Name of the channel to track.
6062
Channel string `json:"channel"`
6163
// List of channels to fallback when the main channel is not available.

bundle/manifests/operator.ibm.com_operandregistries.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ spec:
6767
channel:
6868
description: Name of the channel to track.
6969
type: string
70+
configName:
71+
description: Name of service listed in OperandConfig
72+
type: string
7073
description:
7174
description: Description of a common service.
7275
type: string

config/crd/bases/operator.ibm.com_operandregistries.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ spec:
6363
channel:
6464
description: Name of the channel to track.
6565
type: string
66+
configName:
67+
description: Name of service listed in OperandConfig
68+
type: string
6669
description:
6770
description: Description of a common service.
6871
type: string

config/e2e/crd/bases/operator.ibm.com_operandregistries.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,18 @@ spec:
6161
channel:
6262
description: Name of the channel to track.
6363
type: string
64+
configName:
65+
description: Name of service listed in OperandConfig
66+
type: string
6467
description:
6568
description: Description of a common service.
6669
type: string
70+
fallbackChannels:
71+
description: List of channels to fallback when the main channel
72+
is not available.
73+
items:
74+
type: string
75+
type: array
6776
installMode:
6877
description: 'The install mode of an operator, either namespace
6978
or cluster. Valid values are: - "namespace" (default): operator

controllers/operandconfig/operandconfig_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (r *Reconciler) updateStatus(ctx context.Context, instance *operatorv1alpha
120120

121121
op := op
122122

123-
service := instance.GetService(op.Name)
123+
service := instance.GetService(op.Name, op.ConfigName)
124124
if service == nil {
125125
continue
126126
}

controllers/operandrequest/operandrequest_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ func (r *Reconciler) addFinalizer(ctx context.Context, cr *operatorv1alpha1.Oper
240240
}
241241

242242
func (r *Reconciler) checkFinalizer(ctx context.Context, requestInstance *operatorv1alpha1.OperandRequest) error {
243-
klog.V(1).Infof("Deleting OperandRequest %s in the namespace %s", requestInstance.Name, requestInstance.Namespace)
244243
remainingOperands := gset.NewSet()
245244
for _, m := range requestInstance.Status.Members {
246245
remainingOperands.Add(m.Name)

controllers/operandrequest/reconcile_operand.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (r *Reconciler) reconcileOperand(ctx context.Context, requestInstance *oper
192192
configInstance, err := r.GetOperandConfig(ctx, registryKey)
193193
if err == nil {
194194
// Check the requested Service Config if exist in specific OperandConfig
195-
opdConfig := configInstance.GetService(operand.Name)
195+
opdConfig := configInstance.GetService(operand.Name, opdRegistry.ConfigName)
196196
if opdConfig == nil && !opdRegistry.UserManaged {
197197
klog.V(2).Infof("There is no service: %s from the OperandConfig instance: %s/%s, Skip reconciling Operands", operand.Name, registryKey.Namespace, req.Registry)
198198
continue
@@ -586,7 +586,7 @@ func (r *Reconciler) reconcileK8sResource(ctx context.Context, res operatorv1alp
586586
}
587587

588588
// deleteAllCustomResource remove custom resource base on OperandConfig and CSV alm-examples
589-
func (r *Reconciler) deleteAllCustomResource(ctx context.Context, csv *olmv1alpha1.ClusterServiceVersion, requestInstance *operatorv1alpha1.OperandRequest, csc *operatorv1alpha1.OperandConfig, operandName, namespace string) error {
589+
func (r *Reconciler) deleteAllCustomResource(ctx context.Context, csv *olmv1alpha1.ClusterServiceVersion, requestInstance *operatorv1alpha1.OperandRequest, csc *operatorv1alpha1.OperandConfig, opConfigName, operandName, namespace string) error {
590590

591591
customeResourceMap := make(map[string]operatorv1alpha1.OperandCRMember)
592592
for _, member := range requestInstance.Status.Members {
@@ -638,7 +638,7 @@ func (r *Reconciler) deleteAllCustomResource(ctx context.Context, csv *olmv1alph
638638
return merr
639639
}
640640

641-
service := csc.GetService(operandName)
641+
service := csc.GetService(operandName, opConfigName)
642642
if service == nil {
643643
return nil
644644
}
@@ -1332,9 +1332,9 @@ func (r *Reconciler) updateK8sRoute(ctx context.Context, existingK8sRes unstruct
13321332
}
13331333

13341334
// deleteAllK8sResource remove k8s resource base on OperandConfig
1335-
func (r *Reconciler) deleteAllK8sResource(ctx context.Context, csc *operatorv1alpha1.OperandConfig, operandName, namespace string) error {
1335+
func (r *Reconciler) deleteAllK8sResource(ctx context.Context, csc *operatorv1alpha1.OperandConfig, opConfigName, operandName, namespace string) error {
13361336

1337-
service := csc.GetService(operandName)
1337+
service := csc.GetService(operandName, opConfigName)
13381338
if service == nil {
13391339
return nil
13401340
}

controllers/operandrequest/reconcile_operator.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (r *Reconciler) reconcileOperator(ctx context.Context, requestInstance *ope
5353
for _, m := range requestInstance.Status.Members {
5454
remainingOperands.Add(m.Name)
5555
}
56+
5657
// Update request status
5758
defer func() {
5859
requestInstance.FreshMemberStatus(&remainingOperands)
@@ -230,6 +231,12 @@ func (r *Reconciler) reconcileSubscription(ctx context.Context, requestInstance
230231
sub.Annotations[registryKey.Namespace+"."+registryKey.Name+"/config"] = "true"
231232
sub.Annotations[requestInstance.Namespace+"."+requestInstance.Name+"."+operand.Name+"/request"] = opt.Channel
232233
sub.Annotations[requestInstance.Namespace+"."+requestInstance.Name+"."+operand.Name+"/operatorNamespace"] = namespace
234+
// Only add the config annotation if ConfigName is not empty
235+
if opt.ConfigName != "" {
236+
sub.Annotations[requestInstance.Namespace+"."+requestInstance.Name+"."+operand.Name+"/config"] = opt.ConfigName
237+
} else {
238+
sub.Annotations[requestInstance.Namespace+"."+requestInstance.Name+"."+operand.Name+"/config"] = opt.Name
239+
}
233240

234241
if opt.InstallMode == operatorv1alpha1.InstallModeNoop {
235242
isMatchedChannel = true
@@ -445,11 +452,11 @@ func (r *Reconciler) uninstallOperatorsAndOperands(ctx context.Context, operandN
445452
klog.Infof("Found %d ClusterServiceVersions for Subscription %s/%s", len(csvList), sub.Namespace, sub.Name)
446453
if uninstallOperand {
447454
klog.V(2).Infof("Deleting all the Custom Resources for CSV, Namespace: %s, Name: %s", csvList[0].Namespace, csvList[0].Name)
448-
if err := r.deleteAllCustomResource(ctx, csvList[0], requestInstance, configInstance, operandName, configInstance.Namespace); err != nil {
455+
if err := r.deleteAllCustomResource(ctx, csvList[0], requestInstance, configInstance, op.ConfigName, operandName, configInstance.Namespace); err != nil {
449456
return err
450457
}
451458
klog.V(2).Infof("Deleting all the k8s Resources for CSV, Namespace: %s, Name: %s", csvList[0].Namespace, csvList[0].Name)
452-
if err := r.deleteAllK8sResource(ctx, configInstance, operandName, configInstance.Namespace); err != nil {
459+
if err := r.deleteAllK8sResource(ctx, configInstance, op.ConfigName, operandName, configInstance.Namespace); err != nil {
453460
return err
454461
}
455462
}
@@ -533,11 +540,11 @@ func (r *Reconciler) uninstallOperands(ctx context.Context, operandName string,
533540
klog.Infof("Found %d ClusterServiceVersions for package %s/%s", len(csvList), op.Name, namespace)
534541
if uninstallOperand {
535542
klog.V(2).Infof("Deleting all the Custom Resources for CSV, Namespace: %s, Name: %s", csvList[0].Namespace, csvList[0].Name)
536-
if err := r.deleteAllCustomResource(ctx, csvList[0], requestInstance, configInstance, operandName, configInstance.Namespace); err != nil {
543+
if err := r.deleteAllCustomResource(ctx, csvList[0], requestInstance, configInstance, op.ConfigName, operandName, configInstance.Namespace); err != nil {
537544
return err
538545
}
539546
klog.V(2).Infof("Deleting all the k8s Resources for CSV, Namespace: %s, Name: %s", csvList[0].Namespace, csvList[0].Name)
540-
if err := r.deleteAllK8sResource(ctx, configInstance, operandName, configInstance.Namespace); err != nil {
547+
if err := r.deleteAllK8sResource(ctx, configInstance, op.ConfigName, operandName, configInstance.Namespace); err != nil {
541548
return err
542549
}
543550
}
@@ -613,7 +620,6 @@ func (r *Reconciler) absentOperatorsAndOperands(ctx context.Context, requestInst
613620
}
614621

615622
func (r *Reconciler) getNeedDeletedOperands(requestInstance *operatorv1alpha1.OperandRequest) gset.Set {
616-
klog.V(3).Info("Getting the operator need to be delete")
617623
deployedOperands := gset.NewSet()
618624
for _, req := range requestInstance.Status.Members {
619625
deployedOperands.Add(req.Name)
@@ -739,13 +745,24 @@ func checkSubAnnotationsForUninstall(reqName, reqNs, opName, installMode string,
739745
uninstallOperator := true
740746
uninstallOperand := true
741747

748+
// Store the current operator's config name before removing its annotations
749+
var currentConfigName string
750+
configKey := reqNs + "." + reqName + "." + opName + "/config"
751+
if configVal, exists := sub.Annotations[configKey]; exists {
752+
currentConfigName = configVal
753+
}
754+
742755
delete(sub.Annotations, reqNs+"."+reqName+"."+opName+"/request")
743756
delete(sub.Annotations, reqNs+"."+reqName+"."+opName+"/operatorNamespace")
757+
delete(sub.Annotations, configKey)
744758

745759
var opreqNsSlice []string
746760
var operatorNameSlice []string
761+
var configNameSlice []string // Track all remaining config names
762+
747763
namespaceReg, _ := regexp.Compile(`^(.*)\.(.*)\.(.*)\/operatorNamespace`)
748764
channelReg, _ := regexp.Compile(`^(.*)\.(.*)\.(.*)\/request`)
765+
configReg, _ := regexp.Compile(`^(.*)\.(.*)\.(.*)\/config`)
749766

750767
for key, value := range sub.Annotations {
751768
if namespaceReg.MatchString(key) {
@@ -758,6 +775,10 @@ func checkSubAnnotationsForUninstall(reqName, reqNs, opName, installMode string,
758775
annoPrefix := strings.Split(keyParts[0], ".")
759776
operatorNameSlice = append(operatorNameSlice, annoPrefix[len(annoPrefix)-1])
760777
}
778+
if configReg.MatchString(key) {
779+
// Add config name to the list
780+
configNameSlice = append(configNameSlice, value)
781+
}
761782
}
762783

763784
// If one of remaining <prefix>/operatorNamespace annotations' values is the same as subscription's namespace,
@@ -773,8 +794,12 @@ func checkSubAnnotationsForUninstall(reqName, reqNs, opName, installMode string,
773794
// When one of following conditions are met, the operand will NOT be uninstalled:
774795
// 1. operator is not uninstalled AND intallMode is no-op.
775796
// 2. operator is uninstalled AND at least one other <prefix>/operatorNamespace annotation exists.
776-
// 2. remaining <prefix>/request annotation's values contain the same operator name
777-
if (!uninstallOperator && installMode == operatorv1alpha1.InstallModeNoop) || (uninstallOperator && len(opreqNsSlice) != 0) || util.Contains(operatorNameSlice, opName) {
797+
// 3. remaining <prefix>/request annotation's values contain the same operator name
798+
// 4. remaining <prefix>/config annotation's values contain the same configName as this operator
799+
if (!uninstallOperator && installMode == operatorv1alpha1.InstallModeNoop) ||
800+
(uninstallOperator && len(opreqNsSlice) != 0) ||
801+
util.Contains(operatorNameSlice, opName) ||
802+
(currentConfigName != "" && util.Contains(configNameSlice, currentConfigName)) {
778803
uninstallOperand = false
779804
}
780805

controllers/operator/manager.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,6 @@ func (m *ODLMOperator) processExpressionCondition(ctx context.Context, templateR
820820
instanceType, instanceNs, instanceName, key, err)
821821
return "", err
822822
}
823-
klog.Infof("010101 key %s and result is %v", key, result)
824823

825824
if result {
826825
// Use 'then' branch when condition is true

0 commit comments

Comments
 (0)