Skip to content

Commit

Permalink
redpanda: refactor resource flag computations
Browse files Browse the repository at this point in the history
This commit has no user facing changes. It refactors the various functions that
affect and define the redpanda container's resources and the resource related
CLI flags (`--smp`, `--memory`, `--reserve-memory`, and `--overprovisioned`)
into methods on the `RedpandaResource` struct.
  • Loading branch information
chrisseto committed Jan 8, 2025
1 parent 7b7280e commit 8ddd726
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 306 deletions.
18 changes: 4 additions & 14 deletions charts/redpanda/configmap.tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func rpkNodeConfig(dot *helmette.Dot) map[string]any {
result := map[string]any{
"overprovisioned": values.Resources.GetOverProvisionValue(),
"enable_memory_locking": ptr.Deref(values.Resources.Memory.EnableMemoryLocking, false),
"additional_start_flags": RedpandaAdditionalStartFlags(dot, RedpandaSMP(dot)),
"additional_start_flags": RedpandaAdditionalStartFlags(dot),
"kafka_api": map[string]any{
"brokers": brokerList,
"tls": brokerTLS,
Expand Down Expand Up @@ -613,22 +613,12 @@ func createInternalListenerCfg(port int32) map[string]any {
// RedpandaAdditionalStartFlags returns a string list of flags suitable for use
// as `additional_start_flags`. User provided flags will override any of those
// set by default.
func RedpandaAdditionalStartFlags(dot *helmette.Dot, smp int64) []string {
func RedpandaAdditionalStartFlags(dot *helmette.Dot) []string {
values := helmette.Unwrap[Values](dot.Values)

// All `additional_start_flags` that are set by the chart.
chartFlags := map[string]string{
"smp": fmt.Sprintf("%d", int(smp)),
// TODO: The transpiled go template will return float64 from both RedpandaMemory and RedpandaReserveMemory
// By wrapping return value from that function the sprintf will work as expected
// https://github.com/redpanda-data/helm-charts/issues/1249
"memory": fmt.Sprintf("%dM", int(RedpandaMemory(dot))),
// TODO: The transpiled go template will return float64 from both RedpandaMemory and RedpandaReserveMemory
// By wrapping return value from that function the sprintf will work as expected
// https://github.com/redpanda-data/helm-charts/issues/1249
"reserve-memory": fmt.Sprintf("%dM", int(RedpandaReserveMemory(dot))),
"default-log-level": values.Logging.LogLevel,
}
chartFlags := values.Resources.GetRedpandaStartFlags()
chartFlags["default-log-level"] = values.Logging.LogLevel

// If in developer_mode, don't set reserve-memory.
if values.Config.Node["developer_mode"] == true {
Expand Down
12 changes: 0 additions & 12 deletions charts/redpanda/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,6 @@ func cleanForK8sWithSuffix(s, suffix string) string {
return fmt.Sprintf("%s-%s", s, suffix)
}

func RedpandaSMP(dot *helmette.Dot) int64 {
values := helmette.Unwrap[Values](dot.Values)

coresInMillies := values.Resources.CPU.Cores.MilliValue()

if coresInMillies < 1000 {
return 1
}

return values.Resources.CPU.Cores.Value()
}

// coalesce returns the first non-nil pointer. This is distinct from helmette's
// Coalesce which returns the first non-EMPTY pointer.
// It accepts a slice as variadic methods are not currently supported in
Expand Down
90 changes: 0 additions & 90 deletions charts/redpanda/memory.go

This file was deleted.

20 changes: 4 additions & 16 deletions charts/redpanda/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func statefulSetInitContainerConfigurator(dot *helmette.Dot) *corev1.Container {

func StatefulSetContainers(dot *helmette.Dot) []corev1.Container {
var containers []corev1.Container
containers = append(containers, *statefulSetContainerRedpanda(dot))
containers = append(containers, statefulSetContainerRedpanda(dot))
if c := statefulSetContainerConfigWatcher(dot); c != nil {
containers = append(containers, *c)
}
Expand All @@ -668,12 +668,12 @@ func wrapLifecycleHook(hook string, timeoutSeconds int64, cmd []string) []string
return []string{"bash", "-c", fmt.Sprintf("timeout -v %d %s 2>&1 | sed \"s/^/lifecycle-hook %s $(date): /\" | tee /proc/1/fd/1; true", timeoutSeconds, wrapped, hook)}
}

func statefulSetContainerRedpanda(dot *helmette.Dot) *corev1.Container {
func statefulSetContainerRedpanda(dot *helmette.Dot) corev1.Container {
values := helmette.Unwrap[Values](dot.Values)

internalAdvertiseAddress := fmt.Sprintf("%s.%s", "$(SERVICE_NAME)", InternalDomain(dot))

container := &corev1.Container{
container := corev1.Container{
Name: Name(dot),
Image: fmt.Sprintf(`%s:%s`, values.Image.Repository, Tag(dot)),
Env: bootstrapEnvVars(dot, statefulSetRedpandaEnv()),
Expand Down Expand Up @@ -755,7 +755,7 @@ func statefulSetContainerRedpanda(dot *helmette.Dot) *corev1.Container {
VolumeMounts: append(StatefulSetVolumeMounts(dot),
templateToVolumeMounts(dot, values.Statefulset.ExtraVolumeMounts)...),
SecurityContext: ptr.To(ContainerSecurityContext(dot)),
Resources: corev1.ResourceRequirements{},
Resources: values.Resources.GetResourceRequirements(),
}

if !helmette.Dig(values.Config.Node, false, `recovery_mode_enabled`).(bool) {
Expand Down Expand Up @@ -861,18 +861,6 @@ func statefulSetContainerRedpanda(dot *helmette.Dot) *corev1.Container {
)
}

container.Resources.Limits = helmette.UnmarshalInto[corev1.ResourceList](map[string]any{
"cpu": values.Resources.CPU.Cores,
"memory": values.Resources.Memory.Container.Max,
})

if values.Resources.Memory.Container.Min != nil {
container.Resources.Requests = helmette.UnmarshalInto[corev1.ResourceList](map[string]any{
"cpu": values.Resources.CPU.Cores,
"memory": *values.Resources.Memory.Container.Min,
})
}

return container
}

Expand Down
6 changes: 3 additions & 3 deletions charts/redpanda/templates/_configmap.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
{{- if (gt ((get (fromJson (include "_shims.len" (dict "a" (list $tls_8) ))) "r") | int) (0 | int)) -}}
{{- $schemaRegistryTLS = $tls_8 -}}
{{- end -}}
{{- $result := (dict "overprovisioned" (get (fromJson (include "redpanda.RedpandaResources.GetOverProvisionValue" (dict "a" (list $values.resources) ))) "r") "enable_memory_locking" (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.resources.memory.enable_memory_locking false) ))) "r") "additional_start_flags" (get (fromJson (include "redpanda.RedpandaAdditionalStartFlags" (dict "a" (list $dot ((get (fromJson (include "redpanda.RedpandaSMP" (dict "a" (list $dot) ))) "r") | int64)) ))) "r") "kafka_api" (dict "brokers" $brokerList "tls" $brokerTLS ) "admin_api" (dict "addresses" (get (fromJson (include "redpanda.Listeners.AdminList" (dict "a" (list $values.listeners ($values.statefulset.replicas | int) (get (fromJson (include "redpanda.Fullname" (dict "a" (list $dot) ))) "r") (get (fromJson (include "redpanda.InternalDomain" (dict "a" (list $dot) ))) "r")) ))) "r") "tls" $adminTLS ) "schema_registry" (dict "addresses" (get (fromJson (include "redpanda.Listeners.SchemaRegistryList" (dict "a" (list $values.listeners ($values.statefulset.replicas | int) (get (fromJson (include "redpanda.Fullname" (dict "a" (list $dot) ))) "r") (get (fromJson (include "redpanda.InternalDomain" (dict "a" (list $dot) ))) "r")) ))) "r") "tls" $schemaRegistryTLS ) ) -}}
{{- $result := (dict "overprovisioned" (get (fromJson (include "redpanda.RedpandaResources.GetOverProvisionValue" (dict "a" (list $values.resources) ))) "r") "enable_memory_locking" (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.resources.memory.enable_memory_locking false) ))) "r") "additional_start_flags" (get (fromJson (include "redpanda.RedpandaAdditionalStartFlags" (dict "a" (list $dot) ))) "r") "kafka_api" (dict "brokers" $brokerList "tls" $brokerTLS ) "admin_api" (dict "addresses" (get (fromJson (include "redpanda.Listeners.AdminList" (dict "a" (list $values.listeners ($values.statefulset.replicas | int) (get (fromJson (include "redpanda.Fullname" (dict "a" (list $dot) ))) "r") (get (fromJson (include "redpanda.InternalDomain" (dict "a" (list $dot) ))) "r")) ))) "r") "tls" $adminTLS ) "schema_registry" (dict "addresses" (get (fromJson (include "redpanda.Listeners.SchemaRegistryList" (dict "a" (list $values.listeners ($values.statefulset.replicas | int) (get (fromJson (include "redpanda.Fullname" (dict "a" (list $dot) ))) "r") (get (fromJson (include "redpanda.InternalDomain" (dict "a" (list $dot) ))) "r")) ))) "r") "tls" $schemaRegistryTLS ) ) -}}
{{- $result = (merge (dict ) $result (get (fromJson (include "redpanda.Tuning.Translate" (dict "a" (list $values.tuning) ))) "r")) -}}
{{- $result = (merge (dict ) $result (get (fromJson (include "redpanda.Config.CreateRPKConfiguration" (dict "a" (list $values.config) ))) "r")) -}}
{{- $_is_returning = true -}}
Expand Down Expand Up @@ -541,11 +541,11 @@

{{- define "redpanda.RedpandaAdditionalStartFlags" -}}
{{- $dot := (index .a 0) -}}
{{- $smp := (index .a 1) -}}
{{- range $_ := (list 1) -}}
{{- $_is_returning := false -}}
{{- $values := $dot.Values.AsMap -}}
{{- $chartFlags := (dict "smp" (printf "%d" ($smp | int)) "memory" (printf "%dM" (((get (fromJson (include "redpanda.RedpandaMemory" (dict "a" (list $dot) ))) "r") | int64) | int)) "reserve-memory" (printf "%dM" (((get (fromJson (include "redpanda.RedpandaReserveMemory" (dict "a" (list $dot) ))) "r") | int64) | int)) "default-log-level" $values.logging.logLevel ) -}}
{{- $chartFlags := (get (fromJson (include "redpanda.RedpandaResources.GetRedpandaStartFlags" (dict "a" (list $values.resources) ))) "r") -}}
{{- $_ := (set $chartFlags "default-log-level" $values.logging.logLevel) -}}
{{- if (eq (index $values.config.node "developer_mode") true) -}}
{{- $_ := (unset $chartFlags "reserve-memory") -}}
{{- end -}}
Expand Down
47 changes: 15 additions & 32 deletions charts/redpanda/templates/_helpers.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -464,23 +464,6 @@
{{- end -}}
{{- end -}}

{{- define "redpanda.RedpandaSMP" -}}
{{- $dot := (index .a 0) -}}
{{- range $_ := (list 1) -}}
{{- $_is_returning := false -}}
{{- $values := $dot.Values.AsMap -}}
{{- $coresInMillies := ((get (fromJson (include "_shims.resource_MilliValue" (dict "a" (list $values.resources.cpu.cores) ))) "r") | int64) -}}
{{- if (lt $coresInMillies (1000 | int64)) -}}
{{- $_is_returning = true -}}
{{- (dict "r" (1 | int64)) | toJson -}}
{{- break -}}
{{- end -}}
{{- $_is_returning = true -}}
{{- (dict "r" ((get (fromJson (include "_shims.resource_Value" (dict "a" (list $values.resources.cpu.cores) ))) "r") | int64)) | toJson -}}
{{- break -}}
{{- end -}}
{{- end -}}

{{- define "redpanda.coalesce" -}}
{{- $values := (index .a 0) -}}
{{- range $_ := (list 1) -}}
Expand Down Expand Up @@ -542,9 +525,9 @@
{{- $originalKeys := (dict ) -}}
{{- $overrideByKey := (dict ) -}}
{{- range $_, $el := $override -}}
{{- $_526_key_ok := (get (fromJson (include "_shims.get" (dict "a" (list $el $mergeKey) ))) "r") -}}
{{- $key := (index $_526_key_ok 0) -}}
{{- $ok := (index $_526_key_ok 1) -}}
{{- $_514_key_ok := (get (fromJson (include "_shims.get" (dict "a" (list $el $mergeKey) ))) "r") -}}
{{- $key := (index $_514_key_ok 0) -}}
{{- $ok := (index $_514_key_ok 1) -}}
{{- if (not $ok) -}}
{{- continue -}}
{{- end -}}
Expand All @@ -555,13 +538,13 @@
{{- end -}}
{{- $merged := (coalesce nil) -}}
{{- range $_, $el := $original -}}
{{- $_538_key__ := (get (fromJson (include "_shims.get" (dict "a" (list $el $mergeKey) ))) "r") -}}
{{- $key := (index $_538_key__ 0) -}}
{{- $_ := (index $_538_key__ 1) -}}
{{- $_526_key__ := (get (fromJson (include "_shims.get" (dict "a" (list $el $mergeKey) ))) "r") -}}
{{- $key := (index $_526_key__ 0) -}}
{{- $_ := (index $_526_key__ 1) -}}
{{- $_ := (set $originalKeys $key true) -}}
{{- $_540_elOverride_7_ok_8 := (get (fromJson (include "_shims.dicttest" (dict "a" (list $overrideByKey $key (coalesce nil)) ))) "r") -}}
{{- $elOverride_7 := (index $_540_elOverride_7_ok_8 0) -}}
{{- $ok_8 := (index $_540_elOverride_7_ok_8 1) -}}
{{- $_528_elOverride_7_ok_8 := (get (fromJson (include "_shims.dicttest" (dict "a" (list $overrideByKey $key (coalesce nil)) ))) "r") -}}
{{- $elOverride_7 := (index $_528_elOverride_7_ok_8 0) -}}
{{- $ok_8 := (index $_528_elOverride_7_ok_8 1) -}}
{{- if $ok_8 -}}
{{- $merged = (concat (default (list ) $merged) (list (get (fromJson (include $mergeFunc (dict "a" (list $el $elOverride_7) ))) "r"))) -}}
{{- else -}}
Expand All @@ -572,15 +555,15 @@
{{- break -}}
{{- end -}}
{{- range $_, $el := $override -}}
{{- $_550_key_ok := (get (fromJson (include "_shims.get" (dict "a" (list $el $mergeKey) ))) "r") -}}
{{- $key := (index $_550_key_ok 0) -}}
{{- $ok := (index $_550_key_ok 1) -}}
{{- $_538_key_ok := (get (fromJson (include "_shims.get" (dict "a" (list $el $mergeKey) ))) "r") -}}
{{- $key := (index $_538_key_ok 0) -}}
{{- $ok := (index $_538_key_ok 1) -}}
{{- if (not $ok) -}}
{{- continue -}}
{{- end -}}
{{- $_555___ok_9 := (get (fromJson (include "_shims.dicttest" (dict "a" (list $originalKeys $key false) ))) "r") -}}
{{- $_ := (index $_555___ok_9 0) -}}
{{- $ok_9 := (index $_555___ok_9 1) -}}
{{- $_543___ok_9 := (get (fromJson (include "_shims.dicttest" (dict "a" (list $originalKeys $key false) ))) "r") -}}
{{- $_ := (index $_543___ok_9 0) -}}
{{- $ok_9 := (index $_543___ok_9 1) -}}
{{- if $ok_9 -}}
{{- continue -}}
{{- end -}}
Expand Down
63 changes: 0 additions & 63 deletions charts/redpanda/templates/_memory.go.tpl

This file was deleted.

Loading

0 comments on commit 8ddd726

Please sign in to comment.