Files

88 lines
2.6 KiB
YAML
Raw Permalink Normal View History

2025-08-25 15:46:12 +08:00
apiVersion: v1
kind: Service
metadata:
name: {{.ObjectMeta.Name}}-svc
namespace: {{.ObjectMeta.Namespace}}
labels:
app: {{.ObjectMeta.Name}}
devstar-resource-type: devstar-application
app.kubernetes.io/name: {{.ObjectMeta.Name}}
app.kubernetes.io/managed-by: devstar-operator
{{- if .Spec.Service}}
{{- if .Spec.Service.Labels}}
{{- range $key, $value := .Spec.Service.Labels}}
{{$key}}: "{{$value}}"
{{- end}}
{{- end}}
{{- end}}
{{- if .Spec.Service}}
{{- if .Spec.Service.Annotations}}
annotations:
{{- range $key, $value := .Spec.Service.Annotations}}
{{$key}}: "{{$value}}"
{{- end}}
{{- end}}
{{- end}}
spec:
selector:
app: {{.ObjectMeta.Name}}
devstar-resource-type: devstar-application
type: {{if .Spec.Service}}{{.Spec.Service.Type | default "ClusterIP"}}{{else}}ClusterIP{{end}}
{{- if and .Spec.Service .Spec.Service.LoadBalancerIP}}
loadBalancerIP: {{.Spec.Service.LoadBalancerIP}}
{{- end}}
{{- if and .Spec.Service .Spec.Service.LoadBalancerSourceRanges}}
loadBalancerSourceRanges:
{{- range .Spec.Service.LoadBalancerSourceRanges}}
- {{.}}
{{- end}}
{{- end}}
{{- if and .Spec.Service .Spec.Service.ExternalName}}
externalName: {{.Spec.Service.ExternalName}}
{{- end}}
{{- if and .Spec.Service .Spec.Service.SessionAffinity}}
sessionAffinity: {{.Spec.Service.SessionAffinity}}
{{- else}}
sessionAffinity: None
{{- end}}
{{- if and .Spec.Service (eq .Spec.Service.Type "ExternalName")}}
# ExternalName 类型不需要 ports 和 selector
{{- else}}
ports:
{{- if and .Spec.Service .Spec.Service.Ports}}
# 使用自定义端口配置
{{- range .Spec.Service.Ports}}
- name: {{.Name}}
protocol: {{.Protocol | default "TCP"}}
port: {{.Port}}
{{- if .TargetPort}}
targetPort: {{.TargetPort}}
{{- else}}
targetPort: {{.Port}}
{{- end}}
{{- if and (or (eq $.Spec.Service.Type "NodePort") (eq $.Spec.Service.Type "LoadBalancer")) .NodePort}}
nodePort: {{.NodePort}}
{{- end}}
{{- end}}
{{- else}}
# 使用模板中的端口配置
{{- range .Spec.Template.Ports}}
- name: {{.Name | default (printf "port-%d" .Port)}}
protocol: {{.Protocol | default "TCP"}}
port: {{.Port}}
targetPort: {{.Port}}
{{- if and $.Spec.Service (or (eq $.Spec.Service.Type "NodePort") (eq $.Spec.Service.Type "LoadBalancer"))}}
{{- if $.Spec.Service.NodePorts}}
{{- if index $.Spec.Service.NodePorts .Name}}
nodePort: {{index $.Spec.Service.NodePorts .Name}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- end}}