sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/alpha/rollout_resumer.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package alpha
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    30  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
    31  	"sigs.k8s.io/cluster-api/util/annotations"
    32  )
    33  
    34  // ObjectResumer will issue a resume on the specified cluster-api resource.
    35  func (r *rollout) ObjectResumer(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference) error {
    36  	switch ref.Kind {
    37  	case MachineDeployment:
    38  		deployment, err := getMachineDeployment(ctx, proxy, ref.Name, ref.Namespace)
    39  		if err != nil || deployment == nil {
    40  			return errors.Wrapf(err, "failed to fetch %v/%v", ref.Kind, ref.Name)
    41  		}
    42  		if !deployment.Spec.Paused {
    43  			return errors.Errorf("MachineDeployment is not currently paused: %v/%v\n", ref.Kind, ref.Name) //nolint:revive // MachineDeployment is intentionally capitalized.
    44  		}
    45  		if err := resumeMachineDeployment(ctx, proxy, ref.Name, ref.Namespace); err != nil {
    46  			return err
    47  		}
    48  	case KubeadmControlPlane:
    49  		kcp, err := getKubeadmControlPlane(ctx, proxy, ref.Name, ref.Namespace)
    50  		if err != nil || kcp == nil {
    51  			return errors.Wrapf(err, "failed to fetch %v/%v", ref.Kind, ref.Name)
    52  		}
    53  		if !annotations.HasPaused(kcp.GetObjectMeta()) {
    54  			return errors.Errorf("KubeadmControlPlane is not currently paused: %v/%v\n", ref.Kind, ref.Name) //nolint:revive // KubeadmControlPlane is intentionally capitalized.
    55  		}
    56  		if err := resumeKubeadmControlPlane(ctx, proxy, ref.Name, ref.Namespace); err != nil {
    57  			return err
    58  		}
    59  	default:
    60  		return errors.Errorf("invalid resource type %q, valid values are %v", ref.Kind, validResourceTypes)
    61  	}
    62  	return nil
    63  }
    64  
    65  // resumeMachineDeployment sets Paused to true in the MachineDeployment's spec.
    66  func resumeMachineDeployment(ctx context.Context, proxy cluster.Proxy, name, namespace string) error {
    67  	patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%t}}", false)))
    68  
    69  	return patchMachineDeployment(ctx, proxy, name, namespace, patch)
    70  }
    71  
    72  // resumeKubeadmControlPlane removes paused annotation.
    73  func resumeKubeadmControlPlane(ctx context.Context, proxy cluster.Proxy, name, namespace string) error {
    74  	// In the paused annotation we must replace slashes to ~1, see https://datatracker.ietf.org/doc/html/rfc6901#section-3.
    75  	pausedAnnotation := strings.Replace(clusterv1.PausedAnnotation, "/", "~1", -1)
    76  	patch := client.RawPatch(types.JSONPatchType, []byte(fmt.Sprintf("[{\"op\": \"remove\", \"path\": \"/metadata/annotations/%s\"}]", pausedAnnotation)))
    77  
    78  	return patchKubeadmControlPlane(ctx, proxy, name, namespace, patch)
    79  }