Configure configures mutate/validating webhooks that provides exec access into workspace for creator only
(ctx context.Context, mgr manager.Manager)
| 37 | |
| 38 | // Configure configures mutate/validating webhooks that provides exec access into workspace for creator only |
| 39 | func Configure(ctx context.Context, mgr manager.Manager) error { |
| 40 | log.Info("Configuring devworkspace webhooks") |
| 41 | c, err := createClient() |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | namespace, err := infrastructure.GetOperatorNamespace() |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | mutateWebhookCfg := BuildMutateWebhookCfg(namespace) |
| 52 | validateWebhookCfg := buildValidatingWebhookCfg(namespace) |
| 53 | |
| 54 | if !server.IsSetUp() { |
| 55 | _, mutatingWebhookErr := getMutatingWebhook(ctx, c, mutateWebhookCfg) |
| 56 | _, validatingWebhookErr := getValidateWebhook(ctx, c, validateWebhookCfg) |
| 57 | |
| 58 | // No errors from either means that they are on the cluster |
| 59 | if mutatingWebhookErr == nil || validatingWebhookErr == nil { |
| 60 | return errors.New(`Webhooks have previously been set up and cannot be removed automatically. Configure the controller to use webhooks again or make sure that all workspaces are stopped, webhooks configuration are removed and then restart the controller`) |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | saUID, saName, err := controllerSAUID(ctx, c) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | if err := c.Create(ctx, mutateWebhookCfg); err != nil { |
| 71 | if !apierrors.IsAlreadyExists(err) { |
| 72 | return err |
| 73 | } |
| 74 | // Webhook Configuration already exists, we want to update it |
| 75 | // as we do not know if any fields might have changed. |
| 76 | existingCfg, err := getMutatingWebhook(ctx, c, mutateWebhookCfg) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | mutateWebhookCfg.ResourceVersion = existingCfg.ResourceVersion |
| 82 | err = c.Update(ctx, mutateWebhookCfg) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | log.Info("Updated devworkspace mutating webhook configuration") |
| 87 | } else { |
| 88 | log.Info("Created devworkspace mutating webhook configuration") |
| 89 | } |
| 90 | |
| 91 | server.GetWebhookServer().Register(mutateWebhookPath, &webhook.Admission{Handler: NewResourcesMutator(saUID, saName, mgr)}) |
| 92 | |
| 93 | if err := c.Create(ctx, validateWebhookCfg); err != nil { |
| 94 | if !apierrors.IsAlreadyExists(err) { |
| 95 | return err |
| 96 | } |
no test coverage detected