@@ -14,6 +14,8 @@ import (
1414 "strings"
1515 "time"
1616
17+ "github.com/nginx/kubernetes-ingress/internal/metadata"
18+
1719 license_reporting "github.com/nginx/kubernetes-ingress/internal/license_reporting"
1820 nl "github.com/nginx/kubernetes-ingress/internal/logger"
1921 "github.com/nginx/kubernetes-ingress/internal/metrics/collectors"
@@ -51,8 +53,9 @@ const (
5153)
5254
5355var (
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+)?(-.+)?$` )
5659)
5760
5861// ServerConfig holds the config data for an upstream server in NGINX Plus.
@@ -99,7 +102,7 @@ type Manager interface {
99102 DeleteKeyValStateFiles (virtualServerName string )
100103}
101104
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
103106// updates NGINX Plus upstream servers. It assumes that NGINX is running in the same container.
104107type LocalManager struct {
105108 confdPath string
@@ -119,6 +122,7 @@ type LocalManager struct {
119122 metricsCollector collectors.ManagerCollector
120123 licenseReporter * license_reporting.LicenseReporter
121124 licenseReporterCancel context.CancelFunc
125+ deploymentMetadata * metadata.Metadata
122126 appProtectPluginPid int
123127 appProtectDosAgentPid int
124128 agentPid int
@@ -127,7 +131,7 @@ type LocalManager struct {
127131}
128132
129133// 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 {
131135 l := nl .LoggerFromContext (ctx )
132136 verifyConfigGenerator , err := newVerifyConfigGenerator ()
133137 if err != nil {
@@ -149,6 +153,7 @@ func NewLocalManager(ctx context.Context, confPath string, debug bool, mc collec
149153 verifyClient : newVerifyClient (timeout ),
150154 metricsCollector : mc ,
151155 licenseReporter : lr ,
156+ deploymentMetadata : metadata ,
152157 nginxPlus : nginxPlus ,
153158 logger : l ,
154159 }
@@ -607,10 +612,34 @@ func (lm *LocalManager) AppProtectDosAgentStart(apdaDone chan error, debug bool,
607612
608613// AgentStart starts the AppProtect plugin and sets AppProtect log level.
609614func (lm * LocalManager ) AgentStart (agentDone chan error , instanceGroup string ) {
615+ ctx := nl .ContextWithLogger (context .Background (), lm .logger )
610616 nl .Debugf (lm .logger , "Starting Agent" )
611617 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 )
614643 }
615644 cmd := exec .Command (agentPath , args ... ) // #nosec G204
616645
0 commit comments