Skip to content

Commit

Permalink
chore: remove built-in vars in custom labels and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Aug 26, 2024
1 parent f8b783e commit f35de14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
52 changes: 6 additions & 46 deletions pkg/controller/component/synthesize_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"context"
"fmt"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -274,15 +273,6 @@ func buildComp2CompDefs(ctx context.Context, cli client.Reader, cluster *appsv1a

// buildLabelsAndAnnotations builds labels and annotations for synthesizedComponent.
func buildLabelsAndAnnotations(compDef *appsv1alpha1.ComponentDefinition, comp *appsv1alpha1.Component, synthesizeComp *SynthesizedComponent) {
replaceEnvPlaceholderTokens := func(clusterName, uid, componentName string, kvMap map[string]string) map[string]string {
replacedMap := make(map[string]string, len(kvMap))
builtInEnvMap := GetReplacementMapForBuiltInEnv(clusterName, uid, componentName)
for k, v := range kvMap {
replacedMap[ReplaceNamedVars(builtInEnvMap, k, -1, true)] = ReplaceNamedVars(builtInEnvMap, v, -1, true)
}
return replacedMap
}

mergeMaps := func(baseMap, overrideMap map[string]string) map[string]string {
for k, v := range overrideMap {
baseMap[k] = v
Expand All @@ -291,18 +281,18 @@ func buildLabelsAndAnnotations(compDef *appsv1alpha1.ComponentDefinition, comp *
}

if compDef.Spec.Labels != nil || comp.Labels != nil {
baseLabels := make(map[string]string)
if compDef.Spec.Labels != nil {
baseLabels = replaceEnvPlaceholderTokens(synthesizeComp.ClusterName, synthesizeComp.ClusterUID, synthesizeComp.Name, compDef.Spec.Labels)
baseLabels := compDef.Spec.Labels
if baseLabels == nil {
baseLabels = make(map[string]string)
}
// override labels from component
synthesizeComp.Labels = mergeMaps(baseLabels, comp.Labels)
}

if compDef.Spec.Annotations != nil || comp.Annotations != nil {
baseAnnotations := make(map[string]string)
if compDef.Spec.Annotations != nil {
baseAnnotations = replaceEnvPlaceholderTokens(synthesizeComp.ClusterName, synthesizeComp.ClusterUID, synthesizeComp.Name, compDef.Spec.Annotations)
baseAnnotations := compDef.Spec.Annotations
if baseAnnotations == nil {
baseAnnotations = make(map[string]string)
}
// override annotations from component
synthesizeComp.Annotations = mergeMaps(baseAnnotations, comp.Annotations)
Expand Down Expand Up @@ -582,36 +572,6 @@ func buildCompatibleHorizontalScalePolicy(compDef *appsv1alpha1.ComponentDefinit
}
}

// GetReplacementMapForBuiltInEnv gets the replacement map for KubeBlocks built-in environment variables.
func GetReplacementMapForBuiltInEnv(clusterName, clusterUID, componentName string) map[string]string {
cc := constant.GenerateClusterComponentName(clusterName, componentName)
replacementMap := map[string]string{
constant.EnvPlaceHolder(constant.KBEnvClusterName): clusterName,
constant.EnvPlaceHolder(constant.KBEnvCompName): componentName,
constant.EnvPlaceHolder(constant.KBEnvClusterCompName): cc,
constant.KBComponentEnvCMPlaceHolder: constant.GenerateClusterComponentEnvPattern(clusterName, componentName),
}
clusterUIDPostfix := clusterUID
if len(clusterUID) > 8 {
clusterUIDPostfix = clusterUID[len(clusterUID)-8:]
}
replacementMap[constant.EnvPlaceHolder(constant.KBEnvClusterUIDPostfix8Deprecated)] = clusterUIDPostfix
return replacementMap
}

// ReplaceNamedVars replaces the placeholder in targetVar if it is match and returns the replaced result
func ReplaceNamedVars(namedValuesMap map[string]string, targetVar string, limits int, matchAll bool) string {
for placeHolderKey, mappingValue := range namedValuesMap {
r := strings.Replace(targetVar, placeHolderKey, mappingValue, limits)
// early termination on matching, when matchAll = false
if r != targetVar && !matchAll {
return r
}
targetVar = r
}
return targetVar
}

func GetConfigSpecByName(synthesizedComp *SynthesizedComponent, configSpec string) *appsv1alpha1.ComponentConfigSpec {
for i := range synthesizedComp.ConfigTemplates {
template := &synthesizedComp.ConfigTemplates[i]
Expand Down
19 changes: 18 additions & 1 deletion pkg/controller/configuration/builtin_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
coreclient "sigs.k8s.io/controller-runtime/pkg/client"

cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/generics"
)
Expand Down Expand Up @@ -228,7 +229,7 @@ func (w *envWrapper) doEnvReplace(replacedVars *set.LinkedHashSetString, oldValu
clusterName = w.clusterName
clusterUID = w.clusterUID
componentName = w.componentName
builtInEnvMap = component.GetReplacementMapForBuiltInEnv(clusterName, clusterUID, componentName)
builtInEnvMap = getReplacementMapForBuiltInEnv(clusterName, clusterUID, componentName)
)

kbInnerEnvReplaceFn := func(envName string, strToReplace string) string {
Expand Down Expand Up @@ -307,3 +308,19 @@ func containerResourceRefValue(fieldSelector *corev1.ResourceFieldSelector, c *c
func fieldRefValue(podReference *corev1.ObjectFieldSelector, podSpec *corev1.PodSpec) (string, error) {
return "", cfgcore.MakeError("not support pod field ref")
}

func getReplacementMapForBuiltInEnv(clusterName, clusterUID, componentName string) map[string]string {
cc := constant.GenerateClusterComponentName(clusterName, componentName)
replacementMap := map[string]string{
constant.EnvPlaceHolder(constant.KBEnvClusterName): clusterName,
constant.EnvPlaceHolder(constant.KBEnvCompName): componentName,
constant.EnvPlaceHolder(constant.KBEnvClusterCompName): cc,
constant.KBComponentEnvCMPlaceHolder: constant.GenerateClusterComponentEnvPattern(clusterName, componentName),
}
clusterUIDPostfix := clusterUID
if len(clusterUID) > 8 {
clusterUIDPostfix = clusterUID[len(clusterUID)-8:]
}
replacementMap[constant.EnvPlaceHolder(constant.KBEnvClusterUIDPostfix8Deprecated)] = clusterUIDPostfix
return replacementMap
}

0 comments on commit f35de14

Please sign in to comment.