Skip to content

Commit

Permalink
Add webhooks for machines (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Mar 9, 2023
1 parent c74476b commit 7c33b16
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 107 deletions.
31 changes: 0 additions & 31 deletions charts/karpenter-core/templates/webhooks.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: defaulting.webhook.karpenter.sh
labels:
{{- include "karpenter.labels" . | nindent 4 }}
{{- with .Values.additionalAnnotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
webhooks:
- name: defaulting.webhook.karpenter.sh
admissionReviewVersions: ["v1"]
clientConfig:
service:
name: {{ include "karpenter.fullname" . }}
namespace: {{ .Release.Namespace }}
failurePolicy: Fail
sideEffects: None
rules:
- apiGroups:
- karpenter.sh
apiVersions:
- v1alpha5
resources:
- provisioners
- provisioners/status
operations:
- CREATE
- UPDATE
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validation.webhook.karpenter.sh
Expand Down
12 changes: 2 additions & 10 deletions pkg/apis/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ package apis
import (
_ "embed"

"github.com/samber/lo"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/webhook/resourcesemantics"

"github.com/samber/lo"

"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
Expand All @@ -36,12 +33,7 @@ var (
)
// AddToScheme may be used to add all resources defined in the project to a Scheme
AddToScheme = Builder.AddToScheme
// Resources defined in the project
Resources = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
v1alpha5.SchemeGroupVersion.WithKind("Provisioner"): &v1alpha5.Provisioner{},
v1alpha5.SchemeGroupVersion.WithKind("Machine"): &v1alpha5.Machine{},
}
Settings = []settings.Injectable{&settings.Settings{}}
Settings = []settings.Injectable{&settings.Settings{}}
)

//go:generate controller-gen crd object:headerFile="../../hack/boilerplate.go.txt" paths="./..." output:crd:artifacts:config=crds
Expand Down
20 changes: 0 additions & 20 deletions pkg/apis/v1alpha5/machine_defaults.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/apis/v1alpha5/machine_validation.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/apis/v1alpha5/provisioner_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ import (
)

// SetDefaults for the provisioner
func (p *Provisioner) SetDefaults(ctx context.Context) {
}
func (p *Provisioner) SetDefaults(_ context.Context) {}
1 change: 0 additions & 1 deletion pkg/controllers/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (c *Controller) cleanupNodeForMachine(ctx context.Context, machine *v1alpha
}
if err = c.terminator.Drain(ctx, node); err != nil {
if terminator.IsNodeDrainError(err) {
logging.FromContext(ctx).Errorf("%s", err)
c.recorder.Publish(terminatorevents.NodeFailedToDrain(node, err))
}
return fmt.Errorf("draining node, %w", err)
Expand Down
24 changes: 7 additions & 17 deletions pkg/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,33 @@ package webhooks
import (
"context"

"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
knativeinjection "knative.dev/pkg/injection"
"knative.dev/pkg/logging"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/configmaps"
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
"knative.dev/pkg/webhook/resourcesemantics"
"knative.dev/pkg/webhook/resourcesemantics/validation"

"github.com/aws/karpenter-core/pkg/apis"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
)

func NewWebhooks() []knativeinjection.ControllerConstructor {
return []knativeinjection.ControllerConstructor{
certificates.NewController,
NewCRDDefaultingWebhook,
NewCRDValidationWebhook,
NewConfigValidationWebhook,
}
}

func NewCRDDefaultingWebhook(ctx context.Context, w configmap.Watcher) *controller.Impl {
return defaulting.NewAdmissionController(ctx,
"defaulting.webhook.karpenter.sh",
"/default/karpenter.sh",
apis.Resources,
InjectContext,
true,
)
}

func NewCRDValidationWebhook(ctx context.Context, w configmap.Watcher) *controller.Impl {
return validation.NewAdmissionController(ctx,
"validation.webhook.karpenter.sh",
"/validate/karpenter.sh",
apis.Resources,
InjectContext,
Resources,
func(ctx context.Context) context.Context { return ctx },
true,
)
}
Expand All @@ -68,6 +58,6 @@ func NewConfigValidationWebhook(ctx context.Context, cmw configmap.Watcher) *con
)
}

func InjectContext(ctx context.Context) context.Context {
return ctx
var Resources = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
v1alpha5.SchemeGroupVersion.WithKind("Provisioner"): &v1alpha5.Provisioner{},
}

0 comments on commit 7c33b16

Please sign in to comment.