Skip to content

Commit

Permalink
Replaced hard coded image tags in kotsadm
Browse files Browse the repository at this point in the history
  • Loading branch information
murphybytes committed Sep 16, 2021
1 parent eae7c1f commit 8eae9f2
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 13 deletions.
24 changes: 20 additions & 4 deletions cmd/imagedeps/main.go
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -85,24 +86,39 @@ func generateTaggedImageFiles(ctx generationContext) error {
return fmt.Errorf("no references to images found")
}

if err := generateOutput(ctx.outputConstantFilename, constantFileTemplate, references); err != nil {
if err := generateOutput(ctx.outputConstantFilename, constantFileTemplate, references, goFmt); err != nil {
return fmt.Errorf("failed to generate output file %q %w", ctx.outputConstantFilename, err)
}

if err := generateOutput(ctx.outputEnvFilename, environmentFileTemplate, references); err != nil {
if err := generateOutput(ctx.outputEnvFilename, environmentFileTemplate, references, noopPostProcessor); err != nil {
return fmt.Errorf("failed to generate file %q %w", ctx.outputEnvFilename, err)
}

return nil
}

func generateOutput(filename, fileTemplate string, refs []*ImageRef) error {
type templatePostProcessorFn func(buff []byte)([]byte, error)

func goFmt(buff []byte)([]byte, error){
return format.Source(buff)
}

func noopPostProcessor(buff []byte)([]byte, error) {
return buff, nil
}

func generateOutput(filename, fileTemplate string, refs []*ImageRef, fn templatePostProcessorFn) error {
var out bytes.Buffer
if err := template.Must(template.New("constants").Parse(fileTemplate)).Execute(&out, refs); err != nil {
return err
}

if err := ioutil.WriteFile(filename, out.Bytes(), 0644); err != nil {
buff, err := fn(out.Bytes())
if err != nil {
return err
}

if err := ioutil.WriteFile(filename, buff, 0644); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/imagedeps/testdata/basic/constants.go
Expand Up @@ -6,4 +6,4 @@ package image

const (
Minio = "minio/minio:RELEASE.2021-09-09T21-37-07Z.fips"
)
)
2 changes: 1 addition & 1 deletion cmd/imagedeps/testdata/filter-github/constants.go
Expand Up @@ -6,4 +6,4 @@ package image

const (
Minio = "minio/minio:RELEASE.2021-09-09T21-37-06Z.xxx"
)
)
2 changes: 1 addition & 1 deletion cmd/imagedeps/testdata/postgres/constants.go
Expand Up @@ -7,4 +7,4 @@ package image
const (
PostgresAlpine = "postgres:10.18-alpine"
PostgresDebian = "postgres:10.18"
)
)
4 changes: 2 additions & 2 deletions cmd/imagedeps/testdata/with-overrides/constants.go
Expand Up @@ -5,7 +5,7 @@ package image
// image name.

const (
Minio = "minio/minio:RELEASE.2021-09-09T21-37-07Z.fips"
Minio = "minio/minio:RELEASE.2021-09-09T21-37-07Z.fips"
PostgresAlpine = "postgres:10.18-alpine"
PostgresDebian = "postgres:10.18"
)
)
13 changes: 13 additions & 0 deletions pkg/image/image.go
Expand Up @@ -10,6 +10,19 @@ import (
kustomizetypes "sigs.k8s.io/kustomize/api/types"
)

// GetTag extracts the image tag from an image reference
func GetTag(imageRef string) (string, error) {
parts := strings.Split(imageRef, ":")
if len(parts) < 2 {
return "", fmt.Errorf("malformed image reference %q could not find tag", imageRef)
}
tag := parts[len(parts)-1]
if len(tag) == 0 {
return "", fmt.Errorf("empty tag")
}
return parts[len(parts)-1], nil
}

func ImageInfoFromFile(registry registry.RegistryOptions, nameParts []string) (kustomizetypes.Image, error) {
// imageNameParts looks like this:
// ["quay.io", "someorg", "imagename", "imagetag"]
Expand Down
48 changes: 48 additions & 0 deletions pkg/image/image_test.go
Expand Up @@ -543,3 +543,51 @@ func Test_stripImageTag(t *testing.T) {
})
}
}

func TestGetTag(t *testing.T) {
tt := []struct{
name string
imageRef string
expectedTag string
wantErr bool
}{
{
name:"happy path",
imageRef: "some/image:v1.2.3",
expectedTag: "v1.2.3",
},
{
name: "failed case",
imageRef: "",
wantErr: true,
},
{
name: "no tag",
imageRef: "foo/bar",
wantErr: true,
},
{
name: "fat fingered",
imageRef: "some/image:",
wantErr: true,
},
{
name: "long image",
imageRef: "some/image/image2:v1.2.3",
expectedTag: "v1.2.3",

},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T){
actual, err := GetTag(tc.imageRef)
if tc.wantErr {
require.NotNil(t, err)
return
}
require.Nil(t, err)
require.Equal(t, tc.expectedTag, actual )
})
}
}
6 changes: 4 additions & 2 deletions pkg/kotsadm/objects/images.go
Expand Up @@ -3,6 +3,7 @@ package kotsadm
import (
"fmt"

"github.com/replicatedhq/kots/pkg/image"
"github.com/replicatedhq/kots/pkg/kotsadm/types"
kotsadmversion "github.com/replicatedhq/kots/pkg/kotsadm/version"
)
Expand All @@ -12,9 +13,10 @@ func GetAdminConsoleImage(deployOptions types.DeployOptions, imageKey string) st
}

func GetAdminConsoleImages(deployOptions types.DeployOptions) map[string]string {
minioTag := "RELEASE.2021-08-05T22-01-19Z"
// TODO: Add error handling to this function
minioTag, _ := image.GetTag(image.Minio)
postgresTag := getPostgresTag(deployOptions)
dexTag := "v2.28.1"
dexTag, _ := image.GetTag(image.Dex)

if deployOptions.KotsadmOptions.OverrideVersion != "" {
minioTag = deployOptions.KotsadmOptions.OverrideVersion
Expand Down
6 changes: 4 additions & 2 deletions pkg/kotsadm/objects/postgres_objects.go
Expand Up @@ -2,6 +2,7 @@ package kotsadm

import (
"fmt"
"github.com/replicatedhq/kots/pkg/image"

"github.com/blang/semver"
"github.com/pkg/errors"
Expand Down Expand Up @@ -251,8 +252,9 @@ func PostgresService(namespace string) *corev1.Service {
func getPostgresTag(deployOptions types.DeployOptions) string {
// use the debian stretch based image for openshift because of this issue in alpine https://github.com/docker-library/postgres/issues/359
// TODO: This breaks when the hidden kotsadm-tag CLI flag is used to push images. There will be only one postgres image.
alpineTag := "10.17-alpine"
debianTag := "10.17" // use this when version cannot be determined because this tag always works
// TODO: Add error handling getPostgres tag should return error
alpineTag, _ := image.GetTag(image.PostgresAlpine)
debianTag, _ := image.GetTag(image.PostgresDebian) // use this when version cannot be determined because this tag always works

if !deployOptions.IsOpenShift {
return alpineTag
Expand Down

0 comments on commit 8eae9f2

Please sign in to comment.