Skip to content

CP/DP Split: provision NGINX Plus #3148

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
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
CP/DP Split: provision NGINX Plus
Continuation from the previous commit to add support for provisioning with NGINX Plus. This adds support for duplicating any NGINX Plus or docker registry secrets into the Gateway namespace.

Added unit tests.
  • Loading branch information
sjberman committed Feb 21, 2025
commit e387eefc68df79ba08c0a8fcd18c32e88c1f06f7
8 changes: 8 additions & 0 deletions charts/nginx-gateway-fabric/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ spec:
- --gatewayclass={{ .Values.nginxGateway.gatewayClassName }}
- --config={{ include "nginx-gateway.config-name" . }}
- --service={{ include "nginx-gateway.fullname" . }}
{{- if .Values.nginx.imagePullSecret }}
- --nginx-docker-secret={{ .Values.nginx.imagePullSecret }}
{{- end }}
{{- if .Values.nginx.imagePullSecrets }}
{{- range .Values.nginx.imagePullSecrets }}
- --nginx-docker-secret={{ . }}
{{- end }}
{{- end }}
{{- if .Values.nginx.plus }}
- --nginx-plus
{{- if .Values.nginx.usage.secretName }}
Expand Down
16 changes: 14 additions & 2 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func createStaticModeCommand() *cobra.Command {
leaderElectionLockNameFlag = "leader-election-lock-name"
productTelemetryDisableFlag = "product-telemetry-disable"
gwAPIExperimentalFlag = "gateway-api-experimental-features"
nginxDockerSecretFlag = "nginx-docker-secret" //nolint:gosec // not credentials
usageReportSecretFlag = "usage-report-secret"
usageReportEndpointFlag = "usage-report-endpoint"
usageReportResolverFlag = "usage-report-resolver"
Expand Down Expand Up @@ -120,7 +121,10 @@ func createStaticModeCommand() *cobra.Command {

snippetsFilters bool

plus bool
plus bool
nginxDockerSecrets = stringSliceValidatingValue{
validator: validateResourceName,
}
usageReportSkipVerify bool
usageReportSecretName = stringValidatingValue{
validator: validateResourceName,
Expand Down Expand Up @@ -249,7 +253,8 @@ func createStaticModeCommand() *cobra.Command {
Names: flagKeys,
Values: flagValues,
},
SnippetsFilters: snippetsFilters,
SnippetsFilters: snippetsFilters,
NginxDockerSecretNames: nginxDockerSecrets.values,
}

if err := static.StartManager(conf); err != nil {
Expand Down Expand Up @@ -378,6 +383,13 @@ func createStaticModeCommand() *cobra.Command {
"Requires the Gateway APIs installed from the experimental channel.",
)

cmd.Flags().Var(
&nginxDockerSecrets,
nginxDockerSecretFlag,
"The name of the NGINX docker registry Secret(s). Must exist in the same namespace "+
"that the NGINX Gateway Fabric control plane is running in (default namespace: nginx-gateway).",
)

cmd.Flags().Var(
&usageReportSecretName,
usageReportSecretFlag,
Expand Down
27 changes: 27 additions & 0 deletions cmd/gateway/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func TestStaticModeCmdFlagValidation(t *testing.T) {
"--leader-election-lock-name=my-lock",
"--leader-election-disable=false",
"--nginx-plus",
"--nginx-docker-secret=secret1",
"--nginx-docker-secret=secret2",
"--usage-report-secret=my-secret",
"--usage-report-endpoint=example.com",
"--usage-report-resolver=resolver.com",
Expand Down Expand Up @@ -314,6 +316,31 @@ func TestStaticModeCmdFlagValidation(t *testing.T) {
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--leader-election-disable" flag: strconv.ParseBool`,
},
{
name: "nginx-docker-secret is set to empty string",
args: []string{
"--nginx-docker-secret=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--nginx-docker-secret" flag: must be set`,
},
{
name: "nginx-docker-secret is invalid",
args: []string{
"--nginx-docker-secret=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--nginx-docker-secret" flag: invalid format: `,
},
{
name: "one nginx-docker-secret is invalid",
args: []string{
"--nginx-docker-secret=valid",
"--nginx-docker-secret=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--nginx-docker-secret" flag: invalid format: `,
},
{
name: "usage-report-secret is set to empty string",
args: []string{
Expand Down
50 changes: 50 additions & 0 deletions cmd/gateway/validating_types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"bytes"
"encoding/csv"
"fmt"
"strconv"
"strings"

"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -30,6 +33,53 @@ func (v *stringValidatingValue) Type() string {
return "string"
}

// stringSliceValidatingValue is a string slice flag value with custom validation logic.
// it implements the pflag.Value interface.
type stringSliceValidatingValue struct {
validator func(v string) error
values []string
changed bool
}

func (v *stringSliceValidatingValue) String() string {
b := &bytes.Buffer{}
w := csv.NewWriter(b)
err := w.Write(v.values)
if err != nil {
return ""
}

w.Flush()
str := strings.TrimSuffix(b.String(), "\n")
return "[" + str + "]"
}

func (v *stringSliceValidatingValue) Set(param string) error {
if err := v.validator(param); err != nil {
return err
}

stringReader := strings.NewReader(param)
csvReader := csv.NewReader(stringReader)
str, err := csvReader.Read()
if err != nil {
return err
}

if !v.changed {
v.values = str
} else {
v.values = append(v.values, str...)
}
v.changed = true

return nil
}

func (v *stringSliceValidatingValue) Type() string {
return "stringSlice"
}

type intValidatingValue struct {
validator func(v int) error
value int
Expand Down
1 change: 1 addition & 0 deletions deploy/experimental-nginx-plus/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ spec:
- --gatewayclass=nginx
- --config=nginx-gateway-config
- --service=nginx-gateway
- --nginx-docker-secret=nginx-plus-registry-secret
- --nginx-plus
- --usage-report-secret=nplus-license
- --metrics-port=9113
Expand Down
1 change: 1 addition & 0 deletions deploy/nginx-plus/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ spec:
- --gatewayclass=nginx
- --config=nginx-gateway-config
- --service=nginx-gateway
- --nginx-docker-secret=nginx-plus-registry-secret
- --nginx-plus
- --usage-report-secret=nplus-license
- --metrics-port=9113
Expand Down
1 change: 1 addition & 0 deletions deploy/snippets-filters-nginx-plus/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ spec:
- --gatewayclass=nginx
- --config=nginx-gateway-config
- --service=nginx-gateway
- --nginx-docker-secret=nginx-plus-registry-secret
- --nginx-plus
- --usage-report-secret=nplus-license
- --metrics-port=9113
Expand Down
4 changes: 2 additions & 2 deletions internal/framework/controller/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import "fmt"

// CreateNginxResourceName creates the base resource name for all nginx resources
// created by the control plane.
func CreateNginxResourceName(gatewayName, gatewayClassName string) string {
return fmt.Sprintf("%s-%s", gatewayName, gatewayClassName)
func CreateNginxResourceName(prefix, suffix string) string {
return fmt.Sprintf("%s-%s", prefix, suffix)
}
2 changes: 2 additions & 0 deletions internal/mode/static/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Config struct {
ConfigName string
// GatewayClassName is the name of the GatewayClass resource that the Gateway will use.
GatewayClassName string
// NginxDockerSecretNames are the names of any Docker registry Secrets for the NGINX container.
NginxDockerSecretNames []string
// LeaderElection contains the configuration for leader election.
LeaderElection LeaderElectionConfig
// ProductTelemetryConfig contains the configuration for collecting product telemetry.
Expand Down
6 changes: 6 additions & 0 deletions internal/mode/static/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func (h *eventHandlerImpl) HandleEventBatch(ctx context.Context, logger logr.Log
h.sendNginxConfig(ctx, logger, gr, changeType)
}

// enable is called when the pod becomes leader to ensure the provisioner has
// the latest configuration.
func (h *eventHandlerImpl) enable(ctx context.Context) {
h.sendNginxConfig(ctx, h.cfg.logger, h.cfg.processor.GetLatestGraph(), state.ClusterStateChange)
}

func (h *eventHandlerImpl) sendNginxConfig(
ctx context.Context,
logger logr.Logger,
Expand Down
17 changes: 10 additions & 7 deletions internal/mode/static/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,15 @@ func StartManager(cfg config.Config) error {
ctx,
mgr,
provisioner.Config{
DeploymentStore: nginxUpdater.NginxDeployments,
StatusQueue: statusQueue,
Logger: cfg.Logger.WithName("provisioner"),
EventRecorder: recorder,
GatewayPodConfig: cfg.GatewayPodConfig,
GCName: cfg.GatewayClassName,
Plus: cfg.Plus,
DeploymentStore: nginxUpdater.NginxDeployments,
StatusQueue: statusQueue,
Logger: cfg.Logger.WithName("provisioner"),
EventRecorder: recorder,
GatewayPodConfig: &cfg.GatewayPodConfig,
GCName: cfg.GatewayClassName,
Plus: cfg.Plus,
NginxDockerSecretNames: cfg.NginxDockerSecretNames,
PlusUsageConfig: &cfg.UsageReportConfig,
},
)
if err != nil {
Expand Down Expand Up @@ -265,6 +267,7 @@ func StartManager(cfg config.Config) error {
if err = mgr.Add(runnables.NewCallFunctionsAfterBecameLeader([]func(context.Context){
groupStatusUpdater.Enable,
nginxProvisioner.Enable,
eventHandler.enable,
})); err != nil {
return fmt.Errorf("cannot register functions that get called after Pod becomes leader: %w", err)
}
Expand Down
Loading