Skip to content

Commit

Permalink
fix(service): replace clusterIP to service domain for service (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
Muzry authored Sep 3, 2021
1 parent 9d68b46 commit 17eeab9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
22 changes: 4 additions & 18 deletions modules/scheduler/executor/plugins/k8s/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,30 +318,16 @@ func (k *Kubernetes) inspectStateless(sg *apistructs.ServiceGroup) (*apistructs.
ns = sg.ProjectNamespace
k.setProjectServiceName(sg)
}
services, err := k.service.List(ns, map[string]string{
LabelServiceGroupID: sg.ID,
})
if err != nil {
return nil, fmt.Errorf("list service in ns %s error %v", ns, err)
}
serviceMap := make(map[string]string, len(services.Items))

for _, svc := range services.Items {
serviceMap[svc.Name] = svc.Spec.ClusterIP
}

for i, svc := range sg.Services {
serviceName := getServiceName(&svc)
if len(svc.Ports) == 0 {
continue
}
clusterIP, ok := serviceMap[serviceName]
if ok {
logrus.Errorf("failed to get service cluster ip, namespace: %s, name: %s, not found", ns, svc.Name)
}
sg.Services[i].ProxyIp = clusterIP
sg.Services[i].Vip = strutil.Join([]string{serviceName, ns, "svc.cluster.local"}, ".")
sg.Services[i].ShortVIP = clusterIP
serviceHost := strutil.Join([]string{serviceName, ns, "svc.cluster.local"}, ".")
sg.Services[i].ProxyIp = serviceHost
sg.Services[i].Vip = serviceHost
sg.Services[i].ShortVIP = serviceHost
sg.Services[i].ProxyPorts = diceyml.ComposeIntPortsFromServicePorts(svc.Ports)
}
return sg, nil
Expand Down
62 changes: 62 additions & 0 deletions modules/scheduler/executor/plugins/k8s/runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package k8s

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/parser/diceyml"
"github.com/erda-project/erda/pkg/strutil"
)

func TestKubernetes_InspectStateful(t *testing.T) {

kubernetes := &Kubernetes{}

serviceName := "fake-service"

sg := &apistructs.ServiceGroup{
Dice: apistructs.Dice{
Type: "service",
ID: "fakeTest",
Services: []apistructs.Service{
{
Name: "fake-service",
Ports: []diceyml.ServicePort{
{
Port: 1234,
Protocol: "HTTPS",
L4Protocol: "TCP",
},
{
Port: 5678,
Protocol: "UDP",
L4Protocol: "UDP",
},
},
},
},
},
}
hostName := strutil.Join([]string{serviceName, "service--fakeTest", "svc.cluster.local"}, ".")
sg, err := kubernetes.inspectStateless(sg)
assert.Nil(t, err)
assert.Equal(t, sg.Services[0].ProxyIp, hostName)
assert.Equal(t, sg.Services[0].Vip, hostName)
assert.Equal(t, sg.Services[0].ShortVIP, hostName)
assert.Equal(t, sg.Services[0].ProxyPorts, []int{1234, 5678})
}
7 changes: 7 additions & 0 deletions modules/scheduler/executor/plugins/k8s/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func newService(service *apistructs.Service, selectors map[string]string) *apiv1
}

setServiceLabelSelector(k8sService, selectors)
if selectors == nil {
k8sService.Labels = map[string]string{
"app": service.Name,
}
} else {
k8sService.Labels = selectors
}

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

0 comments on commit 17eeab9

Please sign in to comment.