Skip to content

Commit

Permalink
Create istioctl config_dump helper to avoid use of explicit index (is…
Browse files Browse the repository at this point in the history
…tio#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
  • Loading branch information
Sam Naser authored and istio-testing committed Aug 22, 2019
1 parent 92d8d8a commit 37f66ed
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
12 changes: 4 additions & 8 deletions istioctl/pkg/util/configdump/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 4 additions & 7 deletions istioctl/pkg/util/configdump/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package configdump

import (
"fmt"
"sort"

adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 4 additions & 7 deletions istioctl/pkg/util/configdump/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package configdump

import (
"fmt"
"sort"

adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 4 additions & 12 deletions istioctl/pkg/util/configdump/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package configdump

import (
"fmt"
"sort"
"time"

Expand Down Expand Up @@ -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
}
Expand Down
46 changes: 46 additions & 0 deletions istioctl/pkg/util/configdump/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 37f66ed

Please sign in to comment.