From 37f66ed43167ae05b9889866f029bb5f33c92403 Mon Sep 17 00:00:00 2001 From: Sam Naser Date: Thu, 22 Aug 2019 10:44:28 -0700 Subject: [PATCH] Create istioctl config_dump helper to avoid use of explicit index (#16454) * Create istioctl config_dump helper to avoid use of explicit index into config dump * Rename helper, use enum to represent config dump section TypeURLs --- istioctl/pkg/util/configdump/bootstrap.go | 12 ++---- istioctl/pkg/util/configdump/cluster.go | 11 ++---- istioctl/pkg/util/configdump/listener.go | 11 ++---- istioctl/pkg/util/configdump/route.go | 16 ++------ istioctl/pkg/util/configdump/util.go | 46 +++++++++++++++++++++++ 5 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 istioctl/pkg/util/configdump/util.go diff --git a/istioctl/pkg/util/configdump/bootstrap.go b/istioctl/pkg/util/configdump/bootstrap.go index ba9cfec4cc5d..e1e6c75d06e2 100644 --- a/istioctl/pkg/util/configdump/bootstrap.go +++ b/istioctl/pkg/util/configdump/bootstrap.go @@ -15,22 +15,18 @@ package configdump import ( - "fmt" - adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" proto "github.com/gogo/protobuf/types" ) // GetBootstrapConfigDump retrieves the bootstrap config dump from the ConfigDump func (w *Wrapper) GetBootstrapConfigDump() (*adminapi.BootstrapConfigDump, error) { - // The bootstrap dump is the first one in the list. - // See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto - if len(w.Configs) < 1 { - return nil, fmt.Errorf("config dump has no bootstrap dump") + bootstrapDumpAny, err := w.getSection(bootstrap) + if err != nil { + return nil, err } - bootstrapDumpAny := w.Configs[0] bootstrapDump := &adminapi.BootstrapConfigDump{} - err := proto.UnmarshalAny(bootstrapDumpAny, bootstrapDump) + err = proto.UnmarshalAny(&bootstrapDumpAny, bootstrapDump) if err != nil { return nil, err } diff --git a/istioctl/pkg/util/configdump/cluster.go b/istioctl/pkg/util/configdump/cluster.go index 13a7cdaf64eb..85b7bcc5961a 100644 --- a/istioctl/pkg/util/configdump/cluster.go +++ b/istioctl/pkg/util/configdump/cluster.go @@ -15,7 +15,6 @@ package configdump import ( - "fmt" "sort" adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" @@ -43,14 +42,12 @@ func (w *Wrapper) GetDynamicClusterDump(stripVersions bool) (*adminapi.ClustersC // GetClusterConfigDump retrieves the cluster config dump from the ConfigDump func (w *Wrapper) GetClusterConfigDump() (*adminapi.ClustersConfigDump, error) { - // The cluster dump is the second one in the list. - // See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto - if len(w.Configs) < 2 { - return nil, fmt.Errorf("config dump has no cluster dump") + clusterDumpAny, err := w.getSection(clusters) + if err != nil { + return nil, err } - clusterDumpAny := w.Configs[1] clusterDump := &adminapi.ClustersConfigDump{} - err := proto.UnmarshalAny(clusterDumpAny, clusterDump) + err = proto.UnmarshalAny(&clusterDumpAny, clusterDump) if err != nil { return nil, err } diff --git a/istioctl/pkg/util/configdump/listener.go b/istioctl/pkg/util/configdump/listener.go index 8ed7537e77d4..de7b346a8be2 100644 --- a/istioctl/pkg/util/configdump/listener.go +++ b/istioctl/pkg/util/configdump/listener.go @@ -15,7 +15,6 @@ package configdump import ( - "fmt" "sort" adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha" @@ -43,14 +42,12 @@ func (w *Wrapper) GetDynamicListenerDump(stripVersions bool) (*adminapi.Listener // GetListenerConfigDump retrieves the listener config dump from the ConfigDump func (w *Wrapper) GetListenerConfigDump() (*adminapi.ListenersConfigDump, error) { - // The listener dump is the third one in the list. - // See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto - if len(w.Configs) < 3 { - return nil, fmt.Errorf("config dump has no listener dump") + listenerDumpAny, err := w.getSection(listeners) + if err != nil { + return nil, err } - listenerDumpAny := w.Configs[2] listenerDump := &adminapi.ListenersConfigDump{} - err := proto.UnmarshalAny(listenerDumpAny, listenerDump) + err = proto.UnmarshalAny(&listenerDumpAny, listenerDump) if err != nil { return nil, err } diff --git a/istioctl/pkg/util/configdump/route.go b/istioctl/pkg/util/configdump/route.go index f8efecfcc078..6c92ccf2cee8 100644 --- a/istioctl/pkg/util/configdump/route.go +++ b/istioctl/pkg/util/configdump/route.go @@ -15,7 +15,6 @@ package configdump import ( - "fmt" "sort" "time" @@ -69,19 +68,12 @@ func (w *Wrapper) GetDynamicRouteDump(stripVersions bool) (*adminapi.RoutesConfi // GetRouteConfigDump retrieves the route config dump from the ConfigDump func (w *Wrapper) GetRouteConfigDump() (*adminapi.RoutesConfigDump, error) { - // The route dump is no longer the 4th one; - // now envoy.admin.v2alpha.ScopedRoutesConfigDump may be 4th. - var routeDumpAny proto.Any - for _, conf := range w.Configs { - if conf.TypeUrl == "type.googleapis.com/envoy.admin.v2alpha.RoutesConfigDump" { - routeDumpAny = *conf - } - } - if routeDumpAny.TypeUrl == "" { - return nil, fmt.Errorf("config dump has no route dump") + routeDumpAny, err := w.getSection(routes) + if err != nil { + return nil, err } routeDump := &adminapi.RoutesConfigDump{} - err := proto.UnmarshalAny(&routeDumpAny, routeDump) + err = proto.UnmarshalAny(&routeDumpAny, routeDump) if err != nil { return nil, err } diff --git a/istioctl/pkg/util/configdump/util.go b/istioctl/pkg/util/configdump/util.go new file mode 100644 index 000000000000..b2ed9c639846 --- /dev/null +++ b/istioctl/pkg/util/configdump/util.go @@ -0,0 +1,46 @@ +// Copyright 2018 Istio 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 configdump + +import ( + "fmt" + + proto "github.com/gogo/protobuf/types" +) + +type configTypeURL string + +// See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto +const ( + bootstrap configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.BootstrapConfigDump" + listeners configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.ListenersConfigDump" + clusters configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.ClustersConfigDump" + routes configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.RoutesConfigDump" +) + +// getSection takes a TypeURL and returns the types.Any from the config dump corresponding to that URL +func (w *Wrapper) getSection(sectionTypeURL configTypeURL) (proto.Any, error) { + var dumpAny proto.Any + for _, conf := range w.Configs { + if conf.TypeUrl == string(sectionTypeURL) { + dumpAny = *conf + } + } + if dumpAny.TypeUrl == "" { + return proto.Any{}, fmt.Errorf("config dump has no route %s", sectionTypeURL) + } + + return dumpAny, nil +}