2021-12-01 11:53:10 +01:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-12-01 13:55:22 +01:00
|
|
|
"fmt"
|
2021-12-01 11:53:10 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-12-01 13:55:22 +01:00
|
|
|
// InlineConfig is made to represent a configMap or a secret
|
|
|
|
type InlineConfig interface {
|
|
|
|
AddEnvFile(filename string) error
|
|
|
|
Metadata() *Metadata
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:53:10 +01:00
|
|
|
type ConfigMap struct {
|
|
|
|
*K8sBase `yaml:",inline"`
|
|
|
|
Data map[string]string `yaml:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigMap(name string) *ConfigMap {
|
|
|
|
base := NewBase()
|
2021-12-01 13:30:58 +01:00
|
|
|
base.ApiVersion = "v1"
|
2021-12-01 11:53:10 +01:00
|
|
|
base.Kind = "ConfigMap"
|
2021-12-05 09:05:48 +01:00
|
|
|
base.Metadata.Name = RELEASE_NAME + "-" + name
|
2021-12-01 15:17:34 +01:00
|
|
|
base.Metadata.Labels[K+"/component"] = name
|
2021-12-01 11:53:10 +01:00
|
|
|
return &ConfigMap{
|
|
|
|
K8sBase: base,
|
|
|
|
Data: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:55:22 +01:00
|
|
|
func (c *ConfigMap) Metadata() *Metadata {
|
|
|
|
return c.K8sBase.Metadata
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:53:10 +01:00
|
|
|
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)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return errors.New("The environment file " + file + " is not valid")
|
|
|
|
}
|
|
|
|
c.Data[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2021-12-01 13:55:22 +01:00
|
|
|
|
|
|
|
type Secret struct {
|
|
|
|
*K8sBase `yaml:",inline"`
|
|
|
|
Data map[string]string `yaml:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSecret(name string) *Secret {
|
|
|
|
base := NewBase()
|
|
|
|
base.ApiVersion = "v1"
|
|
|
|
base.Kind = "Secret"
|
2021-12-05 09:05:48 +01:00
|
|
|
base.Metadata.Name = RELEASE_NAME + "-" + name
|
2021-12-01 15:17:34 +01:00
|
|
|
base.Metadata.Labels[K+"/component"] = name
|
2021-12-01 13:55:22 +01:00
|
|
|
return &Secret{
|
|
|
|
K8sBase: base,
|
|
|
|
Data: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Secret) 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)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return errors.New("The environment file " + file + " is not valid")
|
|
|
|
}
|
|
|
|
s.Data[parts[0]] = fmt.Sprintf(`{{ "%s" | b64enc }}`, parts[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func (s *Secret) Metadata() *Metadata {
|
|
|
|
return s.K8sBase.Metadata
|
|
|
|
}
|