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