Skip to content

Commit de4c096

Browse files
committed
fix: ignore not existing cluster in MachineSet teardown flow
If the cluster is not found it's not critical for teardown flow, so we shouldn't skip reconcile in that case. Signed-off-by: Artem Chernyshev <[email protected]>
1 parent d3e3eef commit de4c096

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

internal/backend/runtime/omni/controllers/omni/internal/machineset/control_planes_handler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func TestControlPlanesHandler(t *testing.T) {
187187
require := require.New(t)
188188

189189
machineSet.TypedSpec().Value = tt.machineSet
190+
machineSet.Metadata().Labels().Set(omni.LabelCluster, tt.name)
190191

191192
cluster := omni.NewCluster(resources.DefaultNamespace, tt.name)
192193
cluster.TypedSpec().Value.TalosVersion = "v1.5.4"

internal/backend/runtime/omni/controllers/omni/internal/machineset/operations.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ func (c *Create) Apply(ctx context.Context, r controller.ReaderWriter, logger *z
4747
helpers.UpdateInputsVersions(clusterMachine, configPatches...)
4848
setPatches(clusterMachineConfigPatches, configPatches)
4949

50-
clusterMachine.TypedSpec().Value.KubernetesVersion = rc.GetCluster().TypedSpec().Value.KubernetesVersion
50+
var err error
51+
52+
clusterMachine.TypedSpec().Value.KubernetesVersion, err = rc.GetKubernetesVersion()
53+
if err != nil {
54+
return err
55+
}
5156

5257
logger.Info("create cluster machine", zap.String("machine", c.ID))
5358

54-
if err := r.Create(ctx, clusterMachine); err != nil {
59+
if err = r.Create(ctx, clusterMachine); err != nil {
5560
return err
5661
}
5762

@@ -78,7 +83,7 @@ func (d *Teardown) Apply(ctx context.Context, r controller.ReaderWriter, logger
7883
return fmt.Errorf(
7984
"error tearing down machine %q in cluster %q: %w",
8085
d.ID,
81-
rc.GetCluster().Metadata().ID(),
86+
rc.GetClusterName(),
8287
err,
8388
)
8489
}
@@ -135,14 +140,17 @@ func (u *Update) Apply(ctx context.Context, r controller.ReaderWriter, logger *z
135140
return safe.WriterModify(ctx, r, clusterMachine, func(res *omni.ClusterMachine) error {
136141
// don't update the ClusterMachine if it's still owned by another cluster
137142
currentClusterName, ok := res.Metadata().Labels().Get(omni.LabelCluster)
138-
if ok && currentClusterName != rc.cluster.Metadata().ID() {
143+
if ok && currentClusterName != rc.GetClusterName() {
139144
return nil
140145
}
141146

142147
helpers.CopyAllAnnotations(clusterMachine, res)
143148

144149
if res.TypedSpec().Value.KubernetesVersion == "" {
145-
res.TypedSpec().Value.KubernetesVersion = rc.GetCluster().TypedSpec().Value.KubernetesVersion
150+
res.TypedSpec().Value.KubernetesVersion, err = rc.GetKubernetesVersion()
151+
if err != nil {
152+
return err
153+
}
146154
}
147155

148156
return nil

internal/backend/runtime/omni/controllers/omni/internal/machineset/operations_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestCreate(t *testing.T) {
8585
cluster.TypedSpec().Value.KubernetesVersion = "v1.6.2"
8686

8787
machineSet := omni.NewMachineSet(resources.DefaultNamespace, "machineset")
88+
machineSet.Metadata().Labels().Set(omni.LabelCluster, cluster.Metadata().ID())
8889

8990
create := machineset.Create{ID: "aa"}
9091

@@ -157,6 +158,7 @@ func TestUpdate(t *testing.T) {
157158
cluster.TypedSpec().Value.KubernetesVersion = "v1.6.4"
158159

159160
machineSet := omni.NewMachineSet(resources.DefaultNamespace, "machineset")
161+
machineSet.Metadata().Labels().Set(omni.LabelCluster, cluster.Metadata().ID())
160162

161163
quota := &machineset.ChangeQuota{
162164
Update: 2,
@@ -292,6 +294,7 @@ func TestTeardown(t *testing.T) {
292294
cluster.TypedSpec().Value.KubernetesVersion = "v1.6.3"
293295

294296
machineSet := omni.NewMachineSet(resources.DefaultNamespace, "machineset")
297+
machineSet.Metadata().Labels().Set(omni.LabelCluster, cluster.Metadata().ID())
295298

296299
quota := machineset.ChangeQuota{
297300
Teardown: 1,
@@ -358,6 +361,7 @@ func TestDestroy(t *testing.T) {
358361
cluster.TypedSpec().Value.KubernetesVersion = "v1.6.3"
359362

360363
machineSet := omni.NewMachineSet(resources.DefaultNamespace, "machineset")
364+
machineSet.Metadata().Labels().Set(omni.LabelCluster, cluster.Metadata().ID())
361365

362366
require := require.New(t)
363367

internal/backend/runtime/omni/controllers/omni/internal/machineset/reconciliation_context.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ package machineset
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112

1213
"github.com/cosi-project/runtime/pkg/controller"
13-
"github.com/cosi-project/runtime/pkg/controller/generic/qtransform"
1414
"github.com/cosi-project/runtime/pkg/resource"
1515
"github.com/cosi-project/runtime/pkg/safe"
1616
"github.com/cosi-project/runtime/pkg/state"
17-
"github.com/siderolabs/gen/xerrors"
1817
"github.com/siderolabs/gen/xslices"
1918

2019
"github.com/siderolabs/omni/client/api/omni/specs"
@@ -76,6 +75,8 @@ type ReconciliationContext struct {
7675
clusterMachineConfigPatchesMap map[resource.ID]*omni.ClusterMachineConfigPatches
7776
clusterMachineStatusesMap map[resource.ID]*omni.ClusterMachineStatus
7877

78+
clusterName string
79+
7980
runningMachineSetNodesSet Set[string]
8081
idsTearingDown Set[string]
8182
idsUnconfigured Set[string]
@@ -104,11 +105,7 @@ func BuildReconciliationContext(
104105
}
105106

106107
cluster, err := safe.ReaderGetByID[*omni.Cluster](ctx, r, clusterName)
107-
if err != nil {
108-
if state.IsNotFoundError(err) {
109-
return nil, xerrors.NewTagged[qtransform.SkipReconcileTag](err)
110-
}
111-
108+
if err != nil && !state.IsNotFoundError(err) {
112109
return nil, err
113110
}
114111

@@ -179,9 +176,15 @@ func NewReconciliationContext(
179176
clusterMachineConfigPatches []*omni.ClusterMachineConfigPatches,
180177
clusterMachineStatuses []*omni.ClusterMachineStatus,
181178
) (*ReconciliationContext, error) {
179+
clusterName, ok := machineSet.Metadata().Labels().Get(omni.LabelCluster)
180+
if !ok {
181+
return nil, fmt.Errorf("failed to determine the cluster of the machine set %q", machineSet.Metadata().ID())
182+
}
183+
182184
rc := &ReconciliationContext{
183185
machineSet: machineSet,
184186
cluster: cluster,
187+
clusterName: clusterName,
185188
patchesByMachine: map[resource.ID][]*omni.ConfigPatch{},
186189
}
187190

@@ -345,9 +348,18 @@ func (rc *ReconciliationContext) GetOutdatedMachines() Set[string] {
345348
return rc.idsOutdated
346349
}
347350

348-
// GetCluster reads the related cluster resource.
349-
func (rc *ReconciliationContext) GetCluster() *omni.Cluster {
350-
return rc.cluster
351+
// GetKubernetesVersion reads kubernetes version from the related cluster if the cluster exists.
352+
func (rc *ReconciliationContext) GetKubernetesVersion() (string, error) {
353+
if rc.cluster == nil {
354+
return "", errors.New("failed to get kubernetes version for the machine set as the cluster couldn't be found")
355+
}
356+
357+
return rc.cluster.TypedSpec().Value.KubernetesVersion, nil
358+
}
359+
360+
// GetClusterName reads cluster name from the context.
361+
func (rc *ReconciliationContext) GetClusterName() string {
362+
return rc.clusterName
351363
}
352364

353365
// GetMachineSet reads the related machine set resource.

internal/backend/runtime/omni/controllers/omni/internal/machineset/reconciliation_context_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ func TestReconciliationContext(t *testing.T) {
312312

313313
machineSet := omni.NewMachineSet(resources.DefaultNamespace, tt.name)
314314
machineSet.TypedSpec().Value = tt.machineSet
315+
machineSet.Metadata().Labels().Set(omni.LabelCluster, tt.name)
315316

316317
cluster := omni.NewCluster(resources.DefaultNamespace, tt.name)
317318
cluster.TypedSpec().Value.TalosVersion = "v1.6.4"

internal/backend/runtime/omni/controllers/omni/internal/machineset/status_handler_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ func TestStatusHandler(t *testing.T) {
329329
machineSet = omni.NewMachineSet(resources.DefaultNamespace, "test")
330330
}
331331

332+
machineSet.Metadata().Labels().Set(omni.LabelCluster, "test")
333+
332334
require := require.New(t)
333335

334336
clusterMachineConfigStatuses := make([]*omni.ClusterMachineConfigStatus, 0, len(tt.clusterMachines))

internal/backend/runtime/omni/controllers/omni/internal/machineset/workers_handler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func TestWorkersHandler(t *testing.T) {
173173
require := require.New(t)
174174

175175
machineSet.TypedSpec().Value = tt.machineSet
176+
machineSet.Metadata().Labels().Set(omni.LabelCluster, tt.name)
176177

177178
cluster := omni.NewCluster(resources.DefaultNamespace, tt.name)
178179
cluster.TypedSpec().Value.TalosVersion = "v1.6.0"

0 commit comments

Comments
 (0)