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