Skip to content

Commit 6531362

Browse files
authored
Revert: Get elastic-agent-managed-daemonset.yaml from upstream 7.x instead of using a local static file (#459)
1 parent e1c0ac3 commit 6531362

File tree

7 files changed

+37
-95
lines changed

7 files changed

+37
-95
lines changed

internal/configuration/locations/locations.go

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

2424
fieldsCachedDir = "cache/fields"
2525

26-
terraformDeployerYmlFile = "terraform-deployer.yml"
26+
kubernetesDeployerElasticAgentYmlFile = "elastic-agent.yml"
27+
terraformDeployerYmlFile = "terraform-deployer.yml"
2728
)
2829

2930
var (
@@ -83,6 +84,11 @@ func (loc LocationManager) KubernetesDeployerDir() string {
8384
return filepath.Join(loc.stackPath, kubernetesDeployerDir)
8485
}
8586

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+
8692
// TerraformDeployerDir returns the Terraform Directory
8793
func (loc LocationManager) TerraformDeployerDir() string {
8894
return filepath.Join(loc.stackPath, terraformDeployerDir)

internal/install/install.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"path/filepath"
11+
"strings"
1112
"time"
1213

1314
"github.com/pkg/errors"
@@ -59,6 +60,11 @@ func EnsureInstalled() error {
5960
return errors.Wrap(err, "writing stack resources failed")
6061
}
6162

63+
err = writeKubernetesDeployerResources(elasticPackagePath)
64+
if err != nil {
65+
return errors.Wrap(err, "writing Kubernetes deployer resources failed")
66+
}
67+
6268
err = writeTerraformDeployerResources(elasticPackagePath)
6369
if err != nil {
6470
return errors.Wrap(err, "writing Terraform deployer resources failed")
@@ -166,6 +172,26 @@ func writeStackResources(elasticPackagePath *locations.LocationManager) error {
166172

167173
}
168174

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

internal/kubectl/kubectl.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,3 @@ 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: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,6 @@ 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-
10387
func handleApplyCommandOutput(out []byte) error {
10488
logger.Debugf("Extract resources from command output")
10589
resources, err := extractResources(out)

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

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

77
import (
8-
"io"
9-
"net/http"
108
"os"
119
"path/filepath"
12-
"regexp"
1310
"strings"
1411

1512
"github.com/pkg/errors"
1613

17-
"github.com/elastic/elastic-package/internal/install"
14+
"github.com/elastic/elastic-package/internal/configuration/locations"
1815
"github.com/elastic/elastic-package/internal/kind"
1916
"github.com/elastic/elastic-package/internal/kubectl"
2017
"github.com/elastic/elastic-package/internal/logger"
2118
)
2219

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

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

158-
err = kubectl.ApplyStdin(elasticAgentManagedYaml)
152+
err = kubectl.Apply(locationManager.KubernetesDeployerAgentYml())
159153
if err != nil {
160154
return errors.Wrap(err, "can't install Elastic-Agent in Kubernetes cluster")
161155
}
162156
return nil
163157
}
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-
}

test/packages/kubernetes/_dev/build/build.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@
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)