Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: Added RLS Cluster Specifier Plugin #4996

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions xds/internal/balancer/cdsbalancer/cdsbalancer.go
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
Expand All @@ -42,7 +43,8 @@ import (
)

const (
cdsName = "cds_experimental"
cdsName = "cds_experimental"
clusterPrefix = "cluster:"
)

var (
Expand Down Expand Up @@ -179,7 +181,7 @@ func (b *cdsBalancer) handleClientConnUpdate(update *ccUpdate) {
b.handleErrorFromUpdate(err, true)
return
}
b.clusterHandler.updateRootCluster(update.clusterName)
b.clusterHandler.updateRootCluster(strings.TrimPrefix(update.clusterName, clusterPrefix))
}

// handleSecurityConfig processes the security configuration received from the
Expand Down
96 changes: 96 additions & 0 deletions xds/internal/clusterspecifier/rls/rls.go
@@ -0,0 +1,96 @@
/*
*
* Copyright 2021 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Package rls implements the RLS cluster specifier plugin.
package rls

import (
"encoding/json"
"fmt"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"

"google.golang.org/grpc/xds/internal/clusterspecifier"
"google.golang.org/protobuf/types/known/anypb"
)

func init() {
clusterspecifier.Register(rls{})
}

type rls struct{}

func (rls) TypeURLs() []string {
return []string{"google.golang.org/grpc/xds/internal/clusterspecifier/rls/RouteLookupClusterSpecifier"}
}

// Same as balancer/rls/internal.lbConfigJSON
type lbConfigJSON struct {
RouteLookupConfig json.RawMessage
ChildPolicy []map[string]json.RawMessage
ChildPolicyConfigTargetFieldName string
}

func (rls) ParseClusterSpecifierConfig(cfg proto.Message) (clusterspecifier.BalancerConfig, error) {
if cfg == nil {
return nil, fmt.Errorf("rls_csp: nil configuration message provided")
}
any, ok := cfg.(*anypb.Any)
if !ok {
return nil, fmt.Errorf("rls_csp: error parsing config %v: unknown type %T", cfg, cfg)
}
rlcs := new(RouteLookupClusterSpecifier)

if err := ptypes.UnmarshalAny(any, rlcs); err != nil {
return nil, fmt.Errorf("rls_csp: error parsing config %v: %v", cfg, err)
}
rlcJSON, err := json.Marshal(rlcs.GetRouteLookupConfig())
if err != nil {
return nil, fmt.Errorf("rls_csp: error marshaling config: %v: %v", rlcs.GetRouteLookupConfig(), err)
}

lbCfgJSON := &lbConfigJSON{
RouteLookupConfig: rlcJSON,
ChildPolicy: []map[string]json.RawMessage{
{
"cds_experimental": {},
},
},
ChildPolicyConfigTargetFieldName: "cluster",
}

rawJSON, err := json.Marshal(lbCfgJSON)
if err != nil {
return nil, fmt.Errorf("rls_csp: error marshaling config %v: %v", lbCfgJSON, err)
}

// _, err := rls.ParseConfig(rawJSON) (will this function need to change its logic at all?)
// if err != nil {
// return nil, fmt.Errorf("error: %v", err)
// }

cfgRet := clusterspecifier.BalancerConfig{}

err = json.Unmarshal(rawJSON, cfgRet)
if err != nil {
return nil, fmt.Errorf("error: %v", err)
}

return cfgRet, nil
}