Skip to content

Commit 17eeab9

Browse files
authored
fix(service): replace clusterIP to service domain for service (#1658)
1 parent 9d68b46 commit 17eeab9

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

modules/scheduler/executor/plugins/k8s/runtime.go

+4-18
Original file line numberDiff line numberDiff line change
@@ -318,30 +318,16 @@ func (k *Kubernetes) inspectStateless(sg *apistructs.ServiceGroup) (*apistructs.
318318
ns = sg.ProjectNamespace
319319
k.setProjectServiceName(sg)
320320
}
321-
services, err := k.service.List(ns, map[string]string{
322-
LabelServiceGroupID: sg.ID,
323-
})
324-
if err != nil {
325-
return nil, fmt.Errorf("list service in ns %s error %v", ns, err)
326-
}
327-
serviceMap := make(map[string]string, len(services.Items))
328-
329-
for _, svc := range services.Items {
330-
serviceMap[svc.Name] = svc.Spec.ClusterIP
331-
}
332321

333322
for i, svc := range sg.Services {
334323
serviceName := getServiceName(&svc)
335324
if len(svc.Ports) == 0 {
336325
continue
337326
}
338-
clusterIP, ok := serviceMap[serviceName]
339-
if ok {
340-
logrus.Errorf("failed to get service cluster ip, namespace: %s, name: %s, not found", ns, svc.Name)
341-
}
342-
sg.Services[i].ProxyIp = clusterIP
343-
sg.Services[i].Vip = strutil.Join([]string{serviceName, ns, "svc.cluster.local"}, ".")
344-
sg.Services[i].ShortVIP = clusterIP
327+
serviceHost := strutil.Join([]string{serviceName, ns, "svc.cluster.local"}, ".")
328+
sg.Services[i].ProxyIp = serviceHost
329+
sg.Services[i].Vip = serviceHost
330+
sg.Services[i].ShortVIP = serviceHost
345331
sg.Services[i].ProxyPorts = diceyml.ComposeIntPortsFromServicePorts(svc.Ports)
346332
}
347333
return sg, nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// This program is free software: you can use, redistribute, and/or modify
4+
// it under the terms of the GNU Affero General Public License, version 3
5+
// or later ("AGPL"), as published by the Free Software Foundation.
6+
//
7+
// This program is distributed in the hope that it will be useful, but WITHOUT
8+
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9+
// FITNESS FOR A PARTICULAR PURPOSE.
10+
//
11+
// You should have received a copy of the GNU Affero General Public License
12+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
13+
14+
package k8s
15+
16+
import (
17+
"testing"
18+
19+
"github.com/stretchr/testify/assert"
20+
21+
"github.com/erda-project/erda/apistructs"
22+
"github.com/erda-project/erda/pkg/parser/diceyml"
23+
"github.com/erda-project/erda/pkg/strutil"
24+
)
25+
26+
func TestKubernetes_InspectStateful(t *testing.T) {
27+
28+
kubernetes := &Kubernetes{}
29+
30+
serviceName := "fake-service"
31+
32+
sg := &apistructs.ServiceGroup{
33+
Dice: apistructs.Dice{
34+
Type: "service",
35+
ID: "fakeTest",
36+
Services: []apistructs.Service{
37+
{
38+
Name: "fake-service",
39+
Ports: []diceyml.ServicePort{
40+
{
41+
Port: 1234,
42+
Protocol: "HTTPS",
43+
L4Protocol: "TCP",
44+
},
45+
{
46+
Port: 5678,
47+
Protocol: "UDP",
48+
L4Protocol: "UDP",
49+
},
50+
},
51+
},
52+
},
53+
},
54+
}
55+
hostName := strutil.Join([]string{serviceName, "service--fakeTest", "svc.cluster.local"}, ".")
56+
sg, err := kubernetes.inspectStateless(sg)
57+
assert.Nil(t, err)
58+
assert.Equal(t, sg.Services[0].ProxyIp, hostName)
59+
assert.Equal(t, sg.Services[0].Vip, hostName)
60+
assert.Equal(t, sg.Services[0].ShortVIP, hostName)
61+
assert.Equal(t, sg.Services[0].ProxyPorts, []int{1234, 5678})
62+
}

modules/scheduler/executor/plugins/k8s/service.go

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ func newService(service *apistructs.Service, selectors map[string]string) *apiv1
114114
}
115115

116116
setServiceLabelSelector(k8sService, selectors)
117+
if selectors == nil {
118+
k8sService.Labels = map[string]string{
119+
"app": service.Name,
120+
}
121+
} else {
122+
k8sService.Labels = selectors
123+
}
117124

118125
for i, port := range service.Ports {
119126
k8sService.Spec.Ports = append(k8sService.Spec.Ports, apiv1.ServicePort{

0 commit comments

Comments
 (0)