Skip to content

Add init container for setting up base config #2649

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

Merged
merged 2 commits into from
Oct 8, 2024
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
30 changes: 30 additions & 0 deletions charts/nginx-gateway-fabric/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ spec:
{{- end }}
{{- end }}
spec:
initContainers:
- name: copy-nginx-config
image: {{ .Values.nginxGateway.image.repository }}:{{ default .Chart.AppVersion .Values.nginxGateway.image.tag }}
imagePullPolicy: {{ .Values.nginxGateway.image.pullPolicy }}
command:
- /usr/bin/gateway
- copy
- --source
- /includes/main.conf
- --destination
- /etc/nginx/main-includes/main.conf
securityContext:
seccompProfile:
type: RuntimeDefault
capabilities:
add:
- KILL # Set because the binary has CAP_KILL for the main controller process. Not used by init.
drop:
- ALL
readOnlyRootFilesystem: true
runAsUser: 102
runAsGroup: 1001
volumeMounts:
- name: nginx-includes-configmap
mountPath: /includes
- name: nginx-main-includes
mountPath: /etc/nginx/main-includes
containers:
- args:
- static-mode
Expand Down Expand Up @@ -223,6 +250,9 @@ spec:
emptyDir: {}
- name: nginx-includes
emptyDir: {}
- name: nginx-includes-configmap
configMap:
name: nginx-includes
{{- with .Values.extraVolumes -}}
{{ toYaml . | nindent 6 }}
{{- end }}
Expand Down
14 changes: 14 additions & 0 deletions charts/nginx-gateway-fabric/templates/include-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-includes
namespace: {{ .Release.Namespace }}
labels:
{{- include "nginx-gateway.labels" . | nindent 4 }}
data:
main.conf: |
{{- if and .Values.nginx.config .Values.nginx.config.logging .Values.nginx.config.logging.errorLevel }}
error_log stderr {{ .Values.nginx.config.logging.errorLevel }};
{{ else }}
error_log stderr info;
{{- end }}
1 change: 1 addition & 0 deletions charts/nginx-gateway-fabric/templates/scc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ seccompProfiles:
volumes:
- emptyDir
- secret
- configMap
users:
- {{ printf "system:serviceaccount:%s:%s" .Release.Namespace (include "nginx-gateway.serviceAccountName" .) }}
allowedCapabilities:
Expand Down
58 changes: 58 additions & 0 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"errors"
"fmt"
"io"
"os"
"runtime/debug"
"strconv"
Expand Down Expand Up @@ -481,6 +482,63 @@
return cmd
}

func createCopyCommand() *cobra.Command {
// flag names
const srcFlag = "source"
const destFlag = "destination"
// flag values
var src, dest string

cmd := &cobra.Command{
Use: "copy",
Short: "Copy a file to a destination",
RunE: func(_ *cobra.Command, _ []string) error {
if len(src) == 0 {
return errors.New("source must not be empty")

Check warning on line 497 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L496-L497

Added lines #L496 - L497 were not covered by tests
}
if len(dest) == 0 {
return errors.New("destination must not be empty")

Check warning on line 500 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L499-L500

Added lines #L499 - L500 were not covered by tests
}

srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("error opening source file: %w", err)

Check warning on line 505 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L503-L505

Added lines #L503 - L505 were not covered by tests
}
defer srcFile.Close()

Check warning on line 507 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L507

Added line #L507 was not covered by tests

destFile, err := os.Create(dest)
if err != nil {
return fmt.Errorf("error creating destination file: %w", err)

Check warning on line 511 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L509-L511

Added lines #L509 - L511 were not covered by tests
}
defer destFile.Close()

Check warning on line 513 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L513

Added line #L513 was not covered by tests

if _, err := io.Copy(destFile, srcFile); err != nil {
return fmt.Errorf("error copying file contents: %w", err)

Check warning on line 516 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L515-L516

Added lines #L515 - L516 were not covered by tests
}

return nil

Check warning on line 519 in cmd/gateway/commands.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/commands.go#L519

Added line #L519 was not covered by tests
},
}

cmd.Flags().StringVar(
&src,
srcFlag,
"",
"The source file to be copied",
)

cmd.Flags().StringVar(
&dest,
destFlag,
"",
"The destination for the source file to be copied to",
)

cmd.MarkFlagsRequiredTogether(srcFlag, destFlag)

return cmd
}

func parseFlags(flags *pflag.FlagSet) ([]string, []string) {
var flagKeys, flagValues []string

Expand Down
45 changes: 45 additions & 0 deletions cmd/gateway/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,51 @@ func TestSleepCmdFlagValidation(t *testing.T) {
}
}

func TestCopyCmdFlagValidation(t *testing.T) {
t.Parallel()
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--source=/my/file",
"--destination=dest/file",
},
wantErr: false,
},
{
name: "omitted flags",
args: nil,
wantErr: false,
},
{
name: "source set without destination",
args: []string{
"--source=/my/file",
},
wantErr: true,
expectedErrPrefix: "if any flags in the group [source destination] are set they must all be set; " +
"missing [destination]",
},
{
name: "destination set without source",
args: []string{
"--destination=/dest/file",
},
wantErr: true,
expectedErrPrefix: "if any flags in the group [source destination] are set they must all be set; " +
"missing [source]",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cmd := createCopyCommand()
testFlag(t, cmd, test)
})
}
}

func TestParseFlags(t *testing.T) {
t.Parallel()
g := NewWithT(t)
Expand Down
1 change: 1 addition & 0 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
rootCmd.AddCommand(
createStaticModeCommand(),
createProvisionerModeCommand(),
createCopyCommand(),

Check warning on line 26 in cmd/gateway/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gateway/main.go#L26

Added line #L26 was not covered by tests
createSleepCommand(),
)

Expand Down
30 changes: 30 additions & 0 deletions config/tests/static-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ spec:
app.kubernetes.io/name: nginx-gateway
app.kubernetes.io/instance: nginx-gateway
spec:
initContainers:
- name: copy-nginx-config
image: ghcr.io/nginxinc/nginx-gateway-fabric:edge
imagePullPolicy: Always
command:
- /usr/bin/gateway
- copy
- --source
- /includes/main.conf
- --destination
- /etc/nginx/main-includes/main.conf
securityContext:
seccompProfile:
type: RuntimeDefault
capabilities:
add:
- KILL # Set because the binary has CAP_KILL for the main controller process. Not used by init.
drop:
- ALL
readOnlyRootFilesystem: true
runAsUser: 102
runAsGroup: 1001
volumeMounts:
- name: nginx-includes-configmap
mountPath: /includes
- name: nginx-main-includes
mountPath: /etc/nginx/main-includes
containers:
- args:
- static-mode
Expand Down Expand Up @@ -137,3 +164,6 @@ spec:
emptyDir: {}
- name: nginx-includes
emptyDir: {}
- name: nginx-includes-configmap
configMap:
name: nginx-includes
43 changes: 43 additions & 0 deletions deploy/aws-nlb/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ subjects:
namespace: nginx-gateway
---
apiVersion: v1
data:
main.conf: |
error_log stderr info;
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/instance: nginx-gateway
app.kubernetes.io/name: nginx-gateway
app.kubernetes.io/version: edge
name: nginx-includes
namespace: nginx-gateway
---
apiVersion: v1
kind: Service
metadata:
annotations:
Expand Down Expand Up @@ -290,6 +303,33 @@ spec:
name: nginx-cache
- mountPath: /etc/nginx/includes
name: nginx-includes
initContainers:
- command:
- /usr/bin/gateway
- copy
- --source
- /includes/main.conf
- --destination
- /etc/nginx/main-includes/main.conf
image: ghcr.io/nginxinc/nginx-gateway-fabric:edge
imagePullPolicy: Always
name: copy-nginx-config
securityContext:
capabilities:
add:
- KILL
drop:
- ALL
readOnlyRootFilesystem: true
runAsGroup: 1001
runAsUser: 102
seccompProfile:
type: RuntimeDefault
volumeMounts:
- mountPath: /includes
name: nginx-includes-configmap
- mountPath: /etc/nginx/main-includes
name: nginx-main-includes
securityContext:
fsGroup: 1001
runAsNonRoot: true
Expand All @@ -311,6 +351,9 @@ spec:
name: nginx-cache
- emptyDir: {}
name: nginx-includes
- configMap:
name: nginx-includes
name: nginx-includes-configmap
---
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
Expand Down
43 changes: 43 additions & 0 deletions deploy/azure/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ subjects:
namespace: nginx-gateway
---
apiVersion: v1
data:
main.conf: |
error_log stderr info;
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/instance: nginx-gateway
app.kubernetes.io/name: nginx-gateway
app.kubernetes.io/version: edge
name: nginx-includes
namespace: nginx-gateway
---
apiVersion: v1
kind: Service
metadata:
labels:
Expand Down Expand Up @@ -287,6 +300,33 @@ spec:
name: nginx-cache
- mountPath: /etc/nginx/includes
name: nginx-includes
initContainers:
- command:
- /usr/bin/gateway
- copy
- --source
- /includes/main.conf
- --destination
- /etc/nginx/main-includes/main.conf
image: ghcr.io/nginxinc/nginx-gateway-fabric:edge
imagePullPolicy: Always
name: copy-nginx-config
securityContext:
capabilities:
add:
- KILL
drop:
- ALL
readOnlyRootFilesystem: true
runAsGroup: 1001
runAsUser: 102
seccompProfile:
type: RuntimeDefault
volumeMounts:
- mountPath: /includes
name: nginx-includes-configmap
- mountPath: /etc/nginx/main-includes
name: nginx-main-includes
nodeSelector:
kubernetes.io/os: linux
securityContext:
Expand All @@ -310,6 +350,9 @@ spec:
name: nginx-cache
- emptyDir: {}
name: nginx-includes
- configMap:
name: nginx-includes
name: nginx-includes-configmap
---
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
Expand Down
Loading
Loading