Skip to content
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ The following flags are supported:
Paths to a kubeconfig. Only required if out-of-cluster.
-metrics-addr string
The address the metric endpoint binds to. (default ":8080")
-namespace string
Namespace that the StorageOS components, including api-manager, are installed into. Will be auto-detected if unset.
-namespace-delete-gc-delay duration
Startup delay of initial namespace garbage collection. (default 20s)
-namespace-delete-gc-interval duration
Expand All @@ -117,14 +119,30 @@ The following flags are supported:
Frequency of PVC label resync. (default 1h0m0s)
-pvc-label-sync-workers int
Maximum concurrent PVC label sync operations. (default 5)
-scheduler-name string
Name of the Pod scheduler to use for Pods with StorageOS volumes. Set to an empty value to disable setting the Pod scheduler. (default "storageos-scheduler")
-webhook-cert-refresh-interval duration
Frequency of webhook certificate refresh. (default 30m0s)
-webhook-config-mutating string
Name of the mutating webhook configuration. (default "storageos-mutating-webhook")
-webhook-mutate-pods-path string
URL path of the Pod mutating webhook. (default "/mutate-pods")
-webhook-secret-name string
Name of the webhook secret containing the certificate. (default "storageos-webhook")
-webhook-secret-namespace string
Namespace of the webhook secret. Will be auto-detected or value of -namespace if unset.
-webhook-service-name string
Name of the webhook service. (default "storageos-webhook")
-webhook-service-namespace string
Namespace of the webhook service. Will be auto-detected or value of -namespace if unset.
-zap-devel
Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
-zap-encoder value
Zap log encoding (one of 'json' or 'console')
-zap-log-level value
Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', or any integer value > 0 which corresponds to custom debug levels of increasing verbosity
-zap-stacktrace-level value
Zap Level at and above which stacktraces are captured (one of 'info', 'error').
Zap Level at and above which stacktraces are captured (one of 'info', 'error', 'panic').
```

## Tracing
Expand Down
2 changes: 2 additions & 0 deletions config/webhook/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ resources:

configurations:
- kustomizeconfig.yaml

namePrefix: storageos-
25 changes: 25 additions & 0 deletions config/webhook/manifests.yaml
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
3 changes: 1 addition & 2 deletions config/webhook/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
apiVersion: v1
kind: Service
metadata:
name: webhook-service
namespace: system
name: storageos-webhook
spec:
ports:
- port: 443
Expand Down
75 changes: 75 additions & 0 deletions controllers/admission/scheduler/README.md
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.

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.

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.

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.

ok, we can update when we add cert-manager support.


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.
6 changes: 6 additions & 0 deletions controllers/admission/scheduler/doc.go
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
134 changes: 134 additions & 0 deletions controllers/admission/scheduler/handler.go
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
}
Loading