- reduce complexity - use better tools to format the code - add more tests - and too many things to list here We are rewriting for V3, so these commits are sometimes big and not fully detailed. Of course, further work will be more documented.
130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
package generator
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
v1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"katenary/utils"
|
|
)
|
|
|
|
var _ Yaml = (*VolumeClaim)(nil)
|
|
|
|
const persistenceKey = "persistence"
|
|
|
|
// VolumeClaim is a kubernetes VolumeClaim. This is a PersistentVolumeClaim.
|
|
type VolumeClaim struct {
|
|
*v1.PersistentVolumeClaim
|
|
service *types.ServiceConfig `yaml:"-"`
|
|
volumeName string
|
|
nameOverride string
|
|
}
|
|
|
|
// NewVolumeClaim creates a new VolumeClaim from a compose service.
|
|
func NewVolumeClaim(service types.ServiceConfig, volumeName, appName string) *VolumeClaim {
|
|
return &VolumeClaim{
|
|
volumeName: volumeName,
|
|
service: &service,
|
|
PersistentVolumeClaim: &v1.PersistentVolumeClaim{
|
|
TypeMeta: metav1.TypeMeta{
|
|
Kind: "PersistentVolumeClaim",
|
|
APIVersion: "v1",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: utils.TplName(service.Name, appName) + "-" + volumeName,
|
|
Labels: GetLabels(service.Name, appName),
|
|
Annotations: Annotations,
|
|
},
|
|
Spec: v1.PersistentVolumeClaimSpec{
|
|
AccessModes: []v1.PersistentVolumeAccessMode{
|
|
v1.ReadWriteOnce,
|
|
},
|
|
StorageClassName: utils.StrPtr(
|
|
`{{ .Values.` +
|
|
service.Name +
|
|
"." + persistenceKey +
|
|
"." + volumeName + `.storageClass }}`,
|
|
),
|
|
Resources: v1.VolumeResourceRequirements{
|
|
Requests: v1.ResourceList{
|
|
v1.ResourceStorage: resource.MustParse("1Gi"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Yaml marshals a VolumeClaim into yaml.
|
|
func (v *VolumeClaim) Yaml() ([]byte, error) {
|
|
serviceName := v.service.Name
|
|
if v.nameOverride != "" {
|
|
serviceName = v.nameOverride
|
|
}
|
|
volumeName := v.volumeName
|
|
out, err := yaml.Marshal(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// replace 1Gi to {{ .Values.serviceName.volume.size }}
|
|
out = []byte(
|
|
strings.Replace(
|
|
string(out),
|
|
"1Gi",
|
|
utils.TplValue(serviceName, persistenceKey+"."+volumeName+".size"),
|
|
1,
|
|
),
|
|
)
|
|
|
|
out = []byte(
|
|
strings.Replace(
|
|
string(out),
|
|
"- ReadWriteOnce",
|
|
"{{- .Values."+
|
|
serviceName+
|
|
"."+persistenceKey+
|
|
"."+volumeName+
|
|
".accessMode | toYaml | nindent __indent__ }}",
|
|
1,
|
|
),
|
|
)
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
for i, line := range lines {
|
|
if strings.Contains(line, "storageClass") {
|
|
lines[i] = utils.Wrap(
|
|
line,
|
|
"{{- if ne .Values."+
|
|
serviceName+
|
|
"."+persistenceKey+
|
|
"."+volumeName+".storageClass \"-\" }}",
|
|
"{{- end }}",
|
|
)
|
|
}
|
|
}
|
|
out = []byte(strings.Join(lines, "\n"))
|
|
|
|
// add condition
|
|
out = []byte(
|
|
"{{- if .Values." +
|
|
serviceName +
|
|
"." + persistenceKey +
|
|
"." + volumeName +
|
|
".enabled }}\n" +
|
|
string(out) +
|
|
"\n{{- end }}",
|
|
)
|
|
|
|
return out, nil
|
|
}
|
|
|
|
// Filename returns the suggested filename for a VolumeClaim.
|
|
func (v *VolumeClaim) Filename() string {
|
|
return v.service.Name + "." + v.volumeName + ".volumeclaim.yaml"
|
|
}
|