github.com/fafucoder/cilium@v1.6.11/cilium/cmd/endpoint_config.go (about)

     1  // Copyright 2017-2019 Authors of Cilium
     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 cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/cilium/cilium/pkg/command"
    22  	endpointid "github.com/cilium/cilium/pkg/endpoint/id"
    23  	"github.com/cilium/cilium/pkg/option"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var listOptions bool
    29  
    30  // endpointConfigCmd represents the endpoint_config command
    31  var endpointConfigCmd = &cobra.Command{
    32  	Use:     "config <endpoint id> [<option>=(enable|disable) ...]",
    33  	Short:   "View & modify endpoint configuration",
    34  	Example: "endpoint config 5421 DropNotification=false TraceNotification=false",
    35  	Run: func(cmd *cobra.Command, args []string) {
    36  		if listOptions {
    37  			listEndpointOptions()
    38  			return
    39  		}
    40  
    41  		requireEndpointID(cmd, args)
    42  		configEndpoint(cmd, args)
    43  	},
    44  }
    45  
    46  func init() {
    47  	endpointCmd.AddCommand(endpointConfigCmd)
    48  	endpointConfigCmd.Flags().BoolVarP(&listOptions, "list-options", "", false, "List available options")
    49  	command.AddJSONOutput(endpointConfigCmd)
    50  }
    51  
    52  var endpointMutableOptionLibrary = option.GetEndpointMutableOptionLibrary()
    53  
    54  func listEndpointOptions() {
    55  	for k, s := range endpointMutableOptionLibrary {
    56  		fmt.Printf("%-24s %s\n", k, s.Description)
    57  	}
    58  }
    59  
    60  func configEndpoint(cmd *cobra.Command, args []string) {
    61  	_, id, _ := endpointid.Parse(args[0])
    62  	cfg, err := client.EndpointConfigGet(id)
    63  	if err != nil {
    64  		Fatalf("Cannot get configuration of endpoint %s: %s\n", id, err)
    65  	}
    66  
    67  	opts := args[1:]
    68  	if len(opts) == 0 {
    69  		if command.OutputJSON() {
    70  			if err := command.PrintOutput(cfg); err != nil {
    71  				os.Exit(1)
    72  			}
    73  			return
    74  		}
    75  
    76  		dumpConfig(cfg.Immutable)
    77  		dumpConfig(cfg.Realized.Options)
    78  		return
    79  	}
    80  
    81  	// modify the configuration we fetched directly since we don't need it
    82  	modifiedOptsCfg := cfg.Realized
    83  	for k := range opts {
    84  		name, value, err := option.ParseOption(opts[k], &endpointMutableOptionLibrary)
    85  		if err != nil {
    86  			Fatalf("Cannot parse option %s: %s", opts[k], err)
    87  		}
    88  		modifiedOptsCfg.Options[name] = fmt.Sprintf("%d", value)
    89  	}
    90  
    91  	err = client.EndpointConfigPatch(id, modifiedOptsCfg)
    92  	if err != nil {
    93  		Fatalf("Cannot update endpoint %s: %s", id, err)
    94  	}
    95  
    96  	fmt.Printf("Endpoint %s configuration updated successfully\n", id)
    97  }