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