Skip to content
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

feat: add helm package mage target #7232

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
20 changes: 12 additions & 8 deletions .buildkite/pipeline.elastic-agent-helm-charts.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/buildkite/pipeline-schema/main/schema.json
env:
HELM_REPO_ENV: ${HELM_REPO_ENV:-dev}
SNAPSHOT: ${SNAPSHOT:-true}

steps:

- label: ":elastic-stack: Create helm chart"
command: |
echo "TBC"

- wait: ~

- label: ":elastic-stack: Publish helm chart"
command: |
echo "TBC"
.buildkite/scripts/steps/helm-charts.sh
plugins:
- elastic/oblt-google-auth#feature/support-different-projects:
lifetime: 1800 # seconds
project-id: "elastic-observability-ci"
project-number: "911195782929"
agents:
provider: "gcp"
machineType: "n1-standard-8"
23 changes: 23 additions & 0 deletions .buildkite/scripts/steps/helm-charts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See

image

# This script runs the helm-charts for the given environment
# uploads the package to GCS and
# triggers the unified-release-publish-helm-charts pipeline.

# shellcheck disable=SC1091
source .buildkite/scripts/common.sh

set -euo pipefail

echo "--- mage helm:package"
mage helm:package

echo "--- upload package tests"
STORAGE=elastic-agent-helm-chart
gcloud storage cp elastic-agent-*.tgz gs://"${STORAGE}" --print-created-message

echo "--- load trigger pipeline"
HELM_CHART_FILE=$(ls -1 elastic-agent-*.tgz)
CHART_URL="https://storage.googleapis.com/${STORAGE}/${HELM_CHART_FILE}"
export CHART_URL
.buildkite/scripts/steps/trigger-publish-helm-charts.sh
.buildkite/scripts/steps/trigger-publish-helm-charts.sh | buildkite-agent pipeline upload
28 changes: 28 additions & 0 deletions .buildkite/scripts/steps/trigger-publish-helm-charts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
#
# Create a dynamic buildkite step that triggers the
# unified-release-publish-helm-charts pipeline.
#
# Required environment variables:
# - HELM_REPO_ENV
# - CHART_URL
#

set -eo pipefail

HELM_REPO_ENV=${HELM_REPO_ENV:-"dev"}

if [ -z "$CHART_URL" ] ; then
echo "CHART_URL could not be found."
exit 1
fi

cat << EOF
- label: ":elastic-stack: Publish helm chart"
trigger: unified-release-publish-helm-charts
build:
message: "publish helm-chart for elastic-agent in ${HELM_REPO_ENV}"
env:
CHARTS_URL: "${CHART_URL}"
HELM_REPO_ENV: ${HELM_REPO_ENV}
EOF
66 changes: 66 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3628,10 +3628,76 @@ func updateYamlFile(path string, keyVal ...struct {
return nil
}

// BuildDependencies builds the dependencies for the Elastic-Agent Helm chart.
func (Helm) BuildDependencies() error {
return helm.BuildChartDependencies(helmChartPath)
}

// Package packages the Elastic-Agent Helm chart. Note that you need to set SNAPSHOT="false" to build a production-ready package.
func (h Helm) Package() error {
mg.SerialDeps(h.BuildDependencies)

agentVersion := bversion.GetParsedAgentPackageVersion()
agentCoreVersion := agentVersion.CoreVersion()
agentImageTag := agentCoreVersion + "-SNAPSHOT"
Copy link
Member

@v1v v1v Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason SNAPSHOT=true is not honoured

CHARTS_URL: "https://storage.googleapis.com/elastic-agent-helm-chart/elastic-agent-9.1.0-beta.tgz"

when using SNAPSHOT=true mage helm:package

is that the expected behaviour?


// need to explicitly set SNAPSHOT="false" to produce a production-ready package
productionPackage := os.Getenv("SNAPSHOT") == "false"

agentChartVersion := agentCoreVersion + "-beta"
switch {
case productionPackage && agentVersion.Major() >= 9:
// for 9.0.0 and later versions, elastic-agent Helm chart is GA
agentChartVersion = agentCoreVersion
case productionPackage && agentVersion.Major() >= 8 && agentVersion.Minor() >= 18:
// for 8.18.0 and later versions, elastic-agent Helm chart is GA
agentChartVersion = agentCoreVersion
}

for yamlFile, keyVals := range map[string][]struct {
key string
value string
}{
// values file for elastic-agent Helm Chart
filepath.Join(helmChartPath, "values.yaml"): {
{"agent.version", agentCoreVersion},
// always use the SNAPSHOT version for image tag
// for the chart that resides in the git repo
{"agent.image.tag", agentImageTag},
},
// Chart.yaml for elastic-agent Helm Chart
filepath.Join(helmChartPath, "Chart.yaml"): {
{"appVersion", agentCoreVersion},
{"version", agentChartVersion},
},
} {
if err := updateYamlFile(yamlFile, keyVals...); err != nil {
return fmt.Errorf("failed to update agent version: %w", err)
}
}

// lint before packaging
if err := h.Lint(); err != nil {
return err
}

settings := cli.New() // Helm CLI settings
actionConfig := &action.Configuration{}

err := actionConfig.Init(settings.RESTClientGetter(), "default", "",
func(format string, v ...interface{}) {})
if err != nil {
return fmt.Errorf("failed to init helm action config: %w", err)
}

packageAction := action.NewPackage()
_, err = packageAction.Run(helmChartPath, nil)
if err != nil {
return fmt.Errorf("failed to package helm chart: %w", err)
}
return nil
}

func updateYamlNodes(rootNode *yaml.Node, value string, keys ...string) error {
if len(keys) == 0 {
return fmt.Errorf("no keys provided")
Expand Down
Loading