Skip to content

Commit beba904

Browse files
committed
Add docker image name template and renamed fips cloud specs (#8429)
* Add docker image name template and renamed fips cloud specs * Add debug log for saving docker images
1 parent f283e09 commit beba904

File tree

3 files changed

+100
-22
lines changed

3 files changed

+100
-22
lines changed

dev-tools/mage/dockerbuilder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ import (
1111
"fmt"
1212
"io"
1313
"io/fs"
14+
"log"
15+
"maps"
1416
"os"
1517
"os/exec"
1618
"path/filepath"
1719
"strings"
1820
"time"
1921

22+
"github.com/magefile/mage/mg"
23+
24+
"github.com/elastic/elastic-agent/internal/pkg/agent/install"
25+
"github.com/elastic/elastic-agent/pkg/component"
26+
2027
"github.com/magefile/mage/sh"
2128
)
2229

@@ -207,6 +214,11 @@ func (b *dockerBuilder) dockerSave(tag string) error {
207214
}
208215
outputFile = filepath.Join(distributionsDir, outputTar)
209216
}
217+
218+
if mg.Verbose() {
219+
log.Printf(">>>> saving docker image %s to %s", tag, outputFile)
220+
}
221+
210222
var stderr bytes.Buffer
211223
cmd := exec.Command("docker", "save", tag)
212224
cmd.Stderr = &stderr

dev-tools/mage/pkgtypes.go

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io"
1515
"io/fs"
1616
"log"
17+
"math"
1718
"os"
1819
"path/filepath"
1920
"reflect"
@@ -22,6 +23,7 @@ import (
2223
"slices"
2324
"strconv"
2425
"strings"
26+
"text/template"
2527

2628
"github.com/magefile/mage/mg"
2729
"github.com/magefile/mage/sh"
@@ -87,26 +89,27 @@ type OSPackageArgs struct {
8789

8890
// PackageSpec specifies package metadata and the contents of the package.
8991
type PackageSpec struct {
90-
Name string `yaml:"name,omitempty"`
91-
ServiceName string `yaml:"service_name,omitempty"`
92-
OS string `yaml:"os,omitempty"`
93-
Arch string `yaml:"arch,omitempty"`
94-
Vendor string `yaml:"vendor,omitempty"`
95-
Snapshot bool `yaml:"snapshot"`
96-
FIPS bool `yaml:"fips"`
97-
Version string `yaml:"version,omitempty"`
98-
License string `yaml:"license,omitempty"`
99-
URL string `yaml:"url,omitempty"`
100-
Description string `yaml:"description,omitempty"`
101-
DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
102-
PreInstallScript string `yaml:"pre_install_script,omitempty"`
103-
PostInstallScript string `yaml:"post_install_script,omitempty"`
104-
PostRmScript string `yaml:"post_rm_script,omitempty"`
105-
Files map[string]PackageFile `yaml:"files"`
106-
Qualifier string `yaml:"qualifier,omitempty"` // Optional
107-
OutputFile string `yaml:"output_file,omitempty"` // Optional
108-
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
109-
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
92+
Name string `yaml:"name,omitempty"`
93+
ServiceName string `yaml:"service_name,omitempty"`
94+
OS string `yaml:"os,omitempty"`
95+
Arch string `yaml:"arch,omitempty"`
96+
Vendor string `yaml:"vendor,omitempty"`
97+
Snapshot bool `yaml:"snapshot"`
98+
FIPS bool `yaml:"fips"`
99+
Version string `yaml:"version,omitempty"`
100+
License string `yaml:"license,omitempty"`
101+
URL string `yaml:"url,omitempty"`
102+
Description string `yaml:"description,omitempty"`
103+
DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
104+
DockerImageNameTemplate string `yaml:"docker_image_name_template,omitempty"` // Optional: template of the docker image name
105+
PreInstallScript string `yaml:"pre_install_script,omitempty"`
106+
PostInstallScript string `yaml:"post_install_script,omitempty"`
107+
PostRmScript string `yaml:"post_rm_script,omitempty"`
108+
Files map[string]PackageFile `yaml:"files"`
109+
Qualifier string `yaml:"qualifier,omitempty"` // Optional
110+
OutputFile string `yaml:"output_file,omitempty"` // Optional
111+
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
112+
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
110113

111114
evalContext map[string]interface{}
112115
packageDir string
@@ -475,6 +478,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {
475478

476479
// ImageName computes the image name from the spec.
477480
func (s PackageSpec) ImageName() string {
481+
if s.DockerImageNameTemplate != "" {
482+
imageNameTmpl, err := template.New("dockerImageTemplate").Parse(s.DockerImageNameTemplate)
483+
if err != nil {
484+
panic(fmt.Errorf("parsing docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
485+
}
486+
487+
data := s.toMap()
488+
for k, v := range varMap() {
489+
data[k] = v
490+
}
491+
492+
buf := new(strings.Builder)
493+
err = imageNameTmpl.Execute(buf, data)
494+
if err != nil {
495+
panic(fmt.Errorf("rendering docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
496+
}
497+
498+
imageName := buf.String()
499+
if mg.Verbose() {
500+
log.Printf("rendered image name for %s variant %s: %s", s.Name, s.DockerVariant, imageName)
501+
}
502+
return imageName
503+
}
504+
478505
if s.DockerVariant == Basic {
479506
// no suffix for basic docker variant
480507
return s.Name
@@ -774,7 +801,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
774801
args = append(args, "--vendor", spec.Vendor)
775802
}
776803
if spec.License != "" {
777-
args = append(args, "--license", strings.Replace(spec.License, " ", "-", -1))
804+
args = append(args, "--license", strings.ReplaceAll(spec.License, " ", "-"))
778805
}
779806
if spec.Description != "" {
780807
args = append(args, "--description", spec.Description)

dev-tools/packaging/packages.yml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ shared:
518518

519519
- &agent_docker_spec
520520
<<: *agent_binary_spec
521+
# Default docker image name is <spec name>-<docker variant>
521522
extra_vars:
522523
dockerfile: 'Dockerfile.elastic-agent.tmpl'
523524
docker_entrypoint: 'docker-entrypoint.elastic-agent.tmpl'
@@ -1418,7 +1419,6 @@ specs:
14181419
source: data/{{.BeatName}}-{{ commit_short }}/{{.BeatName}}{{.BinaryExt}}
14191420
symlink: true
14201421
mode: 0755
1421-
14221422
- os: linux
14231423
arch: amd64
14241424
types: [ docker ]
@@ -1445,6 +1445,40 @@ specs:
14451445
'{{.BeatName}}{{.BinaryExt}}':
14461446
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
14471447

1448+
# remove this spec once the elastic-agent-cloud-fips below is correctly uploaded as a DRA
1449+
- os: linux
1450+
arch: amd64
1451+
types: [ docker ]
1452+
spec:
1453+
<<: *docker_fips_spec
1454+
<<: *agent_docker_fips_spec
1455+
# The cloud image is always based on Wolfi
1456+
<<: *docker_builder_spec
1457+
<<: *agent_docker_cloud_fips_spec
1458+
<<: *elastic_license_for_binaries
1459+
name: "elastic-agent-fips-cloud"
1460+
docker_image_name_template: "{{.BeatName}}-fips-cloud"
1461+
files:
1462+
'{{.BeatName}}{{.BinaryExt}}':
1463+
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1464+
1465+
# remove this spec once the elastic-agent-cloud-fips below is correctly uploaded as a DRA
1466+
- os: linux
1467+
arch: arm64
1468+
types: [ docker ]
1469+
spec:
1470+
<<: *docker_fips_spec
1471+
<<: *agent_docker_fips_spec
1472+
# The cloud image is always based on Wolfi
1473+
<<: *docker_builder_arm_spec
1474+
<<: *agent_docker_cloud_fips_spec
1475+
<<: *elastic_license_for_binaries
1476+
name: "elastic-agent-fips-cloud"
1477+
docker_image_name_template: "{{.BeatName}}-fips-cloud"
1478+
files:
1479+
'{{.BeatName}}{{.BinaryExt}}':
1480+
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1481+
14481482
- os: linux
14491483
arch: amd64
14501484
types: [ docker ]
@@ -1455,9 +1489,12 @@ specs:
14551489
<<: *docker_builder_spec
14561490
<<: *agent_docker_cloud_fips_spec
14571491
<<: *elastic_license_for_binaries
1492+
name: "elastic-agent-cloud-fips"
1493+
docker_image_name_template: "{{.BeatName}}-cloud-fips"
14581494
files:
14591495
'{{.BeatName}}{{.BinaryExt}}':
14601496
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1497+
14611498
- os: linux
14621499
arch: arm64
14631500
types: [ docker ]
@@ -1468,6 +1505,8 @@ specs:
14681505
<<: *docker_builder_arm_spec
14691506
<<: *agent_docker_cloud_fips_spec
14701507
<<: *elastic_license_for_binaries
1508+
name: "elastic-agent-cloud-fips"
1509+
docker_image_name_template: "{{.BeatName}}-cloud-fips"
14711510
files:
14721511
'{{.BeatName}}{{.BinaryExt}}':
14731512
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

0 commit comments

Comments
 (0)