@@ -14,6 +14,8 @@ import (
14
14
"strings"
15
15
"time"
16
16
17
+ "github.com/nginx/kubernetes-ingress/internal/metadata"
18
+
17
19
license_reporting "github.com/nginx/kubernetes-ingress/internal/license_reporting"
18
20
nl "github.com/nginx/kubernetes-ingress/internal/logger"
19
21
"github.com/nginx/kubernetes-ingress/internal/metrics/collectors"
@@ -51,8 +53,9 @@ const (
51
53
)
52
54
53
55
var (
54
- ossre = regexp .MustCompile (`(?P<name>\S+)/(?P<version>\S+)` )
55
- plusre = regexp .MustCompile (`(?P<name>\S+)/(?P<version>\S+).\((?P<plus>\S+plus\S+)\)` )
56
+ ossre = regexp .MustCompile (`(?P<name>\S+)/(?P<version>\S+)` )
57
+ plusre = regexp .MustCompile (`(?P<name>\S+)/(?P<version>\S+).\((?P<plus>\S+plus\S+)\)` )
58
+ agentre = regexp .MustCompile (`^v(?P<major>\d+)\.?(?P<minor>\d+)?\.?(?P<patch>\d+)?(-.+)?$` )
56
59
)
57
60
58
61
// ServerConfig holds the config data for an upstream server in NGINX Plus.
@@ -99,7 +102,7 @@ type Manager interface {
99
102
DeleteKeyValStateFiles (virtualServerName string )
100
103
}
101
104
102
- // LocalManager updates NGINX configuration, starts, reloads and quits NGINX, updates License Reporting file
105
+ // LocalManager updates NGINX configuration, starts, reloads and quits NGINX, updates License Reporting and the Deployment Metadata file
103
106
// updates NGINX Plus upstream servers. It assumes that NGINX is running in the same container.
104
107
type LocalManager struct {
105
108
confdPath string
@@ -119,6 +122,7 @@ type LocalManager struct {
119
122
metricsCollector collectors.ManagerCollector
120
123
licenseReporter * license_reporting.LicenseReporter
121
124
licenseReporterCancel context.CancelFunc
125
+ deploymentMetadata * metadata.Metadata
122
126
appProtectPluginPid int
123
127
appProtectDosAgentPid int
124
128
agentPid int
@@ -127,7 +131,7 @@ type LocalManager struct {
127
131
}
128
132
129
133
// NewLocalManager creates a LocalManager.
130
- func NewLocalManager (ctx context.Context , confPath string , debug bool , mc collectors.ManagerCollector , lr * license_reporting.LicenseReporter , timeout time.Duration , nginxPlus bool ) * LocalManager {
134
+ func NewLocalManager (ctx context.Context , confPath string , debug bool , mc collectors.ManagerCollector , lr * license_reporting.LicenseReporter , metadata * metadata. Metadata , timeout time.Duration , nginxPlus bool ) * LocalManager {
131
135
l := nl .LoggerFromContext (ctx )
132
136
verifyConfigGenerator , err := newVerifyConfigGenerator ()
133
137
if err != nil {
@@ -149,6 +153,7 @@ func NewLocalManager(ctx context.Context, confPath string, debug bool, mc collec
149
153
verifyClient : newVerifyClient (timeout ),
150
154
metricsCollector : mc ,
151
155
licenseReporter : lr ,
156
+ deploymentMetadata : metadata ,
152
157
nginxPlus : nginxPlus ,
153
158
logger : l ,
154
159
}
@@ -607,10 +612,34 @@ func (lm *LocalManager) AppProtectDosAgentStart(apdaDone chan error, debug bool,
607
612
608
613
// AgentStart starts the AppProtect plugin and sets AppProtect log level.
609
614
func (lm * LocalManager ) AgentStart (agentDone chan error , instanceGroup string ) {
615
+ ctx := nl .ContextWithLogger (context .Background (), lm .logger )
610
616
nl .Debugf (lm .logger , "Starting Agent" )
611
617
args := []string {}
612
- if len (instanceGroup ) > 0 {
613
- args = append (args , "--instance-group" , instanceGroup )
618
+ nl .Debug (lm .logger , lm .AgentVersion ())
619
+ major , _ , _ , err := ExtractAgentVersionValues (lm .AgentVersion ())
620
+ if err != nil {
621
+ nl .Fatalf (lm .logger , "Failed to extract Agent version: %v" , err )
622
+ }
623
+ if major <= 2 {
624
+ if len (instanceGroup ) > 0 {
625
+ args = append (args , "--instance-group" , instanceGroup )
626
+ }
627
+ }
628
+ if major >= 3 {
629
+ metadataInfo , err := lm .deploymentMetadata .CollectAndWrite (ctx )
630
+ if err != nil {
631
+ nl .Fatalf (lm .logger , "Failed to start NGINX Agent: %v" , err )
632
+ }
633
+ labels := []string {
634
+ fmt .Sprintf ("product_name=%s" , metadataInfo .ProductName ),
635
+ fmt .Sprintf ("product_version=%s" , metadataInfo .ProductVersion ),
636
+ fmt .Sprintf ("cluster_id=%s" , metadataInfo .ClusterID ),
637
+ fmt .Sprintf ("deployment_name=%s" , metadataInfo .DeploymentName ),
638
+ fmt .Sprintf ("deployment_id=%s" , metadataInfo .DeploymentID ),
639
+ fmt .Sprintf ("deployment_namespace=%s" , metadataInfo .DeploymentNamespace ),
640
+ }
641
+ metadataLabels := "--labels=" + strings .Join (labels , "," )
642
+ args = append (args , metadataLabels )
614
643
}
615
644
cmd := exec .Command (agentPath , args ... ) // #nosec G204
616
645
0 commit comments