Skip to content

Commit 31d4094

Browse files
pchilamergify[bot]
authored andcommitted
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 (cherry picked from commit 79dec1c) # Conflicts: # dev-tools/mage/dockerbuilder.go # dev-tools/mage/pkgtypes.go
1 parent f283e09 commit 31d4094

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

dev-tools/mage/dockerbuilder.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@ import (
1111
"fmt"
1212
"io"
1313
"io/fs"
14+
<<<<<<< HEAD
15+
=======
16+
"log"
17+
"maps"
18+
>>>>>>> 79dec1cc2 (Add docker image name template and renamed fips cloud specs (#8429))
1419
"os"
1520
"os/exec"
1621
"path/filepath"
1722
"strings"
1823
"time"
1924

25+
<<<<<<< HEAD
26+
=======
27+
"github.com/magefile/mage/mg"
28+
29+
"github.com/elastic/elastic-agent/internal/pkg/agent/install"
30+
"github.com/elastic/elastic-agent/pkg/component"
31+
32+
>>>>>>> 79dec1cc2 (Add docker image name template and renamed fips cloud specs (#8429))
2033
"github.com/magefile/mage/sh"
2134
)
2235

@@ -207,6 +220,11 @@ func (b *dockerBuilder) dockerSave(tag string) error {
207220
}
208221
outputFile = filepath.Join(distributionsDir, outputTar)
209222
}
223+
224+
if mg.Verbose() {
225+
log.Printf(">>>> saving docker image %s to %s", tag, outputFile)
226+
}
227+
210228
var stderr bytes.Buffer
211229
cmd := exec.Command("docker", "save", tag)
212230
cmd.Stderr = &stderr

dev-tools/mage/pkgtypes.go

Lines changed: 51 additions & 1 deletion
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,6 +88,7 @@ type OSPackageArgs struct {
8788

8889
// PackageSpec specifies package metadata and the contents of the package.
8990
type PackageSpec struct {
91+
<<<<<<< HEAD
9092
Name string `yaml:"name,omitempty"`
9193
ServiceName string `yaml:"service_name,omitempty"`
9294
OS string `yaml:"os,omitempty"`
@@ -107,6 +109,30 @@ type PackageSpec struct {
107109
OutputFile string `yaml:"output_file,omitempty"` // Optional
108110
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
109111
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
112+
=======
113+
Name string `yaml:"name,omitempty"`
114+
ServiceName string `yaml:"service_name,omitempty"`
115+
OS string `yaml:"os,omitempty"`
116+
Arch string `yaml:"arch,omitempty"`
117+
Vendor string `yaml:"vendor,omitempty"`
118+
Snapshot bool `yaml:"snapshot"`
119+
FIPS bool `yaml:"fips"`
120+
Version string `yaml:"version,omitempty"`
121+
License string `yaml:"license,omitempty"`
122+
URL string `yaml:"url,omitempty"`
123+
Description string `yaml:"description,omitempty"`
124+
DockerVariant DockerVariant `yaml:"docker_variant,omitempty"`
125+
DockerImageNameTemplate string `yaml:"docker_image_name_template,omitempty"` // Optional: template of the docker image name
126+
PreInstallScript string `yaml:"pre_install_script,omitempty"`
127+
PostInstallScript string `yaml:"post_install_script,omitempty"`
128+
PostRmScript string `yaml:"post_rm_script,omitempty"`
129+
Files map[string]PackageFile `yaml:"files"`
130+
Qualifier string `yaml:"qualifier,omitempty"` // Optional
131+
OutputFile string `yaml:"output_file,omitempty"` // Optional
132+
ExtraVars map[string]string `yaml:"extra_vars,omitempty"` // Optional
133+
ExtraTags []string `yaml:"extra_tags,omitempty"` // Optional
134+
Components []packaging.BinarySpec `yaml:"components"` // Optional: Components required for this package
135+
>>>>>>> 79dec1cc2 (Add docker image name template and renamed fips cloud specs (#8429))
110136

111137
evalContext map[string]interface{}
112138
packageDir string
@@ -475,6 +501,30 @@ func (s PackageSpec) Evaluate(args ...map[string]interface{}) PackageSpec {
475501

476502
// ImageName computes the image name from the spec.
477503
func (s PackageSpec) ImageName() string {
504+
if s.DockerImageNameTemplate != "" {
505+
imageNameTmpl, err := template.New("dockerImageTemplate").Parse(s.DockerImageNameTemplate)
506+
if err != nil {
507+
panic(fmt.Errorf("parsing docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
508+
}
509+
510+
data := s.toMap()
511+
for k, v := range varMap() {
512+
data[k] = v
513+
}
514+
515+
buf := new(strings.Builder)
516+
err = imageNameTmpl.Execute(buf, data)
517+
if err != nil {
518+
panic(fmt.Errorf("rendering docker image name template for %s variant %s: %w", s.Name, s.DockerVariant, err))
519+
}
520+
521+
imageName := buf.String()
522+
if mg.Verbose() {
523+
log.Printf("rendered image name for %s variant %s: %s", s.Name, s.DockerVariant, imageName)
524+
}
525+
return imageName
526+
}
527+
478528
if s.DockerVariant == Basic {
479529
// no suffix for basic docker variant
480530
return s.Name
@@ -774,7 +824,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
774824
args = append(args, "--vendor", spec.Vendor)
775825
}
776826
if spec.License != "" {
777-
args = append(args, "--license", strings.Replace(spec.License, " ", "-", -1))
827+
args = append(args, "--license", strings.ReplaceAll(spec.License, " ", "-"))
778828
}
779829
if spec.Description != "" {
780830
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)