2023-12-06 15:24:02 +01:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-04-05 07:56:27 +02:00
|
|
|
"katenary/utils"
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
)
|
|
|
|
|
2024-04-05 07:56:27 +02:00
|
|
|
var (
|
|
|
|
_ DataMap = (*Secret)(nil)
|
|
|
|
_ Yaml = (*Secret)(nil)
|
|
|
|
)
|
2023-12-06 15:24:02 +01:00
|
|
|
|
|
|
|
// Secret is a kubernetes Secret.
|
|
|
|
//
|
|
|
|
// Implements the DataMap interface.
|
|
|
|
type Secret struct {
|
|
|
|
*corev1.Secret
|
|
|
|
service types.ServiceConfig `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSecret creates a new Secret from a compose service
|
|
|
|
func NewSecret(service types.ServiceConfig, appName string) *Secret {
|
|
|
|
secret := &Secret{
|
|
|
|
service: service,
|
|
|
|
Secret: &corev1.Secret{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Secret",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: utils.TplName(service.Name, appName),
|
|
|
|
Labels: GetLabels(service.Name, appName),
|
|
|
|
Annotations: Annotations,
|
|
|
|
},
|
|
|
|
Data: make(map[string][]byte),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the value should be in values.yaml
|
|
|
|
valueList := []string{}
|
2024-04-24 13:59:21 +02:00
|
|
|
varDescriptons := utils.GetValuesFromLabel(service, LabelValues)
|
2023-12-06 15:24:02 +01:00
|
|
|
for value := range varDescriptons {
|
|
|
|
valueList = append(valueList, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap values with quotes
|
|
|
|
for _, value := range service.Environment {
|
|
|
|
if value == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*value = fmt.Sprintf(`"%s"`, *value)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range valueList {
|
|
|
|
if val, ok := service.Environment[value]; ok {
|
|
|
|
value = strings.TrimPrefix(value, `"`)
|
|
|
|
*val = `.Values.` + service.Name + `.environment.` + value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range service.Environment {
|
|
|
|
if value == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
secret.AddData(key, *value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetData sets the data of the secret.
|
|
|
|
func (s *Secret) SetData(data map[string]string) {
|
|
|
|
for key, value := range data {
|
2024-04-03 21:33:26 +02:00
|
|
|
s.AddData(key, value)
|
2023-12-06 15:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddData adds a key value pair to the secret.
|
|
|
|
func (s *Secret) AddData(key string, value string) {
|
|
|
|
if value == "" {
|
|
|
|
return
|
|
|
|
}
|
2024-04-23 14:24:06 +02:00
|
|
|
s.Data[key] = []byte(`{{ tpl ` + value + ` $ | b64enc }}`)
|
2023-12-06 15:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Yaml returns the yaml representation of the secret.
|
|
|
|
func (s *Secret) Yaml() ([]byte, error) {
|
|
|
|
y, err := yaml.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace the b64 value by the real value
|
|
|
|
for _, value := range s.Data {
|
|
|
|
encoded := base64.StdEncoding.EncodeToString([]byte(value))
|
|
|
|
y = []byte(strings.ReplaceAll(string(y), encoded, string(value)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return y, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filename returns the filename of the secret.
|
|
|
|
func (s *Secret) Filename() string {
|
|
|
|
return s.service.Name + ".secret.yaml"
|
|
|
|
}
|