Skip to content

Commit cb992d3

Browse files
author
Kate Osborn
committed
Use Pod UID for install ID and always send deploy ctx
1 parent cd874cf commit cb992d3

File tree

12 files changed

+293
-227
lines changed

12 files changed

+293
-227
lines changed

charts/nginx-gateway-fabric/templates/deployment.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ spec:
5858
valueFrom:
5959
fieldRef:
6060
fieldPath: metadata.name
61+
- name: POD_UID
62+
valueFrom:
63+
fieldRef:
64+
fieldPath: metadata.uid
6165
securityContext:
6266
seccompProfile:
6367
type: RuntimeDefault
@@ -142,6 +146,10 @@ spec:
142146
valueFrom:
143147
fieldRef:
144148
fieldPath: metadata.name
149+
- name: POD_UID
150+
valueFrom:
151+
fieldRef:
152+
fieldPath: metadata.uid
145153
image: {{ .Values.nginxGateway.image.repository }}:{{ default .Chart.AppVersion .Values.nginxGateway.image.tag }}
146154
imagePullPolicy: {{ .Values.nginxGateway.image.pullPolicy }}
147155
name: nginx-gateway

cmd/gateway/commands.go

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static"
2424
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
2525
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/licensing"
26+
ngxConfig "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config"
2627
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
2728
)
2829

@@ -161,16 +162,6 @@ func createStaticModeCommand() *cobra.Command {
161162
return fmt.Errorf("error validating ports: %w", err)
162163
}
163164

164-
podIP := os.Getenv("POD_IP")
165-
if err := validateIP(podIP); err != nil {
166-
return fmt.Errorf("error validating POD_IP environment variable: %w", err)
167-
}
168-
169-
podNsName, err := getPodNsName()
170-
if err != nil {
171-
return fmt.Errorf("could not get pod namespaced name: %w", err)
172-
}
173-
174165
imageSource := os.Getenv("BUILD_AGENT")
175166
if imageSource != "gha" && imageSource != "local" {
176167
imageSource = "unknown"
@@ -215,6 +206,11 @@ func createStaticModeCommand() *cobra.Command {
215206

216207
flagKeys, flagValues := parseFlags(cmd.Flags())
217208

209+
podConfig, err := createGatewayPodConfig(serviceName.value)
210+
if err != nil {
211+
return fmt.Errorf("error creating gateway pod config: %w", err)
212+
}
213+
218214
conf := config.Config{
219215
GatewayCtlrName: gatewayCtlrName.value,
220216
ConfigName: configName.String(),
@@ -223,12 +219,7 @@ func createStaticModeCommand() *cobra.Command {
223219
GatewayClassName: gatewayClassName.value,
224220
GatewayNsName: gwNsName,
225221
UpdateGatewayClassStatus: updateGCStatus,
226-
GatewayPodConfig: config.GatewayPodConfig{
227-
PodIP: podIP,
228-
ServiceName: serviceName.value,
229-
Namespace: podNsName.Namespace,
230-
Name: podNsName.Name,
231-
},
222+
GatewayPodConfig: podConfig,
232223
HealthConfig: config.HealthConfig{
233224
Enabled: !disableHealth,
234225
Port: healthListenPort.value,
@@ -241,7 +232,7 @@ func createStaticModeCommand() *cobra.Command {
241232
LeaderElection: config.LeaderElectionConfig{
242233
Enabled: !disableLeaderElection,
243234
LockName: leaderElectionLockName.String(),
244-
Identity: podNsName.Name,
235+
Identity: podConfig.Name,
245236
},
246237
UsageReportConfig: usageReportConfig,
247238
ProductTelemetryConfig: config.ProductTelemetryConfig{
@@ -539,9 +530,9 @@ func createInitializeCommand() *cobra.Command {
539530
return err
540531
}
541532

542-
podNsName, err := getPodNsName()
533+
podUID, err := getValueFromEnv("POD_UID")
543534
if err != nil {
544-
return fmt.Errorf("could not get pod namespaced name: %w", err)
535+
return fmt.Errorf("could not get pod UID: %w", err)
545536
}
546537

547538
clusterCfg := ctlr.GetConfigOrDie()
@@ -563,15 +554,16 @@ func createInitializeCommand() *cobra.Command {
563554

564555
dcc := licensing.NewDeploymentContextCollector(licensing.DeploymentContextCollectorConfig{
565556
K8sClientReader: k8sReader,
566-
PodNSName: podNsName,
557+
PodUID: podUID,
567558
Logger: logger.WithName("deployCtxCollector"),
568559
})
569560

570561
return initialize(initializeConfig{
571-
fileManager: file.NewStdLibOSFileManager(),
572-
logger: logger,
573-
plus: plus,
574-
collector: dcc,
562+
fileManager: file.NewStdLibOSFileManager(),
563+
fileGenerator: ngxConfig.NewGeneratorImpl(plus, nil, logger.WithName("generator")),
564+
logger: logger,
565+
plus: plus,
566+
collector: dcc,
575567
copy: copyFiles{
576568
srcFileNames: srcFiles,
577569
destDirName: dest,
@@ -652,16 +644,43 @@ func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
652644
return
653645
}
654646

655-
func getPodNsName() (types.NamespacedName, error) {
656-
namespace := os.Getenv("POD_NAMESPACE")
657-
if namespace == "" {
658-
return types.NamespacedName{}, errors.New("POD_NAMESPACE environment variable must be set")
647+
func createGatewayPodConfig(svcName string) (config.GatewayPodConfig, error) {
648+
podIP, err := getValueFromEnv("POD_IP")
649+
if err != nil {
650+
return config.GatewayPodConfig{}, err
659651
}
660652

661-
podName := os.Getenv("POD_NAME")
662-
if podName == "" {
663-
return types.NamespacedName{}, errors.New("POD_NAME environment variable must be set")
653+
podUID, err := getValueFromEnv("POD_UID")
654+
if err != nil {
655+
return config.GatewayPodConfig{}, err
656+
}
657+
658+
ns, err := getValueFromEnv("POD_NAMESPACE")
659+
if err != nil {
660+
return config.GatewayPodConfig{}, err
661+
}
662+
663+
name, err := getValueFromEnv("POD_NAME")
664+
if err != nil {
665+
return config.GatewayPodConfig{}, err
666+
}
667+
668+
c := config.GatewayPodConfig{
669+
PodIP: podIP,
670+
ServiceName: svcName,
671+
Namespace: ns,
672+
Name: name,
673+
UID: podUID,
674+
}
675+
676+
return c, nil
677+
}
678+
679+
func getValueFromEnv(key string) (string, error) {
680+
val := os.Getenv(key)
681+
if val == "" {
682+
return "", fmt.Errorf("environment variable %s not set", key)
664683
}
665684

666-
return types.NamespacedName{Namespace: namespace, Name: podName}, nil
685+
return val, nil
667686
}

cmd/gateway/commands_test.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"io"
56
"os"
67
"testing"
@@ -9,6 +10,8 @@ import (
910
"github.com/spf13/cobra"
1011
"github.com/spf13/pflag"
1112
"k8s.io/apimachinery/pkg/types"
13+
14+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
1215
)
1316

1417
type flagTestCase struct {
@@ -660,24 +663,49 @@ func TestGetBuildInfo(t *testing.T) {
660663
g.Expect(dirtyBuild).To(Not(Equal("unknown")))
661664
}
662665

663-
func TestGetPodNsName(t *testing.T) {
666+
func TestCreateGatewayPodConfig(t *testing.T) {
664667
t.Parallel()
665668
g := NewWithT(t)
666669

670+
// Order matters here
671+
// We start with all env vars set
672+
g.Expect(os.Setenv("POD_IP", "10.0.0.0")).To(Succeed())
673+
g.Expect(os.Setenv("POD_UID", "1234")).To(Succeed())
667674
g.Expect(os.Setenv("POD_NAMESPACE", "default")).To(Succeed())
668675
g.Expect(os.Setenv("POD_NAME", "my-pod")).To(Succeed())
669676

670-
nsname, err := getPodNsName()
677+
expCfg := config.GatewayPodConfig{
678+
PodIP: "10.0.0.0",
679+
ServiceName: "svc",
680+
Namespace: "default",
681+
Name: "my-pod",
682+
UID: "1234",
683+
}
684+
cfg, err := createGatewayPodConfig("svc")
671685
g.Expect(err).To(Not(HaveOccurred()))
672-
g.Expect(nsname).To(Equal(types.NamespacedName{Name: "my-pod", Namespace: "default"}))
673-
674-
g.Expect(os.Unsetenv("POD_NAMESPACE")).To(Succeed())
675-
nsname, err = getPodNsName()
676-
g.Expect(err).To(HaveOccurred())
677-
g.Expect(nsname).To(Equal(types.NamespacedName{}))
686+
g.Expect(cfg).To(Equal(expCfg))
678687

688+
// unset name
679689
g.Expect(os.Unsetenv("POD_NAME")).To(Succeed())
680-
nsname, err = getPodNsName()
681-
g.Expect(err).To(HaveOccurred())
682-
g.Expect(nsname).To(Equal(types.NamespacedName{}))
690+
cfg, err = createGatewayPodConfig("svc")
691+
g.Expect(err).To(MatchError(errors.New("environment variable POD_NAME not set")))
692+
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
693+
694+
// unset namespace
695+
g.Expect(os.Unsetenv("POD_NAMESPACE")).To(Succeed())
696+
cfg, err = createGatewayPodConfig("svc")
697+
g.Expect(err).To(MatchError(errors.New("environment variable POD_NAMESPACE not set")))
698+
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
699+
700+
// unset UUID
701+
g.Expect(os.Unsetenv("POD_UID")).To(Succeed())
702+
cfg, err = createGatewayPodConfig("svc")
703+
g.Expect(err).To(MatchError(errors.New("environment variable POD_UID not set")))
704+
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
705+
706+
// unset IP
707+
g.Expect(os.Unsetenv("POD_IP")).To(Succeed())
708+
cfg, err = createGatewayPodConfig("svc")
709+
g.Expect(err).To(MatchError(errors.New("environment variable POD_IP not set")))
710+
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
683711
}

cmd/gateway/initialize.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/licensing"
1212
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config"
1313
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
14-
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
1514
)
1615

1716
type copyFiles struct {
@@ -20,11 +19,12 @@ type copyFiles struct {
2019
}
2120

2221
type initializeConfig struct {
23-
collector licensing.Collector
24-
fileManager file.OSFileManager
25-
logger logr.Logger
26-
copy copyFiles
27-
plus bool
22+
collector licensing.Collector
23+
fileManager file.OSFileManager
24+
fileGenerator config.Generator
25+
logger logr.Logger
26+
copy copyFiles
27+
plus bool
2828
}
2929

3030
func initialize(cfg initializeConfig) error {
@@ -44,30 +44,22 @@ func initialize(cfg initializeConfig) error {
4444

4545
depCtx, err := cfg.collector.Collect(ctx)
4646
if err != nil {
47-
return fmt.Errorf("failed to collect deployment context: %w", err)
47+
cfg.logger.Error(err, "error collecting deployment context")
4848
}
4949

5050
cfg.logger.Info("Deployment context collected", "deployment context", depCtx)
5151

52-
if err := writeDeploymentContextFile(cfg.fileManager, depCtx); err != nil {
53-
return fmt.Errorf("failed to write deployment context file: %w", err)
54-
}
55-
56-
cfg.logger.Info("Finished initializing configuration")
57-
58-
return nil
59-
}
60-
61-
func writeDeploymentContextFile(osFileManager file.OSFileManager, depCtx dataplane.DeploymentContext) error {
62-
depCtxFile, err := config.GenerateDeploymentContextFile(depCtx)
52+
depCtxFile, err := cfg.fileGenerator.GenerateDeploymentContext(depCtx)
6353
if err != nil {
6454
return fmt.Errorf("failed to generate deployment context file: %w", err)
6555
}
6656

67-
if err := file.WriteFile(osFileManager, depCtxFile); err != nil {
57+
if err := file.WriteFile(cfg.fileManager, depCtxFile); err != nil {
6858
return fmt.Errorf("failed to write deployment context file: %w", err)
6959
}
7060

61+
cfg.logger.Info("Finished initializing configuration")
62+
7163
return nil
7264
}
7365

0 commit comments

Comments
 (0)