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 all 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
100 changes: 100 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,100 @@
package workload

import (
"strings"

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

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

supportedParsers = []MagicEnvParser{
SecretEnvParser,
// As the default parser, the RawEnvParser should be placed at
// the end.
RawEnvParser,
}
)

// MagicEnvVar generates a specialized EnvVar based on the key and
elliotxx marked this conversation as resolved.
Show resolved Hide resolved
// value of environment.
//
// Examples:
//
// MagicEnvVar("secret_key", "secret://my_secret/my_key")
// MagicEnvVar("key", "value")
func MagicEnvVar(k, v string) *corev1.EnvVar {
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],
},
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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'")
}
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
1 change: 0 additions & 1 deletion 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 Down
Loading