istio
71 строка · 2.0 Кб
1// Copyright 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
15package app
16
17import (
18"fmt"
19"net/http"
20"time"
21
22"github.com/spf13/cobra"
23
24"istio.io/istio/pilot/pkg/request"
25"istio.io/istio/pkg/util/sets"
26)
27
28var (
29debugRequestPort int32 = 15000
30allowedPorts = sets.New[int32](
3115000,
3215021,
3315020,
3415004,
35)
36)
37
38// NB: extra standard output in addition to what's returned from envoy
39// must not be added in this command. Otherwise, it'd break istioctl proxy-config,
40// which interprets the output literally as json document.
41var (
42requestCmd = &cobra.Command{
43Use: "request <method> <path> [<body>]",
44Short: "Makes an HTTP request to the Envoy admin API",
45Args: cobra.MinimumNArgs(2),
46PreRunE: func(cmd *cobra.Command, args []string) error {
47if !allowedPorts.Contains(debugRequestPort) {
48return fmt.Errorf("debug port %d is not in allowed list %v", debugRequestPort, sets.SortedList(allowedPorts))
49}
50return nil
51},
52RunE: func(c *cobra.Command, args []string) error {
53command := &request.Command{
54Address: fmt.Sprintf("localhost:%d", debugRequestPort),
55Client: &http.Client{
56Timeout: 60 * time.Second,
57},
58}
59body := ""
60if len(args) >= 3 {
61body = args[2]
62}
63return command.Do(args[0], args[1], body)
64},
65}
66)
67
68func init() {
69requestCmd.PersistentFlags().Int32Var(&debugRequestPort, "debug-port", debugRequestPort,
70"Set the port to make a local request to. The default points to the Envoy admin API.")
71}
72