Skip to content

Commit

Permalink
Export function ConvertServices
Browse files Browse the repository at this point in the history
Signed-off-by: Jacek Ewertowski <[email protected]>
  • Loading branch information
jewertow committed Jul 17, 2024
1 parent 85a1df2 commit bab7c41
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pilot/pkg/serviceregistry/serviceentry/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 366,7 @@ func getUpdatedConfigs(services []*model.Service) sets.Set[model.ConfigKey] {
func (s *Controller) serviceEntryHandler(old, curr config.Config, event model.Event) {
log.Debugf("Handle event %s for service entry %s/%s", event, curr.Namespace, curr.Name)
currentServiceEntry := curr.Spec.(*networking.ServiceEntry)
cs := convertServices(curr, s.clusterID)
cs := ConvertServices(curr, s.clusterID)
configsUpdated := sets.New[model.ConfigKey]()
key := curr.NamespacedName()

Expand Down
6 changes: 3 additions & 3 deletions pilot/pkg/serviceregistry/serviceentry/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 1616,7 @@ func expectEvents(t testing.TB, ch *xdsfake.Updater, events ...Event) {

func expectServiceInstances(t testing.TB, sd *Controller, cfg *config.Config, port int, expected ...[]*model.ServiceInstance) {
t.Helper()
svcs := convertServices(*cfg, "")
svcs := ConvertServices(*cfg, "")
if len(svcs) != len(expected) {
t.Fatalf("got more services than expected: %v vs %v", len(svcs), len(expected))
}
Expand Down Expand Up @@ -1850,8 1850,8 @@ func TestServicesDiff(t *testing.T) {

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
as := convertServices(*tt.current, "")
bs := convertServices(*tt.new, "")
as := ConvertServices(*tt.current, "")
bs := ConvertServices(*tt.new, "")
added, deleted, updated, unchanged := servicesDiff(as, bs)
for i, item := range []struct {
hostnames []host.Name
Expand Down
4 changes: 2 additions & 2 deletions pilot/pkg/serviceregistry/serviceentry/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 138,7 @@ func ServiceToServiceEntry(svc *model.Service, proxy *model.Proxy) *config.Confi
}

// convertServices transforms a ServiceEntry config to a list of internal Service objects.
func convertServices(cfg config.Config, clusterID cluster.ID) []*model.Service {
func ConvertServices(cfg config.Config, clusterID cluster.ID) []*model.Service {
serviceEntry := cfg.Spec.(*networking.ServiceEntry)
creationTime := cfg.CreationTimestamp

Expand Down Expand Up @@ -325,7 325,7 @@ func (s *Controller) convertServiceEntryToInstances(cfg config.Config, services
return nil
}
if services == nil {
services = convertServices(cfg, s.clusterID)
services = ConvertServices(cfg, s.clusterID)
}

endpointsNum := len(serviceEntry.Endpoints)
Expand Down
8 changes: 4 additions & 4 deletions pilot/pkg/serviceregistry/serviceentry/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 586,7 @@ func makeInstanceWithServiceAccount(cfg *config.Config, addresses []string, port
func makeTarget(cfg *config.Config, address string, port int,
svcPort *networking.ServicePort, svcLabels map[string]string, mtlsMode MTLSMode,
) model.ServiceTarget {
services := convertServices(*cfg, "")
services := ConvertServices(*cfg, "")
svc := services[0] // default
for _, s := range services {
if string(s.Hostname) == address {
Expand Down Expand Up @@ -617,7 617,7 @@ func makeTarget(cfg *config.Config, address string, port int,
func makeInstance(cfg *config.Config, addresses []string, port int,
svcPort *networking.ServicePort, svcLabels map[string]string, mtlsMode MTLSMode,
) *model.ServiceInstance {
services := convertServices(*cfg, "")
services := ConvertServices(*cfg, "")
svc := services[0] // default
getSvc := false
for _, s := range services {
Expand Down Expand Up @@ -780,7 780,7 @@ func testConvertServiceBody(t *testing.T) {
})

for _, tt := range serviceTests {
services := convertServices(*tt.externalSvc, "")
services := ConvertServices(*tt.externalSvc, "")
if err := compare(t, services, tt.services); err != nil {
t.Errorf("testcase: %v\n%v ", tt.externalSvc.Name, err)
}
Expand Down Expand Up @@ -967,7 967,7 @@ func TestConvertWorkloadEntryToServiceInstances(t *testing.T) {

for _, tt := range serviceInstanceTests {
t.Run(tt.name, func(t *testing.T) {
services := convertServices(*tt.se, "")
services := ConvertServices(*tt.se, "")
s := &Controller{meshWatcher: mesh.NewFixedWatcher(mesh.DefaultMeshConfig())}
instances := s.convertWorkloadEntryToServiceInstances(tt.wle, services, tt.se.Spec.(*networking.ServiceEntry), &configKey{}, tt.clusterID)
sortServiceInstances(instances)
Expand Down
105 changes: 46 additions & 59 deletions pkg/dns/server/name_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,12 @@
package server_test

import (
"istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/pkg/serviceregistry/kube"
"istio.io/istio/pilot/pkg/serviceregistry/serviceentry"
"istio.io/istio/pkg/config"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -25,7 31,6 @@ import (
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/serviceregistry/provider"
"istio.io/istio/pkg/cluster"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/config/host"
"istio.io/istio/pkg/config/protocol"
dnsProto "istio.io/istio/pkg/dns/proto"
Expand Down Expand Up @@ -93,48 98,34 @@ func TestNameTable(t *testing.T) {
DNSDomain: "testns.svc.cluster.local",
}

headlessService := &model.Service{
Hostname: host.Name("headless-svc.testns.svc.cluster.local"),
DefaultAddress: constants.UnspecifiedIP,
ClusterVIPs: model.AddressMap{
Addresses: map[cluster.ID][]string{
"Kubernetes": {constants.UnspecifiedIP},
},
headlessService := kube.ConvertService(corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "headless-svc",
Namespace: "testns",
},
Ports: model.PortList{&model.Port{
Name: "tcp-port",
Port: 9500,
Protocol: protocol.TCP,
}},
Resolution: model.Passthrough,
Attributes: model.ServiceAttributes{
Name: "headless-svc",
Namespace: "testns",
ServiceRegistry: provider.Kubernetes,
Spec: corev1.ServiceSpec{
ClusterIP: corev1.ClusterIPNone,
Ports: []corev1.ServicePort{{
Name: "tcp-port",
Port: 9500,
Protocol: corev1.ProtocolTCP,
}},
},
}
}, "cluster.local", "Kubernetes", nil)

headlessServiceForServiceEntry := &model.Service{
Hostname: host.Name("foo.bar.com"),
DefaultAddress: constants.UnspecifiedIP,
ClusterVIPs: model.AddressMap{
Addresses: map[cluster.ID][]string{
"Kubernetes": {constants.UnspecifiedIP},
headlessServiceForServiceEntry := serviceentry.ConvertServices(config.Config{
Spec: &v1alpha3.ServiceEntry{
Hosts: []string{"foo.bar.com"},
Ports: []*v1alpha3.ServicePort{{
Name: "tcp-port",
Number: 9500,
Protocol: "TCP",
}},
WorkloadSelector: &v1alpha3.WorkloadSelector{
Labels: map[string]string{"wl": "headless-foobar"},
},
},
Ports: model.PortList{&model.Port{
Name: "tcp-port",
Port: 9500,
Protocol: protocol.TCP,
}},
Resolution: model.Passthrough,
Attributes: model.ServiceAttributes{
Name: "foo.bar.com",
Namespace: "testns",
ServiceRegistry: provider.External,
LabelSelectors: map[string]string{"wl": "headless-foobar"},
},
}
}, "Kubernetes")[0]

wildcardService := &model.Service{
Hostname: host.Name("*.testns.svc.cluster.local"),
Expand All @@ -159,28 150,24 @@ func TestNameTable(t *testing.T) {
},
}

cidrService := &model.Service{
Hostname: host.Name("*.testns.svc.cluster.local"),
DefaultAddress: "172.217.0.0/16",
Ports: model.PortList{
&model.Port{
Name: "tcp-port",
Port: 9500,
Protocol: protocol.TCP,
},
&model.Port{
Name: "http-port",
Port: 8000,
Protocol: protocol.HTTP,
cidrService := serviceentry.ConvertServices(config.Config{
Spec: &v1alpha3.ServiceEntry{
Hosts: []string{"*.testns.svc.cluster.local"},
Addresses: []string{"172.217.0.0/16"},
Ports: []*v1alpha3.ServicePort{
{
Name: "tcp-port",
Number: 9500,
Protocol: "TCP",
},
{
Name: "http-port",
Number: 8000,
Protocol: "HTTP",
},
},
},
Resolution: model.ClientSideLB,
Attributes: model.ServiceAttributes{
Name: "cidr-svc",
Namespace: "testns",
ServiceRegistry: provider.Kubernetes,
},
}
}, "Kubernetes")

serviceWithVIP1 := &model.Service{
Hostname: host.Name("mysql.foo.bar"),
Expand Down Expand Up @@ -248,7 235,7 @@ func TestNameTable(t *testing.T) {

cpush := model.NewPushContext()
cpush.Mesh = mesh
cpush.AddPublicServices([]*model.Service{cidrService})
cpush.AddPublicServices(cidrService)

sepush := model.NewPushContext()
sepush.Mesh = mesh
Expand Down

0 comments on commit bab7c41

Please sign in to comment.