Skip to content

Commit 6e53ef0

Browse files
Fix downloading agent manifest from upstream (#461)
1 parent 470fc80 commit 6e53ef0

File tree

7 files changed

+125
-211
lines changed

7 files changed

+125
-211
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
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"os"
1010
"path/filepath"
11-
"strings"
1211
"time"
1312

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

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

173167
}
174168

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-
195169
func writeTerraformDeployerResources(elasticPackagePath *locations.LocationManager) error {
196170
terraformDeployer := elasticPackagePath.TerraformDeployerDir()
197171
err := os.MkdirAll(terraformDeployer, 0755)

internal/install/static_kubernetes_elastic_agent_yml.go

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

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: 21 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)
@@ -113,6 +129,11 @@ func waitForReadyResources(resources []resource) error {
113129
kubeClient.Log = func(s string, i ...interface{}) {
114130
logger.Debugf(s, i...)
115131
}
132+
// In case of elastic-agent daemonset Wait will not work as expected
133+
// because in single node clusters one pod of the daemonset can always
134+
// be unavailable (DaemonSet.spec.updateStrategy.rollingUpdate.maxUnavailable defaults to 1).
135+
// daemonSetReady will return true regardless of the pod not being ready yet.
136+
// Can be solved with multi-node clusters.
116137
err := kubeClient.Wait(resList, readinessTimeout)
117138
if err != nil {
118139
return errors.Wrap(err, "waiter failed")

0 commit comments

Comments
 (0)