Skip to content

Commit

Permalink
✨ Change clusterctl alpha rollout to support any controlplane that im…
Browse files Browse the repository at this point in the history
…plements rolloutAfter in it's spec

Signed-off-by: Richard Draycott <[email protected]>
  • Loading branch information
wikoion committed May 20, 2024
1 parent 9dc9c8f commit ff0507e
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 123 deletions.
210 changes: 210 additions & 0 deletions cmd/clusterctl/client/alpha/controlplane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package alpha

import (
"context"
"fmt"
"strings"
"time"

openapi_v2 "github.com/google/gnostic-models/openapiv2"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
)

func getUnstructuredControlPlane(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference) (*unstructured.Unstructured, error) {
c, err := proxy.NewClient(ctx)
if err != nil {
return nil, err
}
if ref.APIVersion == "" {
ref.APIVersion = DefaultAPIVersion
}
gvk := schema.GroupVersionKind{
Group: "controlplane.cluster.x-k8s.io",
Version: ref.APIVersion,
Kind: ref.Kind,
}

// Create an unstructured object
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(gvk)
obj.SetNamespace(ref.Namespace)
obj.SetName(ref.Name)

// Fetch the resource
err = c.Get(ctx, client.ObjectKey{
Namespace: ref.Namespace,
Name: ref.Name,
}, obj)

if err != nil {
return nil, fmt.Errorf("failed to get unstructured object: %v", err)
}

return obj, nil
}

// checkResourceConditions checks for specific conditions on the fetched resource.
func checkControlPlaneRolloutAfter(obj *unstructured.Unstructured) error {
rolloutAfter, _, err := unstructured.NestedString(obj.Object, "spec", "rolloutAfter")

if err != nil {
return errors.Wrapf(err, "error accessing rolloutAfter in spec: %s/%s",
obj.GetNamespace(), obj.GetName())
}
if rolloutAfter == "" {
return nil
}
rolloutTime, err := time.Parse(time.RFC3339, rolloutAfter)
if err != nil {
return errors.Wrapf(err, "invalid rolloutAfter format: %s/%s",
obj.GetNamespace(), obj.GetName())
}
if rolloutTime.After(time.Now()) {
return errors.Errorf("can't update KubeadmControlPlane (remove 'spec.rolloutAfter' first): %v/%v", obj.GetKind(), obj.GetName())
}

return nil
}

// setRolloutAfterOnControlPlane sets the rolloutAfter field on a generic resource.
func setRolloutAfterOnControlPlane(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference) error {
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf(`{"spec":{"rolloutAfter":"%v"}}`, time.Now().Format(time.RFC3339))))
return patchControlPlane(ctx, proxy, ref, patch)
}

// patchControlPlane applies a patch to an unstructured controlplane.
func patchControlPlane(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference, patch client.Patch) error {
c, err := proxy.NewClient(ctx)
if err != nil {
return err
}
if ref.APIVersion == "" {
ref.APIVersion = DefaultAPIVersion
}
gvk := schema.GroupVersionKind{
Group: "controlplane.cluster.x-k8s.io",
Version: ref.APIVersion,
Kind: ref.Kind,
}

obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(gvk)
objKey := client.ObjectKey{
Namespace: ref.Namespace,
Name: ref.Name,
}

// Get the current state of the resource
if err := c.Get(ctx, objKey, obj); err != nil {
return errors.Wrapf(err, "failed to get ControlPlane %s/%s",
obj.GetKind(), obj.GetName())
}

// Apply the patch
if err := c.Patch(ctx, obj, patch); err != nil {
return errors.Wrapf(err, "failed while patching ControlPlane %s/%s",
obj.GetKind(), obj.GetName())
}

return nil
}

func resourceHasRolloutAfter(proxy cluster.Proxy, ref corev1.ObjectReference) (bool, error) {
if ref.APIVersion == "" {
ref.APIVersion = DefaultAPIVersion
}

config, err := proxy.GetConfig()
if err != nil {
return false, err
}

if config == nil {
return false, nil
}

discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return false, err
}

//Fetch the OpenAPI schema

Check failure on line 155 in cmd/clusterctl/client/alpha/controlplane.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
openAPISchema, err := discoveryClient.OpenAPISchema()
if err != nil {
return false, err
}

// Iterate over the schema definitions to find the resource
if openAPISchema == nil {
return false, fmt.Errorf("openAPI schema is nil")
}

for _, definition := range openAPISchema.GetDefinitions().AdditionalProperties {
// Ensure the definition value is not nil
if definition == nil || definition.Value == nil {
continue
}

// Check if the definition matches the resource we are looking for
resourceDefName := fmt.Sprintf("%s.%s.%s", "io.x-k8s.cluster.controlplane", ref.APIVersion, ref.Kind)
if findSpecPropertyForResource(definition, resourceDefName, "rolloutAfter") {
return true, nil
}

}

Check failure on line 178 in cmd/clusterctl/client/alpha/controlplane.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

return false, fmt.Errorf("resource definition for %s.%s.%s not found", "io.x-k8s.cluster.controlplane", ref.APIVersion, ref.Kind)
}

func findSpecPropertyForResource(resourceDefinition *openapi_v2.NamedSchema, resourceDefName, field string) bool {
if !strings.HasSuffix(strings.ToLower(resourceDefinition.Name), resourceDefName) {
return false
}

// Find spec field in crd properties
properties := resourceDefinition.Value.GetProperties().AdditionalProperties
if properties == nil {
return false
}

for _, property := range properties {
if property.GetName() == "spec" {
// Check if rolloutAfter exists in spec properties
specProperties := property.GetValue().GetProperties().AdditionalProperties
if specProperties == nil {
return false
}
for _, specProperty := range specProperties {
if specProperty.GetName() == field {
return true
}
}
}
}

return false
}
75 changes: 0 additions & 75 deletions cmd/clusterctl/client/alpha/kubeadmcontrolplane.go

This file was deleted.

2 changes: 2 additions & 0 deletions cmd/clusterctl/client/alpha/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
MachineDeployment = "machinedeployment"
// KubeadmControlPlane is a resource type.
KubeadmControlPlane = "kubeadmcontrolplane"
// DefaultAPIVersion is what clusterctl will assume if none is provided

Check failure on line 32 in cmd/clusterctl/client/alpha/rollout.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
DefaultAPIVersion = "v1beta1"
)

var validResourceTypes = []string{

Check failure on line 36 in cmd/clusterctl/client/alpha/rollout.go

View workflow job for this annotation

GitHub Actions / lint

var `validResourceTypes` is unused (unused)
Expand Down
23 changes: 11 additions & 12 deletions cmd/clusterctl/client/alpha/rollout_pauser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/util/annotations"
)

// ObjectPauser will issue a pause on the specified cluster-api resource.
Expand All @@ -44,19 +43,19 @@ func (r *rollout) ObjectPauser(ctx context.Context, proxy cluster.Proxy, ref cor
if err := pauseMachineDeployment(ctx, proxy, ref.Name, ref.Namespace); err != nil {
return err
}
case KubeadmControlPlane:
kcp, err := getKubeadmControlPlane(ctx, proxy, ref.Name, ref.Namespace)
if err != nil || kcp == nil {
default:
obj, err := getUnstructuredControlPlane(ctx, proxy, ref)
if err != nil || obj == nil {
return errors.Wrapf(err, "failed to fetch %v/%v", ref.Kind, ref.Name)
}
if annotations.HasPaused(kcp.GetObjectMeta()) {
return errors.Errorf("KubeadmControlPlane is already paused: %v/%v\n", ref.Kind, ref.Name) //nolint:revive // KubeadmControlPlane is intentionally capitalized.

annotations := obj.GetAnnotations()
if paused, ok := annotations["cluster.x-k8s.io/paused"]; ok && paused == "true" {
return errors.Errorf("can't perform operations on paused resource (remove annotation 'cluster.x-k8s.io/paused' first): %v/%v", obj.GetKind(), obj.GetName())
}
if err := pauseKubeadmControlPlane(ctx, proxy, ref.Name, ref.Namespace); err != nil {
if err := pauseControlPlane(ctx, proxy, ref); err != nil {
return err
}
default:
return errors.Errorf("Invalid resource type %q, valid values are %v", ref.Kind, validResourceTypes)
}
return nil
}
Expand All @@ -67,8 +66,8 @@ func pauseMachineDeployment(ctx context.Context, proxy cluster.Proxy, name, name
return patchMachineDeployment(ctx, proxy, name, namespace, patch)
}

// pauseKubeadmControlPlane sets paused annotation to true.
func pauseKubeadmControlPlane(ctx context.Context, proxy cluster.Proxy, name, namespace string) error {
// pauseControlPlane sets paused annotation to true.
func pauseControlPlane(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference) error {
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"metadata\":{\"annotations\":{%q: \"%t\"}}}", clusterv1.PausedAnnotation, true)))
return patchKubeadmControlPlane(ctx, proxy, name, namespace, patch)
return patchControlPlane(ctx, proxy, ref, patch)
}
6 changes: 4 additions & 2 deletions cmd/clusterctl/client/alpha/rollout_pauser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func Test_ObjectPauser(t *testing.T) {
objs: []client.Object{
&controlplanev1.KubeadmControlPlane{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmControlPlane",
Kind: "KubeadmControlPlane",
APIVersion: "controlplane.cluster.x-k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Expand All @@ -120,7 +121,8 @@ func Test_ObjectPauser(t *testing.T) {
objs: []client.Object{
&controlplanev1.KubeadmControlPlane{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmControlPlane",
Kind: "KubeadmControlPlane",
APIVersion: "controlplane.cluster.x-k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Expand Down
Loading

0 comments on commit ff0507e

Please sign in to comment.