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

     1  // Copyright 2017-2018 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  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/cilium/cilium/api/v1/models"
    24  	"github.com/cilium/cilium/pkg/command"
    25  	"github.com/cilium/cilium/pkg/option"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var numPages int
    31  
    32  // configCmd represents the config command
    33  var configCmd = &cobra.Command{
    34  	Use:   "config [<option>=(enable|disable) ...]",
    35  	Short: "Cilium configuration options",
    36  	Run: func(cmd *cobra.Command, args []string) {
    37  		if listOptions {
    38  			for k, s := range option.DaemonMutableOptionLibrary {
    39  				fmt.Printf("%-24s %s\n", k, s.Description)
    40  			}
    41  			return
    42  		}
    43  
    44  		configDaemon(cmd, args)
    45  	},
    46  }
    47  
    48  func init() {
    49  	rootCmd.AddCommand(configCmd)
    50  	configCmd.Flags().BoolVarP(&listOptions, "list-options", "", false, "List available options")
    51  	configCmd.Flags().IntVarP(&numPages, "num-pages", "n", 0, "Number of pages for perf ring buffer. New values have to be > 0")
    52  	command.AddJSONOutput(configCmd)
    53  }
    54  
    55  func configDaemon(cmd *cobra.Command, opts []string) {
    56  	dOpts := make(models.ConfigurationMap, len(opts))
    57  
    58  	resp, err := client.ConfigGet()
    59  	if err != nil {
    60  		Fatalf("Error while retrieving configuration: %s", err)
    61  	}
    62  	if resp.Status == nil {
    63  		Fatalf("Empty configuration status returned")
    64  	}
    65  
    66  	cfgStatus := resp.Status
    67  	if numPages > 0 {
    68  		if cfgStatus.NodeMonitor != nil && numPages != int(cfgStatus.NodeMonitor.Npages) {
    69  			dOpts["MonitorNumPages"] = strconv.Itoa(numPages)
    70  		}
    71  	} else if len(opts) == 0 {
    72  		if command.OutputJSON() {
    73  			if err := command.PrintOutput(cfgStatus.Realized.Options); err != nil {
    74  				os.Exit(1)
    75  			}
    76  			return
    77  		}
    78  		dumpConfig(cfgStatus.Immutable)
    79  		dumpConfig(cfgStatus.Realized.Options)
    80  		fmt.Printf("%-24s %s\n", "k8s-configuration", cfgStatus.K8sConfiguration)
    81  		fmt.Printf("%-24s %s\n", "k8s-endpoint", cfgStatus.K8sEndpoint)
    82  		fmt.Printf("%-24s %s\n", "PolicyEnforcement", cfgStatus.Realized.PolicyEnforcement)
    83  		if cfgStatus.NodeMonitor != nil {
    84  			fmt.Printf("%-24s %d\n", "MonitorNumPages", cfgStatus.NodeMonitor.Npages)
    85  		}
    86  		return
    87  	}
    88  
    89  	var cfg models.DaemonConfigurationSpec
    90  
    91  	for k := range opts {
    92  
    93  		// TODO FIXME - this is a hack, and is not clean
    94  		optionSplit := strings.SplitN(opts[k], "=", 2)
    95  		if len(optionSplit) < 2 {
    96  			Fatalf("Improper configuration format provided")
    97  		}
    98  		arg := optionSplit[0]
    99  		if arg == "PolicyEnforcement" {
   100  			cfg.PolicyEnforcement = optionSplit[1]
   101  			continue
   102  		}
   103  
   104  		name, value, err := option.ParseDaemonOption(opts[k])
   105  		if err != nil {
   106  			fmt.Printf("%s\n", err)
   107  			os.Exit(1)
   108  		}
   109  
   110  		if opt, ok := option.DaemonMutableOptionLibrary[name]; !ok || opt.Parse == nil {
   111  			if value == option.OptionDisabled {
   112  				dOpts[name] = "Disabled"
   113  			} else {
   114  				dOpts[name] = "Enabled"
   115  			}
   116  		} else {
   117  			dOpts[name] = optionSplit[1]
   118  		}
   119  	}
   120  
   121  	cfg.Options = dOpts
   122  	if err := client.ConfigPatch(cfg); err != nil {
   123  		Fatalf("Unable to change agent configuration: %s\n", err)
   124  	}
   125  }