Skip to content

Commit

Permalink
Merge pull request #607 from opendevstack/fix/external-image-deploy
Browse files Browse the repository at this point in the history
Apply correct tag when pushing to external registry
  • Loading branch information
michaelsauter authored Oct 10, 2022
2 parents e947609 + 491b9fa commit 86d9785
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ listed in the changelog.
### Fixed

- Aqua and helm-diff log output is incomplete ([#593](https://github.com/opendevstack/ods-pipeline/issues/593))
- Image is tagged with `latest` instead of correct tag when pushed to external registry ([#606](https://github.com/opendevstack/ods-pipeline/issues/606))

## [0.6.0] - 2022-07-20

Expand Down
19 changes: 11 additions & 8 deletions cmd/deploy-with-helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func main() {
imageStream := imageArtifact.Name
fmt.Printf("Copying image %s ...\n", imageStream)
srcImageURL := imageArtifact.Image
var destImageURL string
// If the source registry should be TLS verified, the destination
// should be verified by default as well.
destRegistryTLSVerify := opts.srcRegistryTLSVerify
Expand All @@ -191,14 +190,10 @@ func main() {
if strings.HasPrefix(imageArtifact.Registry, "kind-registry.kind") {
srcRegistryTLSVerify = false
}
if len(targetConfig.RegistryHost) > 0 {
destImageURL = fmt.Sprintf("%s/%s/%s", targetConfig.RegistryHost, releaseNamespace, imageStream)
if targetConfig.RegistryTLSVerify != nil {
destRegistryTLSVerify = *targetConfig.RegistryTLSVerify
}
} else {
destImageURL = strings.Replace(imageArtifact.Image, "/"+imageArtifact.Repository+"/", "/"+releaseNamespace+"/", -1)
if len(targetConfig.RegistryHost) > 0 && targetConfig.RegistryTLSVerify != nil {
destRegistryTLSVerify = *targetConfig.RegistryTLSVerify
}
destImageURL := getImageDestURL(targetConfig.RegistryHost, releaseNamespace, imageArtifact)
fmt.Printf("src=%s\n", srcImageURL)
fmt.Printf("dest=%s\n", destImageURL)
// TODO: for QA and PROD we want to ensure that the SHA recorded in Nexus
Expand Down Expand Up @@ -465,3 +460,11 @@ func collectImageDigests(imageDigestsDir string) ([]string, error) {
}
return files, nil
}

func getImageDestURL(registryHost, releaseNamespace string, imageArtifact artifact.Image) string {
if registryHost != "" {
return fmt.Sprintf("%s/%s/%s:%s", registryHost, releaseNamespace, imageArtifact.Name, imageArtifact.Tag)
} else {
return strings.Replace(imageArtifact.Image, "/"+imageArtifact.Repository+"/", "/"+releaseNamespace+"/", -1)
}
}
47 changes: 47 additions & 0 deletions cmd/deploy-with-helm/main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"fmt"
"testing"

"github.com/opendevstack/pipeline/pkg/artifact"
)

func TestArtifactFilename(t *testing.T) {
Expand Down Expand Up @@ -52,3 +55,47 @@ func TestArtifactFilename(t *testing.T) {
})
}
}

func TestGetImageURLs(t *testing.T) {
srcHost := "image-registry.openshift-image-registry.svc:5000"
destHost := "default-route-openshift-image-registry.apps.example.com"
imgArtifact := artifact.Image{
Image: fmt.Sprintf("%s/foo/bar:baz", srcHost),
Repository: "foo", Name: "bar", Tag: "baz",
}
tests := map[string]struct {
registryHost string
releaseNamespace string
want string
}{
"same cluster, same namespace": {
registryHost: "",
releaseNamespace: "foo",
want: fmt.Sprintf("%s/foo/bar:baz", srcHost),
},
"same cluster, different namespace": {
registryHost: "",
releaseNamespace: "other",
want: fmt.Sprintf("%s/other/bar:baz", srcHost),
},
"different cluster, same namespace": {
registryHost: destHost,
releaseNamespace: "foo",
want: fmt.Sprintf("%s/foo/bar:baz", destHost),
},
"different cluster, different namespace": {
registryHost: destHost,
releaseNamespace: "other",
want: fmt.Sprintf("%s/other/bar:baz", destHost),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := getImageDestURL(tc.registryHost, tc.releaseNamespace, imgArtifact)
if got != tc.want {
t.Fatalf("want: %s, got: %s", tc.want, got)
}
})
}
}

0 comments on commit 86d9785

Please sign in to comment.