Add possibility to use env file
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
chart/*
|
chart/*
|
||||||
docker-compose.yaml
|
docker-compose.yaml
|
||||||
katenary
|
katenary
|
||||||
|
example.env
|
||||||
|
@@ -24,4 +24,5 @@ type Service struct {
|
|||||||
DependsOn []string `yaml:"depends_on"`
|
DependsOn []string `yaml:"depends_on"`
|
||||||
Volumes []string `yaml:"volumes"`
|
Volumes []string `yaml:"volumes"`
|
||||||
Expose []int `yaml:"expose"`
|
Expose []int `yaml:"expose"`
|
||||||
|
EnvFiles []string `yaml:"env_file"`
|
||||||
}
|
}
|
||||||
|
@@ -42,6 +42,21 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
|
|||||||
|
|
||||||
container := helm.NewContainer(name, s.Image, s.Environment, s.Labels)
|
container := helm.NewContainer(name, s.Image, s.Environment, s.Labels)
|
||||||
|
|
||||||
|
for _, envfile := range s.EnvFiles {
|
||||||
|
configMap := helm.NewConfigMap(name)
|
||||||
|
if err := configMap.AddEnvFile(envfile); err != nil {
|
||||||
|
Red(err.Error())
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
container.EnvFrom = append(container.EnvFrom, map[string]map[string]string{
|
||||||
|
"configMapRef": {
|
||||||
|
"name": configMap.Metadata.Name,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
ret = append(ret, configMap)
|
||||||
|
}
|
||||||
|
|
||||||
container.Image = "{{ .Values." + name + ".image }}"
|
container.Image = "{{ .Values." + name + ".image }}"
|
||||||
Values[name] = map[string]interface{}{
|
Values[name] = map[string]interface{}{
|
||||||
"image": s.Image,
|
"image": s.Image,
|
||||||
|
47
helm/configMap.go
Normal file
47
helm/configMap.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package helm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigMap struct {
|
||||||
|
*K8sBase `yaml:",inline"`
|
||||||
|
Data map[string]string `yaml:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfigMap(name string) *ConfigMap {
|
||||||
|
base := NewBase()
|
||||||
|
base.Kind = "ConfigMap"
|
||||||
|
base.Metadata.Name = "{{ .Release.Name }}-" + name
|
||||||
|
return &ConfigMap{
|
||||||
|
K8sBase: base,
|
||||||
|
Data: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConfigMap) AddEnvFile(file string) error {
|
||||||
|
content, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(string(content), "\n")
|
||||||
|
for _, l := range lines {
|
||||||
|
l = strings.TrimSpace(l)
|
||||||
|
if len(l) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
parts := strings.SplitN(l, "=", 2)
|
||||||
|
log.Printf("%d %v\n", len(parts), parts)
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return errors.New("The environment file " + file + " is not valid")
|
||||||
|
}
|
||||||
|
c.Data[parts[0]] = parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
@@ -38,19 +38,21 @@ type ContainerPort struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Container struct {
|
type Container struct {
|
||||||
Name string `yaml:"name,omitempty"`
|
Name string `yaml:"name,omitempty"`
|
||||||
Image string `yaml:"image"`
|
Image string `yaml:"image"`
|
||||||
Ports []*ContainerPort `yaml:"ports,omitempty"`
|
Ports []*ContainerPort `yaml:"ports,omitempty"`
|
||||||
Env []Value `yaml:"env,omitempty"`
|
Env []Value `yaml:"env,omitempty"`
|
||||||
Command []string `yaml:"command,omitempty"`
|
EnvFrom []map[string]map[string]string `yaml:"envFrom,omitempty"`
|
||||||
VolumeMounts []interface{} `yaml:"volumeMounts,omitempty"`
|
Command []string `yaml:"command,omitempty"`
|
||||||
|
VolumeMounts []interface{} `yaml:"volumeMounts,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(name, image string, environment, labels map[string]string) *Container {
|
func NewContainer(name, image string, environment, labels map[string]string) *Container {
|
||||||
container := &Container{
|
container := &Container{
|
||||||
Image: image,
|
Image: image,
|
||||||
Name: name,
|
Name: name,
|
||||||
Env: make([]Value, len(environment)),
|
Env: make([]Value, len(environment)),
|
||||||
|
EnvFrom: make([]map[string]map[string]string, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
toServices := make([]string, 0)
|
toServices := make([]string, 0)
|
||||||
|
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
const K = "katenary.io"
|
const K = "katenary.io"
|
||||||
|
|
||||||
var Version = "1.0"
|
var Version = "1.0" // should be set from main.Version
|
||||||
|
|
||||||
type Kinded interface {
|
type Kinded interface {
|
||||||
Get() string
|
Get() string
|
||||||
|
Reference in New Issue
Block a user