-
Notifications
You must be signed in to change notification settings - Fork 24
/
container.go
95 lines (82 loc) · 2.91 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cc
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
ver "github.com/aserto-dev/topaz/pkg/version"
"github.com/Masterminds/semver/v3"
"github.com/samber/lo"
)
const (
DefaultValue string = ""
defaultContainerRegistry string = "ghcr.io/aserto-dev"
defaultContainerImage string = "topaz"
defaultContainerTag string = "latest"
defaultContainerName string = "topaz"
)
// Container returns the fully qualified container name (registry/image:tag).
func Container(registry, image, tag string) string {
if container := os.Getenv("CONTAINER"); container != "" {
return container
}
return fmt.Sprintf("%s/%s:%s",
lo.Ternary(registry != "", registry, ContainerRegistry()),
lo.Ternary(image != "", image, ContainerImage()),
lo.Ternary(tag != "", tag, ContainerTag()),
)
}
// ContainerRegistry returns the container registry (host[:port]/repo).
func ContainerRegistry() string {
if containerRegistry := os.Getenv("CONTAINER_REGISTRY"); containerRegistry != "" {
return containerRegistry
}
return defaultContainerRegistry
}
// ContainerImage returns the container image name.
func ContainerImage() string {
if containerImage := os.Getenv("CONTAINER_IMAGE"); containerImage != "" {
return containerImage
}
return defaultContainerImage
}
// ContainerTag returns the container tag (label or semantic version).
func ContainerTag() string {
if containerTag := os.Getenv("CONTAINER_TAG"); containerTag != "" {
return containerTag
}
v, err := semver.NewVersion(ver.GetInfo().Version)
if err != nil {
return defaultContainerTag
}
return v.String()
}
// ContainerPlatform, returns the container platform for multi-platform capable servers.
func ContainerPlatform() string {
if containerPlatform := os.Getenv("CONTAINER_PLATFORM"); containerPlatform != "" {
return containerPlatform
}
return "linux/" + runtime.GOARCH
}
// ContainerName returns the container instance name (docker run --name CONTAINER_NAME).
func ContainerName(defaultConfigFile string) string {
if containerName := os.Getenv("CONTAINER_NAME"); containerName != "" {
return containerName
}
if strings.Contains(defaultConfigFile, "config.yaml") {
return defaultContainerName
}
return fmt.Sprintf("%s-%s", defaultContainerName, strings.Split(filepath.Base(defaultConfigFile), ".")[0])
}
// ContainerVersionTag consolidates the old --container-version with the --container-tag value,
// the command handlers will read the environment variable versions $CONTAINER_VERSION and $CONTAINER_TAG,
// which is why they are not explicitly handled in this function.
func ContainerVersionTag(version, tag string) string {
if version != "" && version != tag {
fmt.Fprintf(os.Stderr, "!!! --container-version incl $CONTAINER_VERSION are obsolete, use: --container-tag and $CONTAINER_TAG instead\n")
fmt.Fprintf(os.Stderr, "!!! --container-version value %q it propagated to --container-tag\n", version)
return version
}
return tag
}