istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/serviceregistry/mock/discovery.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mock 16 17 import ( 18 "fmt" 19 "net/netip" 20 "time" 21 22 "istio.io/istio/pilot/pkg/model" 23 "istio.io/istio/pkg/cluster" 24 "istio.io/istio/pkg/config/host" 25 "istio.io/istio/pkg/config/protocol" 26 ) 27 28 // PortHTTPName is the HTTP port name 29 var PortHTTPName = "http" 30 31 type ServiceArgs struct { 32 Hostname host.Name 33 Address string 34 ServiceAccounts []string 35 ClusterID cluster.ID 36 } 37 38 func MakeServiceInstance(service *model.Service, port *model.Port, version int, locality model.Locality) *model.ServiceInstance { 39 if service.External() { 40 return nil 41 } 42 43 // we make port 80 same as endpoint port, otherwise, it's distinct 44 target := port.Port 45 if target != 80 { 46 target += 1000 47 } 48 49 return &model.ServiceInstance{ 50 Endpoint: &model.IstioEndpoint{ 51 Address: MakeIP(service, version), 52 EndpointPort: uint32(target), 53 ServicePortName: port.Name, 54 Labels: map[string]string{"version": fmt.Sprintf("v%d", version)}, 55 Locality: locality, 56 }, 57 Service: service, 58 ServicePort: port, 59 } 60 } 61 62 // MakeService creates a memory service 63 func MakeService(args ServiceArgs) *model.Service { 64 return &model.Service{ 65 CreationTime: time.Now(), 66 Hostname: args.Hostname, 67 ClusterVIPs: model.AddressMap{ 68 Addresses: map[cluster.ID][]string{args.ClusterID: {args.Address}}, 69 }, 70 DefaultAddress: args.Address, 71 ServiceAccounts: args.ServiceAccounts, 72 Ports: []*model.Port{ 73 { 74 Name: PortHTTPName, 75 Port: 80, // target port 80 76 Protocol: protocol.HTTP, 77 }, { 78 Name: "http-status", 79 Port: 81, // target port 1081 80 Protocol: protocol.HTTP, 81 }, { 82 Name: "custom", 83 Port: 90, // target port 1090 84 Protocol: protocol.TCP, 85 }, { 86 Name: "mongo", 87 Port: 100, // target port 1100 88 Protocol: protocol.Mongo, 89 }, { 90 Name: "redis", 91 Port: 110, // target port 1110 92 Protocol: protocol.Redis, 93 }, { 94 Name: "mysql", 95 Port: 120, // target port 1120 96 Protocol: protocol.MySQL, 97 }, 98 }, 99 } 100 } 101 102 // MakeExternalHTTPService creates memory external service 103 func MakeExternalHTTPService(hostname host.Name, isMeshExternal bool, address string) *model.Service { 104 return &model.Service{ 105 CreationTime: time.Now(), 106 Hostname: hostname, 107 DefaultAddress: address, 108 MeshExternal: isMeshExternal, 109 Ports: []*model.Port{{ 110 Name: "http", 111 Port: 80, 112 Protocol: protocol.HTTP, 113 }}, 114 } 115 } 116 117 // MakeExternalHTTPSService creates memory external service 118 func MakeExternalHTTPSService(hostname host.Name, isMeshExternal bool, address string) *model.Service { 119 return &model.Service{ 120 CreationTime: time.Now(), 121 Hostname: hostname, 122 DefaultAddress: address, 123 MeshExternal: isMeshExternal, 124 Ports: []*model.Port{{ 125 Name: "https", 126 Port: 443, 127 Protocol: protocol.HTTPS, 128 }}, 129 } 130 } 131 132 // MakeIP creates a fake IP address for a service and instance version 133 func MakeIP(service *model.Service, version int) string { 134 // external services have no instances 135 if service.External() { 136 return "" 137 } 138 ipa, ise := netip.ParseAddr(service.DefaultAddress) 139 if ise != nil { 140 return "" 141 } 142 ip := ipa.As4() 143 ip[2] = byte(1) 144 ip[3] = byte(version) 145 return netip.AddrFrom4(ip).String() 146 }