Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pr default tag together with user provided tags #11981

Merged
merged 13 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cmd/image-builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ type Config struct {
DevRegistry Registry `yaml:"dev-registry" json:"dev-registry"`
// Cache options that are directly related to kaniko flags
Cache CacheConfig `yaml:"cache" json:"cache"`
// TagTemplate is go-template field that defines the format of the $_TAG substitution.
// See tags.Tag struct for more information and available fields
// TagTemplate is used to generate default tag for push events for current image-builder version.
// This will be removed after migration.
TagTemplate tags.Tag `yaml:"tag-template" json:"tag-template"`
// Default Tag template used for images build on commit.
// The value can be a go-template string or literal tag value string.
// See tags.Tag struct for more information and available fields
DefaultCommitTag tags.Tag `yaml:"default-commit-tag" json:"default-commit-tag"`
// Default Tag template used for images build on pull request.
// The value can be a go-template string or literal tag value string.
// See tags.Tag struct for more information and available fields
DefaultPRTag tags.Tag `yaml:"default-pr-tag" json:"default-pr-tag"`
// LogFormat defines the format kaniko logs are projected.
// Supported formats are 'color', 'text' and 'json'. Default: 'color'
LogFormat string `yaml:"log-format" json:"log-format"`
Expand Down
32 changes: 24 additions & 8 deletions cmd/image-builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ func Test_ParseConfig(t *testing.T) {
name: "parsed full config one repo",
config: `registry: kyma-project.io/prod-registry
dev-registry: dev.kyma-project.io/dev-registry
tag-template: v{{ .Date }}-{{ .ShortSHA }}`,
default-commit-tag:
name: default_tag
value: v{{ .Date }}-{{ .ShortSHA }}
validation: ^(v[0-9]{8}-[0-9a-f]{8})$
default-pr-tag:
name: default_tag
value: pr-{{ .PRNumber }}
validation: ^(PR-[0-9]+)$`,
expectedConfig: Config{
Registry: []string{"kyma-project.io/prod-registry"},
DevRegistry: []string{"dev.kyma-project.io/dev-registry"},
TagTemplate: tags.Tag{Name: "default_tag", Value: `v{{ .Date }}-{{ .ShortSHA }}`},
Registry: []string{"kyma-project.io/prod-registry"},
DevRegistry: []string{"dev.kyma-project.io/dev-registry"},
DefaultCommitTag: tags.Tag{Name: "default_tag", Value: `v{{ .Date }}-{{ .ShortSHA }}`, Validation: `^(v[0-9]{8}-[0-9a-f]{8})$`},
DefaultPRTag: tags.Tag{Name: "default_tag", Value: `pr-{{ .PRNumber }}`, Validation: `^(PR-[0-9]+)$`},
},
},
{
Expand All @@ -34,11 +42,19 @@ tag-template: v{{ .Date }}-{{ .ShortSHA }}`,
dev-registry:
- dev.kyma-project.io/dev-registry
- dev.kyma-project.io/second-registry
tag-template: v{{ .Date }}-{{ .ShortSHA }}`,
default-commit-tag:
name: default_tag
value: v{{ .Date }}-{{ .ShortSHA }}
validation: ^(v[0-9]{8}-[0-9a-f]{8})$
default-pr-tag:
name: default_tag
value: pr-{{ .PRNumber }}
validation: ^(PR-[0-9]+)$`,
expectedConfig: Config{
Registry: []string{"kyma-project.io/prod-registry", "kyma-project.io/second-registry"},
DevRegistry: []string{"dev.kyma-project.io/dev-registry", "dev.kyma-project.io/second-registry"},
TagTemplate: tags.Tag{Name: "default_tag", Value: `v{{ .Date }}-{{ .ShortSHA }}`},
Registry: []string{"kyma-project.io/prod-registry", "kyma-project.io/second-registry"},
DevRegistry: []string{"dev.kyma-project.io/dev-registry", "dev.kyma-project.io/second-registry"},
DefaultCommitTag: tags.Tag{Name: "default_tag", Value: `v{{ .Date }}-{{ .ShortSHA }}`, Validation: `^(v[0-9]{8}-[0-9a-f]{8})$`},
DefaultPRTag: tags.Tag{Name: "default_tag", Value: `pr-{{ .PRNumber }}`, Validation: `^(PR-[0-9]+)$`},
},
},
{
Expand Down
62 changes: 44 additions & 18 deletions cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,13 @@ func buildLocally(o options) error {
return fmt.Errorf("'sha' could not be determined")
}

defaultTag, err := getDefaultTag(o)
if err != nil {
return err
}

// Get the tags for the image.
parsedTags, err := getTags(pr, sha, append(o.tags, o.TagTemplate))
parsedTags, err := getTags(pr, sha, append(o.tags, defaultTag))
if err != nil {
return err
}
Expand Down Expand Up @@ -638,13 +643,16 @@ func (l *StrList) List() []string {
}

func getTags(pr, sha string, templates []tags.Tag) ([]tags.Tag, error) {
// (Ressetkk): PR tag should not be hardcoded, in the future we have to find a way to parametrize it
if pr != "" {
// assume we are using PR number, build default tag as 'PR-XXXX'
return []tags.Tag{{Name: "default_tag", Value: "PR-" + pr}}, nil
var taggerOptions []tags.TagOption
if len(pr) > 0 {
taggerOptions = append(taggerOptions, tags.PRNumber(pr))
}
if len(sha) > 0 {
taggerOptions = append(taggerOptions, tags.CommitSHA(sha))
}

// build a tag from commit SHA
tagger, err := tags.NewTagger(templates, tags.CommitSHA(sha))
tagger, err := tags.NewTagger(templates, taggerOptions...)
if err != nil {
return nil, fmt.Errorf("get tagger: %w", err)
}
Expand Down Expand Up @@ -914,41 +922,59 @@ func getEnvs(o options, dockerfilePath string) (map[string]string, error) {
}

func parseTags(o options) ([]tags.Tag, error) {
var pr string
sha := o.gitState.BaseCommitSHA
if o.gitState.isPullRequest {
pr = fmt.Sprint(o.gitState.PullRequestNumber)
var (
pr string
sha string
)
if !o.gitState.isPullRequest && o.gitState.BaseCommitSHA != "" {
sha = o.gitState.BaseCommitSHA
}

if sha == "" {
return nil, fmt.Errorf("sha still empty")
if o.gitState.isPullRequest && o.gitState.PullRequestNumber > 0 {
pr = fmt.Sprint(o.gitState.PullRequestNumber)
}

// TODO (dekiel): Tags provided as base64 encoded string should be parsed and added to the tags list when parsing flags.
// This way all tags are available in the tags list from thr very beginning of execution and can be used in any process.
// read tags from base64 encoded string if provided
if o.tagsBase64 != "" {
decoded, err := base64.StdEncoding.DecodeString(o.tagsBase64)
if err != nil {
fmt.Printf("Failed to decode tags, error: %s", err)
os.Exit(1)
return nil, fmt.Errorf("failed to decode tags, error: %w", err)
}
splitedTags := strings.Split(string(decoded), ",")
for _, tag := range splitedTags {
err = o.tags.Set(tag)
if err != nil {
fmt.Printf("Failed to set tag, tag: %s, error: %s", tag, err)
os.Exit(1)
return nil, fmt.Errorf("failed to set tag, tag: %s, error: %w", tag, err)
}
}
}

parsedTags, err := getTags(pr, sha, append(o.tags, o.TagTemplate))
defaultTag, err := getDefaultTag(o)
if err != nil {
return nil, err
}
parsedTags, err := getTags(pr, sha, append(o.tags, defaultTag))
if err != nil {
return nil, err
}

return parsedTags, nil
}

// getDefaultTag returns the default tag based on the read git state.
// The function provid default tag for pull request or commit.
// The default tag is read from the provided options struct.
func getDefaultTag(o options) (tags.Tag, error) {
if o.gitState.isPullRequest && o.gitState.PullRequestNumber > 0 {
return o.DefaultPRTag, nil
}
if len(o.gitState.BaseCommitSHA) > 0 {
return o.DefaultCommitTag, nil
}
return tags.Tag{}, fmt.Errorf("could not determine default tag, no pr number or commit sha provided")
}

func getDockerfileDirPath(o options) (string, error) {
// Get the absolute path to the build context directory.
context, err := filepath.Abs(o.context)
Expand Down
Loading
Loading