feat(k8s): ExternalName service support (#419)

This commit is contained in:
FedotCompot
2025-06-20 14:33:40 +02:00
committed by GitHub
parent ff300d7638
commit 122910c65d
3 changed files with 121 additions and 8 deletions
+16 -3
View File
@@ -235,11 +235,24 @@ func (w *k8sWatcherImpl) extractRoutableServices(obj interface{}) []*routableSer
func (w *k8sWatcherImpl) buildDetails(service *core.Service, externalServiceName string) *routableService {
clusterIp := service.Spec.ClusterIP
port := "25565"
if service.Spec.Type == core.ServiceTypeExternalName {
clusterIp = service.Spec.ExternalName
}
mcRouterPort := ""
mcPort := ""
for _, p := range service.Spec.Ports {
if p.Name == "mc-router" || p.Name == "minecraft" {
port = strconv.Itoa(int(p.Port))
if p.Name == "mc-router" {
mcRouterPort = strconv.Itoa(int(p.Port))
}
if p.Name == "minecraft" {
mcPort = strconv.Itoa(int(p.Port))
}
}
port := "25565"
if len(mcRouterPort) > 0 {
port = mcRouterPort
} else if len(mcPort) > 0 {
port = mcPort
}
rs := &routableService{
externalServiceName: externalServiceName,
+98 -2
View File
@@ -80,7 +80,7 @@ func TestK8sWatcherImpl_handleAddThenUpdate(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// DownScaler needs to be instantiated
DownScaler = NewDownScaler(context.Background(), false, 1 * time.Second)
DownScaler = NewDownScaler(context.Background(), false, 1*time.Second)
Routes.Reset()
watcher := &k8sWatcherImpl{}
@@ -153,7 +153,7 @@ func TestK8sWatcherImpl_handleAddThenDelete(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// DownScaler needs to be instantiated
DownScaler = NewDownScaler(context.Background(), false, 1 * time.Second)
DownScaler = NewDownScaler(context.Background(), false, 1*time.Second)
Routes.Reset()
watcher := &k8sWatcherImpl{}
@@ -175,3 +175,99 @@ func TestK8sWatcherImpl_handleAddThenDelete(t *testing.T) {
})
}
}
func TestK8s_externalName(t *testing.T) {
type scenario struct {
given string
expect string
}
type svcAndScenarios struct {
svc string
scenarios []scenario
}
tests := []struct {
name string
initial svcAndScenarios
update svcAndScenarios
}{
{
name: "typeChange",
initial: svcAndScenarios{
svc: ` {"metadata": {"annotations": {"mc-router.itzg.me/externalServerName": "a.com"}}, "spec":{"type":"ExternalName", "externalName": "mc-server.com"}}`,
scenarios: []scenario{
{given: "a.com", expect: "mc-server.com:25565"},
{given: "b.com", expect: ""},
},
},
update: svcAndScenarios{
svc: ` {"metadata": {"annotations": {"mc-router.itzg.me/externalServerName": "a.com"}}, "spec":{"clusterIP": "1.1.1.1"}}`,
scenarios: []scenario{
{given: "a.com", expect: "1.1.1.1:25565"},
{given: "b.com", expect: ""},
},
},
},
{
name: "typeAndServerChange",
initial: svcAndScenarios{
svc: ` {"metadata": {"annotations": {"mc-router.itzg.me/externalServerName": "a.com"}}, "spec":{"type":"ExternalName", "externalName": "mc-server.com"}}`,
scenarios: []scenario{
{given: "a.com", expect: "mc-server.com:25565"},
{given: "b.com", expect: ""},
},
},
update: svcAndScenarios{
svc: ` {"metadata": {"annotations": {"mc-router.itzg.me/externalServerName": "b.com"}}, "spec":{"clusterIP": "1.1.1.1"}}`,
scenarios: []scenario{
{given: "a.com", expect: ""},
{given: "b.com", expect: "1.1.1.1:25565"},
},
},
},
{
name: "externalNameChange",
initial: svcAndScenarios{
svc: ` {"metadata": {"annotations": {"mc-router.itzg.me/externalServerName": "a.com,b.com"}}, "spec":{"type":"ExternalName", "externalName": "mc-server.com"}}`,
scenarios: []scenario{
{given: "a.com", expect: "mc-server.com:25565"},
{given: "b.com", expect: "mc-server.com:25565"},
},
},
update: svcAndScenarios{
svc: ` {"metadata": {"annotations": {"mc-router.itzg.me/externalServerName": "a.com,b.com"}}, "spec":{"type":"ExternalName", "externalName": "1.1.1.1"}}`,
scenarios: []scenario{
{given: "a.com", expect: "1.1.1.1:25565"},
{given: "b.com", expect: "1.1.1.1:25565"},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// DownScaler needs to be instantiated
DownScaler = NewDownScaler(context.Background(), false, 1*time.Second)
Routes.Reset()
watcher := &k8sWatcherImpl{}
initialSvc := v1.Service{}
err := json.Unmarshal([]byte(test.initial.svc), &initialSvc)
require.NoError(t, err)
watcher.handleAdd(&initialSvc)
for _, s := range test.initial.scenarios {
backend, _, _, _ := Routes.FindBackendForServerAddress(context.Background(), s.given)
assert.Equal(t, s.expect, backend, "initial: given=%s", s.given)
}
updatedSvc := v1.Service{}
err = json.Unmarshal([]byte(test.update.svc), &updatedSvc)
require.NoError(t, err)
watcher.handleUpdate(&initialSvc, &updatedSvc)
for _, s := range test.update.scenarios {
backend, _, _, _ := Routes.FindBackendForServerAddress(context.Background(), s.given)
assert.Equal(t, s.expect, backend, "update: given=%s", s.given)
}
})
}
}