diff --git a/go.mod b/go.mod index b7d6d27..ac44dea 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/onsi/ginkgo/v2 v2.28.1 github.com/onsi/gomega v1.39.1 github.com/pkg/errors v0.9.1 - github.com/projectsveltos/libsveltos v1.8.0 + github.com/projectsveltos/libsveltos v1.8.1-0.20260422193357-a670bbae8df8 github.com/prometheus/client_golang v1.23.2 github.com/spf13/pflag v1.0.10 golang.org/x/text v0.36.0 diff --git a/go.sum b/go.sum index ec96109..9ebc372 100644 --- a/go.sum +++ b/go.sum @@ -143,8 +143,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/projectsveltos/libsveltos v1.8.0 h1:yBHmiYpBbpPS6aNJOx+uOAvvBOQPkHE8cU4VKsXoAao= -github.com/projectsveltos/libsveltos v1.8.0/go.mod h1:0K9ZLuYL/g0OonfhRVkHMyDYWuFt24EhvTtjTbUu3+Y= +github.com/projectsveltos/libsveltos v1.8.1-0.20260422193357-a670bbae8df8 h1:XR0ebx6/L4q+GvYlmvNEhyrF5ALMMVSySgkJ5bW6iAw= +github.com/projectsveltos/libsveltos v1.8.1-0.20260422193357-a670bbae8df8/go.mod h1:0K9ZLuYL/g0OonfhRVkHMyDYWuFt24EhvTtjTbUu3+Y= github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= diff --git a/main.go b/main.go index 4e20ca0..7e196cb 100644 --- a/main.go +++ b/main.go @@ -277,15 +277,18 @@ func capiCRDHandler(gvk *schema.GroupVersionKind, action crd.ChangeType) { if action == crd.Modify { return } - if gvk.Group == clusterv1.GroupVersion.Group { + if gvk.Group == clusterv1.GroupVersion.Group && gvk.Version == clusterv1.GroupVersion.Version { + setupLog.V(logs.LogInfo).Info("Initiating graceful restart due to CAPI CRD update", + "GVK", gvk.String(), "Action", string(action)) + if killErr := syscall.Kill(syscall.Getpid(), syscall.SIGTERM); killErr != nil { panic("kill -TERM failed") } } } -// isCAPIInstalled returns true if CAPI is installed, false otherwise -func isCAPIInstalled(ctx context.Context, c client.Client) (bool, error) { +// isCAPIInstalled returns true if CAPI is installed with v1beta2 served, false otherwise +func isCAPIInstalled(ctx context.Context, c client.Client, logger logr.Logger) (bool, error) { clusterCRD := &apiextensionsv1.CustomResourceDefinition{} 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) { return false, err } - return true, nil + for _, version := range clusterCRD.Spec.Versions { + if version.Name == clusterv1.GroupVersion.Version && version.Served { + return true, nil + } + } + + logger.V(logs.LogInfo).Info("clusterCRD CRD present but v1beta2 not served") + return false, nil } func capiWatchers(ctx context.Context, mgr ctrl.Manager, @@ -306,7 +316,7 @@ func capiWatchers(ctx context.Context, mgr ctrl.Manager, const maxRetries = 20 retries := 0 for { - capiPresent, err := isCAPIInstalled(ctx, mgr.GetClient()) + capiPresent, err := isCAPIInstalled(ctx, mgr.GetClient(), logger) if err != nil { if retries < maxRetries { logger.Info(fmt.Sprintf("failed to verify if CAPI is present: %v", err))