fix(generation): fix the volume var/path name
Underscores are forbidden by Kubernetes (should be a valid URL string), we replace "_" by "-" in names, and we leave the values file using the original name. So a volume named "foo_bar" in compose file, is registered as "foo_bar" in the values file, but "foo-bar" is used as volume name in deployments, volume claims, ...
This commit is contained in:
@@ -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),
|
||||
|
@@ -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,
|
||||
},
|
||||
|
@@ -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, "_", "-")
|
||||
}
|
||||
|
Reference in New Issue
Block a user