|
| 1 | +package k8sclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + "k8s.io/apimachinery/pkg/types" |
| 9 | +) |
| 10 | + |
| 11 | +type KafkaComponent struct { |
| 12 | + name ComponentName |
| 13 | + namespace string |
| 14 | + resource types.NamespacedName |
| 15 | + |
| 16 | + k8s *K8sClient |
| 17 | + base *int32 // baseline replicas |
| 18 | + dirty bool |
| 19 | +} |
| 20 | + |
| 21 | +const kafkaComponentName ComponentName = "kafka" |
| 22 | + |
| 23 | +func NewKafkaComponent(k8s *K8sClient, namespace, statefulSetName string) *KafkaComponent { |
| 24 | + return &KafkaComponent{ |
| 25 | + name: kafkaComponentName, |
| 26 | + namespace: namespace, |
| 27 | + resource: types.NamespacedName{Namespace: namespace, Name: statefulSetName}, |
| 28 | + k8s: k8s, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (k *KafkaComponent) Name() ComponentName { return k.name } |
| 33 | + |
| 34 | +// Snapshot reads the current replicas once and stores them as baseline. |
| 35 | +func (k *KafkaComponent) Snapshot(ctx context.Context) error { |
| 36 | + if k.base != nil { |
| 37 | + // already snapshotted |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + var sts appsv1.StatefulSet |
| 42 | + if err := k.k8s.KubeClient.Get(ctx, k.resource, &sts); err != nil { |
| 43 | + return fmt.Errorf("kafka snapshot: get %s/%s: %w", k.resource.Namespace, k.resource.Name, err) |
| 44 | + } |
| 45 | + |
| 46 | + if sts.Spec.Replicas == nil { |
| 47 | + return fmt.Errorf("kafka snapshot: statefulset %s/%s has nil .spec.replicas", k.resource.Namespace, k.resource.Name) |
| 48 | + } |
| 49 | + |
| 50 | + replicas := *sts.Spec.Replicas |
| 51 | + k.base = &replicas |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// Restore only acts if we've modified Kafka in this scenario (dirty=true). |
| 56 | +func (k *KafkaComponent) Restore(ctx context.Context) error { |
| 57 | + if !k.dirty || k.base == nil { |
| 58 | + return nil |
| 59 | + } |
| 60 | + if err := k.scale(ctx, *k.base); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + k.dirty = false |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// Public helpers used from steps: |
| 68 | + |
| 69 | +func (k *KafkaComponent) MakeUnavailable(ctx context.Context) error { |
| 70 | + if err := k.Snapshot(ctx); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + if err := k.scale(ctx, 0); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + k.dirty = true |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (k *KafkaComponent) MakeAvailable(ctx context.Context) error { |
| 81 | + if k.base == nil { |
| 82 | + // nothing to restore to; safest is to no-op or return error |
| 83 | + return nil |
| 84 | + } |
| 85 | + if err := k.scale(ctx, *k.base); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + k.dirty = false |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func (k *KafkaComponent) scale(ctx context.Context, replicas int32) error { |
| 93 | + var sts appsv1.StatefulSet |
| 94 | + if err := k.k8s.KubeClient.Get(ctx, k.resource, &sts); err != nil { |
| 95 | + return fmt.Errorf("kafka scale: get %s/%s: %w", k.resource.Namespace, k.resource.Name, err) |
| 96 | + } |
| 97 | + sts.Spec.Replicas = &replicas |
| 98 | + if err := k.k8s.KubeClient.Update(ctx, &sts); err != nil { |
| 99 | + return fmt.Errorf("kafka scale: update %s/%s: %w", k.resource.Namespace, k.resource.Name, err) |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
0 commit comments