|
| 1 | +package proposal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 9 | + crcontrollerutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 10 | + |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + kapierrors "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + kutilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 15 | + "k8s.io/client-go/util/workqueue" |
| 16 | + "k8s.io/klog/v2" |
| 17 | + |
| 18 | + configv1 "github.com/openshift/api/config/v1" |
| 19 | + |
| 20 | + i "github.com/openshift/cluster-version-operator/pkg/internal" |
| 21 | + proposalv1alpha1 "github.com/openshift/cluster-version-operator/pkg/proposal/api/v1alpha1" |
| 22 | +) |
| 23 | + |
| 24 | +type Controller struct { |
| 25 | + queueKey string |
| 26 | + queue workqueue.TypedRateLimitingInterface[any] |
| 27 | + updatesGetterFunc UpdatesGetterFunc |
| 28 | + client ctrlruntimeclient.Client |
| 29 | +} |
| 30 | + |
| 31 | +const controllerName = "proposal-lifecycle-controller" |
| 32 | + |
| 33 | +type UpdatesGetterFunc func() ([]configv1.Release, []configv1.ConditionalUpdate, error) |
| 34 | + |
| 35 | +// NewController returns Controller to manage Proposals. |
| 36 | +func NewController(updatesGetterFunc UpdatesGetterFunc, client ctrlruntimeclient.Client) *Controller { |
| 37 | + return &Controller{ |
| 38 | + queueKey: fmt.Sprintf("ClusterVersionOperator/%s", controllerName), |
| 39 | + queue: workqueue.NewTypedRateLimitingQueueWithConfig[any]( |
| 40 | + workqueue.DefaultTypedControllerRateLimiter[any](), |
| 41 | + workqueue.TypedRateLimitingQueueConfig[any]{Name: controllerName}), |
| 42 | + updatesGetterFunc: updatesGetterFunc, |
| 43 | + client: client, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (c *Controller) Queue() workqueue.TypedRateLimitingInterface[any] { |
| 48 | + return c.queue |
| 49 | +} |
| 50 | + |
| 51 | +func (c *Controller) QueueKey() string { |
| 52 | + return c.queueKey |
| 53 | +} |
| 54 | + |
| 55 | +// Sync ensures: |
| 56 | +// TODO: |
| 57 | +func (c *Controller) Sync(ctx context.Context, key string) error { |
| 58 | + startTime := time.Now() |
| 59 | + klog.V(i.Normal).Infof("Started syncing CVO configuration %q", key) |
| 60 | + defer func() { |
| 61 | + klog.V(i.Normal).Infof("Finished syncing CVO configuration (%v)", time.Since(startTime)) |
| 62 | + }() |
| 63 | + |
| 64 | + updates, conditionalUpdates, err := c.updatesGetterFunc() |
| 65 | + if err != nil { |
| 66 | + klog.Errorf("Error getting available updates: %v", err) |
| 67 | + return err |
| 68 | + } |
| 69 | + klog.V(i.Debug).Infof("Got available updates: %#v", updates) |
| 70 | + klog.V(i.Debug).Infof("Got conditional updates: %#v", conditionalUpdates) |
| 71 | + |
| 72 | + proposals := getProposals(updates, conditionalUpdates) |
| 73 | + |
| 74 | + var errs []error |
| 75 | + for _, proposal := range proposals { |
| 76 | + err := ensureProposal(ctx, proposal, c.client) |
| 77 | + if err != nil { |
| 78 | + errs = append(errs, err) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return kutilerrors.NewAggregate(errs) |
| 83 | +} |
| 84 | + |
| 85 | +// TODO: make it real |
| 86 | +func getProposals(_ []configv1.Release, _ []configv1.ConditionalUpdate) []*proposalv1alpha1.Proposal { |
| 87 | + return []*proposalv1alpha1.Proposal{ |
| 88 | + { |
| 89 | + ObjectMeta: metav1.ObjectMeta{ |
| 90 | + Name: "test-proposal", |
| 91 | + Namespace: i.DefaultCVONamespace, |
| 92 | + }, |
| 93 | + // Feed required fields only to pass API server validation |
| 94 | + // The workflow does not exist but that should cause no trouble because no controller watches it before lightspeed-operator is installed on the cluster |
| 95 | + Spec: proposalv1alpha1.ProposalSpec{ |
| 96 | + Request: "some-request", |
| 97 | + WorkflowRef: corev1.LocalObjectReference{ |
| 98 | + Name: "ota-advisory", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func ensureProposal(ctx context.Context, proposal *proposalv1alpha1.Proposal, client ctrlruntimeclient.Client) error { |
| 106 | + p, mutateFn := cvoProposal(proposal) |
| 107 | + return upsertObject(ctx, client, p, mutateFn) |
| 108 | +} |
| 109 | + |
| 110 | +func cvoProposal(proposal *proposalv1alpha1.Proposal) (*proposalv1alpha1.Proposal, crcontrollerutil.MutateFn) { |
| 111 | + p := &proposalv1alpha1.Proposal{ |
| 112 | + ObjectMeta: metav1.ObjectMeta{ |
| 113 | + Namespace: proposal.Namespace, |
| 114 | + Name: proposal.Name, |
| 115 | + }, |
| 116 | + } |
| 117 | + return p, func() error { |
| 118 | + if len(proposal.Annotations) > 0 { |
| 119 | + p.Annotations = make(map[string]string, len(proposal.Annotations)) |
| 120 | + for k, v := range proposal.Annotations { |
| 121 | + p.Annotations[k] = v |
| 122 | + } |
| 123 | + } |
| 124 | + if len(proposal.Labels) > 0 { |
| 125 | + p.Labels = make(map[string]string, len(proposal.Labels)) |
| 126 | + for k, v := range proposal.Labels { |
| 127 | + p.Labels[k] = v |
| 128 | + } |
| 129 | + } |
| 130 | + p.Spec = *proposal.Spec.DeepCopy() |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func upsertObject(ctx context.Context, c ctrlruntimeclient.Client, obj ctrlruntimeclient.Object, mutateFn crcontrollerutil.MutateFn) error { |
| 136 | + result, err := crcontrollerutil.CreateOrUpdate(ctx, c, obj, mutateFn) |
| 137 | + if err != nil && !kapierrors.IsConflict(err) { |
| 138 | + klog.V(i.Normal).Infof("Upsert object (%s/%s) of type %s failed with result %s: %v", obj.GetNamespace(), obj.GetName(), fmt.Sprintf("%T", obj), result, err) |
| 139 | + } else if result != crcontrollerutil.OperationResultNone { |
| 140 | + klog.V(i.Normal).Infof("Upsert object (%s/%s) of type %s succeeded with result %s", obj.GetNamespace(), obj.GetName(), fmt.Sprintf("%T", obj), result) |
| 141 | + } |
| 142 | + return err |
| 143 | +} |
0 commit comments