|
| 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 lifecycle contains the handlers for the lifecycle hooks. |
| 18 | +package lifecycle |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "strconv" |
| 24 | + |
| 25 | + "github.com/pkg/errors" |
| 26 | + "gopkg.in/yaml.v2" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | + ctrl "sigs.k8s.io/controller-runtime" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + |
| 32 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 33 | + runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog" |
| 34 | +) |
| 35 | + |
| 36 | +// Handler is the handler for the lifecycle hooks. |
| 37 | +type Handler struct { |
| 38 | + Client client.Client |
| 39 | +} |
| 40 | + |
| 41 | +// DoBeforeClusterCreate implements the BeforeClusterCreate hook. |
| 42 | +func (h *Handler) DoBeforeClusterCreate(ctx context.Context, request *runtimehooksv1.BeforeClusterCreateRequest, response *runtimehooksv1.BeforeClusterCreateResponse) { |
| 43 | + log := ctrl.LoggerFrom(ctx) |
| 44 | + log.Info("BeforeClusterCreate is called") |
| 45 | + cluster := request.Cluster |
| 46 | + if err := h.recordCallInConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.BeforeClusterCreate); err != nil { |
| 47 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 48 | + response.Message = err.Error() |
| 49 | + return |
| 50 | + } |
| 51 | + log.Info("BeforeClusterCreate has been recorded in configmap", "cm", cluster.Name+"-hookresponses") |
| 52 | + |
| 53 | + err := h.readResponseFromConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.BeforeClusterCreate, response) |
| 54 | + if err != nil { |
| 55 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 56 | + response.Message = err.Error() |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + log.Info(fmt.Sprintf("BeforeClusterCreate responding RetryAfterSeconds: %v\n", response.RetryAfterSeconds)) |
| 61 | +} |
| 62 | + |
| 63 | +// DoBeforeClusterUpgrade implements the BeforeClusterUpgrade hook. |
| 64 | +func (h *Handler) DoBeforeClusterUpgrade(ctx context.Context, request *runtimehooksv1.BeforeClusterUpgradeRequest, response *runtimehooksv1.BeforeClusterUpgradeResponse) { |
| 65 | + log := ctrl.LoggerFrom(ctx) |
| 66 | + log.Info("BeforeClusterUpgrade is called") |
| 67 | + cluster := request.Cluster |
| 68 | + if err := h.recordCallInConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.BeforeClusterUpgrade); err != nil { |
| 69 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 70 | + response.Message = err.Error() |
| 71 | + return |
| 72 | + } |
| 73 | + err := h.readResponseFromConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.BeforeClusterUpgrade, response) |
| 74 | + if err != nil { |
| 75 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 76 | + response.Message = err.Error() |
| 77 | + return |
| 78 | + } |
| 79 | + log.Info(fmt.Sprintf("BeforeClusterUpgrade responding RetryAfterSeconds: %v\n", response.RetryAfterSeconds)) |
| 80 | +} |
| 81 | + |
| 82 | +// DoAfterControlPlaneInitialized implements the AfterControlPlaneInitialized hook. |
| 83 | +func (h *Handler) DoAfterControlPlaneInitialized(ctx context.Context, request *runtimehooksv1.AfterControlPlaneInitializedRequest, response *runtimehooksv1.AfterControlPlaneInitializedResponse) { |
| 84 | + log := ctrl.LoggerFrom(ctx) |
| 85 | + log.Info("AfterControlPlaneInitialized is called") |
| 86 | + cluster := request.Cluster |
| 87 | + if err := h.recordCallInConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.AfterControlPlaneInitialized); err != nil { |
| 88 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 89 | + response.Message = err.Error() |
| 90 | + return |
| 91 | + } |
| 92 | + err := h.readResponseFromConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.AfterControlPlaneInitialized, response) |
| 93 | + if err != nil { |
| 94 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 95 | + response.Message = err.Error() |
| 96 | + return |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// DoAfterControlPlaneUpgrade implements the AfterControlPlaneUpgrade hook. |
| 101 | +func (h *Handler) DoAfterControlPlaneUpgrade(ctx context.Context, request *runtimehooksv1.AfterControlPlaneUpgradeRequest, response *runtimehooksv1.AfterControlPlaneUpgradeResponse) { |
| 102 | + log := ctrl.LoggerFrom(ctx) |
| 103 | + log.Info("AfterControlPlaneUpgrade is called") |
| 104 | + cluster := request.Cluster |
| 105 | + if err := h.recordCallInConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.AfterControlPlaneUpgrade); err != nil { |
| 106 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 107 | + response.Message = err.Error() |
| 108 | + return |
| 109 | + } |
| 110 | + err := h.readResponseFromConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.AfterControlPlaneUpgrade, response) |
| 111 | + if err != nil { |
| 112 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 113 | + response.Message = err.Error() |
| 114 | + return |
| 115 | + } |
| 116 | + log.Info(fmt.Sprintf("AfterControlPlaneUpgrade responding RetryAfterSeconds: %v\n", response.RetryAfterSeconds)) |
| 117 | +} |
| 118 | + |
| 119 | +// DoAfterClusterUpgrade implements the AfterClusterUpgrade hook. |
| 120 | +func (h *Handler) DoAfterClusterUpgrade(ctx context.Context, request *runtimehooksv1.AfterClusterUpgradeRequest, response *runtimehooksv1.AfterClusterUpgradeResponse) { |
| 121 | + log := ctrl.LoggerFrom(ctx) |
| 122 | + log.Info("AfterClusterUpgrade is called") |
| 123 | + cluster := request.Cluster |
| 124 | + if err := h.recordCallInConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.AfterClusterUpgrade); err != nil { |
| 125 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 126 | + response.Message = err.Error() |
| 127 | + return |
| 128 | + } |
| 129 | + err := h.readResponseFromConfigMap(cluster.Name, cluster.Namespace, runtimehooksv1.AfterClusterUpgrade, response) |
| 130 | + if err != nil { |
| 131 | + response.Status = runtimehooksv1.ResponseStatusFailure |
| 132 | + response.Message = err.Error() |
| 133 | + return |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func (h *Handler) readResponseFromConfigMap(name, namespace string, hook runtimecatalog.Hook, response runtimehooksv1.ResponseObject) error { |
| 138 | + hookName := runtimecatalog.HookName(hook) |
| 139 | + configMap := &corev1.ConfigMap{} |
| 140 | + configMapName := name + "-hookresponses" |
| 141 | + if err := h.Client.Get(context.Background(), client.ObjectKey{Namespace: namespace, Name: configMapName}, configMap); err != nil { |
| 142 | + return errors.Wrapf(err, "failed to read the ConfigMap %s/%s", namespace, configMapName) |
| 143 | + } |
| 144 | + m := map[string]string{} |
| 145 | + if err := yaml.Unmarshal([]byte(configMap.Data[hookName+"-response"]), m); err != nil { |
| 146 | + return errors.Wrapf(err, "failed to read %q response information from ConfigMap", hook) |
| 147 | + } |
| 148 | + // If the response was a success return early. |
| 149 | + if status, ok := m["Status"]; ok && status == string(runtimehooksv1.ResponseStatusSuccess) { |
| 150 | + response.SetStatus(runtimehooksv1.ResponseStatusSuccess) |
| 151 | + return nil |
| 152 | + } |
| 153 | + if _, ok := m["Message"]; ok { |
| 154 | + response.SetMessage(m["Message"]) |
| 155 | + } |
| 156 | + if retryResponse, ok := response.(runtimehooksv1.RetryResponseObject); ok { |
| 157 | + retryAfterSeconds, err := strconv.Atoi(m["RetryAfterSeconds"]) //nolint:gosec |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + retryResponse.SetRetryAfterSeconds(int32(retryAfterSeconds)) |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func (h *Handler) recordCallInConfigMap(name, namespace string, hook runtimecatalog.Hook) error { |
| 167 | + hookName := runtimecatalog.HookName(hook) |
| 168 | + configMap := &corev1.ConfigMap{} |
| 169 | + configMapName := name + "-hookresponses" |
| 170 | + if err := h.Client.Get(context.Background(), client.ObjectKey{Namespace: namespace, Name: configMapName}, configMap); err != nil { |
| 171 | + return errors.Wrapf(err, "failed to read the ConfigMap %s/%s", namespace, configMapName) |
| 172 | + } |
| 173 | + |
| 174 | + patch := client.RawPatch(types.MergePatchType, |
| 175 | + []byte(fmt.Sprintf("{\"data\":{\"%s-called\":\"true\"}}", hookName))) |
| 176 | + if err := h.Client.Patch(context.Background(), configMap, patch); err != nil { |
| 177 | + return errors.Wrapf(err, "failed to update the ConfigMap %s/%s", namespace, configMapName) |
| 178 | + } |
| 179 | + return nil |
| 180 | +} |
0 commit comments