Skip to content

Commit e815623

Browse files
Get elastic-agent-managed-daemonset.yaml from upstream 7.x instead of using a local static file (#452)
* Get elastic-agent-managed-daemonset.yaml from upstream 7.x instead of using a local static file
1 parent b7d8e15 commit e815623

File tree

7 files changed

+95
-37
lines changed

7 files changed

+95
-37
lines changed

internal/configuration/locations/locations.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const (
2323

2424
fieldsCachedDir = "cache/fields"
2525

26-
kubernetesDeployerElasticAgentYmlFile = "elastic-agent.yml"
27-
terraformDeployerYmlFile = "terraform-deployer.yml"
26+
terraformDeployerYmlFile = "terraform-deployer.yml"
2827
)
2928

3029
var (
@@ -84,11 +83,6 @@ func (loc LocationManager) KubernetesDeployerDir() string {
8483
return filepath.Join(loc.stackPath, kubernetesDeployerDir)
8584
}
8685

87-
// KubernetesDeployerAgentYml returns the Kubernetes Deployer Elastic Agent yml
88-
func (loc LocationManager) KubernetesDeployerAgentYml() string {
89-
return filepath.Join(loc.stackPath, kubernetesDeployerDir, kubernetesDeployerElasticAgentYmlFile)
90-
}
91-
9286
// TerraformDeployerDir returns the Terraform Directory
9387
func (loc LocationManager) TerraformDeployerDir() string {
9488
return filepath.Join(loc.stackPath, terraformDeployerDir)

internal/install/install.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io/ioutil"
1010
"os"
1111
"path/filepath"
12-
"strings"
1312
"time"
1413

1514
"github.com/pkg/errors"
@@ -61,11 +60,6 @@ func EnsureInstalled() error {
6160
return errors.Wrap(err, "writing stack resources failed")
6261
}
6362

64-
err = writeKubernetesDeployerResources(elasticPackagePath)
65-
if err != nil {
66-
return errors.Wrap(err, "writing Kubernetes deployer resources failed")
67-
}
68-
6963
err = writeTerraformDeployerResources(elasticPackagePath)
7064
if err != nil {
7165
return errors.Wrap(err, "writing Terraform deployer resources failed")
@@ -173,26 +167,6 @@ func writeStackResources(elasticPackagePath *locations.LocationManager) error {
173167

174168
}
175169

176-
func writeKubernetesDeployerResources(elasticPackagePath *locations.LocationManager) error {
177-
err := os.MkdirAll(elasticPackagePath.KubernetesDeployerDir(), 0755)
178-
if err != nil {
179-
return errors.Wrapf(err, "creating directory failed (path: %s)", elasticPackagePath.KubernetesDeployerDir())
180-
}
181-
182-
appConfig, err := Configuration()
183-
if err != nil {
184-
return errors.Wrap(err, "can't read application configuration")
185-
}
186-
187-
err = writeStaticResource(err, elasticPackagePath.KubernetesDeployerAgentYml(),
188-
strings.ReplaceAll(kubernetesDeployerElasticAgentYml, "{{ ELASTIC_AGENT_IMAGE_REF }}",
189-
appConfig.DefaultStackImageRefs().ElasticAgent))
190-
if err != nil {
191-
return errors.Wrap(err, "writing static resource failed")
192-
}
193-
return nil
194-
}
195-
196170
func writeTerraformDeployerResources(elasticPackagePath *locations.LocationManager) error {
197171
terraformDeployer := elasticPackagePath.TerraformDeployerDir()
198172
err := os.MkdirAll(terraformDeployer, 0755)

internal/kubectl/kubectl.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ func modifyKubernetesResources(action string, definitionPaths ...string) ([]byte
4949
}
5050
return output, nil
5151
}
52+
53+
// applyKubernetesResourcesStdin applies a Kubernetes manifest provided as stdin.
54+
// It returns the resources created as output and an error
55+
func applyKubernetesResourcesStdin(input []byte) ([]byte, error) {
56+
// create kubectl apply command
57+
kubectlCmd := exec.Command("kubectl", "apply", "-f", "-", "-o", "yaml")
58+
//Stdin of kubectl command is the manifest provided
59+
kubectlCmd.Stdin = bytes.NewReader(input)
60+
errOutput := new(bytes.Buffer)
61+
kubectlCmd.Stderr = errOutput
62+
63+
logger.Debugf("run command: %s", kubectlCmd)
64+
output, err := kubectlCmd.Output()
65+
if err != nil {
66+
return nil, errors.Wrapf(err, "kubectl apply failed (stderr=%q)", errOutput.String())
67+
}
68+
return output, nil
69+
}

internal/kubectl/kubectl_apply.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ func Apply(definitionPaths ...string) error {
8484
return nil
8585
}
8686

87+
// ApplyStdin function adds resources to the Kubernetes cluster based on provided stdin.
88+
func ApplyStdin(input []byte) error {
89+
logger.Debugf("Apply Kubernetes stdin")
90+
out, err := applyKubernetesResourcesStdin(input)
91+
if err != nil {
92+
return errors.Wrap(err, "can't modify Kubernetes resources (apply stdin)")
93+
}
94+
95+
logger.Debugf("Handle \"apply\" command output")
96+
err = handleApplyCommandOutput(out)
97+
if err != nil {
98+
return errors.Wrap(err, "can't handle command output")
99+
}
100+
return nil
101+
}
102+
87103
func handleApplyCommandOutput(out []byte) error {
88104
logger.Debugf("Extract resources from command output")
89105
resources, err := extractResources(out)

internal/testrunner/runners/system/servicedeployer/kubernetes.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
package servicedeployer
66

77
import (
8+
"io"
89
"io/ioutil"
10+
"net/http"
911
"path/filepath"
12+
"regexp"
1013
"strings"
1114

1215
"github.com/pkg/errors"
1316

14-
"github.com/elastic/elastic-package/internal/configuration/locations"
17+
"github.com/elastic/elastic-package/internal/install"
1518
"github.com/elastic/elastic-package/internal/kind"
1619
"github.com/elastic/elastic-package/internal/kubectl"
1720
"github.com/elastic/elastic-package/internal/logger"
1821
)
1922

23+
const elasticAgentManagedYamlURL = "https://raw.githubusercontent.com/elastic/beats/7.x/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml"
24+
2025
// KubernetesServiceDeployer is responsible for deploying resources in the Kubernetes cluster.
2126
type KubernetesServiceDeployer struct {
2227
definitionsDir string
@@ -144,14 +149,58 @@ func findKubernetesDefinitions(definitionsDir string) ([]string, error) {
144149
func installElasticAgentInCluster() error {
145150
logger.Debug("install Elastic Agent in the Kubernetes cluster")
146151

147-
locationManager, err := locations.NewLocationManager()
152+
elasticAgentManagedYaml, err := getElasticAgentYAML()
153+
logger.Debugf("downloaded %d bytes", len(elasticAgentManagedYaml))
148154
if err != nil {
149-
return errors.Wrap(err, "can't locate Kubernetes file for Elastic Agent in ")
155+
return errors.Wrap(err, "can't retrieve Kubernetes file for Elastic Agent")
150156
}
151157

152-
err = kubectl.Apply(locationManager.KubernetesDeployerAgentYml())
158+
err = kubectl.ApplyStdin(elasticAgentManagedYaml)
153159
if err != nil {
154160
return errors.Wrap(err, "can't install Elastic-Agent in Kubernetes cluster")
155161
}
156162
return nil
157163
}
164+
165+
// downloadElasticAgentManagedYAML will download a url from a path and return the response body.
166+
func downloadElasticAgentManagedYAML(url string) ([]byte, error) {
167+
// Get the data
168+
resp, err := http.Get(url)
169+
if err != nil {
170+
return nil, errors.Wrapf(err, "failed to get file from URL %s", url)
171+
}
172+
defer resp.Body.Close()
173+
174+
b, err := io.ReadAll(resp.Body)
175+
if err != nil {
176+
return nil, errors.Wrap(err, "failed to read response body")
177+
}
178+
return b, nil
179+
}
180+
181+
// getElasticAgentYAML retrieves elastic-agent-managed.yaml from upstream and modifies the file as needed
182+
// to run locally.
183+
func getElasticAgentYAML() ([]byte, error) {
184+
appConfig, err := install.Configuration()
185+
if err != nil {
186+
return nil, errors.Wrap(err, "can't read application configuration")
187+
}
188+
189+
logger.Debugf("downloading elastic-agent-managed-kubernetes.yaml from %s", elasticAgentManagedYamlURL)
190+
elasticAgentManagedYaml, err := downloadElasticAgentManagedYAML(elasticAgentManagedYamlURL)
191+
if err != nil {
192+
return nil, errors.Wrapf(err, "downloading failed for file from source %s", elasticAgentManagedYamlURL)
193+
}
194+
195+
// Set regex to match fleet url from yaml file
196+
fleetURLRegex := regexp.MustCompile("http(s){0,1}:\\/\\/fleet-server:(\\d+)")
197+
// Replace fleet url
198+
elasticAgentManagedYaml = fleetURLRegex.ReplaceAll(elasticAgentManagedYaml, []byte("http://fleet-server:8220"))
199+
200+
// Set regex to match image name from yaml file
201+
imageRegex := regexp.MustCompile("docker.elastic.co/beats/elastic-agent:\\d.+")
202+
// Replace image name
203+
elasticAgentManagedYaml = imageRegex.ReplaceAll(elasticAgentManagedYaml, []byte(appConfig.DefaultStackImageRefs().ElasticAgent))
204+
205+
return elasticAgentManagedYaml, nil
206+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies:
2+
ecs:
3+
reference: [email protected]

test/packages/kubernetes/data_stream/pod/fields/ecs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
- name: service.type
88
type: keyword
99
description: Service type
10+
- name: orchestrator.cluster.name
11+
external: ecs
12+
- name: orchestrator.cluster.url
13+
external: ecs

0 commit comments

Comments
 (0)