This repository is currently being migrated. It's locked while the migration is in progress.
Repository navigation
Set Pod scheduler #40
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ resources: | |
|
|
||
| configurations: | ||
| - kustomizeconfig.yaml | ||
|
|
||
| namePrefix: storageos- | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
| --- | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: MutatingWebhookConfiguration | ||
| metadata: | ||
| creationTimestamp: null | ||
| name: mutating-webhook-configuration | ||
| webhooks: | ||
| - clientConfig: | ||
| caBundle: Cg== | ||
| service: | ||
| name: webhook-service | ||
| namespace: system | ||
| path: /mutate-pods | ||
| failurePolicy: Ignore | ||
| name: pod-mutator.storageos.com | ||
| rules: | ||
| - apiGroups: | ||
| - "" | ||
| apiVersions: | ||
| - v1 | ||
| operations: | ||
| - CREATE | ||
| resources: | ||
| - pods |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Pod Scheduler Admission Controller | ||
|
|
||
| This is a mutating admission controller that adds the name of the | ||
| StorageOS scheduler extender to the Pod's `SchedulerName`, if the Pod has | ||
| volumes backed by StorageOS and does not have the | ||
| `storageos.com/scheduler=false` annotation set on the Pod. | ||
|
|
||
| ## Trigger | ||
|
|
||
| All Pod create events are evaluated. | ||
|
|
||
| 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. | ||
|
|
||
| ## Disabling | ||
|
|
||
| Pod's can be skipped individually by setting the `storageos.com/scheduler=false` | ||
| annotation. | ||
|
|
||
| To disable for all Pods, the api-manager can be started with `-scheduler-name` | ||
| set to an empty string. | ||
|
|
||
| ## Webhook server | ||
|
|
||
| The Pod Scheduler admission controller runs as a webhook within api-manager. | ||
| The webhook server is shared by other admission controllers and uses a | ||
| self-signed certificate. | ||
|
|
||
| Certificates are rotated automatically and stored in a secret. Multiple | ||
| instances of the api-manager shared the same certificate and will check for | ||
| updates periodically, configured with the `-webhook-cert-refresh-interval` flag. | ||
|
|
||
| Certificates are valid for 1 year and re-issued after 6 months. The | ||
| `-webhook-cert-refresh-interval` should be kept to run frequently (default | ||
| `30m`) as restarting the api-manager will reset the refresh timer. | ||
|
|
||
| ## Tunables | ||
|
|
||
| Default values work well when the api-manager is installed by the | ||
| cluster-operator. They should only be changed under advisement from StorageOS | ||
| support. | ||
|
|
||
| `-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)". | ||
|
|
||
| `-webhook-cert-refresh-interval` determines how often the webhook server | ||
| certificate should be checked for updates. (default 30m0s). | ||
|
|
||
| `-webhook-config-mutating` is the name of the mutating webhook configuration. It | ||
| must match the configuration name set in the cluster-operator. (default | ||
| "storageos-mutating-webhook"). | ||
|
|
||
| `-webhook-mutate-pods-path` is the URL path of the Pod mutating webhook. It | ||
| must match the configuration name set in the cluster-operator. (default | ||
| "/mutate-pods"). | ||
|
|
||
| `-webhook-secret-name` Is the name of the webhook secret. (default | ||
| "storageos-webhook"). | ||
|
|
||
| `-webhook-secret-namespace` Is the namespace of the webhook secret. If unset | ||
| (recommended), it will be auto-detected and set to the namespace that | ||
| api-manager is installed into. If auto-detection is not available, it will | ||
| default to the value of `-namespace`, if set. | ||
|
|
||
| `-webhook-service-name` is the name of the webhook service. It must match the | ||
| configuration name set in the cluster-operator. (default "storageos-webhook"). | ||
|
|
||
| `-webhook-service-namespace` is the namespace of the webhook service. If unset | ||
| (recommended), it will be auto-detected and set to the namespace that | ||
| api-manager is installed into. If auto-detection is not available, it will | ||
| default to the value of `-namespace`, if set. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Package scheduler contains mutating admission controller webhook handlers for | ||
| // setting a pod's scheduler. The handler mutates a given pod if the pod has any | ||
| // volume that's managed by the given volume provisioners in | ||
| // PodSchedulerSetter.Provisioners and the associated scheduler is enabled. The | ||
| // scheduler name is set in PodSchedulerSetter.SchedulerName. | ||
| package scheduler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package scheduler | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/pkg/errors" | ||
| corev1 "k8s.io/api/core/v1" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
|
||
| "github.com/storageos/api-manager/internal/pkg/provisioner" | ||
| ) | ||
|
|
||
| const ( | ||
| // PodSchedulerAnnotationKey is the pod annotation key that can be set to | ||
| // skip pod scheduler name mutation. | ||
| PodSchedulerAnnotationKey = "storageos.com/scheduler" | ||
| ) | ||
|
|
||
| // PodSchedulerSetter is responsible for mutating and setting pod scheduler | ||
| // name. | ||
| type PodSchedulerSetter struct { | ||
| // Provisioners is a list of storage provisioners to check a pod volume | ||
| // against. | ||
| Provisioners []string | ||
|
|
||
| // SchedulerName is the name of the scheduler to mutate pods with. | ||
| SchedulerName string | ||
|
|
||
| // SchedulerAnnotationKey is the pod annotation that can be set to skip or | ||
| // apply the mutation. | ||
| SchedulerAnnotationKey string | ||
|
|
||
| client.Client | ||
| decoder *admission.Decoder | ||
| log logr.Logger | ||
| } | ||
|
|
||
| // Check if the Handler interface is implemented. | ||
| var _ admission.Handler = &PodSchedulerSetter{} | ||
|
|
||
| // +kubebuilder:webhook:path=/mutate-pods,mutating=true,failurePolicy=ignore,groups="",resources=pods,verbs=create,versions=v1,name=pod-mutator.storageos.com | ||
|
|
||
| // NewPodSchedulerSetter returns a new Pod Scheduler mutating admission | ||
| // controller. | ||
| func NewPodSchedulerSetter(k8s client.Client, decoder *admission.Decoder, scheduler string) *PodSchedulerSetter { | ||
| return &PodSchedulerSetter{ | ||
| SchedulerName: scheduler, | ||
| Provisioners: []string{provisioner.DriverName}, | ||
| SchedulerAnnotationKey: PodSchedulerAnnotationKey, | ||
| Client: k8s, | ||
| decoder: decoder, | ||
| log: ctrl.Log, | ||
| } | ||
| } | ||
|
|
||
| // Handle handles an admission request and mutates a pod object in the request. | ||
| func (p *PodSchedulerSetter) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
| pod := &corev1.Pod{} | ||
|
|
||
| err := p.decoder.Decode(req, pod) | ||
| if err != nil { | ||
| return admission.Errored(http.StatusBadRequest, err) | ||
| } | ||
|
|
||
| // Get the request namespace. This is needed because sometimes the decoded | ||
| // pod object lacks namespace info. | ||
| namespace := req.AdmissionRequest.Namespace | ||
|
|
||
| // Create a copy of the pod to mutate. | ||
| copy := pod.DeepCopy() | ||
| if err := p.mutatePodsFn(ctx, copy, namespace); err != nil { | ||
| p.log.Error(err, "failed to set pod scheduler") | ||
| return admission.Errored(http.StatusInternalServerError, err) | ||
| } | ||
|
|
||
| marshaledPod, err := json.Marshal(copy) | ||
| if err != nil { | ||
| return admission.Errored(http.StatusInternalServerError, err) | ||
| } | ||
|
|
||
| return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod) | ||
| } | ||
|
|
||
| // mutatePodFn mutates a given pod with a configured scheduler name if the pod | ||
| // is associated with volumes managed by the configured provisioners. | ||
| func (p *PodSchedulerSetter) mutatePodsFn(ctx context.Context, pod *corev1.Pod, namespace string) error { | ||
| log := p.log.WithValues("scheduler", p.SchedulerName, "pod", client.ObjectKeyFromObject(pod).String()) | ||
| log.V(4).Info("received pod for mutation") | ||
|
|
||
| // Skip processing if no scheduler configured. | ||
| if p.SchedulerName == "" { | ||
| log.V(4).Info("pod scheduler not set, skipping") | ||
| return nil | ||
| } | ||
|
|
||
| // Skip mutation if the pod annotation has false schedule annotation. | ||
| if val, exists := pod.ObjectMeta.Annotations[p.SchedulerAnnotationKey]; exists { | ||
| boolVal, err := strconv.ParseBool(val) | ||
| // No error in parsing and the value is false, skip the pod. | ||
| if err == nil && !boolVal { | ||
| log.V(4).Info("pod has storageos.com/scheduler=false annotaion, skipping") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| managedVols := []corev1.Volume{} | ||
|
|
||
| // Find all the managed volumes. | ||
| for _, vol := range pod.Spec.Volumes { | ||
| ok, err := provisioner.IsStorageOSVolume(p.Client, vol, namespace) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to determine if the volume is managed") | ||
| } | ||
| if ok { | ||
| managedVols = append(managedVols, vol) | ||
| } | ||
| } | ||
|
|
||
| // Set scheduler name only if there are managed volumes. | ||
| if len(managedVols) == 0 { | ||
| log.V(4).Info("pod does not have storageos volumes, skipping") | ||
| return nil | ||
| } | ||
|
|
||
| pod.Spec.SchedulerName = p.SchedulerName | ||
| log.Info("set scheduler") | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may have to be less specific here when we add support for cert-manager project or add another paragraph about what happens when cert-manager is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can update when we add cert-manager support.