Skip to content

Commit

Permalink
Merge pull request #198 from jmesnil/197_podtemplate_labels_from_env
Browse files Browse the repository at this point in the history
Add labels to the statefulset's template
  • Loading branch information
jmesnil committed Jul 20, 2021
2 parents dc17a6b + cbf621e commit 6aba9ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions pkg/resources/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
SecretsDir = "/etc/secrets/"
// ConfigMapsDir is the the directory to mount volumes from ConfigMaps
ConfigMapsDir = "/etc/configmaps/"
// StatefuleSetTemplateLabelsEnvVarName is the name of the envvar containg label/value map for pods created from the statefulset's template
StatefuleSetTemplateLabelsEnvVarName string = "STATEFULSET_TEMPLATE_LABELS"
)

var (
Expand All @@ -35,15 +37,15 @@ var (
// JBossHome is read from the env var JBOSS_HOME or, in case of a Bootable JAR, from JBOSS_BOOTABLE_HOME
func JBossHome(bootable bool) string {
if bootable {
return os.Getenv("JBOSS_BOOTABLE_HOME")
return os.Getenv("JBOSS_BOOTABLE_HOME")
}
return os.Getenv("JBOSS_HOME")
}

// JBossHome is read from the env var JBOSS_HOME or, in case of a Bootable JAR, from JBOSS_BOOTABLE_DATA_DIR
func JBossHomeDataDir(bootable bool) string {
if bootable {
return os.Getenv("JBOSS_BOOTABLE_DATA_DIR")
return os.Getenv("JBOSS_BOOTABLE_DATA_DIR")
}
return os.Getenv("JBOSS_HOME")
}
22 changes: 20 additions & 2 deletions pkg/resources/statefulsets/statefulset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package statefulsets

import (
"encoding/json"
"os"
"path"

Expand Down Expand Up @@ -48,6 +49,7 @@ func NewStatefulSet(w *wildflyv1alpha1.WildFlyServer, labels map[string]string,
labelsForActiveWildflyPod := wildflyutil.CopyMap(labels)
labelsForActiveWildflyPod[resources.MarkerOperatedByHeadless] = resources.MarkerServiceActive
labelsForActiveWildflyPod[resources.MarkerOperatedByLoadbalancer] = resources.MarkerServiceActive
applyLabels(resources.StatefuleSetTemplateLabelsEnvVarName, labelsForActiveWildflyPod)

wildflyImageTypeAnnotation := resources.ImageTypeGeneric
if w.Spec.BootableJar {
Expand Down Expand Up @@ -193,7 +195,7 @@ func NewStatefulSet(w *wildflyv1alpha1.WildFlyServer, labels map[string]string,
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "standalone-config-volume",
MountPath: path.Join(resources.JBossHome(w.Spec.BootableJar),"standalone/configuration/standalone.xml"),
MountPath: path.Join(resources.JBossHome(w.Spec.BootableJar), "standalone/configuration/standalone.xml"),
SubPath: "standalone.xml",
})
}
Expand Down Expand Up @@ -340,11 +342,27 @@ func envArgsForBootableJAR(defaultDataDir string) []corev1.EnvVar {
return []corev1.EnvVar{
{
Name: "JAVA_ARGS",
Value: "-Djboss.server.data.dir="+path.Join(resources.JBossHomeDataDir(true), defaultDataDir) + " --install-dir=" + resources.JBossHome(true),
Value: "-Djboss.server.data.dir=" + path.Join(resources.JBossHomeDataDir(true), defaultDataDir) + " --install-dir=" + resources.JBossHome(true),
},
{
Name: "JBOSS_HOME",
Value: resources.JBossHome(true),
},
}
}

func applyLabels(envvar string, labels map[string]string) {
labelsFromEnv := os.Getenv(envvar)
if labelsFromEnv == "" {
return
}
var labelMap map[string]string
if err := json.Unmarshal([]byte(labelsFromEnv), &labelMap); err != nil {
return
}
if len(labelMap) > 0 {
for name, value := range labelMap {
labels[name] = value
}
}
}

0 comments on commit 6aba9ae

Please sign in to comment.