|
5 | 5 | package servicedeployer |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "io" |
8 | 9 | "io/ioutil" |
| 10 | + "net/http" |
9 | 11 | "path/filepath" |
| 12 | + "regexp" |
10 | 13 | "strings" |
11 | 14 |
|
12 | 15 | "github.com/pkg/errors" |
13 | 16 |
|
14 | | - "github.com/elastic/elastic-package/internal/configuration/locations" |
| 17 | + "github.com/elastic/elastic-package/internal/install" |
15 | 18 | "github.com/elastic/elastic-package/internal/kind" |
16 | 19 | "github.com/elastic/elastic-package/internal/kubectl" |
17 | 20 | "github.com/elastic/elastic-package/internal/logger" |
18 | 21 | ) |
19 | 22 |
|
| 23 | +const elasticAgentManagedYamlURL = "https://raw.githubusercontent.com/elastic/beats/7.x/deploy/kubernetes/elastic-agent-managed-kubernetes.yaml" |
| 24 | + |
20 | 25 | // KubernetesServiceDeployer is responsible for deploying resources in the Kubernetes cluster. |
21 | 26 | type KubernetesServiceDeployer struct { |
22 | 27 | definitionsDir string |
@@ -144,14 +149,58 @@ func findKubernetesDefinitions(definitionsDir string) ([]string, error) { |
144 | 149 | func installElasticAgentInCluster() error { |
145 | 150 | logger.Debug("install Elastic Agent in the Kubernetes cluster") |
146 | 151 |
|
147 | | - locationManager, err := locations.NewLocationManager() |
| 152 | + elasticAgentManagedYaml, err := getElasticAgentYAML() |
| 153 | + logger.Debugf("downloaded %d bytes", len(elasticAgentManagedYaml)) |
148 | 154 | 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") |
150 | 156 | } |
151 | 157 |
|
152 | | - err = kubectl.Apply(locationManager.KubernetesDeployerAgentYml()) |
| 158 | + err = kubectl.ApplyStdin(elasticAgentManagedYaml) |
153 | 159 | if err != nil { |
154 | 160 | return errors.Wrap(err, "can't install Elastic-Agent in Kubernetes cluster") |
155 | 161 | } |
156 | 162 | return nil |
157 | 163 | } |
| 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 | +} |
0 commit comments