Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Allow multiple mutators for a single admission controller - #41

Merged
croomes merged 1 commit into
masterfrom
mutators
Apr 14, 2021
Merged

croomes merged 1 commit into
masterfrom
mutators

Conversation

@croomes

@croomes croomes commented Apr 9, 2021

Copy link
Copy Markdown
Contributor

This allows an admission controller to run multiple mutators for different tasks.

For Pod creation, this could be to add the StorageOS scheduler and to set a failure mode or failure mode label that should be applied to all of the Pod's PVCs.

This pattern will be used for PVC create where we will have separate mutators for encryption and labels.

@croomes
croomes requested review from avestuk and darkowlzz April 9, 2021 12:47
// No error in parsing and the value is false, skip the pod.
if err == nil && !boolVal {
if err == nil && !enabled {
log.V(4).Info("pod has storageos.com/scheduler=false annotaion, skipping")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
log.V(4).Info("pod has storageos.com/scheduler=false annotaion, skipping")
log.V(4).Infof("pod has %s=false annotaion, skipping", p.SchedulerAnnotationKey)

Does it make sense to use the const you have defined already? I suppose there's not much chance of it changing. Really I'm just curious about whether you chose to not use the const.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, it probably does. With logr they recommend against vars in the message and encourage k=v pairs instead, but that doesn't work well here. Changed.

Comment thread controllers/pod-mutator/controller.go Outdated
namespace := req.AdmissionRequest.Namespace

// Create a copy of the pod to mutate.
copy := pod.DeepCopy()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do you have to create a copy of the pod as opposed to just modifying the pod object directly and sending that back?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code is based on https://github.com/storageos/cluster-operator/blob/v2.3.4/internal/pkg/admission/scheduler/handler.go#L64 where we used to pass the pod object to PatchResponse at the very end of the function to return a response. In the latest webhook package of controller runtime, PatchResponse is replaced with PatchResponseFromRaw which accepts the raw object in the request. So, we avoid doing a deep copy here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the DeepCopy()

Comment thread controllers/pod-mutator/README.md Outdated

## Disabling

It is not possible to disable the Webhook server. Instead, disable thegla

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thegla looks a typo here.

Pods that have the annotation `storageos.com/scheduler=false` set are ignored.

The Pod's volumes are enumerated, and if at least one volume is backed by
StorageOS, the SchedulerName will be changed to use the StorageOS Pod scheduler.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SchedulerName should be highlighted

Comment thread README.md Outdated
The Pod Mutator is a mutating admission controller that modifies Pods during the
create process.

See [Namespace Delete Controller](controllers/pod-mutator/README.md) for

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
See [Namespace Delete Controller](controllers/pod-mutator/README.md) for
See [Pod Mutator Admission Controller](controllers/pod-mutator/README.md) for

Comment thread controllers/pod-mutator/controller.go Outdated
namespace := req.AdmissionRequest.Namespace

// Create a copy of the pod to mutate.
copy := pod.DeepCopy()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code is based on https://github.com/storageos/cluster-operator/blob/v2.3.4/internal/pkg/admission/scheduler/handler.go#L64 where we used to pass the pod object to PatchResponse at the very end of the function to return a response. In the latest webhook package of controller runtime, PatchResponse is replaced with PatchResponseFromRaw which accepts the raw object in the request. So, we avoid doing a deep copy here.

`-scheduler-name` is the name of the scheduler extender to set in the Pod's
`SchedulerName` field. If set to an empty string, no updates will be made. When
set, the scheduler name must match the name of the scheduler extender configured
in the StorageOS cluster-operator. (default "storageos-scheduler)".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
in the StorageOS cluster-operator. (default "storageos-scheduler)".
in the StorageOS cluster-operator. (default "storageos-scheduler").

// MutatePod mutates a given pod with a configured scheduler name if the pod is
// associated with volumes managed by the configured provisioners.
//
// Errors returned here will block creation of the Pod.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

May not be accurate. It depends on the webhook configuration if the failure is ignored or respected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍 Changed wording

@darkowlzz

Copy link
Copy Markdown
Contributor

The implementation looks good to me. Left some inline comments about minor improvements.

@croomes
croomes requested review from darkowlzz and mhmxs April 13, 2021 16:45
@@ -0,0 +1,45 @@
package controllers

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this way test related codebase looks like production code. I suggest to refactor the code to ensure test functions are only available in tests. If it is not possible a common solution to create a controllertest package for separation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, my bad, this package contains only tests :D

obj.Annotations[m.key] = m.value
return nil
}
func (m testMutator) MutatePVC(ctx context.Context, obj *corev1.PersistentVolumeClaim, namespace string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing new line :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, thanks

@darkowlzz darkowlzz left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Other than the remaining minor comments and squashing the commits, this LGTM!
Thanks.

@croomes
croomes force-pushed the mutators branch 2 times, most recently from a77bd31 to 1a13a94 Compare April 14, 2021 14:46
@croomes
croomes merged commit 5e127ae into master Apr 14, 2021
@croomes
croomes deleted the mutators branch April 14, 2021 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants