Skip to content

Commit 935adbb

Browse files
authored
Update OperatorConfig reconciliation and improve error handling (#1034)
* Update OperatorConfig reconciliation and improve error handling Signed-off-by: Daniel Fan <[email protected]> * Requeue request every three hours for sync Signed-off-by: Daniel Fan <[email protected]> * drop unnecessary else statement Signed-off-by: Daniel Fan <[email protected]> --------- Signed-off-by: Daniel Fan <[email protected]>
1 parent 307337c commit 935adbb

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

controllers/operator/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ func (m *ODLMOperator) GetSubscription(ctx context.Context, name, operatorNs, se
392392

393393
// GetClusterServiceVersion gets the ClusterServiceVersion from the subscription
394394
func (m *ODLMOperator) GetClusterServiceVersion(ctx context.Context, sub *olmv1alpha1.Subscription) (*olmv1alpha1.ClusterServiceVersion, error) {
395+
// Check if subscription is nil
396+
if sub == nil {
397+
klog.Error("The subscription is nil")
398+
return nil, fmt.Errorf("the subscription is nil")
399+
}
395400
// Check the ClusterServiceVersion status in the subscription
396401
if sub.Status.InstalledCSV == "" {
397402
klog.Warningf("The ClusterServiceVersion for Subscription %s is not ready. Will check it again", sub.Name)

controllers/operatorconfig/operatorconfig_controller.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/controller-runtime/pkg/source"
3535

3636
operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1"
37+
"github.com/IBM/operand-deployment-lifecycle-manager/controllers/constant"
3738
deploy "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operator"
3839
)
3940

@@ -73,19 +74,25 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
7374
operand := u
7475
operator := registry.GetOperator(operand.Name)
7576
if operator.OperatorConfig == "" {
76-
break
77+
continue
7778
}
7879

7980
var sub *olmv1alpha1.Subscription
8081
sub, err = r.GetSubscription(ctx, operator.Name, operator.Namespace, registry.Namespace, operator.PackageName)
8182
if err != nil {
8283
return ctrl.Result{}, err
84+
} else if sub == nil {
85+
klog.Infof("Subscription for Operator %s/%s not found", operator.Name, operator.PackageName)
86+
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
8387
}
8488

8589
var csv *olmv1alpha1.ClusterServiceVersion
8690
csv, err = r.GetClusterServiceVersion(ctx, sub)
8791
if err != nil {
8892
return ctrl.Result{}, err
93+
} else if csv == nil {
94+
klog.Infof("ClusterServiceVersion for Operator %s/%s not found", operator.Name, operator.PackageName)
95+
return ctrl.Result{RequeueAfter: constant.DefaultRequeueDuration}, nil
8996
}
9097

9198
klog.Infof("Fetching OperatorConfig: %s", operator.OperatorConfig)
@@ -94,12 +101,16 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
94101
Name: operator.OperatorConfig,
95102
Namespace: registry.Namespace,
96103
}, config); err != nil {
97-
return ctrl.Result{}, client.IgnoreNotFound(err)
104+
if client.IgnoreNotFound(err) != nil {
105+
return ctrl.Result{}, err
106+
}
107+
klog.Infof("OperatorConfig %s/%s does not exist for operand %s in request %s, %s", registry.Namespace, operator.OperatorConfig, operator.Name, instance.Namespace, instance.Name)
108+
continue
98109
}
99110
serviceConfig := config.GetConfigForOperator(operator.Name)
100111
if serviceConfig == nil {
101112
klog.Infof("OperatorConfig: %s, does not have configuration for operator: %s", operator.OperatorConfig, operator.Name)
102-
return ctrl.Result{}, nil
113+
continue
103114
}
104115

105116
copyToCast, err := deepcopy.Anything(csv)
@@ -108,13 +119,17 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
108119
}
109120
csvToUpdate := copyToCast.(*olmv1alpha1.ClusterServiceVersion)
110121
klog.Infof("Applying OperatorConfig: %s to Operator: %s via CSV: %s, %s", operator.OperatorConfig, operator.Name, csv.Name, csv.Namespace)
111-
return r.configCsv(ctx, csvToUpdate, serviceConfig)
122+
if err := r.configCsv(ctx, csvToUpdate, serviceConfig); err != nil {
123+
klog.Errorf("Failed to apply OperatorConfig %s/%s to Operator: %s via CSV: %s, %s", registry.Namespace, operator.OperatorConfig, operator.Name, csv.Namespace, csv.Name)
124+
return ctrl.Result{}, err
125+
}
112126
}
113127
}
114-
return ctrl.Result{}, nil
128+
klog.Infof("Finished reconciling OperatorConfig for request: %s, %s", instance.Namespace, instance.Name)
129+
return ctrl.Result{RequeueAfter: constant.DefaultSyncPeriod}, nil
115130
}
116131

117-
func (r *Reconciler) configCsv(ctx context.Context, csv *olmv1alpha1.ClusterServiceVersion, config *operatorv1alpha1.ServiceOperatorConfig) (ctrl.Result, error) {
132+
func (r *Reconciler) configCsv(ctx context.Context, csv *olmv1alpha1.ClusterServiceVersion, config *operatorv1alpha1.ServiceOperatorConfig) error {
118133
if config.Replicas != nil {
119134
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Replicas = config.Replicas
120135
}
@@ -125,9 +140,9 @@ func (r *Reconciler) configCsv(ctx context.Context, csv *olmv1alpha1.ClusterServ
125140
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.TopologySpreadConstraints = config.TopologySpreadConstraints
126141
}
127142
if err := r.Client.Update(ctx, csv); err != nil {
128-
return ctrl.Result{}, err
143+
return err
129144
}
130-
return ctrl.Result{}, nil
145+
return nil
131146
}
132147

133148
func (r *Reconciler) requestsFromMapFunc(ctx context.Context) handler.MapFunc {

0 commit comments

Comments
 (0)