Skip to content

Commit 0cc410e

Browse files
committed
feat: introduce 'OnDelete' rollout strategy type
With this strategy, machines will be replaced only when existing machine is deleted and new one is created as part of scale-up event. Signed-off-by: Andrey Smirnov <[email protected]> (cherry picked from commit 678aad5)
1 parent 8dd3361 commit 0cc410e

File tree

12 files changed

+113
-10
lines changed

12 files changed

+113
-10
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ RUN --mount=type=cache,target=/.cache go mod download
3333
RUN --mount=type=cache,target=/.cache go mod verify
3434
COPY ./ ./
3535
RUN --mount=type=cache,target=/.cache go list -mod=readonly all >/dev/null
36-
RUN --mount=type=cache,target=/.cache ! go mod tidy -v 2>&1 | grep .
3736

3837
FROM build AS manifests-build
3938
ARG NAME

api/v1alpha3/taloscontrolplane_types.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const (
2929
// RollingUpdateStrategyType replaces the old control planes by new one using rolling update
3030
// i.e. gradually scale up or down the old control planes and scale up or down the new one.
3131
RollingUpdateStrategyType RolloutStrategyType = "RollingUpdate"
32+
// OnDeleteStrategyType doesn't replace the nodes automatically, but if the machine is removed,
33+
// new one will be created from the new spec.
34+
OnDeleteStrategyType RolloutStrategyType = "OnDelete"
3235
)
3336

3437
// TalosControlPlaneSpec defines the desired state of TalosControlPlane
@@ -67,8 +70,12 @@ type RolloutStrategy struct {
6770
// +optional
6871
RollingUpdate *RollingUpdate `json:"rollingUpdate,omitempty"`
6972

70-
// Type of rollout. Currently the only supported strategy is
71-
// "RollingUpdate".
73+
// Change rollout strategy.
74+
//
75+
// Supported strategies:
76+
// * "RollingUpdate".
77+
// * "OnDelete"
78+
//
7279
// Default is RollingUpdate.
7380
// +optional
7481
Type RolloutStrategyType `json:"type,omitempty"`

api/v1alpha3/taloscontrolplane_webhook.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
package v1alpha3
66

77
import (
8+
"fmt"
89
"strings"
910

11+
apierrors "k8s.io/apimachinery/pkg/api/errors"
12+
runtime "k8s.io/apimachinery/pkg/runtime"
13+
"k8s.io/apimachinery/pkg/runtime/schema"
1014
"k8s.io/apimachinery/pkg/util/intstr"
15+
"k8s.io/apimachinery/pkg/util/validation/field"
1116
ctrl "sigs.k8s.io/controller-runtime"
1217
"sigs.k8s.io/controller-runtime/pkg/webhook"
1318
)
@@ -20,8 +25,12 @@ func (r *TalosControlPlane) SetupWebhookWithManager(mgr ctrl.Manager) error {
2025
}
2126

2227
// +kubebuilder:webhook:verbs=create;update,path=/mutate-controlplane-cluster-x-k8s-io-v1alpha3-taloscontrolplane,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=taloscontrolplanes,versions=v1alpha3,name=default.taloscontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
28+
//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-controlplane-cluster-x-k8s-io-v1alpha3-taloscontrolplane,mutating=false,failurePolicy=fail,groups=controlplane.cluster.x-k8s.io,resources=taloscontrolplanes,versions=v1alpha3,name=validate.taloscontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1
2329

24-
var _ webhook.Defaulter = &TalosControlPlane{}
30+
var (
31+
_ webhook.Defaulter = &TalosControlPlane{}
32+
_ webhook.Validator = &TalosControlPlane{}
33+
)
2534

2635
// Default implements webhook.Defaulter so a webhook will be registered for the type.
2736
func (r *TalosControlPlane) Default() {
@@ -63,3 +72,46 @@ func defaultRolloutStrategy(rolloutStrategy *RolloutStrategy) *RolloutStrategy {
6372

6473
return rolloutStrategy
6574
}
75+
76+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
77+
func (r *TalosControlPlane) ValidateCreate() error {
78+
return r.validate()
79+
}
80+
81+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
82+
func (r *TalosControlPlane) ValidateUpdate(old runtime.Object) error {
83+
return r.validate()
84+
}
85+
86+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
87+
func (r *TalosControlPlane) ValidateDelete() error {
88+
return nil
89+
}
90+
91+
func (r *TalosControlPlane) validate() error {
92+
var allErrs field.ErrorList
93+
94+
if r.Spec.RolloutStrategy == nil {
95+
return nil
96+
}
97+
98+
switch r.Spec.RolloutStrategy.Type {
99+
case "":
100+
case RollingUpdateStrategyType:
101+
case OnDeleteStrategyType:
102+
default:
103+
allErrs = append(allErrs,
104+
field.Invalid(field.NewPath("spec").Child("rolloutStrategy"), r.Spec.RolloutStrategy.Type,
105+
fmt.Sprintf("valid values are: %q", []RolloutStrategyType{RollingUpdateStrategyType, OnDeleteStrategyType}),
106+
),
107+
)
108+
}
109+
110+
if len(allErrs) == 0 {
111+
return nil
112+
}
113+
114+
return apierrors.NewInvalid(
115+
schema.GroupKind{Group: GroupVersion.Group, Kind: "TalosControlPlane"},
116+
r.Name, allErrs)
117+
}

api/v1alpha3/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/controlplane.cluster.x-k8s.io_taloscontrolplanes.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ spec:
207207
x-kubernetes-int-or-string: true
208208
type: object
209209
type:
210-
description: Type of rollout. Currently the only supported strategy
211-
is "RollingUpdate". Default is RollingUpdate.
210+
description: "Change rollout strategy. \n Supported strategies:
211+
* \"RollingUpdate\". * \"OnDelete\" \n Default is RollingUpdate."
212212
type: string
213213
type: object
214214
version:

config/default/webhookcainjection_patch.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ metadata:
66
name: mutating-webhook-configuration
77
annotations:
88
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
9+
---
10+
apiVersion: admissionregistration.k8s.io/v1
11+
kind: ValidatingWebhookConfiguration
12+
metadata:
13+
name: validating-webhook-configuration
14+
annotations:
15+
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)

config/webhook/manifests.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,31 @@ webhooks:
2727
resources:
2828
- taloscontrolplanes
2929
sideEffects: None
30+
---
31+
apiVersion: admissionregistration.k8s.io/v1
32+
kind: ValidatingWebhookConfiguration
33+
metadata:
34+
creationTimestamp: null
35+
name: validating-webhook-configuration
36+
webhooks:
37+
- admissionReviewVersions:
38+
- v1
39+
clientConfig:
40+
service:
41+
name: webhook-service
42+
namespace: system
43+
path: /validate-controlplane-cluster-x-k8s-io-v1alpha3-taloscontrolplane
44+
failurePolicy: Fail
45+
name: validate.taloscontrolplane.controlplane.cluster.x-k8s.io
46+
rules:
47+
- apiGroups:
48+
- controlplane.cluster.x-k8s.io
49+
apiVersions:
50+
- v1alpha3
51+
operations:
52+
- CREATE
53+
- UPDATE
54+
- DELETE
55+
resources:
56+
- taloscontrolplanes
57+
sideEffects: None

controllers/controlplane.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/go-logr/logr"
1111
"github.com/pkg/errors"
12+
"github.com/talos-systems/cluster-api-control-plane-provider-talos/api/v1alpha3"
1213
controlplanev1 "github.com/talos-systems/cluster-api-control-plane-provider-talos/api/v1alpha3"
1314
apierrors "k8s.io/apimachinery/pkg/api/errors"
1415
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -62,6 +63,10 @@ func (c *ControlPlane) MachineWithDeleteAnnotation(machines collections.Machines
6263

6364
// MachinesNeedingRollout return a list of machines that need to be rolled out.
6465
func (c *ControlPlane) MachinesNeedingRollout() collections.Machines {
66+
if c.TCP.Spec.RolloutStrategy != nil && c.TCP.Spec.RolloutStrategy.Type == v1alpha3.OnDeleteStrategyType {
67+
return collections.New()
68+
}
69+
6570
// Ignore machines to be deleted.
6671
machines := c.Machines.Filter(collections.Not(collections.HasDeletionTimestamp))
6772

controllers/upgrade.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func (r *TalosControlPlaneReconciler) upgradeControlPlane(
3939
}
4040

4141
return r.scaleDownControlPlane(ctx, cluster, tcp, controlPlane, machinesRequireUpgrade)
42+
case controlplanev1.OnDeleteStrategyType:
43+
// nothing to do, scale up handler will take care of creating machines with the new spec
44+
return ctrl.Result{}, nil
4245
default:
4346
logger.Info("RolloutStrategy type is not set to RollingUpdateStrategyType, unable to determine the strategy for rolling out machines")
4447
return ctrl.Result{}, nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/talos-systems/capi-utils v0.0.0-20220330110254-2b207c1f0aff
1515
github.com/talos-systems/cluster-api-bootstrap-provider-talos v0.5.4
1616
github.com/talos-systems/go-retry v0.3.1
17-
github.com/talos-systems/talos/pkg/machinery v1.1.0
17+
github.com/talos-systems/talos/pkg/machinery v1.1.1
1818
google.golang.org/grpc v1.46.2
1919
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2020
k8s.io/api v0.23.5

0 commit comments

Comments
 (0)