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

Transition to use golang/protobuf to support automated code generation #226

Merged
merged 10 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
small code re-org to fit best practices
Signed-off-by: Kuat Yessenov <kuat@google.com>
  • Loading branch information
kyessenov committed Sep 9, 2019
commit 7fdb3a382dc19f702179adc36746f4c62692be29
9 changes: 9 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ type Response struct {
// Resources to be included in the response.
Resources []Resource
}

// SkipFetchError is the error returned when the cache fetch is short
// circuited due to the client's version already being up-to-date.
type SkipFetchError struct{}

// Error satisfies the error interface
func (e SkipFetchError) Error() string {
return "skip fetch: version up to date"
}
7 changes: 4 additions & 3 deletions pkg/cache/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
v2 "github.com/envoyproxy/go-control-plane/v2/envoy/api/v2"
auth "github.com/envoyproxy/go-control-plane/v2/envoy/api/v2/auth"
hcm "github.com/envoyproxy/go-control-plane/v2/envoy/config/filter/network/http_connection_manager/v2"
"github.com/envoyproxy/go-control-plane/v2/pkg/util"
"github.com/envoyproxy/go-control-plane/v2/pkg/conversion"
"github.com/envoyproxy/go-control-plane/v2/pkg/wellknown"
)

// Resource is the base interface for the xDS payload.
Expand Down Expand Up @@ -102,7 +103,7 @@ func GetResourceReferences(resources map[string]Resource) map[string]bool {
// extract route configuration names from HTTP connection manager
for _, chain := range v.FilterChains {
for _, filter := range chain.Filters {
if filter.Name != util.HTTPConnectionManager {
if filter.Name != wellknown.HTTPConnectionManager {
continue
}

Expand All @@ -112,7 +113,7 @@ func GetResourceReferences(resources map[string]Resource) map[string]bool {
if typedConfig := filter.GetTypedConfig(); typedConfig != nil {
ptypes.UnmarshalAny(typedConfig, config)
} else {
util.StructToMessage(filter.GetConfig(), config)
conversion.StructToMessage(filter.GetConfig(), config)
}

if config == nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/cache/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/envoyproxy/go-control-plane/v2/pkg/log"
"github.com/envoyproxy/go-control-plane/v2/pkg/util"
)

// SnapshotCache is a snapshot-based cache that maintains a single versioned
Expand Down Expand Up @@ -286,7 +285,7 @@ func (cache *snapshotCache) Fetch(ctx context.Context, request Request) (*Respon
// It might be beneficial to hold the request since Envoy will re-attempt the refresh.
version := snapshot.GetVersion(request.TypeUrl)
if request.VersionInfo == version {
return nil, &util.SkipFetchError{}
return nil, &SkipFetchError{}
}

resources := snapshot.GetResources(request.TypeUrl)
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/struct.go → pkg/conversion/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package util contains shared utility functions.
package util
// Package conversion contains shared utility functions for converting xDS resources.
package conversion

import (
"bytes"
Expand Down
12 changes: 6 additions & 6 deletions pkg/util/struct_test.go → pkg/conversion/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util_test
package conversion_test

import (
"reflect"
Expand All @@ -22,15 +22,15 @@ import (

v2 "github.com/envoyproxy/go-control-plane/v2/envoy/api/v2"
core "github.com/envoyproxy/go-control-plane/v2/envoy/api/v2/core"
"github.com/envoyproxy/go-control-plane/v2/pkg/util"
"github.com/envoyproxy/go-control-plane/v2/pkg/conversion"
)

func TestConversion(t *testing.T) {
pb := &v2.DiscoveryRequest{
VersionInfo: "test",
Node: &core.Node{Id: "proxy"},
}
st, err := util.MessageToStruct(pb)
st, err := conversion.MessageToStruct(pb)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
Expand All @@ -47,19 +47,19 @@ func TestConversion(t *testing.T) {
}

out := &v2.DiscoveryRequest{}
err = util.StructToMessage(st, out)
err = conversion.StructToMessage(st, out)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !reflect.DeepEqual(pb, out) {
t.Errorf("StructToMessage(%v) => got %v, want %v", st, out, pb)
}

if _, err = util.MessageToStruct(nil); err == nil {
if _, err = conversion.MessageToStruct(nil); err == nil {
t.Error("MessageToStruct(nil) => got no error")
}

if err = util.StructToMessage(nil, &v2.DiscoveryRequest{}); err == nil {
if err = conversion.StructToMessage(nil, &v2.DiscoveryRequest{}); err == nil {
t.Error("StructToMessage(nil) => got no error")
}
}
3 changes: 1 addition & 2 deletions pkg/server/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
v2 "github.com/envoyproxy/go-control-plane/v2/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/v2/pkg/cache"
"github.com/envoyproxy/go-control-plane/v2/pkg/log"
"github.com/envoyproxy/go-control-plane/v2/pkg/util"
)

// HTTPGateway is a custom implementation of [gRPC gateway](https://github.com/grpc-ecosystem/grpc-gateway)
Expand Down Expand Up @@ -83,7 +82,7 @@ func (h *HTTPGateway) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if err != nil {
// SkipFetchErrors will return a 304 which will signify to the envoy client that
// it is already at the latest version; all other errors will 500 with a message.
if _, ok := err.(*util.SkipFetchError); ok {
if _, ok := err.(*cache.SkipFetchError); ok {
resp.WriteHeader(http.StatusNotModified)
} else {
http.Error(resp, "fetch error: "+err.Error(), http.StatusInternalServerError)
Expand Down
10 changes: 5 additions & 5 deletions pkg/test/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
hcm "github.com/envoyproxy/go-control-plane/v2/envoy/config/filter/network/http_connection_manager/v2"
tcp "github.com/envoyproxy/go-control-plane/v2/envoy/config/filter/network/tcp_proxy/v2"
"github.com/envoyproxy/go-control-plane/v2/pkg/cache"
"github.com/envoyproxy/go-control-plane/v2/pkg/util"
"github.com/envoyproxy/go-control-plane/v2/pkg/wellknown"
)

const (
Expand Down Expand Up @@ -219,10 +219,10 @@ func MakeHTTPListener(mode string, listenerName string, port uint32, route strin
},
},
HttpFilters: []*hcm.HttpFilter{{
Name: util.Router,
Name: wellknown.Router,
}},
AccessLog: []*alf.AccessLog{{
Name: util.HTTPGRPCAccessLog,
Name: wellknown.HTTPGRPCAccessLog,
ConfigType: &alf.AccessLog_TypedConfig{
TypedConfig: alsConfigPbst,
},
Expand All @@ -248,7 +248,7 @@ func MakeHTTPListener(mode string, listenerName string, port uint32, route strin
},
FilterChains: []*listener.FilterChain{{
Filters: []*listener.Filter{{
Name: util.HTTPConnectionManager,
Name: wellknown.HTTPConnectionManager,
ConfigType: &listener.Filter_TypedConfig{
TypedConfig: pbst,
},
Expand Down Expand Up @@ -285,7 +285,7 @@ func MakeTCPListener(listenerName string, port uint32, clusterName string) *v2.L
},
FilterChains: []*listener.FilterChain{{
Filters: []*listener.Filter{{
Name: util.TCPProxy,
Name: wellknown.TCPProxy,
ConfigType: &listener.Filter_TypedConfig{
TypedConfig: pbst,
},
Expand Down
12 changes: 2 additions & 10 deletions pkg/util/wellknown.go → pkg/wellknown/wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
// Package wellknown contains common names for filters, listeners, etc.
package wellknown

// HTTP filter names
const (
Expand Down Expand Up @@ -115,12 +116,3 @@ const (
// HTTPGRPCAccessLog sink for the HTTP gRPC access log service
HTTPGRPCAccessLog = "envoy.http_grpc_access_log"
)

// SkipFetchError is the error returned when the cache fetch is short
// circuited due to the client's version already being up-to-date.
type SkipFetchError struct{}

// Error satisfies the error interface
func (e SkipFetchError) Error() string {
return "skip fetch: version up to date"
}