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

feat: custom security context for kafka addon #1337

Merged
merged 5 commits into from
Jan 22, 2025
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
15 changes: 9 additions & 6 deletions addons/kafka/templates/cmpd-broker-27.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,18 @@ spec:
namespace: {{ .Release.Namespace }}
defaultMode: 0755
runtime:
{{- if .Values.securityContext }}
securityContext:
fsGroup: 1001
{{- toYaml .Values.securityContext | nindent 6 }}
{{- end }}
containers:
- name: kafka
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.kafka.repository }}:{{ default .Values.images.kafka2.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.container.securityContext }}
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.container.securityContext | nindent 10 }}
{{- end }}
command:
- /scripts/kafka-server-setup.sh
env:
Expand Down Expand Up @@ -228,9 +230,10 @@ spec:
- name: jmx-exporter
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.jmxExporter.repository }}:{{ .Values.images.jmxExporter.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.exporter.securityContext }}
securityContext:
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.exporter.securityContext | nindent 10 }}
{{- end }}
command:
- java
args:
Expand Down
15 changes: 9 additions & 6 deletions addons/kafka/templates/cmpd-broker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@ spec:
namespace: {{ .Release.Namespace }}
defaultMode: 0755
runtime:
{{- if .Values.securityContext }}
securityContext:
fsGroup: 1001
{{- toYaml .Values.securityContext | nindent 6 }}
{{- end }}
containers:
- name: kafka
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.kafka.repository }}:{{ default .Chart.AppVersion .Values.images.kafka.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.container.securityContext }}
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.container.securityContext | nindent 10 }}
{{- end }}
command:
- /scripts/kafka-server-setup.sh
env:
Expand Down Expand Up @@ -228,9 +230,10 @@ spec:
- name: jmx-exporter
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.jmxExporter.repository }}:{{ .Values.images.jmxExporter.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.exporter.securityContext }}
securityContext:
runAsNonRoot: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modification logic here differs from the previous implementation.

the default value of allowPrivilegeEscalation is true in k8s?

	// defaultAllowPrivilegeEscalation controls the default setting for whether a
	// process can gain more privileges than its parent process.
	// +optional
	DefaultAllowPrivilegeEscalation *bool `json:"defaultAllowPrivilegeEscalation,omitempty" protobuf:"varint,15,opt,name=defaultAllowPrivilegeEscalation"`
	// allowPrivilegeEscalation determines if a pod can request to allow
	// privilege escalation. If unspecified, defaults to true.
	// +optional
	AllowPrivilegeEscalation *bool `json:"allowPrivilegeEscalation,omitempty" protobuf:"varint,16,opt,name=allowPrivilegeEscalation"`

Copy link
Contributor Author

@lancelot1989 lancelot1989 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so in the previous implementation, the jmx-exporter will be able to sudo. Now it can't.

If jmx-exporter needs AllowPrivilegeEscalation, i can define another value for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using separate value for exporter's securityContext

runAsUser: 1001
{{- toYaml .Values.exporter.securityContext | nindent 10 }}
{{- end }}
command:
- java
args:
Expand Down
15 changes: 9 additions & 6 deletions addons/kafka/templates/cmpd-combine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,18 @@ spec:
namespace: {{ .Release.Namespace }}
defaultMode: 0755
runtime:
{{- if .Values.securityContext }}
securityContext:
fsGroup: 1001
{{- toYaml .Values.securityContext | nindent 6 }}
{{- end }}
containers:
- name: kafka
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.kafka.repository }}:{{ default .Chart.AppVersion .Values.images.kafka.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.container.securityContext }}
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.container.securityContext | nindent 10 }}
{{- end }}
command:
- /scripts/kafka-server-setup.sh
env:
Expand Down Expand Up @@ -219,9 +221,10 @@ spec:
- name: jmx-exporter
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.jmxExporter.repository }}:{{ .Values.images.jmxExporter.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.exporter.securityContext }}
securityContext:
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.exporter.securityContext | nindent 10 }}
{{- end }}
command:
- java
args:
Expand Down
15 changes: 9 additions & 6 deletions addons/kafka/templates/cmpd-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ spec:
namespace: {{ .Release.Namespace }}
defaultMode: 0755
runtime:
{{- if .Values.securityContext }}
securityContext:
fsGroup: 1001
{{- toYaml .Values.securityContext | nindent 6 }}
{{- end }}
containers:
- name: kafka
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.kafka.repository }}:{{ default .Chart.AppVersion .Values.images.kafka.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.container.securityContext }}
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.container.securityContext | nindent 10 }}
{{- end }}
command:
- /scripts/kafka-server-setup.sh
env:
Expand Down Expand Up @@ -155,9 +157,10 @@ spec:
- name: jmx-exporter
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.jmxExporter.repository }}:{{ .Values.images.jmxExporter.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.exporter.securityContext }}
securityContext:
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.exporter.securityContext | nindent 10 }}
{{- end }}
command:
- java
args:
Expand Down
9 changes: 6 additions & 3 deletions addons/kafka/templates/cmpd-exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ spec:
namespace: {{ .Release.Namespace }}
defaultMode: 0755
runtime:
{{- if .Values.securityContext }}
securityContext:
fsGroup: 1001
{{- toYaml .Values.securityContext | nindent 6 }}
{{- end }}
containers:
- name: kafka-exporter
image: {{ .Values.images.registry | default "docker.io" }}/{{ .Values.images.kafkaExporter.repository }}:{{ .Values.images.kafkaExporter.tag }}
imagePullPolicy: {{ default "IfNotPresent" .Values.images.pullPolicy }}
{{- if .Values.exporter.securityContext }}
securityContext:
runAsNonRoot: true
runAsUser: 1001
{{- toYaml .Values.exporter.securityContext | nindent 8 }}
{{- end }}
env:
- name: SERVICE_PORT
value: "9308"
Expand Down
17 changes: 17 additions & 0 deletions addons/kafka/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ storageClassParameters:
metadata:
awsEBSVolumeType: io2
awsEBSEnableBlockExpress: false

## pod security context settings
securityContext:
fsGroup: 1001

## container security context settings
container:
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1001

## exporter security context settings
exporter:
securityContext:
runAsNonRoot: true
runAsUser: 1001
Loading