Go to Katenary V3
This is the next-gen of Katenary
This commit is contained in:
@@ -1,77 +1,121 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"katenary/helm"
|
||||
"strings"
|
||||
|
||||
"github.com/compose-spec/compose-go/types"
|
||||
)
|
||||
|
||||
var (
|
||||
// Values is kept in memory to create a values.yaml file.
|
||||
Values = make(map[string]map[string]interface{})
|
||||
)
|
||||
// Values is a map of all values for all services. Written to values.yaml.
|
||||
// var Values = map[string]any{}
|
||||
|
||||
// AddValues adds values to the values.yaml map.
|
||||
func AddValues(servicename string, values map[string]EnvVal) {
|
||||
locker.Lock()
|
||||
defer locker.Unlock()
|
||||
|
||||
if _, ok := Values[servicename]; !ok {
|
||||
Values[servicename] = make(map[string]interface{})
|
||||
}
|
||||
|
||||
for k, v := range values {
|
||||
Values[servicename][k] = v
|
||||
}
|
||||
// RepositoryValue is a docker repository image and tag that will be saved in values.yaml.
|
||||
type RepositoryValue struct {
|
||||
Image string `yaml:"image"`
|
||||
Tag string `yaml:"tag"`
|
||||
}
|
||||
|
||||
func AddEnvironment(servicename string, key string, val EnvVal) {
|
||||
locker.Lock()
|
||||
defer locker.Unlock()
|
||||
|
||||
if _, ok := Values[servicename]; !ok {
|
||||
Values[servicename] = make(map[string]interface{})
|
||||
}
|
||||
|
||||
if _, ok := Values[servicename]["environment"]; !ok {
|
||||
Values[servicename]["environment"] = make(map[string]EnvVal)
|
||||
}
|
||||
Values[servicename]["environment"].(map[string]EnvVal)[key] = val
|
||||
|
||||
// PersistenceValue is a persistence configuration that will be saved in values.yaml.
|
||||
type PersistenceValue struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
StorageClass string `yaml:"storageClass"`
|
||||
Size string `yaml:"size"`
|
||||
AccessMode []string `yaml:"accessMode"`
|
||||
}
|
||||
|
||||
// setEnvToValues will set the environment variables to the values.yaml map.
|
||||
func setEnvToValues(name string, s *types.ServiceConfig, c *helm.Container) {
|
||||
// crete the "environment" key
|
||||
// IngressValue is a ingress configuration that will be saved in values.yaml.
|
||||
type IngressValue struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Host string `yaml:"host"`
|
||||
Path string `yaml:"path"`
|
||||
Class string `yaml:"class"`
|
||||
Annotations map[string]string `yaml:"annotations"`
|
||||
}
|
||||
|
||||
env := make(map[string]EnvVal)
|
||||
for k, v := range s.Environment {
|
||||
env[k] = v
|
||||
}
|
||||
if len(env) == 0 {
|
||||
return
|
||||
// Value will be saved in values.yaml. It contains configuraiton for all deployment and services.
|
||||
// The content will be lile:
|
||||
//
|
||||
// name_of_component:
|
||||
// repository:
|
||||
// image: image_name
|
||||
// tag: image_tag
|
||||
// persistence:
|
||||
// enabled: true
|
||||
// storageClass: storage_class_name
|
||||
// ingress:
|
||||
// enabled: true
|
||||
// host: host_name
|
||||
// path: path_name
|
||||
// environment:
|
||||
// ENV_VAR_1: value_1
|
||||
// ENV_VAR_2: value_2
|
||||
type Value struct {
|
||||
Repository *RepositoryValue `yaml:"repository,omitempty"`
|
||||
Persistence map[string]*PersistenceValue `yaml:"persistence,omitempty"`
|
||||
Ingress *IngressValue `yaml:"ingress,omitempty"`
|
||||
ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"`
|
||||
Environment map[string]any `yaml:"environment,omitempty"`
|
||||
Replicas *uint32 `yaml:"replicas,omitempty"`
|
||||
CronJob *CronJobValue `yaml:"cronjob,omitempty"`
|
||||
}
|
||||
|
||||
// CronJobValue is a cronjob configuration that will be saved in values.yaml.
|
||||
type CronJobValue struct {
|
||||
Repository *RepositoryValue `yaml:"repository,omitempty"`
|
||||
Environment map[string]any `yaml:"environment,omitempty"`
|
||||
ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"`
|
||||
Schedule string `yaml:"schedule"`
|
||||
}
|
||||
|
||||
// NewValue creates a new Value from a compose service.
|
||||
// The value contains the necessary information to deploy the service (image, tag, replicas, etc.).
|
||||
//
|
||||
// If `main` is true, the tag will be empty because
|
||||
// it will be set in the helm chart appVersion.
|
||||
func NewValue(service types.ServiceConfig, main ...bool) *Value {
|
||||
replicas := uint32(1)
|
||||
v := &Value{
|
||||
Replicas: &replicas,
|
||||
}
|
||||
|
||||
for k, v := range env {
|
||||
k = strings.ReplaceAll(k, ".", "_")
|
||||
AddEnvironment(name, k, v)
|
||||
// find the image tag
|
||||
tag := ""
|
||||
split := strings.Split(service.Image, ":")
|
||||
v.Repository = &RepositoryValue{
|
||||
Image: split[0],
|
||||
}
|
||||
|
||||
//AddValues(name, map[string]EnvVal{"environment": valuesEnv})
|
||||
for k := range env {
|
||||
fixedK := strings.ReplaceAll(k, ".", "_")
|
||||
v := "{{ tpl .Values." + name + ".environment." + fixedK + " . }}"
|
||||
s.Environment[k] = &v
|
||||
touched := false
|
||||
for _, c := range c.Env {
|
||||
if c.Name == k {
|
||||
c.Value = v
|
||||
touched = true
|
||||
}
|
||||
}
|
||||
if !touched {
|
||||
c.Env = append(c.Env, &helm.Value{Name: k, Value: v})
|
||||
// for main service, the tag should the appVersion. So here we set it to empty.
|
||||
if len(main) > 0 && !main[0] {
|
||||
if len(split) > 1 {
|
||||
tag = split[1]
|
||||
}
|
||||
v.Repository.Tag = tag
|
||||
} else {
|
||||
v.Repository.Tag = ""
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
// AddPersistence adds persistence configuration to the Value.
|
||||
func (v *Value) AddPersistence(volumeName string) {
|
||||
if v.Persistence == nil {
|
||||
v.Persistence = make(map[string]*PersistenceValue, 0)
|
||||
}
|
||||
v.Persistence[volumeName] = &PersistenceValue{
|
||||
Enabled: true,
|
||||
StorageClass: "-",
|
||||
Size: "1Gi",
|
||||
AccessMode: []string{"ReadWriteOnce"},
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Value) AddIngress(host, path string) {
|
||||
v.Ingress = &IngressValue{
|
||||
Enabled: true,
|
||||
Host: host,
|
||||
Path: path,
|
||||
Class: "-",
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user