-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use grpc load-balancing when connecting to trustd
Instead of doing our homegrown "try all the endpoints" method, use gRPC load-balancing across configured endpoints. Generalize load-balancer via gRPC resolver we had in Talos API client, use it in remote certificate generator code. Generalized resolver is still under `machinery/`, as `pkg/grpc` is not in `machinery/`, and we can't depend on Talos code from `machinery/`. Related to: #3068 Full fix for #3068 requires dynamic updates to control plane endpoints while apid is running, this is coming in the next PR. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
6 changed files
with
134 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// Package resolver implements gRPC resolvers. | ||
package resolver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package resolver | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
|
||
"github.com/talos-systems/net" | ||
"google.golang.org/grpc/resolver" | ||
) | ||
|
||
// RegisterRoundRobinResolver registers round-robin gRPC resolver for specified port and returns scheme to use in grpc.Dial. | ||
func RegisterRoundRobinResolver(port int) (scheme string) { | ||
scheme = fmt.Sprintf(roundRobinResolverScheme, port) | ||
|
||
resolver.Register(&roundRobinResolverBuilder{ | ||
port: port, | ||
scheme: scheme, | ||
}) | ||
|
||
return | ||
} | ||
|
||
const roundRobinResolverScheme = "taloslist-%d" | ||
|
||
type roundRobinResolverBuilder struct { | ||
port int | ||
scheme string | ||
} | ||
|
||
// Build implements resolver.Builder. | ||
func (b *roundRobinResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { | ||
r := &roundRobinResolver{ | ||
target: target, | ||
cc: cc, | ||
port: b.port, | ||
} | ||
|
||
if err := r.start(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
// Build implements resolver.Builder. | ||
func (b *roundRobinResolverBuilder) Scheme() string { | ||
return b.scheme | ||
} | ||
|
||
type roundRobinResolver struct { | ||
target resolver.Target | ||
cc resolver.ClientConn | ||
port int | ||
} | ||
|
||
func (r *roundRobinResolver) start() error { | ||
var addrs []resolver.Address // nolint: prealloc | ||
|
||
for _, a := range strings.Split(r.target.Endpoint, ",") { | ||
addrs = append(addrs, resolver.Address{ | ||
ServerName: a, | ||
Addr: fmt.Sprintf("%s:%d", net.FormatAddress(a), r.port), | ||
}) | ||
} | ||
|
||
// shuffle the list in case client does just one request | ||
rand.Shuffle(len(addrs), func(i, j int) { | ||
addrs[i], addrs[j] = addrs[j], addrs[i] | ||
}) | ||
|
||
serviceConfigJSON := `{ | ||
"loadBalancingConfig": [{ | ||
"round_robin": {} | ||
}] | ||
}` | ||
|
||
parsedServiceConfig := r.cc.ParseServiceConfig(serviceConfigJSON) | ||
|
||
if parsedServiceConfig.Err != nil { | ||
return parsedServiceConfig.Err | ||
} | ||
|
||
r.cc.UpdateState(resolver.State{ | ||
Addresses: addrs, | ||
ServiceConfig: parsedServiceConfig, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
// ResolveNow implements resolver.Resolver. | ||
func (r *roundRobinResolver) ResolveNow(o resolver.ResolveNowOptions) {} | ||
|
||
// ResolveNow implements resolver.Resolver. | ||
func (r *roundRobinResolver) Close() {} |