@@ -3,9 +3,7 @@ package main
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "io"
7
6
"os"
8
- "path/filepath"
9
7
"runtime/debug"
10
8
"strconv"
11
9
"time"
@@ -16,14 +14,20 @@ import (
16
14
"k8s.io/apimachinery/pkg/types"
17
15
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
18
16
"k8s.io/klog/v2"
17
+ ctlr "sigs.k8s.io/controller-runtime"
18
+ "sigs.k8s.io/controller-runtime/pkg/client"
19
19
"sigs.k8s.io/controller-runtime/pkg/log"
20
20
ctlrZap "sigs.k8s.io/controller-runtime/pkg/log/zap"
21
21
22
22
"github.com./nginxinc/nginx-gateway-fabric/internal/mode/provisioner"
23
23
"github.com./nginxinc/nginx-gateway-fabric/internal/mode/static"
24
24
"github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/config"
25
+ "github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/licensing"
26
+ ngxConfig "github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config"
27
+ "github.com./nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
25
28
)
26
29
30
+ // These flags are shared by multiple commands.
27
31
const (
28
32
domain = "gateway.nginx.org"
29
33
gatewayClassFlag = "gatewayclass"
@@ -32,6 +36,7 @@ const (
32
36
gatewayCtlrNameFlag = "gateway-ctlr-name"
33
37
gatewayCtlrNameUsageFmt = `The name of the Gateway controller. ` +
34
38
`The controller name must be of the form: DOMAIN/PATH. The controller's domain is '%s'`
39
+ plusFlag = "nginx-plus"
35
40
)
36
41
37
42
func createRootCommand () * cobra.Command {
@@ -47,7 +52,6 @@ func createRootCommand() *cobra.Command {
47
52
return rootCmd
48
53
}
49
54
50
- //nolint:gocyclo
51
55
func createStaticModeCommand () * cobra.Command {
52
56
// flag names
53
57
const (
@@ -63,7 +67,6 @@ func createStaticModeCommand() *cobra.Command {
63
67
leaderElectionDisableFlag = "leader-election-disable"
64
68
leaderElectionLockNameFlag = "leader-election-lock-name"
65
69
productTelemetryDisableFlag = "product-telemetry-disable"
66
- plusFlag = "nginx-plus"
67
70
gwAPIExperimentalFlag = "gateway-api-experimental-features"
68
71
usageReportSecretFlag = "usage-report-secret"
69
72
usageReportEndpointFlag = "usage-report-endpoint"
@@ -159,21 +162,6 @@ func createStaticModeCommand() *cobra.Command {
159
162
return fmt .Errorf ("error validating ports: %w" , err )
160
163
}
161
164
162
- podIP := os .Getenv ("POD_IP" )
163
- if err := validateIP (podIP ); err != nil {
164
- return fmt .Errorf ("error validating POD_IP environment variable: %w" , err )
165
- }
166
-
167
- namespace := os .Getenv ("POD_NAMESPACE" )
168
- if namespace == "" {
169
- return errors .New ("POD_NAMESPACE environment variable must be set" )
170
- }
171
-
172
- podName := os .Getenv ("POD_NAME" )
173
- if podName == "" {
174
- return errors .New ("POD_NAME environment variable must be set" )
175
- }
176
-
177
165
imageSource := os .Getenv ("BUILD_AGENT" )
178
166
if imageSource != "gha" && imageSource != "local" {
179
167
imageSource = "unknown"
@@ -218,6 +206,11 @@ func createStaticModeCommand() *cobra.Command {
218
206
219
207
flagKeys , flagValues := parseFlags (cmd .Flags ())
220
208
209
+ podConfig , err := createGatewayPodConfig (serviceName .value )
210
+ if err != nil {
211
+ return fmt .Errorf ("error creating gateway pod config: %w" , err )
212
+ }
213
+
221
214
conf := config.Config {
222
215
GatewayCtlrName : gatewayCtlrName .value ,
223
216
ConfigName : configName .String (),
@@ -226,12 +219,7 @@ func createStaticModeCommand() *cobra.Command {
226
219
GatewayClassName : gatewayClassName .value ,
227
220
GatewayNsName : gwNsName ,
228
221
UpdateGatewayClassStatus : updateGCStatus ,
229
- GatewayPodConfig : config.GatewayPodConfig {
230
- PodIP : podIP ,
231
- ServiceName : serviceName .value ,
232
- Namespace : namespace ,
233
- Name : podName ,
234
- },
222
+ GatewayPodConfig : podConfig ,
235
223
HealthConfig : config.HealthConfig {
236
224
Enabled : ! disableHealth ,
237
225
Port : healthListenPort .value ,
@@ -244,7 +232,7 @@ func createStaticModeCommand() *cobra.Command {
244
232
LeaderElection : config.LeaderElectionConfig {
245
233
Enabled : ! disableLeaderElection ,
246
234
LockName : leaderElectionLockName .String (),
247
- Identity : podName ,
235
+ Identity : podConfig . Name ,
248
236
},
249
237
UsageReportConfig : usageReportConfig ,
250
238
ProductTelemetryConfig : config.ProductTelemetryConfig {
@@ -524,29 +512,63 @@ func createSleepCommand() *cobra.Command {
524
512
return cmd
525
513
}
526
514
527
- func createCopyCommand () * cobra.Command {
515
+ func createInitializeCommand () * cobra.Command {
528
516
// flag names
529
517
const srcFlag = "source"
530
518
const destFlag = "destination"
519
+
531
520
// flag values
532
521
var srcFiles []string
533
522
var dest string
523
+ var plus bool
534
524
535
525
cmd := & cobra.Command {
536
- Use : "copy " ,
537
- Short : "Copy files to another directory " ,
526
+ Use : "initialize " ,
527
+ Short : "Write initial configuration files " ,
538
528
RunE : func (_ * cobra.Command , _ []string ) error {
539
- if err := validateSleepArgs (srcFiles , dest ); err != nil {
529
+ if err := validateCopyArgs (srcFiles , dest ); err != nil {
540
530
return err
541
531
}
542
532
543
- for _ , src := range srcFiles {
544
- if err := copyFile (src , dest ); err != nil {
545
- return err
546
- }
533
+ podUID , err := getValueFromEnv ("POD_UID" )
534
+ if err != nil {
535
+ return fmt .Errorf ("could not get pod UID: %w" , err )
547
536
}
548
537
549
- return nil
538
+ clusterCfg := ctlr .GetConfigOrDie ()
539
+ k8sReader , err := client .New (clusterCfg , client.Options {})
540
+ if err != nil {
541
+ return fmt .Errorf ("unable to initialize k8s client: %w" , err )
542
+ }
543
+
544
+ logger := ctlrZap .New ()
545
+ klog .SetLogger (logger )
546
+ logger .Info (
547
+ "Starting init container" ,
548
+ "source filenames to copy" , srcFiles ,
549
+ "destination directory" , dest ,
550
+ "nginx-plus" ,
551
+ plus ,
552
+ )
553
+ log .SetLogger (logger )
554
+
555
+ dcc := licensing .NewDeploymentContextCollector (licensing.DeploymentContextCollectorConfig {
556
+ K8sClientReader : k8sReader ,
557
+ PodUID : podUID ,
558
+ Logger : logger .WithName ("deployCtxCollector" ),
559
+ })
560
+
561
+ return initialize (initializeConfig {
562
+ fileManager : file .NewStdLibOSFileManager (),
563
+ fileGenerator : ngxConfig .NewGeneratorImpl (plus , nil , logger .WithName ("generator" )),
564
+ logger : logger ,
565
+ plus : plus ,
566
+ collector : dcc ,
567
+ copy : copyFiles {
568
+ srcFileNames : srcFiles ,
569
+ destDirName : dest ,
570
+ },
571
+ })
550
572
},
551
573
}
552
574
@@ -564,31 +586,18 @@ func createCopyCommand() *cobra.Command {
564
586
"The destination directory for the source files to be copied to" ,
565
587
)
566
588
589
+ cmd .Flags ().BoolVar (
590
+ & plus ,
591
+ plusFlag ,
592
+ false ,
593
+ "Use NGINX Plus" ,
594
+ )
595
+
567
596
cmd .MarkFlagsRequiredTogether (srcFlag , destFlag )
568
597
569
598
return cmd
570
599
}
571
600
572
- func copyFile (src , dest string ) error {
573
- srcFile , err := os .Open (src )
574
- if err != nil {
575
- return fmt .Errorf ("error opening source file: %w" , err )
576
- }
577
- defer srcFile .Close ()
578
-
579
- destFile , err := os .Create (filepath .Join (dest , filepath .Base (src )))
580
- if err != nil {
581
- return fmt .Errorf ("error creating destination file: %w" , err )
582
- }
583
- defer destFile .Close ()
584
-
585
- if _ , err := io .Copy (destFile , srcFile ); err != nil {
586
- return fmt .Errorf ("error copying file contents: %w" , err )
587
- }
588
-
589
- return nil
590
- }
591
-
592
601
func parseFlags (flags * pflag.FlagSet ) ([]string , []string ) {
593
602
var flagKeys , flagValues []string
594
603
@@ -634,3 +643,44 @@ func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
634
643
635
644
return
636
645
}
646
+
647
+ func createGatewayPodConfig (svcName string ) (config.GatewayPodConfig , error ) {
648
+ podIP , err := getValueFromEnv ("POD_IP" )
649
+ if err != nil {
650
+ return config.GatewayPodConfig {}, err
651
+ }
652
+
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 )
683
+ }
684
+
685
+ return val , nil
686
+ }
0 commit comments