Skip to content

Commit

Permalink
feat: Add "dry run" build mode (#228)
Browse files Browse the repository at this point in the history
* feat: Add "dry run" build mode

* feat: Do not create AMI if build is a "dry run"

* chore: Remove unnecessary AMI cleanup make target

* fixup! feat: Do not create AMI if build is a "dry run"

Run `make docs`

* fixup! feat: Do not create AMI if build is a "dry run"

Enable build dry-run by default

* fixup! feat: Do not create AMI if build is a "dry run"

Remove unused cleanup script
  • Loading branch information
dlipovetsky authored Feb 4, 2022
1 parent cb01b8c commit e56fdea
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
51 changes: 34 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ OS := $(shell uname -s)

INTERACTIVE := $(shell [ -t 0 ] && echo 1)

# BUILD_DRY_RUN determines the value of the --dry-run flag of the build command. Should be 'true' or 'false'.
BUILD_DRY_RUN := true

root_mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
export REPO_ROOT_DIR := $(dir $(root_mkfile_path))
export REPO_REV := $(shell cd $(REPO_ROOT_DIR) && git describe --abbrev=12 --tags --match='v*' HEAD)
Expand Down Expand Up @@ -169,6 +172,7 @@ devkit.run: devkit
centos7: build
centos7: ## Build Centos 7 image
./bin/konvoy-image build images/ami/centos-7.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

Expand All @@ -185,6 +189,7 @@ centos7-offline: ## Build Centos 7 image
centos7-nvidia: build
centos7-nvidia: ## Build Centos 7 image with GPU support
./bin/konvoy-image build images/ami/centos-7.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES}) \
Expand All @@ -194,13 +199,15 @@ centos7-nvidia: ## Build Centos 7 image with GPU support
rhel82: build
rhel82: ## Build RHEL 8.2 image
./bin/konvoy-image build images/ami/rhel-82.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

.PHONY: rhel82-nvidia
rhel82-nvidia: build
rhel82-nvidia: ## Build RHEL 8.2 image with GPU support
./bin/konvoy-image build images/ami/rhel-82.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES}) \
Expand All @@ -223,6 +230,7 @@ rhel82-fips-offline:
rhel84: build
rhel84: ## Build RHEL 8.4 image
./bin/konvoy-image build images/ami/rhel-84.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

Expand All @@ -243,6 +251,7 @@ rhel84-fips-offline:
rhel84-nvidia: build
rhel84-nvidia: ## Build RHEL 8.4 image with GPU support
./bin/konvoy-image build images/ami/rhel-84.yaml \
--dry-run=$(BUILD_DRY_RUN) \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES}) \
--aws-instance-type p2.xlarge
Expand All @@ -251,6 +260,7 @@ rhel84-nvidia: ## Build RHEL 8.4 image with GPU support
rhel79: build
rhel79: ## Build RHEL 7.9 image
./bin/konvoy-image build images/ami/rhel-79.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

Expand All @@ -271,6 +281,7 @@ rhel79-fips-offline: ## Build RHEL 7.9 FIPS image
rhel79-nvidia: build
rhel79-nvidia: ## Build RHEL 7.9 image with GPU support
./bin/konvoy-image build images/ami/rhel-79.yaml \
--dry-run=$(BUILD_DRY_RUN) \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})
--aws-instance-type p2.xlarge
Expand All @@ -279,13 +290,15 @@ rhel79-nvidia: ## Build RHEL 7.9 image with GPU support
sles15: build
sles15: ## Build SLES 15 image
./bin/konvoy-image build images/ami/sles-15.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

.PHONY: sles15-nvidia
sles15-nvidia: build
sles15-nvidia: ## Build SLES 15 image with GPU support
./bin/konvoy-image build images/ami/sles-15.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES}) \
Expand All @@ -298,6 +311,7 @@ flatcar-version.yaml:
flatcar: build flatcar-version.yaml
flatcar: ## Build flatcar image
./bin/konvoy-image build images/ami/flatcar.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
--overrides=flatcar-version.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})
Expand All @@ -306,6 +320,7 @@ flatcar: ## Build flatcar image
flatcar-nvidia: build flatcar-version.yaml
flatcar-nvidia: ## Build flatcar image with GPU support
./bin/konvoy-image build images/ami/flatcar.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES}) \
Expand All @@ -315,20 +330,23 @@ flatcar-nvidia: ## Build flatcar image with GPU support
ubuntu18: build
ubuntu18: ## Build Ubuntu 20 image
./bin/konvoy-image build images/ami/ubuntu-18.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

.PHONY: ubuntu20
ubuntu20: build
ubuntu20: ## Build Ubuntu 20 image
./bin/konvoy-image build images/ami/ubuntu-20.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

.PHONY: ubuntu20-nvidia
ubuntu20-nvidia: build
ubuntu20-nvidia: ## Build Ubuntu 20 image with GPU support
./bin/konvoy-image build images/ami/ubuntu-20.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
--overrides=overrides/nvidia.yaml \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES}) \
Expand All @@ -338,13 +356,15 @@ ubuntu20-nvidia: ## Build Ubuntu 20 image with GPU support
oracle7: build
oracle7: ## Build Oracle Linux 7 image
./bin/konvoy-image build images/ami/oracle-7.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

.PHONY: oracle8
oracle8: build
oracle8: ## Build Oracle Linux 8 image
./bin/konvoy-image build images/ami/oracle-8.yaml \
--dry-run=$(BUILD_DRY_RUN) \
-v ${VERBOSITY} \
$(if $(ADDITIONAL_OVERRIDES),--overrides=${ADDITIONAL_OVERRIDES})

Expand Down Expand Up @@ -531,9 +551,6 @@ define print-target
@printf "Executing target: \033[36m$@\033[0m\n"
endef

docker.clean-latest-ami:
./test/scripts/clean-latest-ami.sh

# requires ANSIBLE_PATH, otherwise run `make ci.e2e.ansible`
e2e.ansible:
make -C test/e2e/ansible e2e
Expand Down Expand Up @@ -564,34 +581,34 @@ ci.e2e.build.all: ci.e2e.build.sles-15-nvidia
ci.e2e.build.%:
make devkit.run WHAT="make e2e.build.$*"

e2e.build.centos-7: centos7 docker.clean-latest-ami
e2e.build.centos-7: centos7

# Run os-packages-artifacts outside devkit container.
e2e.build.centos-7-offline: centos7-offline docker.clean-latest-ami infra.aws.destroy
e2e.build.centos-7-offline: centos7-offline infra.aws.destroy

e2e.build.rhel-7.9-offline-fips: rhel79-fips-offline docker.clean-latest-ami infra.aws.destroy
e2e.build.rhel-7.9-offline-fips: rhel79-fips-offline infra.aws.destroy

e2e.build.rhel-8.2-offline-fips: rhel82-fips-offline docker.clean-latest-ami infra.aws.destroy
e2e.build.rhel-8.2-offline-fips: rhel82-fips-offline infra.aws.destroy

e2e.build.rhel-8.4-offline-fips: rhel84-fips-offline docker.clean-latest-ami infra.aws.destroy
e2e.build.rhel-8.4-offline-fips: rhel84-fips-offline infra.aws.destroy

e2e.build.ubuntu-18: ubuntu18 docker.clean-latest-ami
e2e.build.ubuntu-18: ubuntu18

e2e.build.ubuntu-20: ubuntu20 docker.clean-latest-ami
e2e.build.ubuntu-20: ubuntu20

e2e.build.sles-15: sles15 docker.clean-latest-ami
e2e.build.sles-15: sles15

e2e.build.oracle-7: oracle7 docker.clean-latest-ami
e2e.build.oracle-7: oracle7

e2e.build.oracle-8: oracle8 docker.clean-latest-ami
e2e.build.oracle-8: oracle8

e2e.build.flatcar: flatcar docker.clean-latest-ami
e2e.build.flatcar: flatcar

e2e.build.rhel-8-fips: rhel82-fips docker.clean-latest-ami
e2e.build.rhel-8-fips: rhel82-fips

e2e.build.centos-7-nvidia: centos7-nvidia docker.clean-latest-ami
e2e.build.centos-7-nvidia: centos7-nvidia

e2e.build.sles-15-nvidia: sles15-nvidia docker.clean-latest-ami
e2e.build.sles-15-nvidia: sles15-nvidia

# use sibling containers to handle dependencies and avoid DinD
ci.e2e.ansible:
Expand Down
3 changes: 3 additions & 0 deletions cmd/konvoy-image/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type buildCLIFlags struct {
packerManifestPath string
packerBuildOnError string
workDir string
dryRun bool

generateCLIFlags
}
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewBuildOptions() app.BuildOptions {
OnError: buildFlags.packerBuildOnError,
},
CustomManifestPath: buildFlags.packerManifestPath,
DryRun: buildFlags.dryRun,
}
}

Expand All @@ -71,4 +73,5 @@ func init() {
fs.StringVar(&buildFlags.packerBuildOnError,
"packer-on-error", "", "[advanced] set error strategy for packer. strategies [cleanup, abort, run-cleanup-provisioner]")
fs.StringVar(&buildFlags.workDir, workDirFlagName, "", "path to custom work directory generated by the generate command")
fs.BoolVar(&buildFlags.dryRun, "dry-run", false, "do not create artifacts, or delete them after creating. Recommended for tests.")
}
1 change: 1 addition & 0 deletions docs/cli/konvoy-image_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build --region us-west-2 --source-ami=ami-12345abcdef images/ami/centos-7.yaml
--ami-users stringArray a list AWS user accounts which are allowed use the image
--aws-instance-type string instance type used to build the AMI; the type must be present in the region in which the AMI is built
--containerd-version string the version of containerd to install
--dry-run do not create artifacts, or delete them after creating. Recommended for tests.
--extra-vars strings flag passed Ansible's extra-vars
-h, --help help for build
--kubernetes-version string The version of kubernetes to install. Example: 1.21.6
Expand Down
6 changes: 5 additions & 1 deletion pkg/app/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type BuildOptions struct {
PackerPath string
PackerBuildFlags packer.BuildFlags
CustomManifestPath string
DryRun bool
}

type UserArgs struct {
Expand Down Expand Up @@ -210,7 +211,10 @@ func (b *Builder) Run(workDir string, buildOptions BuildOptions) error {
if builderType == "" {
return BuildError(fmt.Sprintf("%s is not defined in image manifest", packerBuilderTypeKey))
}
opts := packer.RenderOptions{SourceAMIDefined: isSourceAMIProvided(config)}
opts := packer.RenderOptions{
SourceAMIDefined: isSourceAMIProvided(config),
DryRun: buildOptions.DryRun,
}

var data []byte
data, err = packer.GetManifest(builderType, &opts)
Expand Down
1 change: 1 addition & 0 deletions pkg/packer/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type RenderOptions struct {
SourceAMIDefined bool
DryRun bool
}

var packerManifests = map[string][]byte{
Expand Down
3 changes: 3 additions & 0 deletions pkg/packer/manifests/aws/packer.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"most_recent": true
},
((- end ))
((- if .DryRun ))
"skip_create_ami": true,
((- end ))
"ami_name": "konvoy-ami-{{user `build_name`}}-{{user `kubernetes_full_version` | clean_resource_name}}-{{user `build_timestamp`}}",
"snapshot_tags": {
"ami_name": "konvoy-ami-{{user `build_name`}}-{{user `kubernetes_full_version` | clean_resource_name}}-{{user `build_timestamp`}}"
Expand Down
23 changes: 0 additions & 23 deletions test/scripts/clean-latest-ami.sh

This file was deleted.

0 comments on commit e56fdea

Please sign in to comment.