Add a "ignore" label to skip a service

This commit is contained in:
2022-05-05 10:22:02 +02:00
parent 910933e5f3
commit 053292f37c
4 changed files with 80 additions and 47 deletions

View File

@@ -166,6 +166,7 @@ services:
These labels could be found by `katenary show-labels`, and can be placed as "labels" inside your docker-compose file:
```
katenary.io/ignore : ignore the container, it will not yied any object in the helm chart
katenary.io/secret-envfiles : set the given file names as a secret instead of configmap
katenary.io/ports : set the ports to expose as a service (coma separated)
katenary.io/ingress : set the port to expose in an ingress (coma separated)

View File

@@ -45,9 +45,35 @@ func Generate(p *compose.Parser, katernayVersion, appName, appVersion, composeFi
// Manage services, avoid linked pods and store all services port in servicesMap
avoids := make(map[string]bool)
skips := make(map[string]bool)
for _, s := range p.Data.Services {
if s.Labels[helm.LABEL_IGNORE] == "true" {
skips[s.Name] = true
}
}
// remove skipped services
for s := range skips {
for i, service := range p.Data.Services {
if service.Name == s {
p.Data.Services = append(p.Data.Services[:i], p.Data.Services[i+1:]...)
break
}
}
}
for i, service := range p.Data.Services {
n := service.Name
// if the service depends on a skipped service, remove the link
for dep := range service.DependsOn {
if skips[dep] {
delete(service.DependsOn, dep)
}
}
// if the service port is declared in labels, add it to the service.
if ports, ok := service.Labels[helm.LABEL_PORT]; ok {
if service.Ports == nil {
service.Ports = make([]types.ServicePortConfig, 0)

52
helm/labels.go Normal file
View File

@@ -0,0 +1,52 @@
package helm
import (
"bytes"
"html/template"
)
const RELEASE_NAME = "{{ .Release.Name }}"
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"
LABEL_HEALTHCHECK = K + "/healthcheck"
LABEL_SAMEPOD = K + "/same-pod"
LABEL_EMPTYDIRS = K + "/empty-dirs"
LABEL_IGNORE = K + "/ignore"
)
// GetLabelsDocumentation returns the documentation for the labels.
func GetLabelsDocumentation() string {
t, _ := template.New("labels").Parse(`
# Labels
{{ .LABEL_IGNORE | printf "%-33s"}}: ignore the container, it will not yied any object in the helm chart
{{.LABEL_ENV_SECRET | printf "%-33s"}}: set the given file names as a secret instead of configmap
{{.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)
{{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the given service name
{{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of persistentVolumeClaim (coma separated)
{{.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,
"LABEL_SAMEPOD": LABEL_SAMEPOD,
"LABEL_EMPTYDIRS": LABEL_EMPTYDIRS,
"LABEL_IGNORE": LABEL_IGNORE,
})
return buff.String()
}

View File

@@ -1,77 +1,31 @@
package helm
import (
"bytes"
"os"
"strings"
"text/template"
)
const K = "katenary.io"
const RELEASE_NAME = "{{ .Release.Name }}"
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"
LABEL_HEALTHCHECK = K + "/healthcheck"
LABEL_SAMEPOD = K + "/same-pod"
LABEL_EMPTYDIRS = K + "/empty-dirs"
)
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
{{.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)
{{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the given service name
{{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of persistentVolumeClaim (coma separated)
{{.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,
"LABEL_SAMEPOD": LABEL_SAMEPOD,
"LABEL_EMPTYDIRS": LABEL_EMPTYDIRS,
})
return buff.String()
}
var (
Appname = ""
Appname = "" // set at runtime
Version = "1.0" // should be set from main.Version
)
// Kinded represent an object with a kind.
type Kinded interface {
// Get must resturn the kind name.
Get() string
}
// Signable represents an object with a signature.
type Signable interface {
// BuildSHA must return the signature.
BuildSHA(filename string)
}
// Named represents an object with a name.
type Named interface {
// Name must return the name of the object (from metadata).
Name() string
}