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

     1  /*
     2  Copyright 2022 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  	"time"
    23  
    24  	"github.com/pkg/errors"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
    29  	controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
    30  )
    31  
    32  // getKubeadmControlPlane retrieves the KubeadmControlPlane object corresponding to the name and namespace specified.
    33  func getKubeadmControlPlane(ctx context.Context, proxy cluster.Proxy, name, namespace string) (*controlplanev1.KubeadmControlPlane, error) {
    34  	kcpObj := &controlplanev1.KubeadmControlPlane{}
    35  	c, err := proxy.NewClient(ctx)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	kcpObjKey := client.ObjectKey{
    40  		Namespace: namespace,
    41  		Name:      name,
    42  	}
    43  	if err := c.Get(ctx, kcpObjKey, kcpObj); err != nil {
    44  		return nil, errors.Wrapf(err, "failed to get KubeadmControlPlane %s/%s",
    45  			kcpObjKey.Namespace, kcpObjKey.Name)
    46  	}
    47  	return kcpObj, nil
    48  }
    49  
    50  // setRolloutAfterOnKCP sets KubeadmControlPlane.spec.rolloutAfter.
    51  func setRolloutAfterOnKCP(ctx context.Context, proxy cluster.Proxy, name, namespace string) error {
    52  	patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf(`{"spec":{"rolloutAfter":"%v"}}`, time.Now().Format(time.RFC3339))))
    53  	return patchKubeadmControlPlane(ctx, proxy, name, namespace, patch)
    54  }
    55  
    56  // patchKubeadmControlPlane applies a patch to a KubeadmControlPlane.
    57  func patchKubeadmControlPlane(ctx context.Context, proxy cluster.Proxy, name, namespace string, patch client.Patch) error {
    58  	cFrom, err := proxy.NewClient(ctx)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	kcpObj := &controlplanev1.KubeadmControlPlane{}
    63  	kcpObjKey := client.ObjectKey{
    64  		Namespace: namespace,
    65  		Name:      name,
    66  	}
    67  	if err := cFrom.Get(ctx, kcpObjKey, kcpObj); err != nil {
    68  		return errors.Wrapf(err, "failed to get KubeadmControlPlane %s/%s", kcpObj.GetNamespace(), kcpObj.GetName())
    69  	}
    70  
    71  	if err := cFrom.Patch(ctx, kcpObj, patch); err != nil {
    72  		return errors.Wrapf(err, "failed while patching KubeadmControlPlane %s/%s", kcpObj.GetNamespace(), kcpObj.GetName())
    73  	}
    74  	return nil
    75  }