Skip to content

Commit 79dec1c

Browse files
authored
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 c01278b commit 79dec1c

File tree

3 files changed

+96
-23
lines changed

3 files changed

+96
-23
lines changed

dev-tools/mage/dockerbuilder.go

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

22+
"github.com/magefile/mage/mg"
23+
2124
"github.com/elastic/elastic-agent/internal/pkg/agent/install"
2225
"github.com/elastic/elastic-agent/pkg/component"
2326

@@ -270,6 +273,11 @@ func (b *dockerBuilder) dockerSave(tag string, templateExtraArgs ...map[string]i
270273
}
271274
outputFile = filepath.Join(distributionsDir, outputTar)
272275
}
276+
277+
if mg.Verbose() {
278+
log.Printf(">>>> saving docker image %s to %s", tag, outputFile)
279+
}
280+
273281
var stderr bytes.Buffer
274282
cmd := exec.Command("docker", "save", tag)
275283
cmd.Stderr = &stderr

dev-tools/mage/pkgtypes.go

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"slices"
2424
"strconv"
2525
"strings"
26+
"text/template"
2627

2728
"github.com/magefile/mage/mg"
2829
"github.com/magefile/mage/sh"
@@ -88,27 +89,28 @@ type OSPackageArgs struct {
8889

8990
// PackageSpec specifies package metadata and the contents of the package.
9091
type PackageSpec struct {
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-
PreInstallScript string `yaml:"pre_install_script,omitempty"`
104-
PostInstallScript string `yaml:"post_install_script,omitempty"`
105-
PostRmScript string `yaml:"post_rm_script,omitempty"`
106-
Files map[string]PackageFile `yaml:"files"`
107-
Qualifier string `yaml:"qualifier,omitempty"` // Optional
108-
OutputFile string `yaml:"output_file,omitempty"` // Optional
109-
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
110-
ExtraTags []string `yaml:"extra_tags,omitempty"` // Optional
111-
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+
ExtraTags []string `yaml:"extra_tags,omitempty"` // Optional
113+
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
112114

113115
evalContext map[string]interface{}
114116
packageDir string
@@ -488,6 +490,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {
488490

489491
// ImageName computes the image name from the spec.
490492
func (s PackageSpec) ImageName() string {
493+
if s.DockerImageNameTemplate != "" {
494+
imageNameTmpl, err := template.New("dockerImageTemplate").Parse(s.DockerImageNameTemplate)
495+
if err != nil {
496+
panic(fmt.Errorf("parsing docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
497+
}
498+
499+
data := s.toMap()
500+
for k, v := range varMap() {
501+
data[k] = v
502+
}
503+
504+
buf := new(strings.Builder)
505+
err = imageNameTmpl.Execute(buf, data)
506+
if err != nil {
507+
panic(fmt.Errorf("rendering docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
508+
}
509+
510+
imageName := buf.String()
511+
if mg.Verbose() {
512+
log.Printf("rendered image name for %s variant %s: %s", s.Name, s.DockerVariant, imageName)
513+
}
514+
return imageName
515+
}
516+
491517
if s.DockerVariant == Basic {
492518
return s.Name
493519
}
@@ -791,7 +817,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
791817
args = append(args, "--vendor", spec.Vendor)
792818
}
793819
if spec.License != "" {
794-
args = append(args, "--license", strings.Replace(spec.License, " ", "-", -1))
820+
args = append(args, "--license", strings.ReplaceAll(spec.License, " ", "-"))
795821
}
796822
if spec.Description != "" {
797823
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
@@ -503,6 +503,7 @@ shared:
503503

504504
- &agent_docker_spec
505505
<<: *agent_binary_spec
506+
# Default docker image name is <spec name>-<docker variant>
506507
extra_vars:
507508
dockerfile: 'Dockerfile.elastic-agent.tmpl'
508509
docker_entrypoint: 'docker-entrypoint.elastic-agent.tmpl'
@@ -1551,7 +1552,6 @@ specs:
15511552
source: data/{{.BeatName}}-{{ commit_short }}/{{.BeatName}}{{.BinaryExt}}
15521553
symlink: true
15531554
mode: 0755
1554-
15551555
- os: linux
15561556
arch: amd64
15571557
types: [ docker ]
@@ -1578,6 +1578,40 @@ specs:
15781578
'{{.BeatName}}{{.BinaryExt}}':
15791579
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
15801580

1581+
# remove this spec once the elastic-agent-cloud-fips below is correctly uploaded as a DRA
1582+
- os: linux
1583+
arch: amd64
1584+
types: [ docker ]
1585+
spec:
1586+
<<: *docker_fips_spec
1587+
<<: *agent_docker_fips_spec
1588+
# The cloud image is always based on Wolfi
1589+
<<: *docker_builder_spec
1590+
<<: *agent_docker_cloud_fips_spec
1591+
<<: *elastic_license_for_binaries
1592+
name: "elastic-agent-fips-cloud"
1593+
docker_image_name_template: "{{.BeatName}}-fips-cloud"
1594+
files:
1595+
'{{.BeatName}}{{.BinaryExt}}':
1596+
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1597+
1598+
# remove this spec once the elastic-agent-cloud-fips below is correctly uploaded as a DRA
1599+
- os: linux
1600+
arch: arm64
1601+
types: [ docker ]
1602+
spec:
1603+
<<: *docker_fips_spec
1604+
<<: *agent_docker_fips_spec
1605+
# The cloud image is always based on Wolfi
1606+
<<: *docker_builder_arm_spec
1607+
<<: *agent_docker_cloud_fips_spec
1608+
<<: *elastic_license_for_binaries
1609+
name: "elastic-agent-fips-cloud"
1610+
docker_image_name_template: "{{.BeatName}}-fips-cloud"
1611+
files:
1612+
'{{.BeatName}}{{.BinaryExt}}':
1613+
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1614+
15811615
- os: linux
15821616
arch: amd64
15831617
types: [ docker ]
@@ -1588,9 +1622,12 @@ specs:
15881622
<<: *docker_builder_spec
15891623
<<: *agent_docker_cloud_fips_spec
15901624
<<: *elastic_license_for_binaries
1625+
name: "elastic-agent-cloud-fips"
1626+
docker_image_name_template: "{{.BeatName}}-cloud-fips"
15911627
files:
15921628
'{{.BeatName}}{{.BinaryExt}}':
15931629
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}
1630+
15941631
- os: linux
15951632
arch: arm64
15961633
types: [ docker ]
@@ -1601,6 +1638,8 @@ specs:
16011638
<<: *docker_builder_arm_spec
16021639
<<: *agent_docker_cloud_fips_spec
16031640
<<: *elastic_license_for_binaries
1641+
name: "elastic-agent-cloud-fips"
1642+
docker_image_name_template: "{{.BeatName}}-cloud-fips"
16041643
files:
16051644
'{{.BeatName}}{{.BinaryExt}}':
16061645
source: ./build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}}

0 commit comments

Comments
 (0)