2023-12-06 15:24:02 +01:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
2025-07-13 15:02:26 +02:00
|
|
|
"fmt"
|
2023-12-06 15:24:02 +01:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2025-08-19 23:58:51 +02:00
|
|
|
"katenary.io/internal/utils"
|
2025-08-19 23:09:50 +02:00
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Yaml = (*Service)(nil)
|
|
|
|
|
|
|
|
// Service is a kubernetes Service.
|
|
|
|
type Service struct {
|
|
|
|
*v1.Service `yaml:",inline"`
|
|
|
|
service *types.ServiceConfig `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new Service from a compose service.
|
|
|
|
func NewService(service types.ServiceConfig, appName string) *Service {
|
|
|
|
ports := []v1.ServicePort{}
|
|
|
|
|
|
|
|
s := &Service{
|
|
|
|
service: &service,
|
|
|
|
Service: &v1.Service{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Service",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: utils.TplName(service.Name, appName),
|
|
|
|
Labels: GetLabels(service.Name, appName),
|
|
|
|
Annotations: Annotations,
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Selector: GetMatchLabels(service.Name, appName),
|
|
|
|
Ports: ports,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, port := range service.Ports {
|
|
|
|
s.AddPort(port)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPort adds a port to the service.
|
|
|
|
func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string) {
|
2025-08-03 14:13:42 +02:00
|
|
|
var name string
|
2023-12-06 15:24:02 +01:00
|
|
|
var finalport intstr.IntOrString
|
|
|
|
|
|
|
|
if targetPort := utils.GetServiceNameByPort(int(port.Target)); targetPort == "" {
|
|
|
|
finalport = intstr.FromInt(int(port.Target))
|
2025-07-13 15:02:26 +02:00
|
|
|
name = fmt.Sprintf("port-%d", port.Target)
|
2023-12-06 15:24:02 +01:00
|
|
|
} else {
|
|
|
|
finalport = intstr.FromString(targetPort)
|
|
|
|
name = targetPort
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Spec.Ports = append(s.Spec.Ports, v1.ServicePort{
|
|
|
|
Protocol: v1.ProtocolTCP,
|
|
|
|
Port: int32(port.Target),
|
|
|
|
TargetPort: finalport,
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-17 17:08:42 +02:00
|
|
|
// Filename returns the filename of the service.
|
|
|
|
func (s *Service) Filename() string {
|
|
|
|
return s.service.Name + ".service.yaml"
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
// Yaml returns the yaml representation of the service.
|
|
|
|
func (s *Service) Yaml() ([]byte, error) {
|
2024-11-09 14:18:27 +01:00
|
|
|
var y []byte
|
|
|
|
var err error
|
|
|
|
if y, err = ToK8SYaml(s); err != nil {
|
2024-11-08 13:11:14 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:24:02 +01:00
|
|
|
lines := []string{}
|
2025-08-03 14:13:57 +02:00
|
|
|
for line := range strings.SplitSeq(string(y), "\n") {
|
2023-12-06 15:24:02 +01:00
|
|
|
if regexp.MustCompile(`^\s*loadBalancer:\s*`).MatchString(line) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
y = []byte(strings.Join(lines, "\n"))
|
|
|
|
|
|
|
|
return y, err
|
|
|
|
}
|