Skip to content

Commit

Permalink
Fix downloading agent manifest from upstream (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKatsoulis authored Aug 4, 2021
1 parent 470fc80 commit 6e53ef0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 211 deletions.
8 changes: 1 addition & 7 deletions internal/configuration/locations/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ const (

fieldsCachedDir = "cache/fields"

kubernetesDeployerElasticAgentYmlFile = "elastic-agent.yml"
terraformDeployerYmlFile = "terraform-deployer.yml"
terraformDeployerYmlFile = "terraform-deployer.yml"
)

var (
Expand Down Expand Up @@ -84,11 +83,6 @@ func (loc LocationManager) KubernetesDeployerDir() string {
return filepath.Join(loc.stackPath, kubernetesDeployerDir)
}

// KubernetesDeployerAgentYml returns the Kubernetes Deployer Elastic Agent yml
func (loc LocationManager) KubernetesDeployerAgentYml() string {
return filepath.Join(loc.stackPath, kubernetesDeployerDir, kubernetesDeployerElasticAgentYmlFile)
}

// TerraformDeployerDir returns the Terraform Directory
func (loc LocationManager) TerraformDeployerDir() string {
return filepath.Join(loc.stackPath, terraformDeployerDir)
Expand Down
26 changes: 0 additions & 26 deletions internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

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

err = writeKubernetesDeployerResources(elasticPackagePath)
if err != nil {
return errors.Wrap(err, "writing Kubernetes deployer resources failed")
}

err = writeTerraformDeployerResources(elasticPackagePath)
if err != nil {
return errors.Wrap(err, "writing Terraform deployer resources failed")
Expand Down Expand Up @@ -172,26 +166,6 @@ func writeStackResources(elasticPackagePath *locations.LocationManager) error {

}

func writeKubernetesDeployerResources(elasticPackagePath *locations.LocationManager) error {
err := os.MkdirAll(elasticPackagePath.KubernetesDeployerDir(), 0755)
if err != nil {
return errors.Wrapf(err, "creating directory failed (path: %s)", elasticPackagePath.KubernetesDeployerDir())
}

appConfig, err := Configuration()
if err != nil {
return errors.Wrap(err, "can't read application configuration")
}

err = writeStaticResource(err, elasticPackagePath.KubernetesDeployerAgentYml(),
strings.ReplaceAll(kubernetesDeployerElasticAgentYml, "{{ ELASTIC_AGENT_IMAGE_REF }}",
appConfig.DefaultStackImageRefs().ElasticAgent))
if err != nil {
return errors.Wrap(err, "writing static resource failed")
}
return nil
}

func writeTerraformDeployerResources(elasticPackagePath *locations.LocationManager) error {
terraformDeployer := elasticPackagePath.TerraformDeployerDir()
err := os.MkdirAll(terraformDeployer, 0755)
Expand Down
173 changes: 0 additions & 173 deletions internal/install/static_kubernetes_elastic_agent_yml.go

This file was deleted.

18 changes: 18 additions & 0 deletions internal/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,21 @@ func modifyKubernetesResources(action string, definitionPaths ...string) ([]byte
}
return output, nil
}

// applyKubernetesResourcesStdin applies a Kubernetes manifest provided as stdin.
// It returns the resources created as output and an error
func applyKubernetesResourcesStdin(input []byte) ([]byte, error) {
// create kubectl apply command
kubectlCmd := exec.Command("kubectl", "apply", "-f", "-", "-o", "yaml")
//Stdin of kubectl command is the manifest provided
kubectlCmd.Stdin = bytes.NewReader(input)
errOutput := new(bytes.Buffer)
kubectlCmd.Stderr = errOutput

logger.Debugf("run command: %s", kubectlCmd)
output, err := kubectlCmd.Output()
if err != nil {
return nil, errors.Wrapf(err, "kubectl apply failed (stderr=%q)", errOutput.String())
}
return output, nil
}
21 changes: 21 additions & 0 deletions internal/kubectl/kubectl_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ func Apply(definitionPaths ...string) error {
return nil
}

// ApplyStdin function adds resources to the Kubernetes cluster based on provided stdin.
func ApplyStdin(input []byte) error {
logger.Debugf("Apply Kubernetes stdin")
out, err := applyKubernetesResourcesStdin(input)
if err != nil {
return errors.Wrap(err, "can't modify Kubernetes resources (apply stdin)")
}

logger.Debugf("Handle \"apply\" command output")
err = handleApplyCommandOutput(out)
if err != nil {
return errors.Wrap(err, "can't handle command output")
}
return nil
}

func handleApplyCommandOutput(out []byte) error {
logger.Debugf("Extract resources from command output")
resources, err := extractResources(out)
Expand Down Expand Up @@ -113,6 +129,11 @@ func waitForReadyResources(resources []resource) error {
kubeClient.Log = func(s string, i ...interface{}) {
logger.Debugf(s, i...)
}
// In case of elastic-agent daemonset Wait will not work as expected
// because in single node clusters one pod of the daemonset can always
// be unavailable (DaemonSet.spec.updateStrategy.rollingUpdate.maxUnavailable defaults to 1).
// daemonSetReady will return true regardless of the pod not being ready yet.
// Can be solved with multi-node clusters.
err := kubeClient.Wait(resList, readinessTimeout)
if err != nil {
return errors.Wrap(err, "waiter failed")
Expand Down
Loading

0 comments on commit 6e53ef0

Please sign in to comment.