2023-08-11 13:12:13 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2023-12-06 19:45:37 +00:00
|
|
|
|
|
|
|
goversion "github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
MinVersionAgentTokenPartition = goversion.Must(goversion.NewVersion("v1.11.0"))
|
|
|
|
MinVersionPeering = goversion.Must(goversion.NewVersion("v1.13.0"))
|
|
|
|
MinVersionTLS = goversion.Must(goversion.NewVersion("v1.12.0"))
|
2023-07-17 22:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Images struct {
|
2023-10-26 17:12:20 +00:00
|
|
|
// Consul is the image used for creating the container,
|
|
|
|
// Use ChooseConsul() to control which image (ConsulCE or ConsulEnterprise) assign to Consul
|
|
|
|
Consul string `json:",omitempty"`
|
|
|
|
// ConsulCE sets the CE image
|
|
|
|
ConsulCE string `json:",omitempty"`
|
2023-12-06 19:45:37 +00:00
|
|
|
// consulVersion is the version part of Consul image,
|
|
|
|
// e.g., if Consul image is hashicorp/consul-enterprise:1.15.0-ent,
|
|
|
|
// consulVersion is 1.15.0-ent
|
|
|
|
consulVersion string
|
2023-10-26 17:12:20 +00:00
|
|
|
// ConsulEnterprise sets the ent image
|
2023-07-17 22:15:22 +00:00
|
|
|
ConsulEnterprise string `json:",omitempty"`
|
|
|
|
Envoy string
|
|
|
|
Dataplane string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Images) LocalDataplaneImage() string {
|
|
|
|
if i.Dataplane == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
img, tag, ok := strings.Cut(i.Dataplane, ":")
|
|
|
|
if !ok {
|
|
|
|
tag = "latest"
|
|
|
|
}
|
|
|
|
|
2023-11-08 23:20:00 +00:00
|
|
|
name := strings.ReplaceAll(img, "/", "-")
|
2023-07-17 22:15:22 +00:00
|
|
|
|
|
|
|
// ex: local/hashicorp-consul-dataplane:1.1.0
|
|
|
|
return "local/" + name + ":" + tag
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:13:16 +00:00
|
|
|
func (i Images) LocalDataplaneTProxyImage() string {
|
|
|
|
return spliceImageNamesAndTags(i.Dataplane, i.Consul, "tproxy")
|
|
|
|
}
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
func (i Images) EnvoyConsulImage() string {
|
2023-11-02 21:13:16 +00:00
|
|
|
return spliceImageNamesAndTags(i.Consul, i.Envoy, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func spliceImageNamesAndTags(base1, base2, nameSuffix string) string {
|
|
|
|
if base1 == "" || base2 == "" {
|
2023-07-17 22:15:22 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:13:16 +00:00
|
|
|
img1, tag1, ok1 := strings.Cut(base1, ":")
|
|
|
|
img2, tag2, ok2 := strings.Cut(base2, ":")
|
2023-07-17 22:15:22 +00:00
|
|
|
if !ok1 {
|
|
|
|
tag1 = "latest"
|
|
|
|
}
|
|
|
|
if !ok2 {
|
|
|
|
tag2 = "latest"
|
|
|
|
}
|
|
|
|
|
2023-11-08 23:20:00 +00:00
|
|
|
name1 := strings.ReplaceAll(img1, "/", "-")
|
|
|
|
name2 := strings.ReplaceAll(img2, "/", "-")
|
2023-07-17 22:15:22 +00:00
|
|
|
|
2023-11-02 21:13:16 +00:00
|
|
|
if nameSuffix != "" {
|
|
|
|
nameSuffix = "-" + nameSuffix
|
|
|
|
}
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
// ex: local/hashicorp-consul-and-envoyproxy-envoy:1.15.0-with-v1.26.2
|
2023-11-02 21:13:16 +00:00
|
|
|
return "local/" + name1 + "-and-" + name2 + nameSuffix + ":" + tag1 + "-with-" + tag2
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 23:46:34 +00:00
|
|
|
// TODO: what is this for and why do we need to do this and why is it named this?
|
2023-07-17 22:15:22 +00:00
|
|
|
func (i Images) ChooseNode(kind NodeKind) Images {
|
|
|
|
switch kind {
|
|
|
|
case NodeKindServer:
|
|
|
|
i.Envoy = ""
|
|
|
|
i.Dataplane = ""
|
|
|
|
case NodeKindClient:
|
|
|
|
i.Dataplane = ""
|
|
|
|
case NodeKindDataplane:
|
|
|
|
i.Envoy = ""
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:12:20 +00:00
|
|
|
// ChooseConsul controls which image assigns to Consul
|
2023-07-17 22:15:22 +00:00
|
|
|
func (i Images) ChooseConsul(enterprise bool) Images {
|
|
|
|
if enterprise {
|
|
|
|
i.Consul = i.ConsulEnterprise
|
|
|
|
} else {
|
2023-08-22 14:46:03 +00:00
|
|
|
i.Consul = i.ConsulCE
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
i.ConsulEnterprise = ""
|
2023-08-22 14:46:03 +00:00
|
|
|
i.ConsulCE = ""
|
2023-12-06 19:45:37 +00:00
|
|
|
|
|
|
|
// extract the version part of Consul
|
|
|
|
i.consulVersion = i.Consul[strings.Index(i.Consul, ":")+1:]
|
2023-07-17 22:15:22 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2023-12-06 19:45:37 +00:00
|
|
|
// GreaterThanVersion compares the image version to a specified version
|
|
|
|
func (i Images) GreaterThanVersion(version *goversion.Version) bool {
|
|
|
|
if i.consulVersion == "local" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
iVer := goversion.Must(goversion.NewVersion(i.consulVersion))
|
|
|
|
return iVer.GreaterThanOrEqual(version)
|
|
|
|
}
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
func (i Images) OverrideWith(i2 Images) Images {
|
|
|
|
if i2.Consul != "" {
|
|
|
|
i.Consul = i2.Consul
|
|
|
|
}
|
2023-08-22 14:46:03 +00:00
|
|
|
if i2.ConsulCE != "" {
|
|
|
|
i.ConsulCE = i2.ConsulCE
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
if i2.ConsulEnterprise != "" {
|
|
|
|
i.ConsulEnterprise = i2.ConsulEnterprise
|
|
|
|
}
|
|
|
|
if i2.Envoy != "" {
|
|
|
|
i.Envoy = i2.Envoy
|
|
|
|
}
|
|
|
|
if i2.Dataplane != "" {
|
|
|
|
i.Dataplane = i2.Dataplane
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultImages controls which specific docker images are used as default
|
|
|
|
// values for topology components that do not specify values.
|
|
|
|
//
|
|
|
|
// These can be bulk-updated using the make target 'make update-defaults'
|
|
|
|
func DefaultImages() Images {
|
|
|
|
return Images{
|
|
|
|
Consul: "",
|
2023-11-30 17:41:30 +00:00
|
|
|
ConsulCE: DefaultConsulCEImage,
|
2023-07-17 22:15:22 +00:00
|
|
|
ConsulEnterprise: DefaultConsulEnterpriseImage,
|
|
|
|
Envoy: DefaultEnvoyImage,
|
|
|
|
Dataplane: DefaultDataplaneImage,
|
|
|
|
}
|
|
|
|
}
|