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

Align kompose env interpolation with k8s #1979

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion client/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package client

import (
"fmt"
v1 "k8s.io/api/core/v1"
"sort"
"testing"

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

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -84,6 +85,24 @@ func TestConvertWithDefaultOptions(t *testing.T) {
}
}

func TestConvertWithEnv(t *testing.T) {
client, err := NewClient(WithErrorOnWarning())
assert.Check(t, is.Equal(err, nil))
objects, err := client.Convert(ConvertOptions{
ToStdout: true,
InputFiles: []string{
"./testdata/docker-compose-env.yaml",
},
})
assert.Check(t, is.Equal(err, nil))
for _, object := range objects {
if deployment, ok := object.(*appsv1.Deployment); ok {
assert.Check(t, is.Equal(deployment.Spec.Template.Spec.Containers[0].Args[2], "$(REDIS_PASSWORD)"))
assert.Check(t, is.Equal(deployment.Spec.Template.Spec.Containers[0].LivenessProbe.Exec.Command[0], "redis-cli -a $REDIS_PASSWORD --raw incr ping"))
}
}
}

func TestConvertWithProfiles(t *testing.T) {
client, err := NewClient(WithErrorOnWarning())
assert.Check(t, is.Equal(err, nil))
Expand Down
9 changes: 9 additions & 0 deletions client/testdata/docker-compose-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3"
services:
redis:
image: redis
command: redis-server --requirepass $$REDIS_PASSWORD
healthcheck:
test: redis-cli -a $$REDIS_PASSWORD --raw incr ping
ports:
- 6379
5 changes: 4 additions & 1 deletion pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,9 +1030,12 @@ func FormatResourceName(name string) string {

// GetContainerArgs update the interpolation of env variables if exists.
// example: [curl, $PROTOCOL://$DOMAIN] => [curl, $(PROTOCOL)://$(DOMAIN)]
//
// > Environment variable names consist of letters, numbers, underscores, dots, or hyphens, but the first character cannot be a digit
// https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config
func GetContainerArgs(service kobject.ServiceConfig) []string {
var args []string
re := regexp.MustCompile(`\$([a-zA-Z0-9]*)`)
re := regexp.MustCompile(`\$([a-zA-Z0-9.-_]+)`)
for _, arg := range service.Args {
arg = re.ReplaceAllString(arg, `$($1)`)
args = append(args, arg)
Expand Down
Loading