Skip to content

Commit 03191dd

Browse files
faiqjimmidyson
andauthored
feat: implements BeforeClusterUpgrade hook (#682)
**What problem does this PR solve?**: This implements BeforeClusterUpgrade hook and executes the same logic. There's some common code that I'd like ideas on how to consolidate while also having the handlers be separate lifecycle hooks. **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. --> --------- Co-authored-by: Jimmi Dyson <[email protected]>
1 parent 30a98da commit 03191dd

File tree

27 files changed

+305
-154
lines changed

27 files changed

+305
-154
lines changed

charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses/aws-cluster-class.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ spec:
2222
name: aws-quick-start
2323
patches:
2424
- external:
25-
discoverVariablesExtension: awsclusterconfigvars.cluster-api-runtime-extensions-nutanix
26-
generateExtension: awsclusterconfigpatch.cluster-api-runtime-extensions-nutanix
25+
discoverVariablesExtension: awsclusterconfigvars-dv.cluster-api-runtime-extensions-nutanix
26+
generateExtension: awsclusterconfigpatch-gp.cluster-api-runtime-extensions-nutanix
2727
name: cluster-config
2828
- external:
29-
discoverVariablesExtension: awsworkerconfigvars.cluster-api-runtime-extensions-nutanix
30-
generateExtension: awsworkerconfigpatch.cluster-api-runtime-extensions-nutanix
29+
discoverVariablesExtension: awsworkerconfigvars-dv.cluster-api-runtime-extensions-nutanix
30+
generateExtension: awsworkerconfigpatch-gp.cluster-api-runtime-extensions-nutanix
3131
name: worker-config
3232
- definitions:
3333
- jsonPatches:

charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses/docker-cluster-class.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ spec:
2222
name: docker-quick-start-cluster
2323
patches:
2424
- external:
25-
discoverVariablesExtension: dockerclusterconfigvars.cluster-api-runtime-extensions-nutanix
26-
generateExtension: dockerclusterconfigpatch.cluster-api-runtime-extensions-nutanix
25+
discoverVariablesExtension: dockerclusterconfigvars-dv.cluster-api-runtime-extensions-nutanix
26+
generateExtension: dockerclusterconfigpatch-gp.cluster-api-runtime-extensions-nutanix
2727
name: cluster-config
2828
- external:
29-
discoverVariablesExtension: dockerworkerconfigvars.cluster-api-runtime-extensions-nutanix
30-
generateExtension: dockerworkerconfigpatch.cluster-api-runtime-extensions-nutanix
29+
discoverVariablesExtension: dockerworkerconfigvars-dv.cluster-api-runtime-extensions-nutanix
30+
generateExtension: dockerworkerconfigpatch-gp.cluster-api-runtime-extensions-nutanix
3131
name: worker-config
3232
workers:
3333
machineDeployments:

charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses/nutanix-cluster-class.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ spec:
6666
name: nutanix-quick-start-nct
6767
patches:
6868
- external:
69-
discoverVariablesExtension: nutanixclusterconfigvars.cluster-api-runtime-extensions-nutanix
70-
generateExtension: nutanixclusterconfigpatch.cluster-api-runtime-extensions-nutanix
69+
discoverVariablesExtension: nutanixclusterconfigvars-dv.cluster-api-runtime-extensions-nutanix
70+
generateExtension: nutanixclusterconfigpatch-gp.cluster-api-runtime-extensions-nutanix
7171
name: cluster-config
7272
- external:
73-
discoverVariablesExtension: nutanixworkerconfigvars.cluster-api-runtime-extensions-nutanix
74-
generateExtension: nutanixworkerconfigpatch.cluster-api-runtime-extensions-nutanix
73+
discoverVariablesExtension: nutanixworkerconfigvars-dv.cluster-api-runtime-extensions-nutanix
74+
generateExtension: nutanixworkerconfigpatch-gp.cluster-api-runtime-extensions-nutanix
7575
name: worker-config
7676
workers:
7777
machineDeployments:

common/pkg/server/server.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/spf13/pflag"
1111
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
1212
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
13-
"sigs.k8s.io/cluster-api/exp/runtime/server"
13+
runtimeserver "sigs.k8s.io/cluster-api/exp/runtime/server"
1414
ctrl "sigs.k8s.io/controller-runtime"
1515

1616
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
@@ -69,7 +69,7 @@ func (s *Server) Start(ctx context.Context) error {
6969
setupLog := ctrl.Log.WithName("runtimehooks")
7070

7171
// Create a http server for serving runtime extensions
72-
webhookServer, err := server.New(server.Options{
72+
webhookServer, err := runtimeserver.New(runtimeserver.Options{
7373
Catalog: s.catalog,
7474
Port: s.opts.webhookPort,
7575
CertDir: s.opts.webhookCertDir,
@@ -83,9 +83,9 @@ func (s *Server) Start(ctx context.Context) error {
8383
h := s.hooks[idx]
8484

8585
if t, ok := h.(lifecycle.BeforeClusterCreate); ok {
86-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
86+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
8787
Hook: runtimehooksv1.BeforeClusterCreate,
88-
Name: strings.ToLower(h.Name()),
88+
Name: strings.ToLower(h.Name()) + "-bcc",
8989
HandlerFunc: t.BeforeClusterCreate,
9090
}); err != nil {
9191
setupLog.Error(err, "error adding handler")
@@ -94,9 +94,9 @@ func (s *Server) Start(ctx context.Context) error {
9494
}
9595

9696
if t, ok := h.(lifecycle.AfterControlPlaneInitialized); ok {
97-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
97+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
9898
Hook: runtimehooksv1.AfterControlPlaneInitialized,
99-
Name: strings.ToLower(h.Name()),
99+
Name: strings.ToLower(h.Name()) + "-acpi",
100100
HandlerFunc: t.AfterControlPlaneInitialized,
101101
}); err != nil {
102102
setupLog.Error(err, "error adding handler")
@@ -105,9 +105,9 @@ func (s *Server) Start(ctx context.Context) error {
105105
}
106106

107107
if t, ok := h.(lifecycle.BeforeClusterUpgrade); ok {
108-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
108+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
109109
Hook: runtimehooksv1.BeforeClusterUpgrade,
110-
Name: strings.ToLower(h.Name()),
110+
Name: strings.ToLower(h.Name()) + "-bcu",
111111
HandlerFunc: t.BeforeClusterUpgrade,
112112
}); err != nil {
113113
setupLog.Error(err, "error adding handler")
@@ -116,9 +116,9 @@ func (s *Server) Start(ctx context.Context) error {
116116
}
117117

118118
if t, ok := h.(lifecycle.AfterControlPlaneUpgrade); ok {
119-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
119+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
120120
Hook: runtimehooksv1.AfterControlPlaneUpgrade,
121-
Name: h.Name(),
121+
Name: h.Name() + "-acpu",
122122
HandlerFunc: t.AfterControlPlaneUpgrade,
123123
}); err != nil {
124124
setupLog.Error(err, "error adding handler")
@@ -127,9 +127,9 @@ func (s *Server) Start(ctx context.Context) error {
127127
}
128128

129129
if t, ok := h.(lifecycle.BeforeClusterDelete); ok {
130-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
130+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
131131
Hook: runtimehooksv1.BeforeClusterDelete,
132-
Name: strings.ToLower(h.Name()),
132+
Name: strings.ToLower(h.Name()) + "-bcd",
133133
HandlerFunc: t.BeforeClusterDelete,
134134
}); err != nil {
135135
setupLog.Error(err, "error adding handler")
@@ -138,9 +138,9 @@ func (s *Server) Start(ctx context.Context) error {
138138
}
139139

140140
if t, ok := h.(mutation.DiscoverVariables); ok {
141-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
141+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
142142
Hook: runtimehooksv1.DiscoverVariables,
143-
Name: strings.ToLower(h.Name()),
143+
Name: strings.ToLower(h.Name()) + "-dv",
144144
HandlerFunc: t.DiscoverVariables,
145145
}); err != nil {
146146
setupLog.Error(err, "error adding handler")
@@ -149,9 +149,9 @@ func (s *Server) Start(ctx context.Context) error {
149149
}
150150

151151
if t, ok := h.(mutation.GeneratePatches); ok {
152-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
152+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
153153
Hook: runtimehooksv1.GeneratePatches,
154-
Name: strings.ToLower(h.Name()),
154+
Name: strings.ToLower(h.Name()) + "-gp",
155155
HandlerFunc: t.GeneratePatches,
156156
}); err != nil {
157157
setupLog.Error(err, "error adding handler")
@@ -160,9 +160,9 @@ func (s *Server) Start(ctx context.Context) error {
160160
}
161161

162162
if t, ok := h.(mutation.ValidateTopology); ok {
163-
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
163+
if err := webhookServer.AddExtensionHandler(runtimeserver.ExtensionHandler{
164164
Hook: runtimehooksv1.ValidateTopology,
165-
Name: strings.ToLower(h.Name()),
165+
Name: strings.ToLower(h.Name()) + "-vt",
166166
HandlerFunc: t.ValidateTopology,
167167
}); err != nil {
168168
setupLog.Error(err, "error adding handler")

hack/examples/overlays/clusterclasses/aws/kustomization.yaml.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ patches:
1919
value:
2020
- name: "cluster-config"
2121
external:
22-
generateExtension: "awsclusterconfigpatch.cluster-api-runtime-extensions-nutanix"
23-
discoverVariablesExtension: "awsclusterconfigvars.cluster-api-runtime-extensions-nutanix"
22+
generateExtension: "awsclusterconfigpatch-gp.cluster-api-runtime-extensions-nutanix"
23+
discoverVariablesExtension: "awsclusterconfigvars-dv.cluster-api-runtime-extensions-nutanix"
2424
- name: "worker-config"
2525
external:
26-
generateExtension: "awsworkerconfigpatch.cluster-api-runtime-extensions-nutanix"
27-
discoverVariablesExtension: "awsworkerconfigvars.cluster-api-runtime-extensions-nutanix"
26+
generateExtension: "awsworkerconfigpatch-gp.cluster-api-runtime-extensions-nutanix"
27+
discoverVariablesExtension: "awsworkerconfigvars-dv.cluster-api-runtime-extensions-nutanix"
2828
- name: identityRef
2929
definitions:
3030
- jsonPatches:

hack/examples/overlays/clusterclasses/docker/kustomization.yaml.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ patches:
1919
value:
2020
- name: "cluster-config"
2121
external:
22-
generateExtension: "dockerclusterconfigpatch.cluster-api-runtime-extensions-nutanix"
23-
discoverVariablesExtension: "dockerclusterconfigvars.cluster-api-runtime-extensions-nutanix"
22+
generateExtension: "dockerclusterconfigpatch-gp.cluster-api-runtime-extensions-nutanix"
23+
discoverVariablesExtension: "dockerclusterconfigvars-dv.cluster-api-runtime-extensions-nutanix"
2424
- name: "worker-config"
2525
external:
26-
generateExtension: "dockerworkerconfigpatch.cluster-api-runtime-extensions-nutanix"
27-
discoverVariablesExtension: "dockerworkerconfigvars.cluster-api-runtime-extensions-nutanix"
26+
generateExtension: "dockerworkerconfigpatch-gp.cluster-api-runtime-extensions-nutanix"
27+
discoverVariablesExtension: "dockerworkerconfigvars-dv.cluster-api-runtime-extensions-nutanix"

hack/examples/overlays/clusterclasses/nutanix/kustomization.yaml.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ patches:
1919
value:
2020
- name: "cluster-config"
2121
external:
22-
generateExtension: "nutanixclusterconfigpatch.cluster-api-runtime-extensions-nutanix"
23-
discoverVariablesExtension: "nutanixclusterconfigvars.cluster-api-runtime-extensions-nutanix"
22+
generateExtension: "nutanixclusterconfigpatch-gp.cluster-api-runtime-extensions-nutanix"
23+
discoverVariablesExtension: "nutanixclusterconfigvars-dv.cluster-api-runtime-extensions-nutanix"
2424
- name: "worker-config"
2525
external:
26-
generateExtension: "nutanixworkerconfigpatch.cluster-api-runtime-extensions-nutanix"
27-
discoverVariablesExtension: "nutanixworkerconfigvars.cluster-api-runtime-extensions-nutanix"
26+
generateExtension: "nutanixworkerconfigpatch-gp.cluster-api-runtime-extensions-nutanix"
27+
discoverVariablesExtension: "nutanixworkerconfigvars-dv.cluster-api-runtime-extensions-nutanix"

pkg/handlers/generic/lifecycle/ccm/handler.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type CCMHandler struct {
4444
var (
4545
_ commonhandlers.Named = &CCMHandler{}
4646
_ lifecycle.AfterControlPlaneInitialized = &CCMHandler{}
47+
_ lifecycle.BeforeClusterUpgrade = &CCMHandler{}
4748
)
4849

4950
func New(
@@ -67,14 +68,36 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
6768
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
6869
resp *runtimehooksv1.AfterControlPlaneInitializedResponse,
6970
) {
70-
clusterKey := ctrlclient.ObjectKeyFromObject(&req.Cluster)
71+
commonResponse := &runtimehooksv1.CommonResponse{}
72+
c.apply(ctx, &req.Cluster, commonResponse)
73+
resp.Status = commonResponse.GetStatus()
74+
resp.Message = commonResponse.GetMessage()
75+
}
76+
77+
func (c *CCMHandler) BeforeClusterUpgrade(
78+
ctx context.Context,
79+
req *runtimehooksv1.BeforeClusterUpgradeRequest,
80+
resp *runtimehooksv1.BeforeClusterUpgradeResponse,
81+
) {
82+
commonResponse := &runtimehooksv1.CommonResponse{}
83+
c.apply(ctx, &req.Cluster, commonResponse)
84+
resp.Status = commonResponse.GetStatus()
85+
resp.Message = commonResponse.GetMessage()
86+
}
87+
88+
func (c *CCMHandler) apply(
89+
ctx context.Context,
90+
cluster *clusterv1.Cluster,
91+
resp *runtimehooksv1.CommonResponse,
92+
) {
93+
clusterKey := ctrlclient.ObjectKeyFromObject(cluster)
7194

7295
log := ctrl.LoggerFrom(ctx).WithValues(
7396
"cluster",
7497
clusterKey,
7598
)
7699

77-
varMap := variables.ClusterVariablesToVariablesMap(req.Cluster.Spec.Topology.Variables)
100+
varMap := variables.ClusterVariablesToVariablesMap(cluster.Spec.Topology.Variables)
78101

79102
_, err := variables.Get[v1alpha1.CCM](varMap, c.variableName, c.variablePath...)
80103
if err != nil {
@@ -114,7 +137,7 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
114137
}
115138

116139
// There's a 1:1 mapping of infra to CCM provider. We derive the CCM provider from the infra.
117-
infraKind := req.Cluster.Spec.InfrastructureRef.Kind
140+
infraKind := cluster.Spec.InfrastructureRef.Kind
118141
log.Info(fmt.Sprintf("finding CCM handler for %s", infraKind))
119142
var handler CCMProvider
120143
switch {
@@ -127,7 +150,7 @@ func (c *CCMHandler) AfterControlPlaneInitialized(
127150
return
128151
}
129152

130-
err = handler.Apply(ctx, &req.Cluster, &clusterConfigVar, log)
153+
err = handler.Apply(ctx, cluster, &clusterConfigVar, log)
131154
if err != nil {
132155
log.Error(
133156
err,

pkg/handlers/generic/lifecycle/clusterautoscaler/handler.go

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

1010
"github.com/go-logr/logr"
1111
"github.com/spf13/pflag"
12+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1213
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1314
ctrl "sigs.k8s.io/controller-runtime"
1415
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -24,12 +25,11 @@ import (
2425
type addonStrategy interface {
2526
apply(
2627
context.Context,
27-
*runtimehooksv1.AfterControlPlaneInitializedRequest,
28+
*clusterv1.Cluster,
2829
string,
2930
logr.Logger,
3031
) error
3132
}
32-
3333
type Config struct {
3434
*options.GlobalOptions
3535

@@ -54,6 +54,7 @@ type DefaultClusterAutoscaler struct {
5454
var (
5555
_ commonhandlers.Named = &DefaultClusterAutoscaler{}
5656
_ lifecycle.AfterControlPlaneInitialized = &DefaultClusterAutoscaler{}
57+
_ lifecycle.BeforeClusterUpgrade = &DefaultClusterAutoscaler{}
5758
)
5859

5960
func New(
@@ -79,14 +80,36 @@ func (n *DefaultClusterAutoscaler) AfterControlPlaneInitialized(
7980
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
8081
resp *runtimehooksv1.AfterControlPlaneInitializedResponse,
8182
) {
82-
clusterKey := ctrlclient.ObjectKeyFromObject(&req.Cluster)
83+
commonResponse := &runtimehooksv1.CommonResponse{}
84+
n.apply(ctx, &req.Cluster, commonResponse)
85+
resp.Status = commonResponse.GetStatus()
86+
resp.Message = commonResponse.GetMessage()
87+
}
88+
89+
func (n *DefaultClusterAutoscaler) BeforeClusterUpgrade(
90+
ctx context.Context,
91+
req *runtimehooksv1.BeforeClusterUpgradeRequest,
92+
resp *runtimehooksv1.BeforeClusterUpgradeResponse,
93+
) {
94+
commonResponse := &runtimehooksv1.CommonResponse{}
95+
n.apply(ctx, &req.Cluster, commonResponse)
96+
resp.Status = commonResponse.GetStatus()
97+
resp.Message = commonResponse.GetMessage()
98+
}
99+
100+
func (n *DefaultClusterAutoscaler) apply(
101+
ctx context.Context,
102+
cluster *clusterv1.Cluster,
103+
resp *runtimehooksv1.CommonResponse,
104+
) {
105+
clusterKey := ctrlclient.ObjectKeyFromObject(cluster)
83106

84107
log := ctrl.LoggerFrom(ctx).WithValues(
85108
"cluster",
86109
clusterKey,
87110
)
88111

89-
varMap := variables.ClusterVariablesToVariablesMap(req.Cluster.Spec.Topology.Variables)
112+
varMap := variables.ClusterVariablesToVariablesMap(cluster.Spec.Topology.Variables)
90113

91114
cniVar, err := variables.Get[v1alpha1.ClusterAutoscaler](
92115
varMap,
@@ -151,7 +174,7 @@ func (n *DefaultClusterAutoscaler) AfterControlPlaneInitialized(
151174
return
152175
}
153176

154-
if err = strategy.apply(ctx, req, n.config.DefaultsNamespace(), log); err != nil {
177+
if err = strategy.apply(ctx, cluster, n.config.DefaultsNamespace(), log); err != nil {
155178
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
156179
resp.SetMessage(err.Error())
157180
return

pkg/handlers/generic/lifecycle/clusterautoscaler/strategy_crs.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/spf13/pflag"
1212
corev1 "k8s.io/api/core/v1"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1415
"sigs.k8s.io/cluster-api/controllers/remote"
15-
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1616
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1717

1818
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
@@ -40,7 +40,7 @@ type crsStrategy struct {
4040

4141
func (s crsStrategy) apply(
4242
ctx context.Context,
43-
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
43+
cluster *clusterv1.Cluster,
4444
defaultsNamespace string,
4545
log logr.Logger,
4646
) error {
@@ -66,8 +66,6 @@ func (s crsStrategy) apply(
6666

6767
log.Info("Ensuring cluster-autoscaler ConfigMap exists for cluster")
6868

69-
cluster := &req.Cluster
70-
7169
data := templateData(cluster, defaultCM.Data)
7270
cm := &corev1.ConfigMap{
7371
TypeMeta: metav1.TypeMeta{

0 commit comments

Comments
 (0)