@@ -57,7 +57,7 @@ type kubernetesDeployedAgent struct {
5757}
5858
5959func (s kubernetesDeployedAgent ) TearDown (ctx context.Context ) error {
60- elasticAgentManagedYaml , err := getElasticAgentYAML (s .profile , s .stackVersion , s .agentInfo . Policy . Name , s .agentName )
60+ elasticAgentManagedYaml , err := getElasticAgentYAML (ctx , s .profile , s .agentInfo , s .stackVersion , s .agentName )
6161 if err != nil {
6262 return fmt .Errorf ("can't retrieve Kubernetes file for Elastic Agent: %w" , err )
6363 }
@@ -123,7 +123,7 @@ func (ksd *KubernetesAgentDeployer) SetUp(ctx context.Context, agentInfo AgentIn
123123 if ksd .runTearDown || ksd .runTestsOnly {
124124 logger .Debug ("Skip install Elastic Agent in cluster" )
125125 } else {
126- err = installElasticAgentInCluster (ctx , ksd .profile , ksd . stackVersion , agentInfo . Policy . Name , agentName )
126+ err = installElasticAgentInCluster (ctx , ksd .profile , agentInfo , ksd . stackVersion , agentName )
127127 if err != nil {
128128 return nil , fmt .Errorf ("can't install Elastic-Agent in the Kubernetes cluster: %w" , err )
129129 }
@@ -155,10 +155,10 @@ func (ksd *KubernetesAgentDeployer) agentName() string {
155155
156156var _ AgentDeployer = new (KubernetesAgentDeployer )
157157
158- func installElasticAgentInCluster (ctx context.Context , profile * profile.Profile , stackVersion , policyName , agentName string ) error {
158+ func installElasticAgentInCluster (ctx context.Context , profile * profile.Profile , agentInfo AgentInfo , stackVersion , agentName string ) error {
159159 logger .Debug ("install Elastic Agent in the Kubernetes cluster" )
160160
161- elasticAgentManagedYaml , err := getElasticAgentYAML (profile , stackVersion , policyName , agentName )
161+ elasticAgentManagedYaml , err := getElasticAgentYAML (ctx , profile , agentInfo , stackVersion , agentName )
162162 if err != nil {
163163 return fmt .Errorf ("can't retrieve Kubernetes file for Elastic Agent: %w" , err )
164164 }
@@ -176,8 +176,36 @@ func installElasticAgentInCluster(ctx context.Context, profile *profile.Profile,
176176//go:embed _static/elastic-agent-managed.yaml.tmpl
177177var elasticAgentManagedYamlTmpl string
178178
179- func getElasticAgentYAML (profile * profile.Profile , stackVersion , policyName , agentName string ) ([]byte , error ) {
179+ func getElasticAgentYAML (ctx context. Context , profile * profile.Profile , agentInfo AgentInfo , stackVersion , agentName string ) ([]byte , error ) {
180180 logger .Debugf ("Prepare YAML definition for Elastic Agent running in stack v%s" , stackVersion )
181+ config , err := stack .LoadConfig (profile )
182+ if err != nil {
183+ return nil , fmt .Errorf ("failed to load config from profile: %w" , err )
184+ }
185+ fleetURL := "https://fleet-server:8220"
186+ kibanaURL := "https://kibana:5601"
187+ if config .Provider != stack .ProviderCompose {
188+ kibanaURL = config .KibanaHost
189+ }
190+ if url , ok := config .Parameters [stack .ParamServerlessFleetURL ]; ok {
191+ fleetURL = url
192+ }
193+ if version , ok := config .Parameters [stack .ParamServerlessLocalStackVersion ]; ok {
194+ stackVersion = version
195+ }
196+
197+ enrollmentToken := ""
198+ if config .ElasticsearchAPIKey != "" {
199+ // TODO: Review if this is the correct place to get the enrollment token.
200+ kibanaClient , err := stack .NewKibanaClientFromProfile (profile )
201+ if err != nil {
202+ return nil , fmt .Errorf ("failed to create kibana client: %w" , err )
203+ }
204+ enrollmentToken , err = kibanaClient .GetEnrollmentTokenForPolicyID (ctx , agentInfo .Policy .ID )
205+ if err != nil {
206+ return nil , fmt .Errorf ("failed to get enrollment token for policy %q: %w" , agentInfo .Policy .Name , err )
207+ }
208+ }
181209
182210 appConfig , err := install .Configuration (install .OptionWithStackVersion (stackVersion ))
183211 if err != nil {
@@ -193,11 +221,14 @@ func getElasticAgentYAML(profile *profile.Profile, stackVersion, policyName, age
193221
194222 var elasticAgentYaml bytes.Buffer
195223 err = tmpl .Execute (& elasticAgentYaml , map [string ]string {
196- "fleetURL" : "https://fleet-server:8220" ,
197- "kibanaURL" : "https://kibana:5601" ,
224+ "fleetURL" : fleetURL ,
225+ "kibanaURL" : kibanaURL ,
226+ "username" : config .ElasticsearchUsername ,
227+ "password" : config .ElasticsearchPassword ,
228+ "enrollmentToken" : enrollmentToken ,
198229 "caCertPem" : caCert ,
199230 "elasticAgentImage" : appConfig .StackImageRefs ().ElasticAgent ,
200- "elasticAgentTokenPolicyName" : getTokenPolicyName (stackVersion , policyName ),
231+ "elasticAgentTokenPolicyName" : getTokenPolicyName (stackVersion , agentInfo . Policy . Name ),
201232 "agentName" : agentName ,
202233 })
203234 if err != nil {
0 commit comments