Skip to content

Commit 23a0b70

Browse files
mergify[bot]pchila
andauthored
[8.19] (backport #8429) Add docker image name template and renamed fips cloud specs (#8443)
* 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 --------- Co-authored-by: Paolo Chilà <paolo.chila@elastic.co>
1 parent 3806b75 commit 23a0b70

File tree

3 files changed

+94
-22
lines changed

3 files changed

+94
-22
lines changed

dev-tools/mage/dockerbuilder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111
"fmt"
1212
"io"
1313
"io/fs"
14+
"log"
1415
"os"
1516
"os/exec"
1617
"path/filepath"
1718
"strings"
1819
"time"
1920

21+
"github.com/magefile/mage/mg"
2022
"github.com/magefile/mage/sh"
2123
)
2224

@@ -207,6 +209,11 @@ func (b *dockerBuilder) dockerSave(tag string) error {
207209
}
208210
outputFile = filepath.Join(distributionsDir, outputTar)
209211
}
212+
213+
if mg.Verbose() {
214+
log.Printf(">>>> saving docker image %s to %s", tag, outputFile)
215+
}
216+
210217
var stderr bytes.Buffer
211218
cmd := exec.Command("docker", "save", tag)
212219
cmd.Stderr = &stderr

dev-tools/mage/pkgtypes.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"slices"
2323
"strconv"
2424
"strings"
25+
"text/template"
2526

2627
"github.com/magefile/mage/mg"
2728
"github.com/magefile/mage/sh"
@@ -87,26 +88,27 @@ type OSPackageArgs struct {
8788

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

111113
evalContext map[string]interface{}
112114
packageDir string
@@ -475,6 +477,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {
475477

476478
// ImageName computes the image name from the spec.
477479
func (s PackageSpec) ImageName() string {
480+
if s.DockerImageNameTemplate != "" {
481+
imageNameTmpl, err := template.New("dockerImageTemplate").Parse(s.DockerImageNameTemplate)
482+
if err != nil {
483+
panic(fmt.Errorf("parsing docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
484+
}
485+
486+
data := s.toMap()
487+
for k, v := range varMap() {
488+
data[k] = v
489+
}
490+
491+
buf := new(strings.Builder)
492+
err = imageNameTmpl.Execute(buf, data)
493+
if err != nil {
494+
panic(fmt.Errorf("rendering docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
495+
}
496+
497+
imageName := buf.String()
498+
if mg.Verbose() {
499+
log.Printf("rendered image name for %s variant %s: %s", s.Name, s.DockerVariant, imageName)
500+
}
501+
return imageName
502+
}
503+
478504
if s.DockerVariant == Basic {
479505
// no suffix for basic docker variant
480506
return s.Name
@@ -774,7 +800,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
774800
args = append(args, "--vendor", spec.Vendor)
775801
}
776802
if spec.License != "" {
777-
args = append(args, "--license", strings.Replace(spec.License, " ", "-", -1))
803+
args = append(args, "--license", strings.ReplaceAll(spec.License, " ", "-"))
778804
}
779805
if spec.Description != "" {
780806
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)