Add containerPort label

This commit is contained in:
2022-06-10 14:36:03 +02:00
parent 8860bb01be
commit b7c867a963
2 changed files with 67 additions and 37 deletions

View File

@@ -852,6 +852,33 @@ func newContainerForDeployment(
prepareInitContainers(containerName, s, container)..., prepareInitContainers(containerName, s, container)...,
) )
// check if there is containerPort assigned in label, add it, and do
// not create service for this.
if ports, ok := s.Labels[helm.LABEL_CONTAINER_PORT]; ok {
for _, port := range strings.Split(ports, ",") {
func(port string, container *helm.Container, s *types.ServiceConfig) {
port = strings.TrimSpace(port)
if port == "" {
return
}
portNumber, err := strconv.Atoi(port)
if err != nil {
return
}
// avoid already declared ports
for _, p := range s.Ports {
if int(p.Target) == portNumber {
return
}
}
container.Ports = append(container.Ports, &helm.ContainerPort{
Name: deployName + "-" + port,
ContainerPort: portNumber,
})
}(port, container, s)
}
}
return container return container
} }

View File

@@ -7,18 +7,19 @@ import (
const ReleaseNameTpl = "{{ .Release.Name }}" const ReleaseNameTpl = "{{ .Release.Name }}"
const ( const (
LABEL_MAP_ENV = K + "/mapenv" LABEL_MAP_ENV = K + "/mapenv"
LABEL_ENV_SECRET = K + "/secret-envfiles" LABEL_ENV_SECRET = K + "/secret-envfiles"
LABEL_PORT = K + "/ports" LABEL_PORT = K + "/ports"
LABEL_INGRESS = K + "/ingress" LABEL_CONTAINER_PORT = K + "/container-ports"
LABEL_VOL_CM = K + "/configmap-volumes" LABEL_INGRESS = K + "/ingress"
LABEL_HEALTHCHECK = K + "/healthcheck" LABEL_VOL_CM = K + "/configmap-volumes"
LABEL_SAMEPOD = K + "/same-pod" LABEL_HEALTHCHECK = K + "/healthcheck"
LABEL_VOLUMEFROM = K + "/volume-from" LABEL_SAMEPOD = K + "/same-pod"
LABEL_EMPTYDIRS = K + "/empty-dirs" LABEL_VOLUMEFROM = K + "/volume-from"
LABEL_IGNORE = K + "/ignore" LABEL_EMPTYDIRS = K + "/empty-dirs"
LABEL_SECRETVARS = K + "/secret-vars" LABEL_IGNORE = K + "/ignore"
LABEL_CRON = K + "/crontabs" LABEL_SECRETVARS = K + "/secret-vars"
LABEL_CRON = K + "/crontabs"
//deprecated: use LABEL_MAP_ENV instead //deprecated: use LABEL_MAP_ENV instead
LABEL_ENV_SERVICE = K + "/env-to-service" LABEL_ENV_SERVICE = K + "/env-to-service"
@@ -27,19 +28,20 @@ const (
// GetLabelsDocumentation returns the documentation for the labels. // GetLabelsDocumentation returns the documentation for the labels.
func GetLabelsDocumentation() string { func GetLabelsDocumentation() string {
t, _ := template.New("labels").Parse(`# Labels t, _ := template.New("labels").Parse(`# Labels
{{.LABEL_IGNORE | printf "%-33s"}}: ignore the container, it will not yied any object in the helm chart (bool) {{.LABEL_IGNORE | printf "%-33s"}}: ignore the container, it will not yied any object in the helm chart (bool)
{{.LABEL_SECRETVARS | printf "%-33s"}}: secret variables to push on a secret file (coma separated) {{.LABEL_SECRETVARS | printf "%-33s"}}: secret variables to push on a secret file (coma separated)
{{.LABEL_ENV_SECRET | printf "%-33s"}}: set the given file names as a secret instead of configmap (coma separated) {{.LABEL_ENV_SECRET | printf "%-33s"}}: set the given file names as a secret instead of configmap (coma separated)
{{.LABEL_MAP_ENV | printf "%-33s"}}: map environment variable to a template string (yaml style, object) contaienr in {{.LABEL_MAP_ENV | printf "%-33s"}}: map environment variable to a template string (yaml style, object)
{{.LABEL_PORT | printf "%-33s"}}: set the ports to expose as a service (coma separated) {{.LABEL_PORT | printf "%-33s"}}: set the ports to assign on the container in pod + expose as a service (coma separated)
{{.LABEL_INGRESS | printf "%-33s"}}: set the port to expose in an ingress (coma separated) {{.LABEL_CONTAINER_PORT | printf "%-33s"}}: set the ports to assign on the contaienr in pod but avoid service (coma separated)
{{.LABEL_VOL_CM | printf "%-33s"}}: specifies that the volumes points on a configmap (coma separated) {{.LABEL_INGRESS | printf "%-33s"}}: set the port to expose in an ingress (coma separated)
{{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the {{.LABEL_VOL_CM | printf "%-33s"}}: specifies that the volumes points on a configmap (coma separated)
{{ printf "%-34s" ""}} given service name (string) {{.LABEL_SAMEPOD | printf "%-33s"}}: specifies that the pod should be deployed in the same pod than the
{{.LABEL_VOLUMEFROM | printf "%-33s"}}: specifies that the volumes to be mounted from the given service (yaml style) {{ printf "%-34s" ""} } given service name (string)
{{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of {{.LABEL_VOLUMEFROM | printf "%-33s"}}: specifies that the volumes to be mounted from the given service (yaml style)
{{ printf "%-34s" ""}} persistentVolumeClaim (coma separated) {{.LABEL_EMPTYDIRS | printf "%-33s"}}: specifies that the given volume names should be "emptyDir" instead of
{{.LABEL_CRON | printf "%-33s"}}: specifies a cronjobs to create (yaml style, array) - this will create a {{ printf "%-34s" ""} } persistentVolumeClaim (coma separated)
{{.LABEL_CRON | printf "%-33s"}}: specifies a cronjobs to create (yaml style, array) - this will create a
{{ printf "%-34s" ""}} cronjob, a service account, a role and a rolebinding to start the command with "kubectl" {{ printf "%-34s" ""}} cronjob, a service account, a role and a rolebinding to start the command with "kubectl"
{{ printf "%-34s" ""}} The form is the following: {{ printf "%-34s" ""}} The form is the following:
{{ printf "%-34s" ""}} - command: the command to run {{ printf "%-34s" ""}} - command: the command to run
@@ -54,18 +56,19 @@ func GetLabelsDocumentation() string {
{{ printf "%-35s" ""}} -> other string is condidered as a "command" healthcheck`) {{ printf "%-35s" ""}} -> other string is condidered as a "command" healthcheck`)
buff := bytes.NewBuffer(nil) buff := bytes.NewBuffer(nil)
t.Execute(buff, map[string]string{ t.Execute(buff, map[string]string{
"LABEL_ENV_SECRET": LABEL_ENV_SECRET, "LABEL_ENV_SECRET": LABEL_ENV_SECRET,
"LABEL_PORT": LABEL_PORT, "LABEL_PORT": LABEL_PORT,
"LABEL_INGRESS": LABEL_INGRESS, "LABEL_CONTAINER_PORT": LABEL_CONTAINER_PORT,
"LABEL_VOL_CM": LABEL_VOL_CM, "LABEL_INGRESS": LABEL_INGRESS,
"LABEL_HEALTHCHECK": LABEL_HEALTHCHECK, "LABEL_VOL_CM": LABEL_VOL_CM,
"LABEL_SAMEPOD": LABEL_SAMEPOD, "LABEL_HEALTHCHECK": LABEL_HEALTHCHECK,
"LABEL_VOLUMEFROM": LABEL_VOLUMEFROM, "LABEL_SAMEPOD": LABEL_SAMEPOD,
"LABEL_EMPTYDIRS": LABEL_EMPTYDIRS, "LABEL_VOLUMEFROM": LABEL_VOLUMEFROM,
"LABEL_IGNORE": LABEL_IGNORE, "LABEL_EMPTYDIRS": LABEL_EMPTYDIRS,
"LABEL_MAP_ENV": LABEL_MAP_ENV, "LABEL_IGNORE": LABEL_IGNORE,
"LABEL_SECRETVARS": LABEL_SECRETVARS, "LABEL_MAP_ENV": LABEL_MAP_ENV,
"LABEL_CRON": LABEL_CRON, "LABEL_SECRETVARS": LABEL_SECRETVARS,
"LABEL_CRON": LABEL_CRON,
}) })
return buff.String() return buff.String()
} }