Skip to content

Commit

Permalink
Merge pull request #94 from vshn/empty-env
Browse files Browse the repository at this point in the history
add: Test for empty env vars
  • Loading branch information
davidgubler authored Nov 10, 2023
2 parents 0eeb1fc + a55479e commit 768d242
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 4 deletions.
13 changes: 10 additions & 3 deletions internal/prechecks.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package internal

import (
"github.com/vshn/k8ify/pkg/util"
"os"

"github.com/vshn/k8ify/pkg/util"

"github.com/sirupsen/logrus"
"github.com/vshn/k8ify/pkg/ir"
)
Expand Down Expand Up @@ -33,14 +34,20 @@ func ComposeServicePrecheck(inputs *ir.Inputs) {
for _, part := range service.GetParts() {
partSingleton := util.IsSingleton(part.AsCompose().Labels)
if partSingleton && !parentSingleton {
logrus.Errorf("Singleton compose service %s can't be part of non-singleton compose service %s", part.Name, service.Name)
logrus.Errorf("Singleton compose service '%s' can't be part of non-singleton compose service '%s'", part.Name, service.Name)
os.Exit(1)
}
if !partSingleton && parentSingleton {
logrus.Errorf("Non-singleton compose service %s can't be part of singleton compose service %s", part.Name, service.Name)
logrus.Errorf("Non-singleton compose service '%s' can't be part of singleton compose service '%s'", part.Name, service.Name)
os.Exit(1)
}
}
environmentValues := service.AsCompose().Environment
for key, value := range environmentValues {
if value == nil {
logrus.Warnf("Service '%s' has environment variable '%s' with value nil. There may be a problem with your compose file(s). Please use empty string \"\" values instead.", service.Name, key)
}
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ func composeServiceToSecret(workload *ir.Service, refSlug string, labels map[str
}
stringData := make(map[string]string)
for key, value := range workload.AsCompose().Environment {
stringData[key] = *value
if value == nil {
stringData[key] = ""
} else {
stringData[key] = *value
}
}
secret := core.Secret{}
secret.APIVersion = "v1"
Expand Down
7 changes: 7 additions & 0 deletions tests/golden/empty-env-vars-list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
environments:
prod:
vars:
foo:
bar: ""
baz: asdf
10 changes: 10 additions & 0 deletions tests/golden/empty-env-vars-list/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.4'
services:
pinger:
image: pinger:4.0
environment:
- testempty1=
- testempty2=$foo
- testempty3=$bar
- testnotempty=$baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
k8ify.ref-slug: oasp
k8ify.service: pinger
name: pinger-oasp
spec:
selector:
matchLabels:
k8ify.ref-slug: oasp
k8ify.service: pinger
strategy:
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
k8ify.ref-slug: oasp
k8ify.service: pinger
spec:
containers:
- envFrom:
- secretRef:
name: pinger-oasp-env
image: pinger:4.0
imagePullPolicy: Always
name: pinger-oasp
resources: {}
restartPolicy: Always
status: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Secret
metadata:
creationTimestamp: null
labels:
k8ify.ref-slug: oasp
k8ify.service: pinger
name: pinger-oasp-env
stringData:
testempty1: ""
testempty2: ""
testempty3: ""
testnotempty: asdf
7 changes: 7 additions & 0 deletions tests/golden/empty-env-vars-map.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
environments:
prod:
vars:
foo:
bar: ""
baz: asdf
12 changes: 12 additions & 0 deletions tests/golden/empty-env-vars-map/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.4'
services:
pinger:
image: pinger:4.0
environment:
testempty1: ""
testempty2: $foo
testempty3: $bar
testempty4:
testempty5: null
testnotempty: $baz

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
k8ify.ref-slug: oasp
k8ify.service: pinger
name: pinger-oasp
spec:
selector:
matchLabels:
k8ify.ref-slug: oasp
k8ify.service: pinger
strategy:
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
k8ify.ref-slug: oasp
k8ify.service: pinger
spec:
containers:
- envFrom:
- secretRef:
name: pinger-oasp-env
image: pinger:4.0
imagePullPolicy: Always
name: pinger-oasp
resources: {}
restartPolicy: Always
status: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Secret
metadata:
creationTimestamp: null
labels:
k8ify.ref-slug: oasp
k8ify.service: pinger
name: pinger-oasp-env
stringData:
testempty1: ""
testempty2: ""
testempty3: ""
testempty4: ""
testempty5: ""
testnotempty: asdf

0 comments on commit 768d242

Please sign in to comment.