|
| 1 | +package stateful |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + apimeta "k8s.io/apimachinery/pkg/api/meta" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 14 | + |
| 15 | + "go.datum.net/workload-operator/api/v1alpha" |
| 16 | + "go.datum.net/workload-operator/internal/controller/instancecontrol" |
| 17 | +) |
| 18 | + |
| 19 | +// Behavior inspired by https://github.com/kubernetes/kubernetes/tree/master/pkg/controller/statefulset |
| 20 | +// Does not currently implement exact behavior. |
| 21 | +type statefulControl struct { |
| 22 | +} |
| 23 | + |
| 24 | +func NewStatefulControl() instancecontrol.Strategy { |
| 25 | + return &statefulControl{} |
| 26 | +} |
| 27 | + |
| 28 | +func (c *statefulControl) GetActions( |
| 29 | + ctx context.Context, |
| 30 | + scheme *runtime.Scheme, |
| 31 | + deployment *v1alpha.WorkloadDeployment, |
| 32 | + currentInstances []v1alpha.Instance, |
| 33 | +) ([]instancecontrol.Action, error) { |
| 34 | + // lowest -> highest |
| 35 | + var createActions []instancecontrol.Action |
| 36 | + var waitActions []instancecontrol.Action |
| 37 | + |
| 38 | + // highest -> lowest |
| 39 | + var updateActions []instancecontrol.Action |
| 40 | + |
| 41 | + // highest -> lowest |
| 42 | + var deleteActions []instancecontrol.Action |
| 43 | + |
| 44 | + // Instances that are desired to exist. We do not currently support the |
| 45 | + // concept of a partition, so will fill the entire slice. |
| 46 | + desiredInstances := make([]*v1alpha.Instance, deployment.Spec.ScaleSettings.MinReplicas) |
| 47 | + |
| 48 | + for _, instance := range currentInstances { |
| 49 | + instanceIndex := getInstanceOrdinal(instance.Name) |
| 50 | + if instanceIndex >= len(desiredInstances) { |
| 51 | + deleteActions = append(deleteActions, instancecontrol.NewDeleteAction( |
| 52 | + instance.Name, |
| 53 | + func(ctx context.Context, c client.Client) error { |
| 54 | + return c.Delete(ctx, &instance) |
| 55 | + }, |
| 56 | + )) |
| 57 | + } else { |
| 58 | + desiredInstances[instanceIndex] = &instance |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // It's possible that the incoming currentInstances will have gaps in |
| 63 | + // instances, so fill them in. |
| 64 | + for i := range deployment.Spec.ScaleSettings.MinReplicas { |
| 65 | + if desiredInstances[i] == nil { |
| 66 | + desiredInstances[i] = &v1alpha.Instance{ |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Labels: deployment.Spec.Template.Labels, |
| 69 | + Annotations: deployment.Spec.Template.Annotations, |
| 70 | + Name: fmt.Sprintf("%s-%d", deployment.Name, i), |
| 71 | + Namespace: deployment.Namespace, |
| 72 | + }, |
| 73 | + Spec: deployment.Spec.Template.Spec, |
| 74 | + } |
| 75 | + |
| 76 | + if desiredInstances[i].Labels == nil { |
| 77 | + desiredInstances[i].Labels = map[string]string{} |
| 78 | + } |
| 79 | + |
| 80 | + desiredInstances[i].Labels[v1alpha.InstanceIndexLabel] = strconv.Itoa(int(i)) |
| 81 | + |
| 82 | + if err := controllerutil.SetControllerReference(deployment, desiredInstances[i], scheme); err != nil { |
| 83 | + return nil, fmt.Errorf("failed to set controller reference: %w", err) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + for _, instance := range desiredInstances { |
| 89 | + if instance.CreationTimestamp.IsZero() { |
| 90 | + action := instancecontrol.NewCreateAction( |
| 91 | + instance.Name, |
| 92 | + func(ctx context.Context, c client.Client) error { |
| 93 | + if err := c.Create(ctx, instance); err != nil { |
| 94 | + return fmt.Errorf("failed to create instance: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | + }, |
| 99 | + ) |
| 100 | + |
| 101 | + createActions = append(createActions, action) |
| 102 | + } else if !instance.DeletionTimestamp.IsZero() { |
| 103 | + // Wait for graceful deletion before continuing processing additional |
| 104 | + // instances. |
| 105 | + waitActions = append(waitActions, instancecontrol.NewWaitAction( |
| 106 | + instance.Name, |
| 107 | + )) |
| 108 | + |
| 109 | + } else if instance.DeletionTimestamp.IsZero() { |
| 110 | + // Wait for the instance to be ready before continuing processing |
| 111 | + if !apimeta.IsStatusConditionTrue(instance.Status.Conditions, v1alpha.InstanceReady) { |
| 112 | + waitActions = append(waitActions, instancecontrol.NewWaitAction( |
| 113 | + instance.Name, |
| 114 | + )) |
| 115 | + } else if needsUpdate(instance, deployment) { |
| 116 | + updateActions = append(updateActions, instancecontrol.NewUpdateAction( |
| 117 | + instance.Name, |
| 118 | + func(ctx context.Context, c client.Client) error { |
| 119 | + instance.Annotations = deployment.Spec.Template.Annotations |
| 120 | + instance.Labels = deployment.Spec.Template.Labels |
| 121 | + instance.Spec = deployment.Spec.Template.Spec |
| 122 | + |
| 123 | + if err := c.Update(ctx, instance); err != nil { |
| 124 | + return fmt.Errorf("failed to update instance: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | + }, |
| 129 | + )) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + slices.SortFunc(updateActions, descendingOrdinal) |
| 135 | + slices.SortFunc(deleteActions, descendingOrdinal) |
| 136 | + |
| 137 | + actions := make([]instancecontrol.Action, 0, len(createActions)+len(waitActions)+len(updateActions)+len(deleteActions)) |
| 138 | + |
| 139 | + switch deployment.Spec.ScaleSettings.InstanceManagementPolicy { |
| 140 | + case v1alpha.OrderedReadyInstanceManagementPolicyType: |
| 141 | + |
| 142 | + // Add create and wait actions, and sort by ordinal. This allows us to wait |
| 143 | + // for instances to be processed in the correct order. |
| 144 | + // |
| 145 | + // For instance, we may have instance 0 that needs to wait to be ready, but |
| 146 | + // instance 1 wants to be created. |
| 147 | + actions = append(actions, createActions...) |
| 148 | + actions = append(actions, waitActions...) |
| 149 | + |
| 150 | + slices.SortFunc(actions, ascendingOrdinal) |
| 151 | + |
| 152 | + actions = append(actions, updateActions...) |
| 153 | + actions = append(actions, deleteActions...) |
| 154 | + |
| 155 | + // Skip all actions except the first one. |
| 156 | + for i := range actions { |
| 157 | + if i > 0 { |
| 158 | + actions[i].SkipExecution() |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + return actions, nil |
| 165 | +} |
0 commit comments