Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Remove runtime update of karpenter-global-settings #174

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
7 changes: 4 additions & 3 deletions charts/karpenter-core/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
{{- include "karpenter.labels" . | nindent 4 }}
{{- with .Values.additionalAnnotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.additionalAnnotations }}
{{- toYaml . | nindent 6 }}
{{- end }}
checksum/settings: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
spec:
replicas: {{ .Values.replicas }}
revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
Expand Down
5 changes: 2 additions & 3 deletions pkg/apis/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import (

"github.com/samber/lo"

"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/utils/functional"
"github.com/aws/karpenter-core/pkg/utils/sets"
)

var (
Expand All @@ -41,7 +40,7 @@ var (
Resources = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
v1alpha5.SchemeGroupVersion.WithKind("Provisioner"): &v1alpha5.Provisioner{},
}
Settings = sets.New(settings.Registration)
Settings = []settings.Injectable{&settings.Settings{}}
)

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

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/apis/crds/karpenter.sh_machines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ spec:
the value must be greater than ImageGCLowThresholdPercent.
format: int32
maximum: 100
minimum: 1
minimum: 0
type: integer
imageGCLowThresholdPercent:
description: ImageGCLowThresholdPercent is the percent of disk
Expand All @@ -101,7 +101,7 @@ spec:
be less than imageGCHighThresholdPercent
format: int32
maximum: 100
minimum: 1
minimum: 0
type: integer
kubeReserved:
additionalProperties:
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/crds/karpenter.sh_provisioners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ spec:
the value must be greater than ImageGCLowThresholdPercent.
format: int32
maximum: 100
minimum: 1
minimum: 0
type: integer
imageGCLowThresholdPercent:
description: ImageGCLowThresholdPercent is the percent of disk
Expand All @@ -110,7 +110,7 @@ spec:
be less than imageGCHighThresholdPercent
format: int32
maximum: 100
minimum: 1
minimum: 0
type: integer
kubeReserved:
additionalProperties:
Expand Down
27 changes: 27 additions & 0 deletions pkg/apis/settings/injectable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package settings
jonathan-innis marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"

v1 "k8s.io/api/core/v1"
)

// Injectable defines a ConfigMap registration to be loaded into context on startup
type Injectable interface {
ConfigMap() string
Inject(context.Context, *v1.ConfigMap) (context.Context, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,13 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/configmap"

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

var ContextKey = Registration
type settingsKeyType struct{}

var Registration = &config.Registration{
ConfigMapName: "karpenter-global-settings",
Constructor: NewSettingsFromConfigMap,
}
var ContextKey = settingsKeyType{}

var defaultSettings = Settings{
var defaultSettings = &Settings{
BatchMaxDuration: metav1.Duration{Duration: time.Second * 10},
BatchIdleDuration: metav1.Duration{Duration: time.Second * 1},
DriftEnabled: false,
Expand All @@ -48,23 +43,25 @@ type Settings struct {
DriftEnabled bool
}

// NewSettingsFromConfigMap creates a Settings from the supplied ConfigMap
func NewSettingsFromConfigMap(cm *v1.ConfigMap) (Settings, error) {
func (*Settings) ConfigMap() string {
return "karpenter-global-settings"
}

// Inject creates a Settings from the supplied ConfigMap
func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context, error) {
s := defaultSettings

if err := configmap.Parse(cm.Data,
AsMetaDuration("batchMaxDuration", &s.BatchMaxDuration),
AsMetaDuration("batchIdleDuration", &s.BatchIdleDuration),
configmap.AsBool("featureGates.driftEnabled", &s.DriftEnabled),
); err != nil {
// Failing to parse means that there is some error in the Settings, so we should crash
panic(fmt.Sprintf("parsing settings, %v", err))
return ctx, fmt.Errorf("parsing settings, %w", err)
}
if err := s.Validate(); err != nil {
// Failing to validate means that there is some error in the Settings, so we should crash
panic(fmt.Sprintf("validating settings, %v", err))
return ctx, fmt.Errorf("validating settings, %w", err)
}
return s, nil
return ToContext(ctx, s), nil
}

// Validate leverages struct tags with go-playground/validator so you can define a struct with custom
Expand All @@ -73,7 +70,7 @@ func NewSettingsFromConfigMap(cm *v1.ConfigMap) (Settings, error) {
// type ExampleStruct struct {
// Example metav1.Duration `json:"example" validate:"required,min=10m"`
// }
func (s Settings) Validate() (err error) {
func (s *Settings) Validate() (err error) {
validate := validator.New()
if s.BatchMaxDuration.Duration <= 0 {
err = multierr.Append(err, fmt.Errorf("batchMaxDuration cannot be negative"))
Expand All @@ -98,15 +95,15 @@ func AsMetaDuration(key string, target *metav1.Duration) configmap.ParseFunc {
}
}

func ToContext(ctx context.Context, s Settings) context.Context {
func ToContext(ctx context.Context, s *Settings) context.Context {
return context.WithValue(ctx, ContextKey, s)
}

func FromContext(ctx context.Context) Settings {
func FromContext(ctx context.Context) *Settings {
data := ctx.Value(ContextKey)
if data == nil {
// This is developer error if this happens, so we should panic
panic("settings doesn't exist in context")
}
return data.(Settings)
return data.(*Settings)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
v1 "k8s.io/api/core/v1"
. "knative.dev/pkg/logging/testing"

. "github.com/aws/karpenter-core/pkg/test/expectations"

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

var ctx context.Context
Expand All @@ -42,7 +40,9 @@ var _ = Describe("Validation", func() {
cm := &v1.ConfigMap{
Data: map[string]string{},
}
s, _ := settings.NewSettingsFromConfigMap(cm)
ctx, err := (&settings.Settings{}).Inject(ctx, cm)
Expect(err).ToNot(HaveOccurred())
s := settings.FromContext(ctx)
Expect(s.BatchMaxDuration.Duration).To(Equal(time.Second * 10))
Expect(s.BatchIdleDuration.Duration).To(Equal(time.Second))
Expect(s.DriftEnabled).To(BeFalse())
Expand All @@ -55,36 +55,38 @@ var _ = Describe("Validation", func() {
"featureGates.driftEnabled": "true",
},
}
s, _ := settings.NewSettingsFromConfigMap(cm)
ctx, err := (&settings.Settings{}).Inject(ctx, cm)
Expect(err).ToNot(HaveOccurred())
s := settings.FromContext(ctx)
Expect(s.BatchMaxDuration.Duration).To(Equal(time.Second * 30))
Expect(s.BatchIdleDuration.Duration).To(Equal(time.Second * 5))
Expect(s.DriftEnabled).To(BeTrue())
})
It("should fail validation with panic when batchMaxDuration is negative", func() {
defer ExpectPanic()
cm := &v1.ConfigMap{
Data: map[string]string{
"batchMaxDuration": "-10s",
},
}
_, _ = settings.NewSettingsFromConfigMap(cm)
_, err := (&settings.Settings{}).Inject(ctx, cm)
Expect(err).To(HaveOccurred())
})
It("should fail validation with panic when batchIdleDuration is negative", func() {
defer ExpectPanic()
cm := &v1.ConfigMap{
Data: map[string]string{
"batchIdleDuration": "-1s",
},
}
_, _ = settings.NewSettingsFromConfigMap(cm)
_, err := (&settings.Settings{}).Inject(ctx, cm)
Expect(err).To(HaveOccurred())
})
It("should fail validation with panic when driftEnabled is not a valid boolean value", func() {
defer ExpectPanic()
cm := &v1.ConfigMap{
Data: map[string]string{
"featureGates.driftEnabled": "foobar",
},
}
_, _ = settings.NewSettingsFromConfigMap(cm)
_, err := (&settings.Settings{}).Inject(ctx, cm)
Expect(err).To(HaveOccurred())
})
})
2 changes: 1 addition & 1 deletion pkg/controllers/deprovisioning/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"knative.dev/pkg/logging"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/controllers/provisioning"
"github.com/aws/karpenter-core/pkg/controllers/state"
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/deprovisioning/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/apis"
"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/cloudprovider"
"github.com/aws/karpenter-core/pkg/cloudprovider/fake"
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/inflightchecks/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/apis"
"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/cloudprovider/fake"
"github.com/aws/karpenter-core/pkg/controllers/inflightchecks"
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/machine/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/apis"
"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/cloudprovider/fake"
"github.com/aws/karpenter-core/pkg/controllers/machine"
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/metrics/state/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/apis"
"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
metricsstate "github.com/aws/karpenter-core/pkg/controllers/metrics/state"
"github.com/aws/karpenter-core/pkg/controllers/state/informer"
Expand Down
17 changes: 12 additions & 5 deletions pkg/controllers/node/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
corecontroller "github.com/aws/karpenter-core/pkg/operator/controller"

Expand All @@ -39,6 +40,10 @@ import (
"github.com/aws/karpenter-core/pkg/utils/result"
)

type nodeReconciler interface {
Reconcile(context.Context, *v1alpha5.Provisioner, *v1.Node) (reconcile.Result, error)
}

var _ corecontroller.TypedController[*v1.Node] = (*Controller)(nil)

// Controller manages a set of properties on karpenter provisioned nodes, such as
Expand Down Expand Up @@ -85,14 +90,16 @@ func (c *Controller) Reconcile(ctx context.Context, node *v1.Node) (reconcile.Re
// Execute Reconcilers
var results []reconcile.Result
var errs error
for _, reconciler := range []interface {
Reconcile(context.Context, *v1alpha5.Provisioner, *v1.Node) (reconcile.Result, error)
}{

reconcilers := []nodeReconciler{
c.initialization,
c.emptiness,
c.finalizer,
c.drift,
} {
}
if settings.FromContext(ctx).DriftEnabled {
reconcilers = append(reconcilers, c.drift)
}
for _, reconciler := range reconcilers {
res, err := reconciler.Reconcile(ctx, provisioner, node)
errs = multierr.Append(errs, err)
results = append(results, res)
Expand Down
5 changes: 0 additions & 5 deletions pkg/controllers/node/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/samber/lo"

"github.com/aws/karpenter-core/pkg/apis/config/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/cloudprovider"
"github.com/aws/karpenter-core/pkg/utils/machine"
Expand All @@ -37,10 +36,6 @@ type Drift struct {
}

func (d *Drift) Reconcile(ctx context.Context, provisioner *v1alpha5.Provisioner, node *v1.Node) (reconcile.Result, error) {
if !settings.FromContext(ctx).DriftEnabled {
return reconcile.Result{RequeueAfter: 5 * time.Minute}, nil
}

if _, ok := node.Annotations[v1alpha5.VoluntaryDisruptionAnnotationKey]; ok {
return reconcile.Result{}, nil
}
Expand Down
Loading