Skip to content

Commit

Permalink
refactor default labels
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab committed Oct 25, 2024
1 parent bc5d659 commit 4d6dd6e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
45 changes: 34 additions & 11 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
DefaultPlatform = "linux/amd64"
DefaultDockerfile = "Dockerfile"
DefaultAuthors = "DiSe/SRE"
DefaultVendor = "Elisa"
DefaultBuildCtx = "."
DefaultExtraCtx = "./target/bin/linux/amd64/"
)
Expand All @@ -48,15 +49,15 @@ func PushAllTags(ctx context.Context, imageName string) error {
}

// BuildDefault builds image with sane defaults.
func BuildDefault(ctx context.Context, imageName, url string) error {
return BuildDefaultWithDockerfile(ctx, imageName, url, DefaultAuthors, DefaultDockerfile)
func BuildDefault(ctx context.Context, imageName string, l *Labels) error {
return BuildDefaultWithDockerfile(ctx, imageName, l, DefaultDockerfile)
}

// BuildDefaultWithDockerfile builds image from custom Dockerfile location
func BuildDefaultWithDockerfile(ctx context.Context, imageName, url, authors string, dockerfile string) error {
func BuildDefaultWithDockerfile(ctx context.Context, imageName string, l *Labels, dockerfile string) error {
fullTags := Tags(imageName)
extraCtx := map[string]string{"bin": DefaultExtraCtx}
labels := DefaultLabels(imageName, url, "", authors)
labels := DefaultLabels(imageName, l)
return Build(ctx, DefaultPlatform, dockerfile, DefaultBuildCtx, fullTags, extraCtx, labels)
}

Expand Down Expand Up @@ -96,18 +97,40 @@ func Tags(imageName string, tags ...string) []string {
return fullTags
}

// DefaultLabels provides labels for Elisa organization.
func DefaultLabels(imageName, url, desc, authors string) map[string]string {
type Labels struct {
Authors string
Vendor string
URL string
Desc string
}

// DefaultLabels provides labels for organization.
func DefaultLabels(imageName string, l *Labels) map[string]string {
if l == nil {
l = &Labels{
Authors: DefaultAuthors,
Vendor: DefaultVendor,
}
}

if l.Authors == "" {
l.Authors = DefaultAuthors
}

if l.Vendor == "" {
l.Vendor = DefaultVendor
}

return map[string]string{
OCILabelTitle: path.Base(imageName),
OCILabelURL: url,
OCILabelURL: l.URL,
OCILabelVersion: "",
OCILabelDescription: desc,
OCILabelDescription: l.Desc,
OCILabelCreated: time.Now().String(),
OCILabelSource: url,
OCILabelSource: l.URL,
OCILabelLicenses: "",
OCILabelAuthors: authors,
OCILabelVendor: "Elisa",
OCILabelAuthors: l.Authors,
OCILabelVendor: l.Vendor,
OCILabelRevision: "",
}
}
7 changes: 6 additions & 1 deletion docker/target/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
ImageName = ""
ProjectUrl = "" // Used for OCI label.
ProjectAuthors = docker.DefaultAuthors
ProjectVendor = docker.DefaultVendor
Dockerfile = docker.DefaultDockerfile
)

Expand All @@ -29,7 +30,11 @@ func (Docker) Push(ctx context.Context) error {

// Build builds docker image
func (Docker) Build(ctx context.Context) error {
return docker.BuildDefaultWithDockerfile(ctx, ImageName, ProjectUrl, ProjectAuthors, Dockerfile)
return docker.BuildDefaultWithDockerfile(ctx, ImageName, &docker.Labels{
URL: ProjectUrl,
Authors: ProjectAuthors,
Vendor: docker.DefaultVendor,
}, Dockerfile)
}

// Up start containers in daemon mode
Expand Down

0 comments on commit 4d6dd6e

Please sign in to comment.