2022-06-22 09:38:07 +02:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
|
)
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// 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"`
|
|
|
|
}
|
2022-06-22 09:38:07 +02:00
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// PersistenceValue is a persistence configuration that will be saved in values.yaml.
|
|
|
|
type PersistenceValue struct {
|
|
|
|
StorageClass string `yaml:"storageClass"`
|
|
|
|
Size string `yaml:"size"`
|
|
|
|
AccessMode []string `yaml:"accessMode"`
|
2024-05-06 21:11:36 +02:00
|
|
|
Enabled bool `yaml:"enabled"`
|
2023-12-06 15:24:02 +01:00
|
|
|
}
|
2022-06-22 09:38:07 +02:00
|
|
|
|
2024-11-08 16:55:18 +01:00
|
|
|
type TLS struct {
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// IngressValue is a ingress configuration that will be saved in values.yaml.
|
|
|
|
type IngressValue struct {
|
2024-05-06 21:11:36 +02:00
|
|
|
Annotations map[string]string `yaml:"annotations"`
|
2023-12-06 15:24:02 +01:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
Class string `yaml:"class"`
|
2024-05-06 21:11:36 +02:00
|
|
|
Enabled bool `yaml:"enabled"`
|
2024-11-08 16:55:18 +01:00
|
|
|
TLS TLS `yaml:"tls"`
|
2022-06-22 09:38:07 +02:00
|
|
|
}
|
|
|
|
|
2024-11-25 12:00:04 +01:00
|
|
|
// Value will be saved in values.yaml. It contains configuration for all deployment and services.
|
2023-12-06 15:24:02 +01:00
|
|
|
type Value struct {
|
|
|
|
Repository *RepositoryValue `yaml:"repository,omitempty"`
|
|
|
|
Persistence map[string]*PersistenceValue `yaml:"persistence,omitempty"`
|
|
|
|
Ingress *IngressValue `yaml:"ingress,omitempty"`
|
|
|
|
Environment map[string]any `yaml:"environment,omitempty"`
|
|
|
|
Replicas *uint32 `yaml:"replicas,omitempty"`
|
|
|
|
CronJob *CronJobValue `yaml:"cronjob,omitempty"`
|
2024-04-21 16:34:21 +02:00
|
|
|
NodeSelector map[string]string `yaml:"nodeSelector"`
|
2024-04-22 13:27:44 +02:00
|
|
|
Resources map[string]any `yaml:"resources"`
|
2024-05-06 21:11:36 +02:00
|
|
|
ImagePullPolicy string `yaml:"imagePullPolicy,omitempty"`
|
|
|
|
ServiceAccount string `yaml:"serviceAccount"`
|
2023-12-06 15:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2022-06-22 09:38:07 +02:00
|
|
|
}
|
2022-06-22 10:55:11 +02:00
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// find the image tag
|
|
|
|
tag := ""
|
2024-10-17 17:08:42 +02:00
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
split := strings.Split(service.Image, ":")
|
2024-10-17 17:08:42 +02:00
|
|
|
if len(split) == 1 {
|
|
|
|
v.Repository = &RepositoryValue{
|
|
|
|
Image: service.Image,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v.Repository = &RepositoryValue{
|
|
|
|
Image: strings.Join(split[:len(split)-1], ":"),
|
|
|
|
}
|
2022-06-22 10:55:11 +02:00
|
|
|
}
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// 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 {
|
2024-10-17 17:08:42 +02:00
|
|
|
tag = split[len(split)-1]
|
2023-12-06 15:24:02 +01:00
|
|
|
}
|
|
|
|
v.Repository.Tag = tag
|
|
|
|
} else {
|
|
|
|
v.Repository.Tag = ""
|
|
|
|
}
|
2022-06-22 09:38:07 +02:00
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
return v
|
|
|
|
}
|
2022-06-22 09:38:07 +02:00
|
|
|
|
2024-10-17 17:08:42 +02:00
|
|
|
func (v *Value) AddIngress(host, path string) {
|
|
|
|
v.Ingress = &IngressValue{
|
|
|
|
Enabled: true,
|
|
|
|
Host: host,
|
|
|
|
Path: path,
|
|
|
|
Class: "-",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// AddPersistence adds persistence configuration to the Value.
|
|
|
|
func (v *Value) AddPersistence(volumeName string) {
|
2024-11-08 13:11:14 +01:00
|
|
|
volumeName = strings.ReplaceAll(volumeName, "-", "_")
|
2023-12-06 15:24:02 +01:00
|
|
|
if v.Persistence == nil {
|
|
|
|
v.Persistence = make(map[string]*PersistenceValue, 0)
|
2022-06-22 09:38:07 +02:00
|
|
|
}
|
2023-12-06 15:24:02 +01:00
|
|
|
v.Persistence[volumeName] = &PersistenceValue{
|
|
|
|
Enabled: true,
|
|
|
|
StorageClass: "-",
|
|
|
|
Size: "1Gi",
|
|
|
|
AccessMode: []string{"ReadWriteOnce"},
|
2022-06-22 09:38:07 +02:00
|
|
|
}
|
2023-12-06 15:24:02 +01:00
|
|
|
}
|
2022-06-22 09:38:07 +02:00
|
|
|
|
2024-10-17 17:08:42 +02:00
|
|
|
// 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"`
|
2022-06-22 09:38:07 +02:00
|
|
|
}
|