Skip to content

Commit 37f66ed

Browse files
Sam Naseristio-testing
authored andcommitted
Create istioctl config_dump helper to avoid use of explicit index (istio#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
1 parent 92d8d8a commit 37f66ed

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed

istioctl/pkg/util/configdump/bootstrap.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,18 @@
1515
package configdump
1616

1717
import (
18-
"fmt"
19-
2018
adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
2119
proto "github.com/gogo/protobuf/types"
2220
)
2321

2422
// GetBootstrapConfigDump retrieves the bootstrap config dump from the ConfigDump
2523
func (w *Wrapper) GetBootstrapConfigDump() (*adminapi.BootstrapConfigDump, error) {
26-
// The bootstrap dump is the first one in the list.
27-
// See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto
28-
if len(w.Configs) < 1 {
29-
return nil, fmt.Errorf("config dump has no bootstrap dump")
24+
bootstrapDumpAny, err := w.getSection(bootstrap)
25+
if err != nil {
26+
return nil, err
3027
}
31-
bootstrapDumpAny := w.Configs[0]
3228
bootstrapDump := &adminapi.BootstrapConfigDump{}
33-
err := proto.UnmarshalAny(bootstrapDumpAny, bootstrapDump)
29+
err = proto.UnmarshalAny(&bootstrapDumpAny, bootstrapDump)
3430
if err != nil {
3531
return nil, err
3632
}

istioctl/pkg/util/configdump/cluster.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package configdump
1616

1717
import (
18-
"fmt"
1918
"sort"
2019

2120
adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
@@ -43,14 +42,12 @@ func (w *Wrapper) GetDynamicClusterDump(stripVersions bool) (*adminapi.ClustersC
4342

4443
// GetClusterConfigDump retrieves the cluster config dump from the ConfigDump
4544
func (w *Wrapper) GetClusterConfigDump() (*adminapi.ClustersConfigDump, error) {
46-
// The cluster dump is the second one in the list.
47-
// See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto
48-
if len(w.Configs) < 2 {
49-
return nil, fmt.Errorf("config dump has no cluster dump")
45+
clusterDumpAny, err := w.getSection(clusters)
46+
if err != nil {
47+
return nil, err
5048
}
51-
clusterDumpAny := w.Configs[1]
5249
clusterDump := &adminapi.ClustersConfigDump{}
53-
err := proto.UnmarshalAny(clusterDumpAny, clusterDump)
50+
err = proto.UnmarshalAny(&clusterDumpAny, clusterDump)
5451
if err != nil {
5552
return nil, err
5653
}

istioctl/pkg/util/configdump/listener.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package configdump
1616

1717
import (
18-
"fmt"
1918
"sort"
2019

2120
adminapi "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
@@ -43,14 +42,12 @@ func (w *Wrapper) GetDynamicListenerDump(stripVersions bool) (*adminapi.Listener
4342

4443
// GetListenerConfigDump retrieves the listener config dump from the ConfigDump
4544
func (w *Wrapper) GetListenerConfigDump() (*adminapi.ListenersConfigDump, error) {
46-
// The listener dump is the third one in the list.
47-
// See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto
48-
if len(w.Configs) < 3 {
49-
return nil, fmt.Errorf("config dump has no listener dump")
45+
listenerDumpAny, err := w.getSection(listeners)
46+
if err != nil {
47+
return nil, err
5048
}
51-
listenerDumpAny := w.Configs[2]
5249
listenerDump := &adminapi.ListenersConfigDump{}
53-
err := proto.UnmarshalAny(listenerDumpAny, listenerDump)
50+
err = proto.UnmarshalAny(&listenerDumpAny, listenerDump)
5451
if err != nil {
5552
return nil, err
5653
}

istioctl/pkg/util/configdump/route.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package configdump
1616

1717
import (
18-
"fmt"
1918
"sort"
2019
"time"
2120

@@ -69,19 +68,12 @@ func (w *Wrapper) GetDynamicRouteDump(stripVersions bool) (*adminapi.RoutesConfi
6968

7069
// GetRouteConfigDump retrieves the route config dump from the ConfigDump
7170
func (w *Wrapper) GetRouteConfigDump() (*adminapi.RoutesConfigDump, error) {
72-
// The route dump is no longer the 4th one;
73-
// now envoy.admin.v2alpha.ScopedRoutesConfigDump may be 4th.
74-
var routeDumpAny proto.Any
75-
for _, conf := range w.Configs {
76-
if conf.TypeUrl == "type.googleapis.com/envoy.admin.v2alpha.RoutesConfigDump" {
77-
routeDumpAny = *conf
78-
}
79-
}
80-
if routeDumpAny.TypeUrl == "" {
81-
return nil, fmt.Errorf("config dump has no route dump")
71+
routeDumpAny, err := w.getSection(routes)
72+
if err != nil {
73+
return nil, err
8274
}
8375
routeDump := &adminapi.RoutesConfigDump{}
84-
err := proto.UnmarshalAny(&routeDumpAny, routeDump)
76+
err = proto.UnmarshalAny(&routeDumpAny, routeDump)
8577
if err != nil {
8678
return nil, err
8779
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2018 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package configdump
16+
17+
import (
18+
"fmt"
19+
20+
proto "github.com/gogo/protobuf/types"
21+
)
22+
23+
type configTypeURL string
24+
25+
// See https://www.envoyproxy.io/docs/envoy/latest/api-v2/admin/v2alpha/config_dump.proto
26+
const (
27+
bootstrap configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.BootstrapConfigDump"
28+
listeners configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.ListenersConfigDump"
29+
clusters configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.ClustersConfigDump"
30+
routes configTypeURL = "type.googleapis.com/envoy.admin.v2alpha.RoutesConfigDump"
31+
)
32+
33+
// getSection takes a TypeURL and returns the types.Any from the config dump corresponding to that URL
34+
func (w *Wrapper) getSection(sectionTypeURL configTypeURL) (proto.Any, error) {
35+
var dumpAny proto.Any
36+
for _, conf := range w.Configs {
37+
if conf.TypeUrl == string(sectionTypeURL) {
38+
dumpAny = *conf
39+
}
40+
}
41+
if dumpAny.TypeUrl == "" {
42+
return proto.Any{}, fmt.Errorf("config dump has no route %s", sectionTypeURL)
43+
}
44+
45+
return dumpAny, nil
46+
}

0 commit comments

Comments
 (0)