Skip to content

Commit 9726797

Browse files
authored
Verify CAPI is installed and with expected version (#1734)
1 parent a0db3c1 commit 9726797

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

cmd/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func capiCRDHandler(gvk *schema.GroupVersionKind, action crd.ChangeType) {
343343
if action == crd.Modify {
344344
return
345345
}
346-
if gvk.Group == clusterv1.GroupVersion.Group {
346+
if gvk.Group == clusterv1.GroupVersion.Group && gvk.Version == clusterv1.GroupVersion.Version {
347347
setupLog.V(logs.LogInfo).Info("Initiating graceful restart due to CAPI CRD update",
348348
"GVK", gvk.String(), "Action", string(action))
349349

@@ -353,8 +353,8 @@ func capiCRDHandler(gvk *schema.GroupVersionKind, action crd.ChangeType) {
353353
}
354354
}
355355

356-
// isCAPIInstalled returns true if CAPI is installed, false otherwise
357-
func isCAPIInstalled(ctx context.Context, c client.Client) (bool, error) {
356+
// isCAPIInstalled returns true if CAPI is installed with v1beta2 served, false otherwise
357+
func isCAPIInstalled(ctx context.Context, c client.Client, logger logr.Logger) (bool, error) {
358358
clusterCRD := &apiextensionsv1.CustomResourceDefinition{}
359359

360360
err := c.Get(ctx, types.NamespacedName{Name: "clusters.cluster.x-k8s.io"}, clusterCRD)
@@ -365,7 +365,14 @@ func isCAPIInstalled(ctx context.Context, c client.Client) (bool, error) {
365365
return false, err
366366
}
367367

368-
return true, nil
368+
for _, version := range clusterCRD.Spec.Versions {
369+
if version.Name == clusterv1.GroupVersion.Version && version.Served {
370+
return true, nil
371+
}
372+
}
373+
374+
logger.V(logs.LogInfo).Info("clusterCRD CRD present but v1beta2 not served")
375+
return false, nil
369376
}
370377

371378
// fluxCRDHandler restarts process if a Flux CRD is updated
@@ -404,7 +411,7 @@ func capiWatchers(ctx context.Context, mgr ctrl.Manager, watchersForCAPI []watch
404411
const maxRetries = 20
405412
retries := 0
406413
for {
407-
capiPresent, err := isCAPIInstalled(ctx, mgr.GetClient())
414+
capiPresent, err := isCAPIInstalled(ctx, mgr.GetClient(), logger)
408415
if err != nil {
409416
if retries < maxRetries {
410417
logger.Info(fmt.Sprintf("failed to verify if CAPI is present: %v", err))

controllers/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,9 @@ func deplAssociatedClusterExist(ctx context.Context, c client.Client, depl *apps
521521
if apierrors.IsNotFound(err) {
522522
return false, clusterNamespace, clusterName, clusterType
523523
}
524+
if clusterType == libsveltosv1beta1.ClusterTypeCapi && meta.IsNoMatchError(err) {
525+
return true, clusterNamespace, clusterName, clusterType
526+
}
524527
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get cluster %s:%s/%s: %v",
525528
clusterNamespace, clusterName, clusterTypeString, err))
526529
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/onsi/ginkgo/v2 v2.28.1
1919
github.com/onsi/gomega v1.39.1
2020
github.com/pkg/errors v0.9.1
21-
github.com/projectsveltos/libsveltos v1.8.0
21+
github.com/projectsveltos/libsveltos v1.8.1-0.20260422123949-ee3c3d0f960e
2222
github.com/prometheus/client_golang v1.23.2
2323
github.com/robfig/cron v1.2.0
2424
github.com/spf13/pflag v1.0.10

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
274274
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
275275
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
276276
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
277-
github.com/projectsveltos/libsveltos v1.8.0 h1:yBHmiYpBbpPS6aNJOx+uOAvvBOQPkHE8cU4VKsXoAao=
278-
github.com/projectsveltos/libsveltos v1.8.0/go.mod h1:0K9ZLuYL/g0OonfhRVkHMyDYWuFt24EhvTtjTbUu3+Y=
277+
github.com/projectsveltos/libsveltos v1.8.1-0.20260422123949-ee3c3d0f960e h1:6U0OrYtquxjcuHLROBpcva1fofBae9c+nGhRg6xqgRI=
278+
github.com/projectsveltos/libsveltos v1.8.1-0.20260422123949-ee3c3d0f960e/go.mod h1:0K9ZLuYL/g0OonfhRVkHMyDYWuFt24EhvTtjTbUu3+Y=
279279
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20251212200258-2b3cdcb7c0f5 h1:khnc+994UszxZYu69J+R5FKiLA/Nk1JQj0EYAkwTWz0=
280280
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20251212200258-2b3cdcb7c0f5/go.mod h1:yVL8KQFa9tmcxgwl9nwIMtKgtmIVC1zaFRSCfOwYvPY=
281281
github.com/projectsveltos/lua-utils/glua-runes v0.0.0-20251212200258-2b3cdcb7c0f5 h1:YbsebwRwTRhV8QacvEAdFqxcxHdeu7JTVtsBovbkgos=

0 commit comments

Comments
 (0)