diff --git a/generator/deployment.go b/generator/deployment.go index bad1f6f..26aad53 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -35,6 +35,7 @@ type Deployment struct { *appsv1.Deployment `yaml:",inline"` chart *HelmChart `yaml:"-"` configMaps map[string]*ConfigMapMount `yaml:"-"` + volumeMap map[string]string `yaml:"-"` // keep map of fixed named to original volume name service *types.ServiceConfig `yaml:"-"` defaultTag string `yaml:"-"` isMainApp bool `yaml:"-"` @@ -90,6 +91,7 @@ func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment { }, }, configMaps: make(map[string]*ConfigMapMount), + volumeMap: make(map[string]string), } // add containers @@ -396,7 +398,8 @@ func (d *Deployment) Yaml() ([]byte, error) { if strings.Contains(volume, "mountPath: ") { spaces = strings.Repeat(" ", utils.CountStartingSpaces(volume)) - content[line] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + volumeName + `.enabled }}` + "\n" + volume + varName := d.volumeMap[volumeName] + content[line] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + volume changing = true } if strings.Contains(volume, nameDirective) && changing { @@ -438,7 +441,8 @@ func (d *Deployment) Yaml() ([]byte, error) { if strings.Contains(line, "- name: ") && inVolumes { spaces = strings.Repeat(" ", utils.CountStartingSpaces(line)) - content[i] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + volumeName + `.enabled }}` + "\n" + line + varName := d.volumeMap[volumeName] + content[i] = spaces + `{{- if .Values.` + serviceName + `.persistence.` + varName + `.enabled }}` + "\n" + line changing = true } if strings.Contains(line, "claimName: ") && changing { @@ -597,8 +601,10 @@ func (d *Deployment) bindVolumes(volume types.ServiceVolumeConfig, isSamePod boo switch volume.Type { case "volume": // Add volume to container + fixedName := utils.FixedResourceName(volume.Source) + d.volumeMap[fixedName] = volume.Source container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ - Name: volume.Source, + Name: fixedName, MountPath: volume.Target, }) // Add volume to values.yaml only if it the service is not in the same pod that another service. @@ -608,7 +614,7 @@ func (d *Deployment) bindVolumes(volume types.ServiceVolumeConfig, isSamePod boo } // Add volume to deployment d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, corev1.Volume{ - Name: volume.Source, + Name: fixedName, VolumeSource: corev1.VolumeSource{ PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ ClaimName: utils.TplName(service.Name, appName, volume.Source), diff --git a/generator/volume.go b/generator/volume.go index 49f189b..2a232d9 100644 --- a/generator/volume.go +++ b/generator/volume.go @@ -1,6 +1,7 @@ package generator import ( + "katenary/utils" "strings" "github.com/compose-spec/compose-go/types" @@ -8,8 +9,6 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/yaml" - - "katenary/utils" ) const persistenceKey = "persistence" @@ -26,6 +25,7 @@ type VolumeClaim struct { // NewVolumeClaim creates a new VolumeClaim from a compose service. func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *VolumeClaim { + fixedName := utils.FixedResourceName(volumeName) return &VolumeClaim{ volumeName: volumeName, service: &service, @@ -35,7 +35,7 @@ func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *Vo APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: utils.TplName(service.Name, appName) + "-" + volumeName, + Name: utils.TplName(service.Name, appName) + "-" + fixedName, Labels: GetLabels(service.Name, appName), Annotations: Annotations, }, diff --git a/utils/utils.go b/utils/utils.go index 2ed7428..0e2e196 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -117,6 +117,7 @@ func PathToName(path string) string { path = strings.ReplaceAll(path, "_", "-") path = strings.ReplaceAll(path, "/", "-") path = strings.ReplaceAll(path, ".", "-") + path = strings.ToLower(path) return path } @@ -192,3 +193,8 @@ func EncodeBasicYaml(data any) ([]byte, error) { } return buf.Bytes(), nil } + +// FixedResourceName returns a resource name without underscores to respect the kubernetes naming convention. +func FixedResourceName(name string) string { + return strings.ReplaceAll(name, "_", "-") +}