Skip to content

Commit

Permalink
Fixes an issue with webhook crashlooping (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellistarn authored Nov 11, 2021
1 parent 42e1c20 commit 4c6d3ff
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ apply: ## Deploy the controller into your ~/.kube/config cluster
delete: ## Delete the controller from your ~/.kube/config cluster
helm template karpenter charts/karpenter --namespace karpenter \
--set serviceAccount.create=false \
--set defaultProvisioner.create=false \
| kubectl delete -f -

codegen: ## Generate code. Must be run if changes are made to ./pkg/apis/...
Expand Down
4 changes: 4 additions & 0 deletions charts/karpenter/templates/webhook/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ spec:
scheme: HTTPS
port: 8443
env:
- name: CLUSTER_NAME
value: {{ .Values.controller.clusterName }}
- name: CLUSTER_ENDPOINT
value: {{ .Values.controller.clusterEndpoint }}
- name: SYSTEM_NAMESPACE
valueFrom:
fieldRef:
Expand Down
3 changes: 1 addition & 2 deletions charts/karpenter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ webhook:
defaultProvisioner:
create: true
ttlSecondsAfterEmpty: 300
#ttlSecondsUntilExpired: 86400
#ttlSecondsUntilExpired: 86400
provider: {}
requirements: []
taints: []
labels: {}

17 changes: 1 addition & 16 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"context"
"flag"
"fmt"

"github.com/awslabs/karpenter/pkg/apis"
Expand All @@ -27,7 +26,6 @@ import (
"github.com/awslabs/karpenter/pkg/controllers/metrics"
"github.com/awslabs/karpenter/pkg/controllers/node"
"github.com/awslabs/karpenter/pkg/controllers/termination"
"github.com/awslabs/karpenter/pkg/utils/env"
"github.com/awslabs/karpenter/pkg/utils/options"
"github.com/awslabs/karpenter/pkg/utils/restconfig"
"github.com/go-logr/zapr"
Expand All @@ -49,7 +47,7 @@ import (

var (
scheme = runtime.NewScheme()
opts = options.Options{}
opts = options.MustParse()
component = "controller"
)

Expand All @@ -59,14 +57,6 @@ func init() {
}

func main() {
flag.StringVar(&opts.ClusterName, "cluster-name", env.WithDefaultString("CLUSTER_NAME", ""), "The kubernetes cluster name for resource discovery")
flag.StringVar(&opts.ClusterEndpoint, "cluster-endpoint", env.WithDefaultString("CLUSTER_ENDPOINT", ""), "The external kubernetes cluster endpoint for new nodes to connect with")
flag.IntVar(&opts.MetricsPort, "metrics-port", env.WithDefaultInt("METRICS_PORT", 8080), "The port the metric endpoint binds to for operating metrics about the controller itself")
flag.IntVar(&opts.HealthProbePort, "health-probe-port", env.WithDefaultInt("HEALTH_PROBE_PORT", 8081), "The port the health probe endpoint binds to for reporting controller health")
flag.IntVar(&opts.KubeClientQPS, "kube-client-qps", env.WithDefaultInt("KUBE_CLIENT_QPS", 200), "The smoothed rate of qps to kube-apiserver")
flag.IntVar(&opts.KubeClientBurst, "kube-client-burst", env.WithDefaultInt("KUBE_CLIENT_BURST", 300), "The maximum allowed burst of queries to the kube-apiserver")
flag.Parse()

if err := opts.Validate(); err != nil {
panic(fmt.Sprintf("Input parameter validation failed, %s", err.Error()))
}
Expand All @@ -77,12 +67,7 @@ func main() {

// Set up logger and watch for changes to log level
ctx := LoggingContextOrDie(config, clientSet)

// Put REST config in context, as it can be used by arbitrary
// parts of the code base
ctx = restconfig.Inject(ctx, config)

// Put CLI args into context for access across code base
ctx = options.Inject(ctx, opts)

// Set up controller runtime controller
Expand Down
17 changes: 6 additions & 11 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package main

import (
"context"
"flag"

"github.com/awslabs/karpenter/pkg/apis"
"github.com/awslabs/karpenter/pkg/cloudprovider"
"github.com/awslabs/karpenter/pkg/cloudprovider/registry"
"github.com/awslabs/karpenter/pkg/utils/options"
"k8s.io/client-go/kubernetes"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
Expand All @@ -37,20 +37,13 @@ import (
)

var (
options = Options{}
opts = options.MustParse()
)

type Options struct {
Port int
}

func main() {
flag.IntVar(&options.Port, "port", 8443, "The port the webhook endpoint binds to for validation and mutation of resources")
flag.Parse()

config := injection.ParseAndGetRESTConfigOrDie()
ctx := webhook.WithOptions(injection.WithNamespaceScope(signals.NewContext(), system.Namespace()), webhook.Options{
Port: options.Port,
Port: opts.WebhookPort,
ServiceName: "karpenter-webhook",
SecretName: "karpenter-webhook-cert",
})
Expand Down Expand Up @@ -97,4 +90,6 @@ func newConfigValidationController(ctx context.Context, cmw configmap.Watcher) *
)
}

func InjectContext(ctx context.Context) context.Context { return ctx }
func InjectContext(ctx context.Context) context.Context {
return options.Inject(ctx, opts)
}
23 changes: 21 additions & 2 deletions pkg/utils/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,37 @@ package options

import (
"context"
"flag"
"fmt"
"net/url"

"github.com/awslabs/karpenter/pkg/utils/env"
"go.uber.org/multierr"
)

func MustParse() Options {
opts := Options{}
flag.StringVar(&opts.ClusterName, "cluster-name", env.WithDefaultString("CLUSTER_NAME", ""), "The kubernetes cluster name for resource discovery")
flag.StringVar(&opts.ClusterEndpoint, "cluster-endpoint", env.WithDefaultString("CLUSTER_ENDPOINT", ""), "The external kubernetes cluster endpoint for new nodes to connect with")
flag.IntVar(&opts.MetricsPort, "metrics-port", env.WithDefaultInt("METRICS_PORT", 8080), "The port the metric endpoint binds to for operating metrics about the controller itself")
flag.IntVar(&opts.HealthProbePort, "health-probe-port", env.WithDefaultInt("HEALTH_PROBE_PORT", 8081), "The port the health probe endpoint binds to for reporting controller health")
flag.IntVar(&opts.WebhookPort, "port", 8443, "The port the webhook endpoint binds to for validation and mutation of resources")
flag.IntVar(&opts.KubeClientQPS, "kube-client-qps", env.WithDefaultInt("KUBE_CLIENT_QPS", 200), "The smoothed rate of qps to kube-apiserver")
flag.IntVar(&opts.KubeClientBurst, "kube-client-burst", env.WithDefaultInt("KUBE_CLIENT_BURST", 300), "The maximum allowed burst of queries to the kube-apiserver")
flag.Parse()
if err := opts.Validate(); err != nil {
panic(err)
}
return opts
}

// Options for running this binary
type Options struct {
ClusterName string
ClusterEndpoint string
MetricsPort int
HealthProbePort int
WebhookPort int
KubeClientQPS int
KubeClientBurst int
}
Expand All @@ -38,8 +57,8 @@ func Get(ctx context.Context) Options {
return ctx.Value(optionsKey{}).(Options)
}

func Inject(ctx context.Context, options Options) context.Context {
return context.WithValue(ctx, optionsKey{}, options)
func Inject(ctx context.Context, opts Options) context.Context {
return context.WithValue(ctx, optionsKey{}, opts)
}

func (o Options) Validate() (err error) {
Expand Down

0 comments on commit 4c6d3ff

Please sign in to comment.