Develop #117
@@ -45,37 +45,12 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
Annotations[labels.LabelName("compose-hash")] = hash
|
Annotations[labels.LabelName("compose-hash")] = hash
|
||||||
chart.composeHash = &hash
|
chart.composeHash = &hash
|
||||||
|
|
||||||
|
// drop all services with the "ignore" label
|
||||||
|
dropIngoredServices(project)
|
||||||
|
|
||||||
// rename all services name to remove dashes
|
// rename all services name to remove dashes
|
||||||
for i, service := range project.Services {
|
if err := fixResourceNames(project); err != nil {
|
||||||
if service.Name != utils.AsResourceName(service.Name) {
|
return nil, err
|
||||||
fixed := utils.AsResourceName(service.Name)
|
|
||||||
for j, s := range project.Services {
|
|
||||||
// for the same-pod services, we need to keep the original name
|
|
||||||
if samepod, ok := s.Labels[labels.LabelSamePod]; ok && samepod == service.Name {
|
|
||||||
s.Labels[labels.LabelSamePod] = fixed
|
|
||||||
project.Services[j] = s
|
|
||||||
}
|
|
||||||
// also, the value-from label should be updated
|
|
||||||
if valuefrom, ok := s.Labels[labels.LabelValueFrom]; ok {
|
|
||||||
vf, err := labelStructs.GetValueFrom(valuefrom)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for varname, bind := range *vf {
|
|
||||||
log.Printf("service %s, varname %s, bind %s", service.Name, varname, bind)
|
|
||||||
bind := strings.ReplaceAll(bind, service.Name, fixed)
|
|
||||||
(*vf)[varname] = bind
|
|
||||||
}
|
|
||||||
output, err := yaml.Marshal(vf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
s.Labels[labels.LabelValueFrom] = string(output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
service.Name = fixed
|
|
||||||
project.Services[i] = service
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the "main-app" label, and set chart.AppVersion to the tag if exists
|
// find the "main-app" label, and set chart.AppVersion to the tag if exists
|
||||||
@@ -222,6 +197,52 @@ func Generate(project *types.Project) (*HelmChart, error) {
|
|||||||
return chart, nil
|
return chart, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dropIngoredServices removes all services with the "ignore" label set to true (or yes).
|
||||||
|
func dropIngoredServices(project *types.Project) {
|
||||||
|
for i, service := range project.Services {
|
||||||
|
if isIgnored(service) {
|
||||||
|
project.Services = append(project.Services[:i], project.Services[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixResourceNames renames all services and related resources to remove dashes.
|
||||||
|
func fixResourceNames(project *types.Project) error {
|
||||||
|
// rename all services name to remove dashes
|
||||||
|
for i, service := range project.Services {
|
||||||
|
if service.Name != utils.AsResourceName(service.Name) {
|
||||||
|
fixed := utils.AsResourceName(service.Name)
|
||||||
|
for j, s := range project.Services {
|
||||||
|
// for the same-pod services, we need to keep the original name
|
||||||
|
if samepod, ok := s.Labels[labels.LabelSamePod]; ok && samepod == service.Name {
|
||||||
|
s.Labels[labels.LabelSamePod] = fixed
|
||||||
|
project.Services[j] = s
|
||||||
|
}
|
||||||
|
// also, the value-from label should be updated
|
||||||
|
if valuefrom, ok := s.Labels[labels.LabelValueFrom]; ok {
|
||||||
|
vf, err := labelStructs.GetValueFrom(valuefrom)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for varname, bind := range *vf {
|
||||||
|
log.Printf("service %s, varname %s, bind %s", service.Name, varname, bind)
|
||||||
|
bind := strings.ReplaceAll(bind, service.Name, fixed)
|
||||||
|
(*vf)[varname] = bind
|
||||||
|
}
|
||||||
|
output, err := yaml.Marshal(vf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.Labels[labels.LabelValueFrom] = string(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
service.Name = fixed
|
||||||
|
project.Services[i] = service
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// serviceIsMain returns true if the service is the main app.
|
// serviceIsMain returns true if the service is the main app.
|
||||||
func serviceIsMain(service types.ServiceConfig) bool {
|
func serviceIsMain(service types.ServiceConfig) bool {
|
||||||
if main, ok := service.Labels[labels.LabelMainApp]; ok {
|
if main, ok := service.Labels[labels.LabelMainApp]; ok {
|
||||||
|
Reference in New Issue
Block a user