Skip to content

Commit 0287233

Browse files
committed
fix: unmarshals generic cluster config with a check
1 parent 10d459c commit 0287233

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

pkg/webhook/preflight/generic/checker.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ import (
1515
)
1616

1717
var Checker = &genericChecker{
18-
registryCheckFactory: newRegistryCheck,
18+
registryCheckFactory: newRegistryCheck,
19+
configurationCheckFactory: newConfigurationCheck,
1920
}
2021

2122
type genericChecker struct {
23+
configurationCheckFactory func(
24+
cd *checkDependencies,
25+
) preflight.Check
2226
registryCheckFactory func(
2327
cd *checkDependencies,
2428
) []preflight.Check
@@ -41,7 +45,11 @@ func (g *genericChecker) Init(
4145
cluster: cluster,
4246
log: ctrl.LoggerFrom(ctx).WithName("preflight/generic"),
4347
}
44-
checks := []preflight.Check{}
48+
checks := []preflight.Check{
49+
// The configuration check must run first, because it initializes data used by all other checks.
50+
g.configurationCheckFactory(cd),
51+
}
4552
checks = append(checks, g.registryCheckFactory(cd)...)
53+
cd.log.Info("returning checks", "checks", checks)
4654
return checks
4755
}

pkg/webhook/preflight/generic/registry.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"regexp"
1010

11+
"github.com/go-logr/logr"
1112
"github.com/regclient/regclient"
1213
"github.com/regclient/regclient/config"
1314
"github.com/regclient/regclient/types/ping"
@@ -36,13 +37,15 @@ type registryCheck struct {
3637
kclient ctrlclient.Client
3738
cluster *clusterv1.Cluster
3839
regClientPingerGetter regClientPingerFactory
40+
log logr.Logger
3941
}
4042

4143
func (r *registryCheck) Name() string {
4244
return "RegistryCredentials"
4345
}
4446

4547
func (r *registryCheck) Run(ctx context.Context) preflight.CheckResult {
48+
r.log.Info("Running registry check")
4649
if r.registryMirror != nil {
4750
return r.checkRegistry(
4851
ctx,
@@ -186,6 +189,7 @@ func newRegistryCheck(
186189
registryMirror: cd.genericClusterConfigSpec.GlobalImageRegistryMirror.DeepCopy(),
187190
cluster: cd.cluster,
188191
regClientPingerGetter: defaultRegClientGetter,
192+
log: cd.log,
189193
})
190194
}
191195
if cd.genericClusterConfigSpec != nil && len(cd.genericClusterConfigSpec.ImageRegistries) > 0 {
@@ -200,6 +204,7 @@ func newRegistryCheck(
200204
imageRegistry: registry.DeepCopy(),
201205
cluster: cd.cluster,
202206
regClientPingerGetter: defaultRegClientGetter,
207+
log: cd.log,
203208
})
204209
}
205210
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package generic
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
8+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
9+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
10+
)
11+
12+
type configurationCheck struct {
13+
result preflight.CheckResult
14+
}
15+
16+
func (c *configurationCheck) Name() string {
17+
return "GenericConfiguration"
18+
}
19+
20+
func (c *configurationCheck) Run(_ context.Context) preflight.CheckResult {
21+
return c.result
22+
}
23+
24+
func newConfigurationCheck(
25+
cd *checkDependencies,
26+
) preflight.Check {
27+
genericClusterConfigSpec := &carenv1.GenericClusterConfigSpec{}
28+
configurationCheck := &configurationCheck{
29+
result: preflight.CheckResult{
30+
Allowed: true,
31+
},
32+
}
33+
err := variables.UnmarshalClusterVariable(
34+
variables.GetClusterVariableByName(
35+
carenv1.ClusterConfigVariableName,
36+
cd.cluster.Spec.Topology.Variables,
37+
),
38+
genericClusterConfigSpec,
39+
)
40+
if err != nil {
41+
configurationCheck.result.Allowed = false
42+
configurationCheck.result.Error = true
43+
configurationCheck.result.Causes = append(configurationCheck.result.Causes,
44+
preflight.Cause{
45+
Message: fmt.Sprintf("Failed to unmarshal cluster variable %s: %s",
46+
carenv1.ClusterConfigVariableName,
47+
err,
48+
),
49+
Field: "cluster.spec.topology.variables[.name=clusterConfig].genericClusterConfigSpec",
50+
},
51+
)
52+
}
53+
cd.genericClusterConfigSpec = genericClusterConfigSpec
54+
return configurationCheck
55+
}

0 commit comments

Comments
 (0)