Skip to content

Commit

Permalink
Grpc default service config (#301)
Browse files Browse the repository at this point in the history
* add load balancer config

Signed-off-by: Babis Kiosidis <[email protected]>

* add policies doc link

Signed-off-by: Babis Kiosidis <[email protected]>

* add available load balancing policies comment

Signed-off-by: Babis Kiosidis <[email protected]>

* add descriptive comment about missing balancer value

Signed-off-by: Babis Kiosidis <[email protected]>

* describe load balancing policy behaviour

Signed-off-by: Babis Kiosidis <[email protected]>

* import balancers

Signed-off-by: Babis Kiosidis <[email protected]>

* skip linting for empty imports

Signed-off-by: Babis Kiosidis <[email protected]>

* lint nolint

Signed-off-by: Babis Kiosidis <[email protected]>

* rely on grpc client functionality for the configs
and remove todo

Signed-off-by: Babis Kiosidis <[email protected]>

* dont repeat the comment

Signed-off-by: Babis Kiosidis <[email protected]>

* change load balancing config to serviceConfig

Signed-off-by: Babis Kiosidis <[email protected]>

* Change config name and remove package preloading

Signed-off-by: Hongxin Liang <[email protected]>

* Test it

Signed-off-by: Hongxin Liang <[email protected]>

Co-authored-by: Babis Kiosidis <[email protected]>
Signed-off-by: Eduardo Apolinario <[email protected]>
  • Loading branch information
2 people authored and eapolinario committed Sep 13, 2023
1 parent 8d23017 commit a05a8e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions flyteidl/clients/go/admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func initializeClients(ctx context.Context, cfg *Config, tokenCache pkce.TokenCa
opts = append(opts, authOpt)
}

if cfg.DefaultServiceConfig != "" {
opts = append(opts, grpc.WithDefaultServiceConfig(cfg.DefaultServiceConfig))
}

adminConnection, err := NewAdminConnection(ctx, cfg, opts...)
if err != nil {
logger.Panicf(ctx, "failed to initialize Admin connection. Err: %s", err.Error())
Expand Down
37 changes: 37 additions & 0 deletions flyteidl/clients/go/admin/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"golang.org/x/oauth2"
_ "google.golang.org/grpc/balancer/roundrobin" //nolint
)

func TestInitializeAndGetAdminClient(t *testing.T) {
Expand Down Expand Up @@ -262,6 +263,42 @@ func Test_getPkceAuthTokenSource(t *testing.T) {
})
}

func TestGetDefaultServiceConfig(t *testing.T) {
u, _ := url.Parse("localhost:8089")
adminServiceConfig := &Config{
Endpoint: config.URL{URL: *u},
DefaultServiceConfig: `{"loadBalancingConfig": [{"round_robin":{}}]}`,
}

assert.NoError(t, SetConfig(adminServiceConfig))

ctx := context.Background()
t.Run("legal", func(t *testing.T) {
u, err := url.Parse("http://localhost:8089")
assert.NoError(t, err)
clientSet, err := ClientSetBuilder().WithConfig(&Config{Endpoint: config.URL{URL: *u}, DefaultServiceConfig: `{"loadBalancingConfig": [{"round_robin":{}}]}`}).Build(ctx)
assert.NoError(t, err)
assert.NotNil(t, clientSet)
assert.NotNil(t, clientSet.AdminClient())
assert.NotNil(t, clientSet.AuthMetadataClient())
assert.NotNil(t, clientSet.IdentityClient())
assert.NotNil(t, clientSet.HealthServiceClient())
})
t.Run("illegal default service config", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()

u, err := url.Parse("http://localhost:8089")
assert.NoError(t, err)
clientSet, err := ClientSetBuilder().WithConfig(&Config{Endpoint: config.URL{URL: *u}, DefaultServiceConfig: `{"loadBalancingConfig": [{"foo":{}}]}`}).Build(ctx)
assert.Error(t, err)
assert.Nil(t, clientSet)
})
}

func ExampleClientSetBuilder() {
ctx := context.Background()
// Create a client set that initializes the connection with flyte admin and sets up Auth (if needed).
Expand Down
11 changes: 8 additions & 3 deletions flyteidl/clients/go/admin/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Initializes an Admin Client that exposes all implemented services by FlyteAdmin server. The library supports different
// authentication flows (see AuthType). It initializes the grpc connection once and reuses it. The gRPC connection is
// sticky (it hogs one server and keeps the connection alive). For better load balancing against the server, place a
// proxy service in between instead.
// authentication flows (see AuthType). It initializes the grpc connection once and reuses it. A grpc load balancing policy
// can be configured as well.
package admin

import (
Expand Down Expand Up @@ -68,6 +67,12 @@ type Config struct {
PkceConfig pkce.Config `json:"pkceConfig" pflag:",Config for Pkce authentication flow."`

Command []string `json:"command" pflag:",Command for external authentication token generation"`

// Set the gRPC service config formatted as a json string https://github.com/grpc/grpc/blob/master/doc/service_config.md
// eg. {"loadBalancingConfig": [{"round_robin":{}}], "methodConfig": [{"name":[{"service": "foo", "method": "bar"}, {"service": "baz"}], "timeout": "1.000000001s"}]}
// find the full schema here https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto#L625
// Note that required packages may need to be preloaded to support certain service config. For example "google.golang.org/grpc/balancer/roundrobin" should be preloaded to have round-robin policy supported.
DefaultServiceConfig string `json:"defaultServiceConfig" pdflag:",Set the default service config for the admin gRPC client"`
}

var (
Expand Down

0 comments on commit a05a8e1

Please sign in to comment.