Files
Minikura/operator/internal/resources/naming.go
T

53 lines
1.5 KiB
Go
Raw Normal View History

2026-08-13 01:57:26 +07:00
package resources
import (
2026-08-13 03:29:29 +07:00
"crypto/sha256"
2026-08-13 01:57:26 +07:00
"fmt"
"strings"
v1alpha1 "github.com/YuzuZensai/Minikura/operator/api/v1alpha1"
)
2026-08-13 03:29:29 +07:00
const dnsLabelMaxLength = 63
func ServerName(name string) string { return limitedName("minecraft-"+name, dnsLabelMaxLength) }
2026-08-13 01:57:26 +07:00
func ProxyName(kind v1alpha1.ProxyKind, name string) string {
2026-08-13 03:29:29 +07:00
return limitedName(fmt.Sprintf("%s-%s", strings.ToLower(string(kind)), name), dnsLabelMaxLength)
}
func ConfigMapName(base string) string { return limitedName(base+"-config", dnsLabelMaxLength) }
func limitedName(name string, limit int) string {
if len(name) <= limit {
return name
}
sum := sha256.Sum256([]byte(name))
suffix := fmt.Sprintf("-%x", sum[:4])
return strings.TrimRight(name[:limit-len(suffix)], "-") + suffix
}
func labelValue(value string) string {
return limitedName(value, dnsLabelMaxLength)
2026-08-13 01:57:26 +07:00
}
func ServerLabels(mc *v1alpha1.MinecraftServer) map[string]string {
return map[string]string{
"app": ServerName(mc.Name),
v1alpha1.LabelServerType: strings.ToLower(string(mc.Spec.Type)),
2026-08-13 03:29:29 +07:00
v1alpha1.LabelServerID: labelValue(mc.Name),
2026-08-13 01:57:26 +07:00
v1alpha1.LabelManagedBy: v1alpha1.ManagerName,
}
}
func ProxyLabels(rp *v1alpha1.ReverseProxyServer) map[string]string {
return map[string]string{
"app": ProxyName(rp.Spec.Type, rp.Name),
v1alpha1.LabelServerType: strings.ToLower(string(rp.Spec.Type)),
2026-08-13 03:29:29 +07:00
v1alpha1.LabelProxyID: labelValue(rp.Name),
2026-08-13 01:57:26 +07:00
v1alpha1.LabelManagedBy: v1alpha1.ManagerName,
}
}
func SelectorLabels(app string) map[string]string {
return map[string]string{"app": app}
}