Skip to content

Commit

Permalink
Shorten list of recommended tags/images in CLI to 1 AC version #2166 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andriisoldatenko authored Nov 17, 2020
1 parent 505ae4a commit fa4e5d6
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
4 changes: 2 additions & 2 deletions airflow/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func Deploy(path, name, wsId string, prompt bool) error {
}

if config.CFG.ShowWarnings.GetBool() && !diResp.Data.DeploymentConfig.IsValidTag(tag) {
validTags := strings.Join(diResp.Data.DeploymentConfig.GetValidTags(), ", ")
validTags := strings.Join(diResp.Data.DeploymentConfig.GetValidTags(tag), ", ")
i, _ := input.InputConfirm(fmt.Sprintf(messages.WARNING_INVALID_IMAGE_TAG, tag, validTags))
if !i {
fmt.Println("Cancelling deploy...")
Expand Down Expand Up @@ -658,7 +658,7 @@ func Deploy(path, name, wsId string, prompt bool) error {
func validImageRepo(image string) bool {
validDockerfileBaseImages := map[string]bool{
"quay.io/astronomer/ap-airflow": true,
"astronomerinc/ap-airflow": true,
"astronomerinc/ap-airflow": true,
}
result, ok := validDockerfileBaseImages[image]
if !ok {
Expand Down
30 changes: 27 additions & 3 deletions houston/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package houston

import (
"fmt"

"github.com/Masterminds/semver"
)

// Response wraps all houston response structs used for json marashalling
type Response struct {
Data struct {
Expand Down Expand Up @@ -231,15 +237,20 @@ type DeploymentConfig struct {
AirflowVersions []string `json:"airflowVersions"`
}

func (config *DeploymentConfig) GetValidTags() (tags []string) {
func (config *DeploymentConfig) GetValidTags(tag string) (tags []string) {
for _, image := range config.AirflowImages {
tags = append(tags, image.Tag)
tagVersion := coerce(tag)
imageTagVersion := coerce(image.Version)
// i = 1 means version greater than
if i := imageTagVersion.Compare(tagVersion); i >= 0 {
tags = append(tags, image.Tag)
}
}
return
}

func (config *DeploymentConfig) IsValidTag(tag string) bool {
for _, validTag := range config.GetValidTags() {
for _, validTag := range config.GetValidTags(tag) {
if tag == validTag {
return true
}
Expand All @@ -254,3 +265,16 @@ type AppConfig struct {
SmtpConfigured bool `json:"smtpConfigured"`
ManualReleaseNames bool `json:"manualReleaseNames"`
}

// coerce a string into SemVer if possible
func coerce(version string) *semver.Version {
v, err := semver.NewVersion(version)
if err != nil {
fmt.Println(err)
}
coerceVer, err := semver.NewVersion(fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch()))
if err != nil {
fmt.Println(err)
}
return coerceVer
}
75 changes: 75 additions & 0 deletions houston/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package houston

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetValidTagsSimpleSemVer(t *testing.T) {
dCfg := DeploymentConfig{
AirflowImages: []AirflowImage{
{Tag: "1.10.5-11-alpine3.10-onbuild", Version: "1.10.5-11"},
{Tag: "1.10.5-11-buster-onbuild", Version: "1.10.5-11"},
{Tag: "1.10.5-11-alpine3.10", Version: "1.10.5-11"},
{Tag: "1.10.5-11-buster", Version: "1.10.5-11"},
{Tag: "1.10.5-buster-onbuild", Version: "1.10.5"},
{Tag: "1.10.5-alpine3.10", Version: "1.10.5"},
{Tag: "1.10.5-buster", Version: "1.10.5"},
{Tag: "1.10.5-alpine3.10-onbuild", Version: "1.10.5"},
{Tag: "1.10.7-7-buster-onbuild", Version: "1.10.7-7"},
{Tag: "1.10.7-7-alpine3.10", Version: "1.10.7-7"},
{Tag: "1.10.7-7-buster", Version: "1.10.7-7"},
{Tag: "1.10.7-7-alpine3.10-onbuild", Version: "1.10.7-7"},
{Tag: "1.10.7-8-alpine3.10-onbuild", Version: "1.10.7-8"},
{Tag: "1.10.7-8-buster-onbuild", Version: "1.10.7-8"},
{Tag: "1.10.7-8-alpine3.10", Version: "1.10.7-8"},
{Tag: "1.10.7-8-buster", Version: "1.10.7-8"},
},
}
validTags := dCfg.GetValidTags("1.10.7")
expectedTags := []string{
"1.10.7-7-buster-onbuild",
"1.10.7-7-alpine3.10",
"1.10.7-7-buster",
"1.10.7-7-alpine3.10-onbuild",
"1.10.7-8-alpine3.10-onbuild",
"1.10.7-8-buster-onbuild",
"1.10.7-8-alpine3.10",
"1.10.7-8-buster"}
assert.Equal(t, expectedTags, validTags)
}

func TestGetValidTagsWithPreReleaseTag(t *testing.T) {
dCfg := DeploymentConfig{
AirflowImages: []AirflowImage{
{Tag: "1.10.5-11-alpine3.10-onbuild", Version: "1.10.5-11"},
{Tag: "1.10.5-11-buster-onbuild", Version: "1.10.5-11"},
{Tag: "1.10.5-11-alpine3.10", Version: "1.10.5-11"},
{Tag: "1.10.5-11-buster", Version: "1.10.5-11"},
{Tag: "1.10.5-buster-onbuild", Version: "1.10.5"},
{Tag: "1.10.5-alpine3.10", Version: "1.10.5"},
{Tag: "1.10.5-buster", Version: "1.10.5"},
{Tag: "1.10.5-alpine3.10-onbuild", Version: "1.10.5"},
{Tag: "1.10.7-7-buster-onbuild", Version: "1.10.7-7"},
{Tag: "1.10.7-7-alpine3.10", Version: "1.10.7-7"},
{Tag: "1.10.7-7-buster", Version: "1.10.7-7"},
{Tag: "1.10.7-7-alpine3.10-onbuild", Version: "1.10.7-7"},
{Tag: "1.10.7-8-alpine3.10-onbuild", Version: "1.10.7-8"},
{Tag: "1.10.7-8-buster-onbuild", Version: "1.10.7-8"},
{Tag: "1.10.7-8-alpine3.10", Version: "1.10.7-8"},
{Tag: "1.10.7-8-buster", Version: "1.10.7-8"},
{Tag: "1.10.12-buster", Version: "1.10.12"},
{Tag: "1.10.12-buster-onbuild", Version: "1.10.12"},
{Tag: "1.10.12-1-buster-onbuild", Version: "1.10.12-1"},
},
}
validTags := dCfg.GetValidTags("1.10.12-1-alpine3.10")
expectedTags := []string{"1.10.12-buster", "1.10.12-buster-onbuild", "1.10.12-1-buster-onbuild"}
assert.Equal(t, expectedTags, validTags)
}

func Test_coerce(t *testing.T) {
assert.Equal(t, "1.10.5", coerce("1.10.5-11-alpine3.10-onbuild").String())
assert.Equal(t, "1.10.5", coerce("1.10.5").String())
assert.Equal(t, "1.10.12", coerce("1.10.12-buster").String())
}

0 comments on commit fa4e5d6

Please sign in to comment.