Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support some magic env parsers #473

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions pkg/generator/appconfiguration/generator/workload/magic_env_var.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package workload

import (
"strings"

corev1 "k8s.io/api/core/v1"
)

var (
SecretEnvParser MagicEnvParser = NewSecretEnvParser()
elliotxx marked this conversation as resolved.
Show resolved Hide resolved
ConfigMapEnvParser = NewConfigMapEnvParser()
elliotxx marked this conversation as resolved.
Show resolved Hide resolved
RawEnvParser = NewRawEnvParser()
)

// MagicEnvVar generates a specialized EnvVar based on the key and
elliotxx marked this conversation as resolved.
Show resolved Hide resolved
// value of environment.
func MagicEnvVar(k, v string) *corev1.EnvVar {
supportedParsers := []MagicEnvParser{
elliotxx marked this conversation as resolved.
Show resolved Hide resolved
SecretEnvParser,
ConfigMapEnvParser,
RawEnvParser,
}
for _, p := range supportedParsers {
if p.Match(k, v) {
return p.Gen(k, v)
}
}
return nil
}

// MagicEnvParser is an interface for environment variable parsers.
type MagicEnvParser interface {
Match(k, v string) (matched bool)
Gen(k, v string) *corev1.EnvVar
}

// rawEnvParser is a parser for raw environment variables.
type rawEnvParser struct{}

// NewRawEnvParser creates a new instance of RawEnvParser.
func NewRawEnvParser() MagicEnvParser {
return &rawEnvParser{}
}

// Match checks if the value matches the raw parser.
func (*rawEnvParser) Match(_ string, _ string) bool {
return true
}

// Gen generates a raw environment variable.
func (*rawEnvParser) Gen(k string, v string) *corev1.EnvVar {
return &corev1.EnvVar{
Name: k,
Value: v,
}
}

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

// NewSecretEnvParser creates a new instance of SecretEnvParser.
func NewSecretEnvParser() MagicEnvParser {
return &secretEnvParser{
prefix: "secret://",
}
}

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

// Gen generates a secret-based environment variable.
func (p *secretEnvParser) 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{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: vs[0],
},
Key: vs[1],
},
},
}
}

// 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
@@ -0,0 +1,29 @@
package workload

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMagicEnvVar(t *testing.T) {
// Test raw environment variable
rawEnv := MagicEnvVar("key", "value")
assert.NotNil(t, rawEnv, "Raw environment variable should not be nil")
assert.Equal(t, "key", rawEnv.Name, "Expected raw environment variable name to be 'key'")
assert.Equal(t, "value", rawEnv.Value, "Expected raw environment variable value to be 'value'")

// Test secret-based environment variable
secretEnv := MagicEnvVar("secret_key", "secret://my_secret/my_key")
assert.NotNil(t, secretEnv, "Secret-based environment variable should not be nil")
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'")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"kusionstack.io/kube-api/apps/v1alpha1"

"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload/container"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ func toOrderedContainers(appContainers map[string]container.Container) ([]corev1
// Create a slice of env vars based on the container's env vars.
var envs []corev1.EnvVar
for k, v := range c.Env {
envs = append(envs, corev1.EnvVar{
Name: k,
Value: v,
})
envs = append(envs, *MagicEnvVar(k, v))
}

// Create a container object and append it to the containers slice.
Expand Down
14 changes: 8 additions & 6 deletions pkg/generator/appconfiguration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"kusionstack.io/kusion/pkg/models"
)

Expand All @@ -23,8 +22,8 @@ func CallGeneratorFuncs(newGenerators ...NewGeneratorFunc) ([]Generator, error)
return gs, nil
}

// CallGenerators calls the Generate method of each AppsGenerator instance
// returned by the given NewGeneratorFuncs.
// CallGenerators calls the Generate method of each AppsGenerator
// instance returned by the given NewGeneratorFuncs.
elliotxx marked this conversation as resolved.
Show resolved Hide resolved
func CallGenerators(spec *models.Spec, newGenerators ...NewGeneratorFunc) error {
gs, err := CallGeneratorFuncs(newGenerators...)
if err != nil {
Expand Down Expand Up @@ -95,7 +94,8 @@ func KubernetesResourceID(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta
return id
}

// AppendToSpec adds a Kubernetes resource to a spec's resources slice.
// AppendToSpec adds a Kubernetes resource to a spec's resources
// slice.
func AppendToSpec(resourceType models.Type, resourceID string, spec *models.Spec, resource any) error {
unstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(resource)
if err != nil {
Expand All @@ -112,12 +112,14 @@ func AppendToSpec(resourceType models.Type, resourceID string, spec *models.Spec
return nil
}

// UniqueAppName returns a unique name for a workload based on its project and app name.
// UniqueAppName returns a unique name for a workload based on its
// project and app name.
func UniqueAppName(projectName, stackName, appName string) string {
return projectName + "-" + stackName + "-" + appName
}

// UniqueAppLabels returns a map of labels that identify an app based on its project and name.
// UniqueAppLabels returns a map of labels that identify an app based
// on its project and name.
func UniqueAppLabels(projectName, appName string) map[string]string {
return map[string]string{
"app.kubernetes.io/part-of": projectName,
Expand Down
Loading