|
| 1 | +package basic_authenticator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "github.com/opdev/subreconciler" |
| 7 | + "github.com/snapp-incubator/simple-authenticator/api/v1alpha1" |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + v1 "k8s.io/api/core/v1" |
| 10 | + "k8s.io/apimachinery/pkg/labels" |
| 11 | + ctrl "sigs.k8s.io/controller-runtime" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 14 | +) |
| 15 | + |
| 16 | +func (r *BasicAuthenticatorReconciler) Cleanup(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 17 | + // Do the actual reconcile work |
| 18 | + subRecs := []subreconciler.FnWithRequest{ |
| 19 | + r.setDeletionStatus, |
| 20 | + r.removeInjectedContainers, |
| 21 | + r.removeCleanupFinalizer, |
| 22 | + } |
| 23 | + for _, rec := range subRecs { |
| 24 | + result, err := rec(ctx, req) |
| 25 | + if subreconciler.ShouldHaltOrRequeue(result, err) { |
| 26 | + return subreconciler.Evaluate(result, err) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return subreconciler.Evaluate(subreconciler.DoNotRequeue()) |
| 31 | +} |
| 32 | +func (r *BasicAuthenticatorReconciler) setDeletionStatus(ctx context.Context, req ctrl.Request) (*ctrl.Result, error) { |
| 33 | + basicAuthenticator := &v1alpha1.BasicAuthenticator{} |
| 34 | + if r, err := r.getLatestBasicAuthenticator(ctx, req, basicAuthenticator); subreconciler.ShouldHaltOrRequeue(r, err) { |
| 35 | + return subreconciler.RequeueWithError(err) |
| 36 | + } |
| 37 | + basicAuthenticator.Status.State = StatusDeleting |
| 38 | + |
| 39 | + if err := r.Update(ctx, basicAuthenticator); err != nil { |
| 40 | + r.logger.Error(err, "Failed to update status while cleaning") |
| 41 | + return subreconciler.RequeueWithError(err) |
| 42 | + } |
| 43 | + return subreconciler.ContinueReconciling() |
| 44 | +} |
| 45 | +func (r *BasicAuthenticatorReconciler) removeInjectedContainers(ctx context.Context, req ctrl.Request) (*ctrl.Result, error) { |
| 46 | + basicAuthenticator := &v1alpha1.BasicAuthenticator{} |
| 47 | + |
| 48 | + if r, err := r.getLatestBasicAuthenticator(ctx, req, basicAuthenticator); subreconciler.ShouldHaltOrRequeue(r, err) { |
| 49 | + return subreconciler.RequeueWithError(err) |
| 50 | + } |
| 51 | + |
| 52 | + if basicAuthenticator.Spec.Type != "sidecar" { |
| 53 | + return subreconciler.ContinueReconciling() |
| 54 | + } |
| 55 | + basicAuthLabel := map[string]string{ |
| 56 | + basicAuthenticatorNameLabel: basicAuthenticator.Name, |
| 57 | + } |
| 58 | + deployments, err := getTargetDeployment(ctx, basicAuthenticator, r.Client, basicAuthLabel) |
| 59 | + if err != nil { |
| 60 | + r.logger.Error(err, "failed to get target deployments to clean up") |
| 61 | + return subreconciler.RequeueWithError(err) |
| 62 | + } |
| 63 | + configmaps, err := getTargetConfigmapNames(ctx, basicAuthenticator, r.Client, basicAuthLabel) |
| 64 | + if err != nil { |
| 65 | + r.logger.Error(err, "failed to get target configmap to clean up") |
| 66 | + return subreconciler.RequeueWithError(err) |
| 67 | + } |
| 68 | + secrets, err := getTargetSecretName(ctx, basicAuthenticator, r.Client, basicAuthLabel) |
| 69 | + if err != nil { |
| 70 | + r.logger.Error(err, "failed to get target secret to clean up") |
| 71 | + return subreconciler.RequeueWithError(err) |
| 72 | + } |
| 73 | + r.logger.Info("debug", "configmap", configmaps, "secret", secrets) |
| 74 | + |
| 75 | + cleanupDeployments := removeInjectedResources(deployments, secrets, configmaps) |
| 76 | + for _, deploy := range cleanupDeployments { |
| 77 | + if err := r.Update(ctx, deploy); err != nil { |
| 78 | + r.logger.Error(err, "failed to add update cleaned up deployments") |
| 79 | + return subreconciler.RequeueWithError(err) |
| 80 | + } |
| 81 | + } |
| 82 | + return subreconciler.ContinueReconciling() |
| 83 | +} |
| 84 | +func (r *BasicAuthenticatorReconciler) removeCleanupFinalizer(ctx context.Context, req ctrl.Request) (*ctrl.Result, error) { |
| 85 | + basicAuthenticator := &v1alpha1.BasicAuthenticator{} |
| 86 | + if r, err := r.getLatestBasicAuthenticator(ctx, req, basicAuthenticator); subreconciler.ShouldHaltOrRequeue(r, err) { |
| 87 | + return subreconciler.RequeueWithError(err) |
| 88 | + } |
| 89 | + if controllerutil.ContainsFinalizer(basicAuthenticator, basicAuthenticatorFinalizer) { |
| 90 | + if ok := controllerutil.RemoveFinalizer(basicAuthenticator, basicAuthenticatorFinalizer); !ok { |
| 91 | + r.logger.Error(errors.New("finalizer not updated"), "Failed to remove finalizer for BasicAuthenticator") |
| 92 | + return subreconciler.Requeue() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if err := r.Update(ctx, basicAuthenticator); err != nil { |
| 97 | + r.logger.Error(err, "Failed to remove finalizer for BasicAuthenticator") |
| 98 | + return subreconciler.RequeueWithError(err) |
| 99 | + } |
| 100 | + return subreconciler.ContinueReconciling() |
| 101 | +} |
| 102 | + |
| 103 | +func getTargetDeployment(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, k8Client client.Client, basicAuthLabels map[string]string) ([]*appsv1.Deployment, error) { |
| 104 | + var deploymentList appsv1.DeploymentList |
| 105 | + if err := k8Client.List( |
| 106 | + ctx, |
| 107 | + &deploymentList, |
| 108 | + client.MatchingLabelsSelector{Selector: labels.SelectorFromSet(basicAuthLabels)}, |
| 109 | + client.InNamespace(basicAuthenticator.Namespace)); err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + resultDeployments := make([]*appsv1.Deployment, 0) |
| 114 | + for _, deploy := range deploymentList.Items { |
| 115 | + resultDeployments = append(resultDeployments, &deploy) |
| 116 | + } |
| 117 | + return resultDeployments, nil |
| 118 | +} |
| 119 | +func getTargetConfigmapNames(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, k8Client client.Client, basicAuthLabels map[string]string) ([]string, error) { |
| 120 | + var configMapList v1.ConfigMapList |
| 121 | + if err := k8Client.List( |
| 122 | + ctx, |
| 123 | + &configMapList, |
| 124 | + client.MatchingLabelsSelector{Selector: labels.SelectorFromSet(basicAuthLabels)}, |
| 125 | + client.InNamespace(basicAuthenticator.Namespace)); err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + resultConfigMaps := make([]string, 0) |
| 129 | + for _, cm := range configMapList.Items { |
| 130 | + resultConfigMaps = append(resultConfigMaps, cm.Name) |
| 131 | + } |
| 132 | + return resultConfigMaps, nil |
| 133 | +} |
| 134 | +func getTargetSecretName(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator, k8Client client.Client, basicAuthLabels map[string]string) ([]string, error) { |
| 135 | + var secretList v1.SecretList |
| 136 | + if err := k8Client.List( |
| 137 | + ctx, |
| 138 | + &secretList, |
| 139 | + client.MatchingLabelsSelector{Selector: labels.SelectorFromSet(basicAuthLabels)}, |
| 140 | + client.InNamespace(basicAuthenticator.Namespace)); err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + resultSecrets := make([]string, 0) |
| 145 | + for _, sec := range secretList.Items { |
| 146 | + resultSecrets = append(resultSecrets, sec.Name) |
| 147 | + } |
| 148 | + return resultSecrets, nil |
| 149 | +} |
| 150 | +func removeInjectedResources(deployments []*appsv1.Deployment, secrets []string, configmap []string) []*appsv1.Deployment { |
| 151 | + for _, deploy := range deployments { |
| 152 | + containers := make([]v1.Container, 0) |
| 153 | + for _, container := range deploy.Spec.Template.Spec.Containers { |
| 154 | + if container.Name != nginxDefaultContainerName { |
| 155 | + containers = append(containers, container) |
| 156 | + } |
| 157 | + } |
| 158 | + deploy.Spec.Template.Spec.Containers = containers |
| 159 | + volumes := make([]v1.Volume, 0) |
| 160 | + for _, vol := range deploy.Spec.Template.Spec.Volumes { |
| 161 | + if !existsInList(secrets, vol.Name) && !existsInList(configmap, vol.Name) { |
| 162 | + volumes = append(volumes, vol) |
| 163 | + } |
| 164 | + } |
| 165 | + deploy.Spec.Template.Spec.Volumes = volumes |
| 166 | + if deploy.Labels != nil { |
| 167 | + delete(deploy.Labels, basicAuthenticatorNameLabel) |
| 168 | + } |
| 169 | + } |
| 170 | + return deployments |
| 171 | +} |
| 172 | + |
| 173 | +func existsInList(strList []string, targetStr string) bool { |
| 174 | + for _, val := range strList { |
| 175 | + if val == targetStr { |
| 176 | + return true |
| 177 | + } |
| 178 | + } |
| 179 | + return false |
| 180 | +} |
0 commit comments