2021-11-30 12:04:28 +01:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
2022-02-14 14:37:09 +01:00
|
|
|
"bytes"
|
2021-12-05 09:05:48 +01:00
|
|
|
"crypto/sha1"
|
2021-12-01 15:17:34 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-11-30 12:04:28 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2022-02-14 14:37:09 +01:00
|
|
|
"text/template"
|
2021-11-30 12:04:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const K = "katenary.io"
|
2021-12-05 09:05:48 +01:00
|
|
|
const RELEASE_NAME = "{{ .Release.Name }}"
|
2021-12-02 14:56:51 +01:00
|
|
|
const (
|
|
|
|
LABEL_ENV_SECRET = K + "/secret-envfiles"
|
|
|
|
LABEL_PORT = K + "/ports"
|
|
|
|
LABEL_INGRESS = K + "/ingress"
|
|
|
|
LABEL_ENV_SERVICE = K + "/env-to-service"
|
|
|
|
LABEL_VOL_CM = K + "/configmap-volumes"
|
2022-02-14 14:37:09 +01:00
|
|
|
LABEL_HEALTHCHECK = K + "/healthcheck"
|
2022-02-16 17:40:11 +01:00
|
|
|
LABEL_SAMEPOD = K + "/same-pod"
|
|
|
|
LABEL_EMPTYDIRS = K + "/empty-dirs"
|
2021-12-02 14:56:51 +01:00
|
|
|
)
|
2021-11-30 12:04:28 +01:00
|
|
|
|
2022-02-14 14:37:09 +01:00
|
|
|
func GetLabelsDocumentation() string {
|
|
|
|
t, _ := template.New("labels").Parse(`
|
|
|
|
# Labels
|
|
|
|
{{.LABEL_ENV_SECRET | printf "%-33s"}}: set the given file names as a secret instead of configmap
|
2022-02-16 17:48:57 +01:00
|
|
|
{{.LABEL_PORT | printf "%-33s"}}: set the ports to expose as a service (coma separated)
|
|
|
|
{{.LABEL_INGRESS | printf "%-33s"}}: set the port to expose in an ingress (coma separated)
|
|
|
|
{{.LABEL_ENV_SERVICE | printf "%-33s"}}: specifies that the environment variable points on a service name (coma separated)
|
|
|
|
{{.LABEL_VOL_CM | printf "%-33s"}}: specifies that the volumes points on a configmap (coma separated)
|
2022-02-16 17:44:25 +01:00
|
|
|
{{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the given service name
|
2022-02-16 17:48:57 +01:00
|
|
|
{{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of persistentVolumeClaim (coma separated)
|
2022-02-14 14:37:09 +01:00
|
|
|
{{.LABEL_HEALTHCHECK | printf "%-33s"}}: specifies that the container should be monitored by a healthcheck, **it overrides the docker-compose healthcheck**.
|
|
|
|
{{ printf "%-34s" ""}} You can use these form of label values:
|
|
|
|
{{ printf "%-35s" ""}}- "http://[not used address][:port][/path]" to specify an http healthcheck
|
|
|
|
{{ printf "%-35s" ""}}- "tcp://[not used address]:port" to specify a tcp healthcheck
|
|
|
|
{{ printf "%-35s" ""}}- other string is condidered as a "command" healthcheck
|
|
|
|
`)
|
|
|
|
buff := bytes.NewBuffer(nil)
|
|
|
|
t.Execute(buff, map[string]string{
|
|
|
|
"LABEL_ENV_SECRET": LABEL_ENV_SECRET,
|
|
|
|
"LABEL_ENV_SERVICE": LABEL_ENV_SERVICE,
|
|
|
|
"LABEL_PORT": LABEL_PORT,
|
|
|
|
"LABEL_INGRESS": LABEL_INGRESS,
|
|
|
|
"LABEL_VOL_CM": LABEL_VOL_CM,
|
|
|
|
"LABEL_HEALTHCHECK": LABEL_HEALTHCHECK,
|
2022-02-16 17:40:11 +01:00
|
|
|
"LABEL_SAMEPOD": LABEL_SAMEPOD,
|
|
|
|
"LABEL_EMPTYDIRS": LABEL_EMPTYDIRS,
|
2022-02-14 14:37:09 +01:00
|
|
|
})
|
|
|
|
return buff.String()
|
|
|
|
}
|
|
|
|
|
2021-12-05 09:05:48 +01:00
|
|
|
var (
|
|
|
|
Appname = ""
|
|
|
|
Version = "1.0" // should be set from main.Version
|
|
|
|
)
|
2021-11-30 12:04:28 +01:00
|
|
|
|
|
|
|
type Kinded interface {
|
|
|
|
Get() string
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:17:34 +01:00
|
|
|
type Signable interface {
|
|
|
|
BuildSHA(filename string)
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:07:15 +01:00
|
|
|
type Named interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
2021-11-30 12:04:28 +01:00
|
|
|
type Metadata struct {
|
|
|
|
Name string `yaml:"name,omitempty"`
|
|
|
|
Labels map[string]string `yaml:"labels"`
|
|
|
|
Annotations map[string]string `yaml:"annotations,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMetadata() *Metadata {
|
|
|
|
return &Metadata{
|
|
|
|
Name: "",
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
Annotations: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type K8sBase struct {
|
|
|
|
ApiVersion string `yaml:"apiVersion"`
|
|
|
|
Kind string `yaml:"kind"`
|
|
|
|
Metadata *Metadata `yaml:"metadata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBase() *K8sBase {
|
|
|
|
|
|
|
|
b := &K8sBase{
|
|
|
|
Metadata: NewMetadata(),
|
|
|
|
}
|
2021-12-05 09:05:48 +01:00
|
|
|
// add some information of the build
|
2021-12-02 14:59:08 +01:00
|
|
|
b.Metadata.Labels[K+"/project"] = GetProjectName()
|
2021-12-05 09:05:48 +01:00
|
|
|
b.Metadata.Labels[K+"/release"] = RELEASE_NAME
|
2021-11-30 12:04:28 +01:00
|
|
|
b.Metadata.Annotations[K+"/version"] = Version
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:17:34 +01:00
|
|
|
func (k *K8sBase) BuildSHA(filename string) {
|
|
|
|
c, _ := ioutil.ReadFile(filename)
|
2021-12-05 09:05:48 +01:00
|
|
|
//sum := sha256.Sum256(c)
|
|
|
|
sum := sha1.Sum(c)
|
|
|
|
k.Metadata.Annotations[K+"/docker-compose-sha1"] = fmt.Sprintf("%x", string(sum[:]))
|
2021-12-01 15:17:34 +01:00
|
|
|
}
|
|
|
|
|
2021-12-02 16:07:15 +01:00
|
|
|
func (k *K8sBase) Get() string {
|
2021-11-30 12:04:28 +01:00
|
|
|
return k.Kind
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:07:15 +01:00
|
|
|
func (k *K8sBase) Name() string {
|
|
|
|
return k.Metadata.Name
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:59:08 +01:00
|
|
|
func GetProjectName() string {
|
2021-12-01 15:17:34 +01:00
|
|
|
if len(Appname) > 0 {
|
|
|
|
return Appname
|
|
|
|
}
|
2021-11-30 12:04:28 +01:00
|
|
|
p, _ := os.Getwd()
|
|
|
|
path := strings.Split(p, string(os.PathSeparator))
|
|
|
|
return path[len(path)-1]
|
|
|
|
}
|