Skip to content

Commit

Permalink
refactor: support unauthenticated docker registry
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Nov 29, 2024
1 parent 72bcaf0 commit 7dead8f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
98 changes: 98 additions & 0 deletions cmd/identify_imagebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,104 @@ func TestImageBuildConfigurationIdentification(t *testing.T) {
},
},
},
{
name: "test2c nginx-php deployment - rootless - unauthenticated",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "internal/testdata/complex/lagoon.varnish.yml",
ImageReferences: map[string]string{
"nginx": "unauthenticated.registry/example-project/main-nginx@sha256:b2001babafaa8128fe89aa8fd11832cade59931d14c3de5b3ca32e2a010fbaa8",
"php": "unauthenticated.registry/example-project/main-php@sha256:b2001babafaa8128fe89aa8fd11832cade59931d14c3de5b3ca32e2a010fbaa8",
"cli": "unauthenticated.registry/example-project/main-cli@sha256:b2001babafaa8128fe89aa8fd11832cade59931d14c3de5b3ca32e2a010fbaa8",
"redis": "unauthenticated.registry/example-project/main-redis@sha256:b2001babafaa8128fe89aa8fd11832cade59931d14c3de5b3ca32e2a010fbaa8",
"varnish": "unauthenticated.registry/example-project/main-varnish@sha256:b2001babafaa8128fe89aa8fd11832cade59931d14c3de5b3ca32e2a010fbaa8",
},
BuildPodVariables: []helpers.EnvironmentVariable{
{
Name: "UNAUTHENTICATED_REGISTRY",
Value: "true",
},
{
Name: "REGISTRY",
Value: "unauthenticated.registry",
},
},
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD",
Value: "enabled",
Scope: "build",
},
{
Name: "LAGOON_FEATURE_FLAG_IMAGECACHE_REGISTRY",
Value: "imagecache.example.com",
Scope: "build",
},
},
}, true),
want: imageBuild{
BuildKit: helpers.BoolPtr(true),
BuildArguments: map[string]string{
"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD": "enabled",
"LAGOON_BUILD_NAME": "lagoon-build-abcdefg",
"LAGOON_PROJECT": "example-project",
"LAGOON_ENVIRONMENT": "main",
"LAGOON_ENVIRONMENT_TYPE": "production",
"LAGOON_BUILD_TYPE": "branch",
"LAGOON_GIT_SOURCE_REPOSITORY": "ssh://[email protected]/lagoon-demo.git",
"LAGOON_KUBERNETES": "remote-cluster1",
"LAGOON_GIT_SHA": "0000000000000000000000000000000000000000",
"LAGOON_GIT_BRANCH": "main",
"CLI_IMAGE": "example-project-main-cli",
"NGINX_IMAGE": "example-project-main-nginx",
"PHP_IMAGE": "example-project-main-php",
"LAGOON_FEATURE_FLAG_IMAGECACHE_REGISTRY": "imagecache.example.com",
"LAGOON_SSH_PRIVATE_KEY": "-----BEGIN OPENSSH PRIVATE KEY-----\nthisisafakekey\n-----END OPENSSH PRIVATE KEY-----",
},
Images: []imageBuilds{
{
Name: "cli",
ImageBuild: generator.ImageBuild{
BuildImage: "unauthenticated.registry/example-project/main-cli:latest",
Context: "internal/testdata/complex/docker",
DockerFile: ".docker/Dockerfile.cli",
TemporaryImage: "example-project-main-cli",
},
}, {
Name: "nginx",
ImageBuild: generator.ImageBuild{
BuildImage: "unauthenticated.registry/example-project/main-nginx:latest",
Context: "internal/testdata/complex/docker",
DockerFile: ".docker/Dockerfile.nginx-drupal",
TemporaryImage: "example-project-main-nginx",
},
}, {
Name: "php",
ImageBuild: generator.ImageBuild{
BuildImage: "unauthenticated.registry/example-project/main-php:latest",
Context: "internal/testdata/complex/docker",
DockerFile: ".docker/Dockerfile.php",
TemporaryImage: "example-project-main-php",
},
}, {
Name: "redis",
ImageBuild: generator.ImageBuild{
BuildImage: "unauthenticated.registry/example-project/main-redis:latest",
PullImage: "quay.io/notlagoon/redis",
},
}, {
Name: "varnish",
ImageBuild: generator.ImageBuild{
BuildImage: "unauthenticated.registry/example-project/main-varnish:latest",
PullImage: "imagecache.example.com/uselagoon/varnish-5-drupal:latest",
},
},
},
},
},
{
name: "test3 - funky pvcs",
description: "only create pvcs of the requested persistent-name in the docker-compose file",
Expand Down
1 change: 1 addition & 0 deletions internal/generator/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type BuildValues struct {
CronjobsDisabled bool `json:"cronjobsDisabled" description:"this controls whether cronjobs are enabled for this environment or not"`
FeatureFlags map[string]bool `json:"-" description:"these are used by templating systems to turn on or off certain functionality based on if feature flags are defined"`
ImageRegistry string `json:"imageRegistry" description:"the image registry in use for this environment, usually harbor"`
UnauthenticatedImageRegistry bool `json:"unauthenticatedImageRegistry" description:"if this is an unauthenticated registry"`
DockerBuildKit *bool `json:"dockerBuildKit" description:"the flag to determine if docker buildkit is used"`
ImageBuildArguments map[string]string `json:"imageBuildArguments" description:"where the calculated image build arguments are stored"`
EnvironmentVariables []lagoon.EnvironmentVariable `json:"environmentVariables" description:"the merged project and environment variables for this environment"`
Expand Down
1 change: 1 addition & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func NewGenerator(
dynamicDBaaSSecrets := helpers.GetEnv("DYNAMIC_DBAAS_SECRETS", strings.Join(generator.DynamicDBaaSSecrets, ","), generator.Debug)
imageCacheBuildArgsJSON := helpers.GetEnv("LAGOON_CACHE_BUILD_ARGS", generator.ImageCacheBuildArgsJSON, generator.Debug)
buildValues.SSHPrivateKey = helpers.GetEnv("SSH_PRIVATE_KEY", generator.SSHPrivateKey, generator.Debug)
buildValues.UnauthenticatedImageRegistry = helpers.GetEnvBool("UNAUTHENTICATED_REGISTRY", generator.CI, generator.Debug)
// this is used by CI systems to influence builds, it is rarely used and should probably be abandoned
buildValues.IsCI = helpers.GetEnvBool("CI", generator.CI, generator.Debug)

Expand Down
13 changes: 10 additions & 3 deletions internal/generator/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,16 @@ func generateImageBuild(buildValues BuildValues, composeServiceValues composetyp
}
// since we know what the final build image will be, we can set it here, this is what all images will be built as during the build
// for `pullimages` they will get retagged as this imagename and pushed to the registry
imageBuild.BuildImage = fmt.Sprintf("%s/%s/%s/%s:%s", buildValues.ImageRegistry, buildValues.Project, buildValues.Environment, composeService, "latest")
if buildValues.BuildType == "promote" {
imageBuild.PromoteImage = fmt.Sprintf("%s/%s/%s/%s:%s", buildValues.ImageRegistry, buildValues.Project, buildValues.PromotionSourceEnvironment, composeService, "latest")
if buildValues.UnauthenticatedImageRegistry {
imageBuild.BuildImage = fmt.Sprintf("%s/%s/%s-%s:%s", buildValues.ImageRegistry, buildValues.Project, buildValues.Environment, composeService, "latest")
if buildValues.BuildType == "promote" {
imageBuild.PromoteImage = fmt.Sprintf("%s/%s/%s-%s:%s", buildValues.ImageRegistry, buildValues.Project, buildValues.PromotionSourceEnvironment, composeService, "latest")
}
} else {
imageBuild.BuildImage = fmt.Sprintf("%s/%s/%s/%s:%s", buildValues.ImageRegistry, buildValues.Project, buildValues.Environment, composeService, "latest")
if buildValues.BuildType == "promote" {
imageBuild.PromoteImage = fmt.Sprintf("%s/%s/%s/%s:%s", buildValues.ImageRegistry, buildValues.Project, buildValues.PromotionSourceEnvironment, composeService, "latest")
}
}
// populate the docker derived information here, this information will be used by the build and pushing scripts
return imageBuild, nil
Expand Down
2 changes: 2 additions & 0 deletions legacy/build-deploy-docker-compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ if [ ! -z ${INTERNAL_REGISTRY_URL} ] ; then
# fi
echo "Set internal registry secrets for token ${INTERNAL_REGISTRY_USERNAME} in ${REGISTRY}"
fi
else
export UNAUTHENTICATED_REGISTRY=true
fi

# log in to any container registries before building or pulling images
Expand Down

0 comments on commit 7dead8f

Please sign in to comment.