Files
katenary/generator/utils.go

80 lines
2.0 KiB
Go

package generator
import (
"strconv"
"strings"
"github.com/compose-spec/compose-go/types"
corev1 "k8s.io/api/core/v1"
"katenary/generator/labelStructs"
"katenary/utils"
)
// findDeployment finds the corresponding target deployment for a service.
func findDeployment(serviceName string, deployments map[string]*Deployment) *Deployment {
for _, d := range deployments {
if d.service.Name == serviceName {
return d
}
}
return nil
}
// addConfigMapToService adds the configmap to the service.
func addConfigMapToService(serviceName, fromservice, chartName string, target *Deployment) {
for i, c := range target.Spec.Template.Spec.Containers {
if c.Name != serviceName {
continue
}
c.EnvFrom = append(c.EnvFrom, corev1.EnvFromSource{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: utils.TplName(fromservice, chartName),
},
},
})
target.Spec.Template.Spec.Containers[i] = c
}
}
// fixPorts checks the "ports" label from container and add it to the service.
func fixPorts(service *types.ServiceConfig) error {
// check the "ports" label from container and add it to the service
portsLabel := ""
ok := false
if portsLabel, ok = service.Labels[LabelPorts]; !ok {
return nil
}
ports, err := labelStructs.PortsFrom(portsLabel)
if err != nil {
// maybe it's a string, comma separated
parts := strings.Split(portsLabel, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
port, err := strconv.ParseUint(part, 10, 32)
if err != nil {
return err
}
ports = append(ports, uint32(port))
}
}
for _, port := range ports {
service.Ports = append(service.Ports, types.ServicePortConfig{
Target: port,
})
}
return nil
}
// isIgnored returns true if the service is ignored.
func isIgnored(service types.ServiceConfig) bool {
if v, ok := service.Labels[LabelIgnore]; ok {
return v == "true" || v == "yes" || v == "1"
}
return false
}