khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 gRPC authors. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | // Package resolver defines APIs for name resolution in gRPC. |
| 20 | // All APIs in this package are experimental. |
| 21 | package resolver |
| 22 | |
| 23 | import ( |
| 24 | "context" |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 25 | "fmt" |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 26 | "net" |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 27 | "net/url" |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 28 | "strings" |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 29 | |
| 30 | "google.golang.org/grpc/attributes" |
| 31 | "google.golang.org/grpc/credentials" |
| 32 | "google.golang.org/grpc/serviceconfig" |
| 33 | ) |
| 34 | |
| 35 | var ( |
| 36 | // m is a map from scheme to resolver builder. |
| 37 | m = make(map[string]Builder) |
| 38 | // defaultScheme is the default scheme to use. |
| 39 | defaultScheme = "passthrough" |
| 40 | ) |
| 41 | |
| 42 | // TODO(bar) install dns resolver in init(){}. |
| 43 | |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 44 | // Register registers the resolver builder to the resolver map. b.Scheme will |
| 45 | // be used as the scheme registered with this builder. The registry is case |
| 46 | // sensitive, and schemes should not contain any uppercase characters. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 47 | // |
| 48 | // NOTE: this function must only be called during initialization time (i.e. in |
| 49 | // an init() function), and is not thread-safe. If multiple Resolvers are |
| 50 | // registered with the same name, the one registered last will take effect. |
| 51 | func Register(b Builder) { |
| 52 | m[b.Scheme()] = b |
| 53 | } |
| 54 | |
| 55 | // Get returns the resolver builder registered with the given scheme. |
| 56 | // |
| 57 | // If no builder is register with the scheme, nil will be returned. |
| 58 | func Get(scheme string) Builder { |
| 59 | if b, ok := m[scheme]; ok { |
| 60 | return b |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // SetDefaultScheme sets the default scheme that will be used. The default |
| 66 | // default scheme is "passthrough". |
| 67 | // |
| 68 | // NOTE: this function must only be called during initialization time (i.e. in |
| 69 | // an init() function), and is not thread-safe. The scheme set last overrides |
| 70 | // previously set values. |
| 71 | func SetDefaultScheme(scheme string) { |
| 72 | defaultScheme = scheme |
| 73 | } |
| 74 | |
| 75 | // GetDefaultScheme gets the default scheme that will be used. |
| 76 | func GetDefaultScheme() string { |
| 77 | return defaultScheme |
| 78 | } |
| 79 | |
| 80 | // AddressType indicates the address type returned by name resolution. |
| 81 | // |
| 82 | // Deprecated: use Attributes in Address instead. |
| 83 | type AddressType uint8 |
| 84 | |
| 85 | const ( |
| 86 | // Backend indicates the address is for a backend server. |
| 87 | // |
| 88 | // Deprecated: use Attributes in Address instead. |
| 89 | Backend AddressType = iota |
| 90 | // GRPCLB indicates the address is for a grpclb load balancer. |
| 91 | // |
| 92 | // Deprecated: to select the GRPCLB load balancing policy, use a service |
| 93 | // config with a corresponding loadBalancingConfig. To supply balancer |
| 94 | // addresses to the GRPCLB load balancing policy, set State.Attributes |
| 95 | // using balancer/grpclb/state.Set. |
| 96 | GRPCLB |
| 97 | ) |
| 98 | |
| 99 | // Address represents a server the client connects to. |
| 100 | // |
Joey Armstrong | ba3d9d1 | 2024-01-15 14:22:11 -0500 | [diff] [blame] | 101 | // # Experimental |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 102 | // |
| 103 | // Notice: This type is EXPERIMENTAL and may be changed or removed in a |
| 104 | // later release. |
| 105 | type Address struct { |
| 106 | // Addr is the server address on which a connection will be established. |
| 107 | Addr string |
| 108 | |
| 109 | // ServerName is the name of this address. |
| 110 | // If non-empty, the ServerName is used as the transport certification authority for |
| 111 | // the address, instead of the hostname from the Dial target string. In most cases, |
| 112 | // this should not be set. |
| 113 | // |
| 114 | // If Type is GRPCLB, ServerName should be the name of the remote load |
| 115 | // balancer, not the name of the backend. |
| 116 | // |
| 117 | // WARNING: ServerName must only be populated with trusted values. It |
| 118 | // is insecure to populate it with data from untrusted inputs since untrusted |
| 119 | // values could be used to bypass the authority checks performed by TLS. |
| 120 | ServerName string |
| 121 | |
| 122 | // Attributes contains arbitrary data about this address intended for |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 123 | // consumption by the SubConn. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 124 | Attributes *attributes.Attributes |
| 125 | |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 126 | // BalancerAttributes contains arbitrary data about this address intended |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 127 | // for consumption by the LB policy. These attributes do not affect SubConn |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 128 | // creation, connection establishment, handshaking, etc. |
| 129 | BalancerAttributes *attributes.Attributes |
| 130 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 131 | // Type is the type of this address. |
| 132 | // |
| 133 | // Deprecated: use Attributes instead. |
| 134 | Type AddressType |
| 135 | |
| 136 | // Metadata is the information associated with Addr, which may be used |
| 137 | // to make load balancing decision. |
| 138 | // |
| 139 | // Deprecated: use Attributes instead. |
| 140 | Metadata interface{} |
| 141 | } |
| 142 | |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 143 | // Equal returns whether a and o are identical. Metadata is compared directly, |
| 144 | // not with any recursive introspection. |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 145 | func (a Address) Equal(o Address) bool { |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 146 | return a.Addr == o.Addr && a.ServerName == o.ServerName && |
| 147 | a.Attributes.Equal(o.Attributes) && |
| 148 | a.BalancerAttributes.Equal(o.BalancerAttributes) && |
| 149 | a.Type == o.Type && a.Metadata == o.Metadata |
| 150 | } |
| 151 | |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 152 | // String returns JSON formatted string representation of the address. |
| 153 | func (a Address) String() string { |
| 154 | var sb strings.Builder |
| 155 | sb.WriteString(fmt.Sprintf("{Addr: %q, ", a.Addr)) |
| 156 | sb.WriteString(fmt.Sprintf("ServerName: %q, ", a.ServerName)) |
| 157 | if a.Attributes != nil { |
| 158 | sb.WriteString(fmt.Sprintf("Attributes: %v, ", a.Attributes.String())) |
| 159 | } |
| 160 | if a.BalancerAttributes != nil { |
| 161 | sb.WriteString(fmt.Sprintf("BalancerAttributes: %v", a.BalancerAttributes.String())) |
| 162 | } |
| 163 | sb.WriteString("}") |
| 164 | return sb.String() |
| 165 | } |
| 166 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 167 | // BuildOptions includes additional information for the builder to create |
| 168 | // the resolver. |
| 169 | type BuildOptions struct { |
| 170 | // DisableServiceConfig indicates whether a resolver implementation should |
| 171 | // fetch service config data. |
| 172 | DisableServiceConfig bool |
| 173 | // DialCreds is the transport credentials used by the ClientConn for |
| 174 | // communicating with the target gRPC service (set via |
| 175 | // WithTransportCredentials). In cases where a name resolution service |
| 176 | // requires the same credentials, the resolver may use this field. In most |
| 177 | // cases though, it is not appropriate, and this field may be ignored. |
| 178 | DialCreds credentials.TransportCredentials |
| 179 | // CredsBundle is the credentials bundle used by the ClientConn for |
| 180 | // communicating with the target gRPC service (set via |
| 181 | // WithCredentialsBundle). In cases where a name resolution service |
| 182 | // requires the same credentials, the resolver may use this field. In most |
| 183 | // cases though, it is not appropriate, and this field may be ignored. |
| 184 | CredsBundle credentials.Bundle |
| 185 | // Dialer is the custom dialer used by the ClientConn for dialling the |
| 186 | // target gRPC service (set via WithDialer). In cases where a name |
| 187 | // resolution service requires the same dialer, the resolver may use this |
| 188 | // field. In most cases though, it is not appropriate, and this field may |
| 189 | // be ignored. |
| 190 | Dialer func(context.Context, string) (net.Conn, error) |
| 191 | } |
| 192 | |
| 193 | // State contains the current Resolver state relevant to the ClientConn. |
| 194 | type State struct { |
| 195 | // Addresses is the latest set of resolved addresses for the target. |
| 196 | Addresses []Address |
| 197 | |
| 198 | // ServiceConfig contains the result from parsing the latest service |
| 199 | // config. If it is nil, it indicates no service config is present or the |
| 200 | // resolver does not provide service configs. |
| 201 | ServiceConfig *serviceconfig.ParseResult |
| 202 | |
| 203 | // Attributes contains arbitrary data about the resolver intended for |
| 204 | // consumption by the load balancing policy. |
| 205 | Attributes *attributes.Attributes |
| 206 | } |
| 207 | |
| 208 | // ClientConn contains the callbacks for resolver to notify any updates |
| 209 | // to the gRPC ClientConn. |
| 210 | // |
| 211 | // This interface is to be implemented by gRPC. Users should not need a |
| 212 | // brand new implementation of this interface. For the situations like |
| 213 | // testing, the new implementation should embed this interface. This allows |
| 214 | // gRPC to add new methods to this interface. |
| 215 | type ClientConn interface { |
| 216 | // UpdateState updates the state of the ClientConn appropriately. |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 217 | // |
| 218 | // If an error is returned, the resolver should try to resolve the |
| 219 | // target again. The resolver should use a backoff timer to prevent |
| 220 | // overloading the server with requests. If a resolver is certain that |
| 221 | // reresolving will not change the result, e.g. because it is |
| 222 | // a watch-based resolver, returned errors can be ignored. |
| 223 | // |
| 224 | // If the resolved State is the same as the last reported one, calling |
| 225 | // UpdateState can be omitted. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 226 | UpdateState(State) error |
| 227 | // ReportError notifies the ClientConn that the Resolver encountered an |
| 228 | // error. The ClientConn will notify the load balancer and begin calling |
| 229 | // ResolveNow on the Resolver with exponential backoff. |
| 230 | ReportError(error) |
| 231 | // NewAddress is called by resolver to notify ClientConn a new list |
| 232 | // of resolved addresses. |
| 233 | // The address list should be the complete list of resolved addresses. |
| 234 | // |
| 235 | // Deprecated: Use UpdateState instead. |
| 236 | NewAddress(addresses []Address) |
| 237 | // NewServiceConfig is called by resolver to notify ClientConn a new |
| 238 | // service config. The service config should be provided as a json string. |
| 239 | // |
| 240 | // Deprecated: Use UpdateState instead. |
| 241 | NewServiceConfig(serviceConfig string) |
| 242 | // ParseServiceConfig parses the provided service config and returns an |
| 243 | // object that provides the parsed config. |
| 244 | ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult |
| 245 | } |
| 246 | |
| 247 | // Target represents a target for gRPC, as specified in: |
| 248 | // https://github.com/grpc/grpc/blob/master/doc/naming.md. |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 249 | // It is parsed from the target string that gets passed into Dial or DialContext |
| 250 | // by the user. And gRPC passes it to the resolver and the balancer. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 251 | // |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 252 | // If the target follows the naming spec, and the parsed scheme is registered |
| 253 | // with gRPC, we will parse the target string according to the spec. If the |
| 254 | // target does not contain a scheme or if the parsed scheme is not registered |
| 255 | // (i.e. no corresponding resolver available to resolve the endpoint), we will |
| 256 | // apply the default scheme, and will attempt to reparse it. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 257 | // |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 258 | // Examples: |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 259 | // |
Joey Armstrong | ba3d9d1 | 2024-01-15 14:22:11 -0500 | [diff] [blame] | 260 | // - "dns://some_authority/foo.bar" |
| 261 | // Target{Scheme: "dns", Authority: "some_authority", Endpoint: "foo.bar"} |
| 262 | // - "foo.bar" |
| 263 | // Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "foo.bar"} |
| 264 | // - "unknown_scheme://authority/endpoint" |
| 265 | // Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "unknown_scheme://authority/endpoint"} |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 266 | type Target struct { |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 267 | // Deprecated: use URL.Scheme instead. |
| 268 | Scheme string |
| 269 | // Deprecated: use URL.Host instead. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 270 | Authority string |
khenaidoo | 5cb0d40 | 2021-12-08 14:09:16 -0500 | [diff] [blame] | 271 | // URL contains the parsed dial target with an optional default scheme added |
| 272 | // to it if the original dial target contained no scheme or contained an |
| 273 | // unregistered scheme. Any query params specified in the original dial |
| 274 | // target can be accessed from here. |
| 275 | URL url.URL |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 278 | // Endpoint retrieves endpoint without leading "/" from either `URL.Path` |
| 279 | // or `URL.Opaque`. The latter is used when the former is empty. |
| 280 | func (t Target) Endpoint() string { |
| 281 | endpoint := t.URL.Path |
| 282 | if endpoint == "" { |
| 283 | endpoint = t.URL.Opaque |
| 284 | } |
| 285 | // For targets of the form "[scheme]://[authority]/endpoint, the endpoint |
| 286 | // value returned from url.Parse() contains a leading "/". Although this is |
| 287 | // in accordance with RFC 3986, we do not want to break existing resolver |
| 288 | // implementations which expect the endpoint without the leading "/". So, we |
| 289 | // end up stripping the leading "/" here. But this will result in an |
| 290 | // incorrect parsing for something like "unix:///path/to/socket". Since we |
| 291 | // own the "unix" resolver, we can workaround in the unix resolver by using |
| 292 | // the `URL` field. |
| 293 | return strings.TrimPrefix(endpoint, "/") |
| 294 | } |
| 295 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 296 | // Builder creates a resolver that will be used to watch name resolution updates. |
| 297 | type Builder interface { |
| 298 | // Build creates a new resolver for the given target. |
| 299 | // |
| 300 | // gRPC dial calls Build synchronously, and fails if the returned error is |
| 301 | // not nil. |
| 302 | Build(target Target, cc ClientConn, opts BuildOptions) (Resolver, error) |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 303 | // Scheme returns the scheme supported by this resolver. Scheme is defined |
| 304 | // at https://github.com/grpc/grpc/blob/master/doc/naming.md. The returned |
| 305 | // string should not contain uppercase characters, as they will not match |
| 306 | // the parsed target's scheme as defined in RFC 3986. |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 307 | Scheme() string |
| 308 | } |
| 309 | |
| 310 | // ResolveNowOptions includes additional information for ResolveNow. |
| 311 | type ResolveNowOptions struct{} |
| 312 | |
| 313 | // Resolver watches for the updates on the specified target. |
| 314 | // Updates include address updates and service config updates. |
| 315 | type Resolver interface { |
| 316 | // ResolveNow will be called by gRPC to try to resolve the target name |
| 317 | // again. It's just a hint, resolver can ignore this if it's not necessary. |
| 318 | // |
| 319 | // It could be called multiple times concurrently. |
| 320 | ResolveNow(ResolveNowOptions) |
| 321 | // Close closes the resolver. |
| 322 | Close() |
| 323 | } |
| 324 | |
| 325 | // UnregisterForTesting removes the resolver builder with the given scheme from the |
| 326 | // resolver map. |
| 327 | // This function is for testing only. |
| 328 | func UnregisterForTesting(scheme string) { |
| 329 | delete(m, scheme) |
| 330 | } |