Skip to content

Commit

Permalink
Add resource requirements to the handler init container (#926)
Browse files Browse the repository at this point in the history
* Set limits and requests for handler init container;

* Update every image in local demo app
  • Loading branch information
ptnapoleon authored Oct 8, 2024
1 parent 78db1ea commit bd7682e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
apiVersion: v2
name: chaos-controller
description: Datadog Chaos Controller chart
version: 3.1.0
version: 3.1.1
2 changes: 2 additions & 0 deletions chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ data:
enabled: {{ .Values.handler.enabled }}
timeout: {{ .Values.handler.timeout | quote }}
maxTimeout: {{ .Values.handler.maxTimeout | quote }}
cpu: {{ .Values.handler.resources.cpu | quote | default "" }}
memory: {{ .Values.handler.resources.memory | quote | default "" }}
3 changes: 3 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ handler:
enabled: true # enable the chaos handler (required to use the onInit disruption feature)
timeout: 10m # time the handler init container will wait before exiting if no signal is received
maxTimeout: 2h # maximum amount of time to allow users to configure for their handler timeout
resources:
cpu: 10m
memory: 5Mi

proxy:
image:
Expand Down
23 changes: 23 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/api/resource"
)

type config struct {
Expand Down Expand Up @@ -101,6 +102,8 @@ type handlerConfig struct {
Image string `json:"image" yaml:"image"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
MaxTimeout time.Duration `json:"maxTimeout" yaml:"maxTimeout"`
CPU string `json:"cpu" yaml:"cpu"`
Memory string `json:"memory" yaml:"memory"`
}

const DefaultDisruptionDeletionTimeout = time.Minute * 15
Expand Down Expand Up @@ -356,6 +359,26 @@ func New(logger *zap.SugaredLogger, osArgs []string) (config, error) {
return cfg, err
}

mainFS.StringVar(&cfg.Handler.CPU, "handler-cpu", "100m", "CPU limit/requests for handler init container")

if err := viper.BindPFlag("handler.cpu", mainFS.Lookup("handler-cpu")); err != nil {
return cfg, err
}

if _, err := resource.ParseQuantity(cfg.Handler.CPU); err != nil {
return cfg, err
}

mainFS.StringVar(&cfg.Handler.Memory, "handler-memory", "100Mi", "Memory limit/requests for handler init container")

if err := viper.BindPFlag("handler.memory", mainFS.Lookup("handler-memory")); err != nil {
return cfg, err
}

if _, err := resource.ParseQuantity(cfg.Handler.Memory); err != nil {
return cfg, err
}

mainFS.StringVar(&cfg.Controller.Webhook.CertDir, "admission-webhook-cert-dir", "", "Admission webhook certificate directory to search for tls.crt and tls.key files")

if err := viper.BindPFlag("controller.webhook.certDir", mainFS.Lookup("admission-webhook-cert-dir")); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions examples/demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ spec:
spec:
containers:
- name: nginx
image: nginx:1.21.6
image: nginx:1.27
livenessProbe:
httpGet:
path: /
Expand Down Expand Up @@ -76,10 +76,11 @@ spec:
app.kubernetes.io/component: client
labels:
app: demo-curl
# chaos.datadoghq.com/disrupt-on-init: "true" # uncomment this and run `kubectl -n chaos-demo apply -f examples/demo.yaml`, for testing the examples/on_init.yaml disruption
spec:
containers:
- name: cpu-stress # jump into pod and run `kill -s TERM 7` to confirm re-start apply cpu reinjection
image: alpine/curl:3.14
image: alpine/curl:8.9.1
command: [/bin/sh, -c]
args:
- ee(){exit 1}; trap 'ee' TERM; tail -f /dev/null
Expand All @@ -92,7 +93,7 @@ spec:
memory: 32Mi
cpu: 1500m
- name: curl
image: alpine/curl:3.14
image: alpine/curl:8.9.1
command: [/bin/sh]
args:
- -c
Expand All @@ -105,7 +106,7 @@ spec:
memory: 32Mi
cpu: 10m
- name: curl-remote
image: alpine/curl:3.14
image: alpine/curl:8.9.1
command: [/bin/sh]
args:
- -c
Expand All @@ -118,7 +119,7 @@ spec:
memory: 32Mi
cpu: 10m
- name: read-file
image: ubuntu:bionic-20220128
image: ubuntu:focal-20240918
command: ["/bin/bash"]
args:
- -c
Expand All @@ -134,7 +135,7 @@ spec:
memory: 32Mi
cpu: 100m
- name: write-file
image: ubuntu:bionic-20220128
image: ubuntu:focal-20240918
command: ["/bin/bash"]
args:
- -c
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"go.uber.org/zap"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

Expand Down Expand Up @@ -407,6 +408,10 @@ func main() {
Timeout: cfg.Handler.Timeout,
MaxTimeout: cfg.Handler.MaxTimeout,
Decoder: webhookDecoder,
ResourceList: &corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(cfg.Handler.CPU),
corev1.ResourceMemory: resource.MustParse(cfg.Handler.Memory),
},
},
})
}
Expand Down
17 changes: 11 additions & 6 deletions webhook/chaos_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
)

type ChaosHandlerMutator struct {
Client client.Client
Log *zap.SugaredLogger
Image string
Timeout time.Duration
MaxTimeout time.Duration
Decoder *admission.Decoder
Client client.Client
Log *zap.SugaredLogger
Image string
Timeout time.Duration
MaxTimeout time.Duration
Decoder *admission.Decoder
ResourceList *corev1.ResourceList
}

func (m *ChaosHandlerMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
Expand Down Expand Up @@ -90,6 +91,10 @@ func (m *ChaosHandlerMutator) Handle(ctx context.Context, req admission.Request)
handlerTimeout,
succeedOnTimeout,
},
Resources: corev1.ResourceRequirements{
Limits: *m.ResourceList,
Requests: *m.ResourceList,
},
}

// prepend chaos handler init container to already existing init containers
Expand Down

0 comments on commit bd7682e

Please sign in to comment.