Skip to content

Commit bb581f9

Browse files
authored
Merge pull request #464 from gianlucam76/capi-version
Verify CAPI is installed and with expected version
2 parents d3ddcc8 + 1320e4e commit bb581f9

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/onsi/ginkgo/v2 v2.28.1
1010
github.com/onsi/gomega v1.39.1
1111
github.com/pkg/errors v0.9.1
12-
github.com/projectsveltos/libsveltos v1.8.0
12+
github.com/projectsveltos/libsveltos v1.8.1-0.20260422193357-a670bbae8df8
1313
github.com/prometheus/client_golang v1.23.2
1414
github.com/spf13/pflag v1.0.10
1515
golang.org/x/text v0.36.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
143143
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
144144
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
145145
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
146-
github.com/projectsveltos/libsveltos v1.8.0 h1:yBHmiYpBbpPS6aNJOx+uOAvvBOQPkHE8cU4VKsXoAao=
147-
github.com/projectsveltos/libsveltos v1.8.0/go.mod h1:0K9ZLuYL/g0OonfhRVkHMyDYWuFt24EhvTtjTbUu3+Y=
146+
github.com/projectsveltos/libsveltos v1.8.1-0.20260422193357-a670bbae8df8 h1:XR0ebx6/L4q+GvYlmvNEhyrF5ALMMVSySgkJ5bW6iAw=
147+
github.com/projectsveltos/libsveltos v1.8.1-0.20260422193357-a670bbae8df8/go.mod h1:0K9ZLuYL/g0OonfhRVkHMyDYWuFt24EhvTtjTbUu3+Y=
148148
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
149149
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
150150
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=

main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,18 @@ func capiCRDHandler(gvk *schema.GroupVersionKind, action crd.ChangeType) {
277277
if action == crd.Modify {
278278
return
279279
}
280-
if gvk.Group == clusterv1.GroupVersion.Group {
280+
if gvk.Group == clusterv1.GroupVersion.Group && gvk.Version == clusterv1.GroupVersion.Version {
281+
setupLog.V(logs.LogInfo).Info("Initiating graceful restart due to CAPI CRD update",
282+
"GVK", gvk.String(), "Action", string(action))
283+
281284
if killErr := syscall.Kill(syscall.Getpid(), syscall.SIGTERM); killErr != nil {
282285
panic("kill -TERM failed")
283286
}
284287
}
285288
}
286289

287-
// isCAPIInstalled returns true if CAPI is installed, false otherwise
288-
func isCAPIInstalled(ctx context.Context, c client.Client) (bool, error) {
290+
// isCAPIInstalled returns true if CAPI is installed with v1beta2 served, false otherwise
291+
func isCAPIInstalled(ctx context.Context, c client.Client, logger logr.Logger) (bool, error) {
289292
clusterCRD := &apiextensionsv1.CustomResourceDefinition{}
290293

291294
err := c.Get(ctx, types.NamespacedName{Name: "clusters.cluster.x-k8s.io"}, clusterCRD)
@@ -296,7 +299,14 @@ func isCAPIInstalled(ctx context.Context, c client.Client) (bool, error) {
296299
return false, err
297300
}
298301

299-
return true, nil
302+
for _, version := range clusterCRD.Spec.Versions {
303+
if version.Name == clusterv1.GroupVersion.Version && version.Served {
304+
return true, nil
305+
}
306+
}
307+
308+
logger.V(logs.LogInfo).Info("clusterCRD CRD present but v1beta2 not served")
309+
return false, nil
300310
}
301311

302312
func capiWatchers(ctx context.Context, mgr ctrl.Manager,
@@ -306,7 +316,7 @@ func capiWatchers(ctx context.Context, mgr ctrl.Manager,
306316
const maxRetries = 20
307317
retries := 0
308318
for {
309-
capiPresent, err := isCAPIInstalled(ctx, mgr.GetClient())
319+
capiPresent, err := isCAPIInstalled(ctx, mgr.GetClient(), logger)
310320
if err != nil {
311321
if retries < maxRetries {
312322
logger.Info(fmt.Sprintf("failed to verify if CAPI is present: %v", err))

0 commit comments

Comments
 (0)