-
Notifications
You must be signed in to change notification settings - Fork 50
Operatorconfig controller #1030
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
Changes from 1 commit
cf80bd8
1ca7454
021f03e
4bb43dc
bc79b64
923b1f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- to apply configuration from OperatorConfig onto CSVs updated Operator type in OperandRegistry to include OperatorConfig name - this assumes OperatorConfigs are in the same namespace as registry, just like OperandConfig updated RBAC to include OperatorConfig Signed-off-by: Henry H Li <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,22 @@ package operatorconfig | |
| import ( | ||
| "context" | ||
|
|
||
| "github.com/barkimedes/go-deepcopy" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/klog" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/builder" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
|
|
||
| operatorv1alpha1 "github.com/IBM/operand-deployment-lifecycle-manager/api/v1alpha1" | ||
| deploy "github.com/IBM/operand-deployment-lifecycle-manager/controllers/operator" | ||
| olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
| ) | ||
|
|
||
| // OperatorConfigReconciler reconciles a OperatorConfig object | ||
|
|
@@ -47,14 +58,111 @@ type Reconciler struct { | |
| func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| _ = log.FromContext(ctx) | ||
|
|
||
| // TODO(user): your logic here | ||
| instance := &operatorv1alpha1.OperandRequest{} | ||
| if err := r.Client.Get(ctx, req.NamespacedName, instance); err != nil { | ||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||
| } | ||
|
|
||
| klog.Infof("Reconciling OperatorConfig for request: %s, %s", instance.Namespace, instance.Name) | ||
|
|
||
| for _, v := range instance.Spec.Requests { | ||
| reqBlock := v | ||
| registry, err := r.GetOperandRegistry(ctx, instance.GetRegistryKey(reqBlock)) | ||
| if err != nil { | ||
| return ctrl.Result{}, err | ||
| } | ||
| for _, u := range reqBlock.Operands { | ||
| operand := u | ||
| operator := registry.GetOperator(operand.Name) | ||
| if operator.OperatorConfig == "" { | ||
| break | ||
| } | ||
|
|
||
| var sub *olmv1alpha1.Subscription | ||
| sub, err = r.GetSubscription(ctx, operator.Name, operator.Namespace, registry.Namespace, operator.PackageName) | ||
| if err != nil { | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
||
| var csv *olmv1alpha1.ClusterServiceVersion | ||
| csv, err = r.GetClusterServiceVersion(ctx, sub) | ||
| if err != nil { | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
||
| klog.Infof("Fetching OperatorConfig: %s", operator.OperatorConfig) | ||
| config := &operatorv1alpha1.OperatorConfig{} | ||
| if err := r.Client.Get(ctx, types.NamespacedName{ | ||
| Name: operator.OperatorConfig, | ||
| Namespace: registry.Namespace, | ||
| }, config); err != nil { | ||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||
| } | ||
| serviceConfig := config.GetConfigForOperator(operator.Name) | ||
| if serviceConfig == nil { | ||
| klog.Infof("OperatorConfig: %s, does not have configuration for operator: %s", operator.OperatorConfig, operator.Name) | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| copyToCast, err := deepcopy.Anything(csv) | ||
| if err != nil { | ||
| return ctrl.Result{}, err | ||
| } | ||
| csvToUpdate := copyToCast.(*olmv1alpha1.ClusterServiceVersion) | ||
| klog.Infof("Applying OperatorConfig: %s to Operator: %s via CSV: %s, %s", operator.OperatorConfig, operator.Name, csv.Name, csv.Namespace) | ||
| return r.configCsv(ctx, csvToUpdate, serviceConfig) | ||
| } | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| func (r *Reconciler) configCsv(ctx context.Context, csv *olmv1alpha1.ClusterServiceVersion, config *operatorv1alpha1.ServiceOperatorConfig) (ctrl.Result, error) { | ||
| if config.Replicas != nil { | ||
| csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Replicas = config.Replicas | ||
| } | ||
| if config.Affinity != nil { | ||
| csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.Affinity = config.Affinity | ||
| } | ||
| if config.TopologySpreadConstraints != nil { | ||
| csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.TopologySpreadConstraints = config.TopologySpreadConstraints | ||
| } | ||
| if err := r.Client.Update(ctx, csv); err != nil { | ||
| return ctrl.Result{}, err | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| func (r *Reconciler) requestsFromMapFunc(ctx context.Context) handler.MapFunc { | ||
| return func(object client.Object) []reconcile.Request { | ||
| requests := []reconcile.Request{} | ||
|
|
||
| operandRequests, _ := r.ListOperandRequests(ctx, nil) | ||
| for _, req := range operandRequests.Items { | ||
| r := reconcile.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Namespace: req.Namespace, | ||
| Name: req.Name, | ||
| }, | ||
| } | ||
| requests = append(requests, r) | ||
| } | ||
| return requests | ||
| } | ||
| } | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| ctx := context.Background() | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| For(&operatorv1alpha1.OperatorConfig{}). | ||
| For(&operatorv1alpha1.OperandRequest{}). | ||
| Watches(&source.Kind{Type: &operatorv1alpha1.OperatorConfig{}}, handler.EnqueueRequestsFromMapFunc(r.requestsFromMapFunc(ctx)), builder.WithPredicates(predicate.Funcs{ | ||
| CreateFunc: func(e event.CreateEvent) bool { | ||
| return true | ||
| }, | ||
| DeleteFunc: func(e event.DeleteEvent) bool { | ||
| // Evaluates to false if the object has been confirmed deleted. | ||
| return !e.DeleteStateUnknown | ||
| }, | ||
|
Comment on lines
+159
to
+165
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if a update event on OperandRequest should trigger the reconciliation as well?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The predicates here are for events to the |
||
| })). | ||
| Complete(r) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.