sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/alpha/rollout_pauser.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  
    23  	"github.com/pkg/errors"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    29  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
    30  	"sigs.k8s.io/cluster-api/util/annotations"
    31  )
    32  
    33  // ObjectPauser will issue a pause on the specified cluster-api resource.
    34  func (r *rollout) ObjectPauser(ctx context.Context, proxy cluster.Proxy, ref corev1.ObjectReference) error {
    35  	switch ref.Kind {
    36  	case MachineDeployment:
    37  		deployment, err := getMachineDeployment(ctx, proxy, ref.Name, ref.Namespace)
    38  		if err != nil || deployment == nil {
    39  			return errors.Wrapf(err, "failed to fetch %v/%v", ref.Kind, ref.Name)
    40  		}
    41  		if deployment.Spec.Paused {
    42  			return errors.Errorf("MachineDeployment is already paused: %v/%v\n", ref.Kind, ref.Name) //nolint:revive // MachineDeployment is intentionally capitalized.
    43  		}
    44  		if err := pauseMachineDeployment(ctx, proxy, ref.Name, ref.Namespace); err != nil {
    45  			return err
    46  		}
    47  	case KubeadmControlPlane:
    48  		kcp, err := getKubeadmControlPlane(ctx, proxy, ref.Name, ref.Namespace)
    49  		if err != nil || kcp == nil {
    50  			return errors.Wrapf(err, "failed to fetch %v/%v", ref.Kind, ref.Name)
    51  		}
    52  		if annotations.HasPaused(kcp.GetObjectMeta()) {
    53  			return errors.Errorf("KubeadmControlPlane is already paused: %v/%v\n", ref.Kind, ref.Name) //nolint:revive // KubeadmControlPlane is intentionally capitalized.
    54  		}
    55  		if err := pauseKubeadmControlPlane(ctx, proxy, ref.Name, ref.Namespace); err != nil {
    56  			return err
    57  		}
    58  	default:
    59  		return errors.Errorf("Invalid resource type %q, valid values are %v", ref.Kind, validResourceTypes)
    60  	}
    61  	return nil
    62  }
    63  
    64  // pauseMachineDeployment sets Paused to true in the MachineDeployment's spec.
    65  func pauseMachineDeployment(ctx context.Context, proxy cluster.Proxy, name, namespace string) error {
    66  	patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%t}}", true)))
    67  	return patchMachineDeployment(ctx, proxy, name, namespace, patch)
    68  }
    69  
    70  // pauseKubeadmControlPlane sets paused annotation to true.
    71  func pauseKubeadmControlPlane(ctx context.Context, proxy cluster.Proxy, name, namespace string) error {
    72  	patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"metadata\":{\"annotations\":{%q: \"%t\"}}}", clusterv1.PausedAnnotation, true)))
    73  	return patchKubeadmControlPlane(ctx, proxy, name, namespace, patch)
    74  }