MCPcopy Create free account
hub / github.com/devfile/devworkspace-operator / Configure

Function Configure

webhook/workspace/config.go:39–117  ·  view source on GitHub ↗

Configure configures mutate/validating webhooks that provides exec access into workspace for creator only

(ctx context.Context, mgr manager.Manager)

Source from the content-addressed store, hash-verified

37
38// Configure configures mutate/validating webhooks that provides exec access into workspace for creator only
39func 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 }

Callers 1

createWebhooksFunction · 0.92

Calls 11

GetOperatorNamespaceFunction · 0.92
IsSetUpFunction · 0.92
GetWebhookServerFunction · 0.92
createClientFunction · 0.85
BuildMutateWebhookCfgFunction · 0.85
getMutatingWebhookFunction · 0.85
getValidateWebhookFunction · 0.85
controllerSAUIDFunction · 0.85
NewResourcesMutatorFunction · 0.85
NewResourcesValidatorFunction · 0.85

Tested by

no test coverage detected