Skip to content

Commit

Permalink
refactor: remove configmap magic env
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotxx committed Aug 21, 2023
1 parent cfc7bb4 commit e5e6a7c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 51 deletions.
46 changes: 2 additions & 44 deletions pkg/generator/appconfiguration/generator/workload/magic_env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
)

var (
SecretEnvParser MagicEnvParser = NewSecretEnvParser()
ConfigMapEnvParser = NewConfigMapEnvParser()
RawEnvParser = NewRawEnvParser()
SecretEnvParser MagicEnvParser = NewSecretEnvParser()
RawEnvParser = NewRawEnvParser()

supportedParsers = []MagicEnvParser{
SecretEnvParser,
ConfigMapEnvParser,
// As the default parser, the RawEnvParser should be placed at
// the end.
RawEnvParser,
Expand All @@ -26,7 +24,6 @@ var (
// Examples:
//
// MagicEnvVar("secret_key", "secret://my_secret/my_key")
// MagicEnvVar("config_key", "configmap://my_configmap/my_key")
// MagicEnvVar("key", "value")
func MagicEnvVar(k, v string) *corev1.EnvVar {
for _, p := range supportedParsers {
Expand Down Expand Up @@ -101,42 +98,3 @@ func (p *secretEnvParser) Gen(k string, v string) *corev1.EnvVar {
},
}
}

// configMapEnvParser is a parser for configmap-based environment
// variables.
type configMapEnvParser struct {
prefix string
}

// NewConfigMapEnvParser creates a new instance of ConfigMapEnvParser.
func NewConfigMapEnvParser() MagicEnvParser {
return &configMapEnvParser{
prefix: "configmap://",
}
}

// Match checks if the value matches the configmap parser.
func (p *configMapEnvParser) Match(_ string, v string) bool {
return strings.HasPrefix(v, p.prefix)
}

// Gen generates a configmap-based environment variable.
func (p *configMapEnvParser) Gen(k string, v string) *corev1.EnvVar {
vv := strings.TrimPrefix(v, p.prefix)
vs := strings.Split(vv, "/")
if len(vs) != 2 {
return nil
}

return &corev1.EnvVar{
Name: k,
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: vs[0],
},
Key: vs[1],
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,4 @@ func TestMagicEnvVar(t *testing.T) {
assert.Equal(t, "secret_key", secretEnv.Name, "Expected secret-based environment variable name to be 'secret_key'")
assert.Equal(t, "my_secret", secretEnv.ValueFrom.SecretKeyRef.LocalObjectReference.Name, "Expected secret name to be 'my_secret'")
assert.Equal(t, "my_key", secretEnv.ValueFrom.SecretKeyRef.Key, "Expected secret key to be 'my_key'")

// Test configmap-based environment variable
configMapEnv := MagicEnvVar("config_key", "configmap://my_configmap/my_key")
assert.NotNil(t, configMapEnv, "Configmap-based environment variable should not be nil")
assert.Equal(t, "config_key", configMapEnv.Name, "Expected configmap-based environment variable name to be 'config_key'")
assert.Equal(t, "my_configmap", configMapEnv.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name, "Expected configmap name to be 'my_configmap'")
assert.Equal(t, "my_key", configMapEnv.ValueFrom.ConfigMapKeyRef.Key, "Expected configmap key to be 'my_key'")
}

0 comments on commit e5e6a7c

Please sign in to comment.