Fix the problem with "ugly" environment syntax
We can now manage "- A=B" format as "A: B" Some others properties than environment may have this problem (e.g., command) so we will fix this later. fix #4
This commit is contained in:
@@ -54,6 +54,10 @@ func (p *Parser) Parse(appname string) {
|
|||||||
services := make(map[string][]string)
|
services := make(map[string][]string)
|
||||||
// get the service list, to be sure that everything is ok
|
// get the service list, to be sure that everything is ok
|
||||||
|
|
||||||
|
for _, s := range p.Data.Services {
|
||||||
|
parseEnv(s)
|
||||||
|
}
|
||||||
|
|
||||||
c := p.Data
|
c := p.Data
|
||||||
for name, s := range c.Services {
|
for name, s := range c.Services {
|
||||||
if portlabel, ok := s.Labels[helm.LABEL_PORT]; ok {
|
if portlabel, ok := s.Labels[helm.LABEL_PORT]; ok {
|
||||||
@@ -121,4 +125,36 @@ func (p *Parser) Parse(appname string) {
|
|||||||
" for the \"" + name + "\" service \x1b[0m")
|
" for the \"" + name + "\" service \x1b[0m")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage environment variables, if the type is map[string]string so we can use it, else we need to split "=" sign
|
||||||
|
// and apply this in env variable
|
||||||
|
func parseEnv(s *Service) {
|
||||||
|
env := make(map[string]string)
|
||||||
|
if s.RawEnvironment == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch s.RawEnvironment.(type) {
|
||||||
|
case map[string]string:
|
||||||
|
env = s.RawEnvironment.(map[string]string)
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, v := range s.RawEnvironment.(map[string]interface{}) {
|
||||||
|
env[k] = v.(string)
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
for _, v := range s.RawEnvironment.([]interface{}) {
|
||||||
|
// Splot the value of the env variable with "="
|
||||||
|
parts := strings.Split(v.(string), "=")
|
||||||
|
env[parts[0]] = parts[1]
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
parts := strings.Split(s.RawEnvironment.(string), "=")
|
||||||
|
env[parts[0]] = parts[1]
|
||||||
|
default:
|
||||||
|
log.Printf("%+v, %T", s.RawEnvironment, s.RawEnvironment)
|
||||||
|
log.Printf("%+v", s)
|
||||||
|
log.Fatal("Environment type not supported")
|
||||||
|
}
|
||||||
|
s.Environment = env
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,8 @@ type HealthCheck struct {
|
|||||||
type Service struct {
|
type Service struct {
|
||||||
Image string `yaml:"image"`
|
Image string `yaml:"image"`
|
||||||
Ports []string `yaml:"ports"`
|
Ports []string `yaml:"ports"`
|
||||||
Environment map[string]string `yaml:"environment"`
|
Environment map[string]string `yaml:"-"`
|
||||||
|
RawEnvironment interface{} `yaml:"environment"`
|
||||||
Labels map[string]string `yaml:"labels"`
|
Labels map[string]string `yaml:"labels"`
|
||||||
DependsOn []string `yaml:"depends_on"`
|
DependsOn []string `yaml:"depends_on"`
|
||||||
Volumes []string `yaml:"volumes"`
|
Volumes []string `yaml:"volumes"`
|
||||||
|
@@ -128,6 +128,13 @@ func parseService(name string, s *compose.Service, linked map[string]*compose.Se
|
|||||||
if len(s.Ports) == 0 {
|
if len(s.Ports) == 0 {
|
||||||
// alert any current or **future** waiters that this service is not exposed
|
// alert any current or **future** waiters that this service is not exposed
|
||||||
go func() {
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
// recover from panic
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
// log the stack trace
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-time.Tick(1 * time.Millisecond):
|
case <-time.Tick(1 * time.Millisecond):
|
||||||
@@ -378,12 +385,15 @@ func prepareVolumes(deployment, name string, s *compose.Service, container *helm
|
|||||||
if v, ok := s.Labels[helm.LABEL_VOL_CM]; ok {
|
if v, ok := s.Labels[helm.LABEL_VOL_CM]; ok {
|
||||||
configMapsVolumes = strings.Split(v, ",")
|
configMapsVolumes = strings.Split(v, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, volume := range s.Volumes {
|
for _, volume := range s.Volumes {
|
||||||
|
|
||||||
parts := strings.Split(volume, ":")
|
parts := strings.Split(volume, ":")
|
||||||
if len(parts) == 1 {
|
if len(parts) == 1 {
|
||||||
// this is a volume declaration for Docker only, avoid it
|
// this is a volume declaration for Docker only, avoid it
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
volname := parts[0]
|
volname := parts[0]
|
||||||
volepath := parts[1]
|
volepath := parts[1]
|
||||||
|
|
||||||
|
@@ -79,6 +79,13 @@ services:
|
|||||||
labels:
|
labels:
|
||||||
katenary.io/ports: 80
|
katenary.io/ports: 80
|
||||||
|
|
||||||
|
# use = sign for environment variables
|
||||||
|
eqenv:
|
||||||
|
image: nginx
|
||||||
|
environment:
|
||||||
|
- SOME_ENV_VAR=some_value
|
||||||
|
- ANOTHER_ENV_VAR=another_value
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
data:
|
data:
|
||||||
driver: local
|
driver: local
|
||||||
@@ -291,3 +298,34 @@ func TestUnmappedVolumes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if service using equal sign for environment works
|
||||||
|
func TestEqualSignOnEnv(t *testing.T) {
|
||||||
|
tmp, p := setUp(t)
|
||||||
|
defer os.RemoveAll(tmp)
|
||||||
|
|
||||||
|
// if the name is eqenv, the service should habe environment
|
||||||
|
for name, _ := range p.Data.Services {
|
||||||
|
if name == "eqenv" {
|
||||||
|
path := filepath.Join(tmp, "templates", name+".deployment.yaml")
|
||||||
|
fp, _ := os.Open(path)
|
||||||
|
defer fp.Close()
|
||||||
|
lines, _ := ioutil.ReadAll(fp)
|
||||||
|
match := 0
|
||||||
|
for _, line := range strings.Split(string(lines), "\n") {
|
||||||
|
// we must find the line with the environment variable name
|
||||||
|
if strings.Contains(line, "SOME_ENV_VAR") {
|
||||||
|
// we must find the line with the environment variable value
|
||||||
|
match++
|
||||||
|
}
|
||||||
|
if strings.Contains(line, "ANOTHER_ENV_VAR") {
|
||||||
|
// we must find the line with the environment variable value
|
||||||
|
match++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if match != 2 {
|
||||||
|
t.Error("eqenv service should have 2 environment variables")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user