Skip to content

Commit

Permalink
enhance: add link, host, and custom labeling (#201)
Browse files Browse the repository at this point in the history
* enhance: add link, host, and custom labeling

* parameters + tidy

* set custom labels in plugin struct
  • Loading branch information
ecrupper authored Nov 20, 2024
1 parent 0e69356 commit 354c553
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 218 deletions.
39 changes: 39 additions & 0 deletions cmd/vela-kaniko/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,21 @@ func main() {
Name: "label.url",
Usage: "direct url of the repository",
},
&cli.StringFlag{
EnvVars: []string{"VELA_BUILD_LINK"},
Name: "label.build_link",
Usage: "direct Vela link to the build",
},
&cli.StringFlag{
EnvVars: []string{"VELA_BUILD_HOST"},
Name: "label.host",
Usage: "host that the image is built on",
},
&cli.StringFlag{
EnvVars: []string{"VELA_BUILD_CUSTOM_LABELS", "PARAMETER_CUSTOM_LABELS"},
Name: "label.custom",
Usage: "custom labels to add to the image in the form LABEL_NAME=ENV_KEY",
},
&cli.StringSliceFlag{
EnvVars: []string{"VELA_REPO_TOPICS"},
Name: "label.topics",
Expand Down Expand Up @@ -381,6 +396,27 @@ func run(c *cli.Context) error {
}
}

// target type for custom labels
var customLabels []string

labelsStr := c.String("label.custom")
if len(labelsStr) > 0 {
customLabelsMap := make(map[string]string)

// attempt to unmarshal to map
err := json.Unmarshal([]byte(labelsStr), &customLabelsMap)
if err != nil {
// fall back on splitting the string
customLabels = strings.Split(labelsStr, ",")
} else {
// iterate through the custom labels map
for key, value := range customLabelsMap {
// add the custom label to the custom labels
customLabels = append(customLabels, fmt.Sprintf("%s=%s", key, value))
}
}
}

// create the plugin
p := &Plugin{
// build configuration
Expand Down Expand Up @@ -436,6 +472,9 @@ func run(c *cli.Context) error {
Number: c.Int("label.number"),
Topics: c.StringSlice("label.topics"),
URL: c.String("label.url"),
BuildURL: c.String("label.build_link"),
Host: c.String("label.host"),
CustomSet: customLabels,
},
Labels: c.StringSlice("repo.labels"),
},
Expand Down
18 changes: 9 additions & 9 deletions cmd/vela-kaniko/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,6 @@ func (p *Plugin) Command() *exec.Cmd {
flags = append(flags, fmt.Sprintf("--destination=%s:%s", p.Repo.Name, tag))
}

// add predefined labels to user provided labels
p.Repo.Labels = append(p.Repo.Labels, p.Repo.AddLabels()...)

// iterate through all repo labels
for _, label := range p.Repo.Labels {
// add flag for tag from provided repo tag
flags = append(flags, "--label", label)
}

// add flag for dockerfile from provided image dockerfile
flags = append(flags, fmt.Sprintf("--dockerfile=%s", p.Image.Dockerfile))

Expand Down Expand Up @@ -188,6 +179,15 @@ func (p *Plugin) Command() *exec.Cmd {
// add flag for logging verbosity
flags = append(flags, fmt.Sprintf("--verbosity=%s", logrus.GetLevel()))

// add predefined labels to user provided labels
p.Repo.Labels = append(p.Repo.Labels, p.Repo.AddLabels()...)

// iterate through all repo labels
for _, label := range p.Repo.Labels {
// add flag for tag from provided repo tag
flags = append(flags, fmt.Sprintf("--label=%s", label))
}

return exec.Command(kanikoBin, flags...)
}

Expand Down
Loading

0 comments on commit 354c553

Please sign in to comment.