-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 #452
Changes from 8 commits
fba955c
6484cfb
228464f
be57036
ed27df4
7345e5a
fc4134e
153bc64
32a52b9
999765f
7d44d2d
cb63b32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 string) error { | ||
logger.Debugf("Apply Kubernetes stdin") | ||
out, err := applyKubernetesResourcesStdin(input) | ||
if err != nil { | ||
return errors.Wrap(err, "can't modify Kubernetes resources (apply from stdin)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think you're right in L89, "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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,23 @@ | |
package servicedeployer | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/elastic-package/internal/configuration/locations" | ||
"github.com/elastic/elastic-package/internal/install" | ||
"github.com/elastic/elastic-package/internal/kind" | ||
"github.com/elastic/elastic-package/internal/kubectl" | ||
"github.com/elastic/elastic-package/internal/logger" | ||
) | ||
|
||
const elasticAgentManagedYamlURL = "https://raw.githubusercontent.com/elastic/beats/7.x/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml" | ||
|
||
// KubernetesServiceDeployer is responsible for deploying resources in the Kubernetes cluster. | ||
type KubernetesServiceDeployer struct { | ||
definitionsDir string | ||
|
@@ -144,14 +149,56 @@ func findKubernetesDefinitions(definitionsDir string) ([]string, error) { | |
func installElasticAgentInCluster() error { | ||
logger.Debug("install Elastic Agent in the Kubernetes cluster") | ||
|
||
locationManager, err := locations.NewLocationManager() | ||
elasticAgentManagedYaml, err := getElasticAgentYaml() | ||
if err != nil { | ||
return errors.Wrap(err, "can't locate Kubernetes file for Elastic Agent in ") | ||
return errors.Wrap(err, "can't retrieve Kubernetes file for Elastic Agent") | ||
} | ||
|
||
err = kubectl.Apply(locationManager.KubernetesDeployerAgentYml()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: logger debug: downloaded N bytes |
||
err = kubectl.ApplyStdin(elasticAgentManagedYaml) | ||
if err != nil { | ||
return errors.Wrap(err, "can't install Elastic-Agent in Kubernetes cluster") | ||
} | ||
return nil | ||
} | ||
|
||
// downloadElasticAgentManagedYAML will download a url from a path and return the response body as a string. | ||
func downloadElasticAgentManagedYAML(url string) (string, error) { | ||
// Get the data | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to get file from url %s", url) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: from URL |
||
} | ||
defer resp.Body.Close() | ||
|
||
// Convert to string | ||
b, err := io.ReadAll(resp.Body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it necessary to go with string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not necessary. I did that in order to use it later in Regexp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds good to me |
||
if err != nil { | ||
return "", errors.Wrap(err, "failed to read response body") | ||
} | ||
return string(b), nil | ||
} | ||
|
||
// getElasticAgentYaml retrieves elastic-agent-managed.yaml from upstream and modifies the file as needed | ||
// to run locally. | ||
func getElasticAgentYaml() (string, error) { | ||
appConfig, err := install.Configuration() | ||
if err != nil { | ||
return "", errors.Wrap(err, "can't read application configuration") | ||
} | ||
elasticAgentManagedYaml, err := downloadElasticAgentManagedYAML(elasticAgentManagedYamlURL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: YAML comes as bytes. Did you check if we can skip converting to string? It seems that byte-body will fit well here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please add a logger debug indicating the download from upstream |
||
if err != nil { | ||
return "", errors.Wrapf(err, "downloading failed for file from source %s", elasticAgentManagedYamlURL) | ||
} | ||
|
||
// Set regex to match fleet url from yaml file | ||
fleetURLRegex := regexp.MustCompile("http(s){0,1}:\\/\\/fleet-server:(\\d+)") | ||
// Replace fleet url | ||
elasticAgentManagedYaml = fleetURLRegex.ReplaceAllString(elasticAgentManagedYaml, "http://fleet-server:8220") | ||
|
||
// Set regex to match image name from yaml file | ||
imageRegex := regexp.MustCompile("docker.elastic.co/beats/elastic-agent:\\d.+") | ||
// Replace image name | ||
elasticAgentManagedYaml = imageRegex.ReplaceAllString(elasticAgentManagedYaml, appConfig.DefaultStackImageRefs().ElasticAgent) | ||
|
||
return elasticAgentManagedYaml, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies: | ||
ecs: | ||
reference: [email protected] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,7 @@ | |
- name: service.type | ||
type: keyword | ||
description: Service type | ||
- name: orchestrator.cluster.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To enable dependency management here, you need to include build.yaml file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I will just add this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to format it with
|
||
external: ecs | ||
- name: orchestrator.cluster.url | ||
external: ecs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: a Kubernetes manifest