Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/mutating-webhook-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Mutating Webhook Configuration

The cluster operator works with the api-manager to create a mutating webhook
admission controller that modifies Pods and PVCs when they are created.

The cluster operator creates the mutating webhook configuration, the service,
and the RBAC required by the api-manager.

The api-manager runs the webhook server and handles certificate signing and
rotation.

See https://github.com/storageos/api-manager/blob/master/README.md for more
information.

## Deployment

The admission controller is enabled in the default installation of the operator.
37 changes: 0 additions & 37 deletions docs/pod-scheduler-mutating-admission-controller.md

This file was deleted.

78 changes: 48 additions & 30 deletions pkg/storageos/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ const (
webhookPort int32 = 443
webhookTargetPort int32 = 9443

// webhookPodMutatorName is the name of the webhook configuration
mutatePodPath = "/mutate-pods"
mutatePVCPath = "/mutate-pvcs"

// webhookPodMutatorName and webhookPVCMutatorName are the names of the
// webhooks.
webhookPodMutatorName = "pod-mutator.storageos.com"
webhookPVCMutatorName = "pvc-mutator.storageos.com"
)

// createWebhookConfiguration creates the webhook service and configuration.
Expand Down Expand Up @@ -60,47 +65,60 @@ func (s Deployment) createWebhookService() error {
// webhooks. The configuration will be updated by the api-manager to include
// the CA cert bundle.
func (s Deployment) createMutatingWebhookConfiguration() error {
failurePolicy := admissionv1.Ignore
matchPolicy := admissionv1.Exact
sideEffects := admissionv1.SideEffectClassNoneOnDryRun
svcPort := webhookPort
scopeAll := admissionv1.AllScopes
path := "/mutate-pods"

webhooks := []admissionv1.MutatingWebhook{
{
Name: webhookPodMutatorName,
ClientConfig: admissionv1.WebhookClientConfig{
Service: &admissionv1.ServiceReference{
Name: WebhookServiceName,
Namespace: s.stos.Spec.GetResourceNS(),
Port: &svcPort,
Path: &path,
s.mutatingWebhookConfiguration(webhookPodMutatorName, mutatePodPath, admissionv1.Ignore, []admissionv1.RuleWithOperations{
{
Operations: []admissionv1.OperationType{admissionv1.Create},
Rule: admissionv1.Rule{
APIGroups: []string{""},
APIVersions: []string{"v1"},
Resources: []string{"pods"},
Scope: &scopeAll,
},
},
Rules: []admissionv1.RuleWithOperations{
{
Operations: []admissionv1.OperationType{admissionv1.Create},
Rule: admissionv1.Rule{
APIGroups: []string{""},
APIVersions: []string{"v1"},
Resources: []string{"pods"},
Scope: &scopeAll,
},
}),
s.mutatingWebhookConfiguration(webhookPVCMutatorName, mutatePVCPath, admissionv1.Ignore, []admissionv1.RuleWithOperations{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we decide to make the failure policy to fail for PVC mutating webhooks? storageos/api-manager#42 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I just wanted to enable it separately after doing more testing: #318 (comment)

{
Operations: []admissionv1.OperationType{admissionv1.Create},
Rule: admissionv1.Rule{
APIGroups: []string{""},
APIVersions: []string{"v1"},
Resources: []string{"persistentvolumeclaims"},
Scope: &scopeAll,
},
},
FailurePolicy: &failurePolicy,
MatchPolicy: &matchPolicy,
NamespaceSelector: &metav1.LabelSelector{},
SideEffects: &sideEffects,
AdmissionReviewVersions: []string{"v1"},
},
}),
}
labels := podLabelsForAPIManager(s.stos.Name)

return s.k8sResourceManager.MutatingWebhookConfiguration(MutatingWebhookConfigName, labels, webhooks).Create()
}

func (s Deployment) mutatingWebhookConfiguration(name string, path string, failurePolicy admissionv1.FailurePolicyType, rules []admissionv1.RuleWithOperations) admissionv1.MutatingWebhook {
matchPolicy := admissionv1.Equivalent
sideEffects := admissionv1.SideEffectClassNoneOnDryRun
port := webhookPort

return admissionv1.MutatingWebhook{
Name: name,
ClientConfig: admissionv1.WebhookClientConfig{
Service: &admissionv1.ServiceReference{
Name: WebhookServiceName,
Namespace: s.stos.Spec.GetResourceNS(),
Port: &port,
Path: &path,
},
},
Rules: rules,
FailurePolicy: &failurePolicy,
MatchPolicy: &matchPolicy,
NamespaceSelector: &metav1.LabelSelector{},
SideEffects: &sideEffects,
AdmissionReviewVersions: []string{"v1"},
}
}

// deleteWebhookConfiguration deletes the webhook service and configuration.
func (s Deployment) deleteWebhookConfiguration() error {
if err := s.k8sResourceManager.MutatingWebhookConfiguration(MutatingWebhookConfigName, nil, nil).Delete(); err != nil {
Expand Down