Skip to content

Commit

Permalink
cli: add -a flag to list all cilium configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Genani <h3llix.pvt@gmail.com>
  • Loading branch information
h3llix authored and aanm committed Oct 2, 2021
1 parent 8417d79 commit 85ae42e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions Documentation/cmdref/cilium_config.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cilium/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
var (
numPages int
listReadOnlyConfigurations bool
listAllConfigurations bool
)

// configCmd represents the config command
Expand All @@ -43,6 +44,7 @@ func init() {
rootCmd.AddCommand(configCmd)
configCmd.Flags().BoolVarP(&listOptions, "list-options", "", false, "List available options")
configCmd.Flags().BoolVarP(&listReadOnlyConfigurations, "read-only", "r", false, "Display read only configurations")
configCmd.Flags().BoolVarP(&listAllConfigurations, "all", "a", false, "Display all cilium configurations")
configCmd.Flags().IntVarP(&numPages, "num-pages", "n", 0, "Number of pages for perf ring buffer. New values have to be > 0")
command.AddJSONOutput(configCmd)
}
Expand Down Expand Up @@ -111,6 +113,10 @@ func printConfigurations(cfgStatus *models.DaemonConfigurationStatus) {
if err := command.PrintOutput(cfgStatus.DaemonConfigurationMap); err != nil {
Fatalf("Cannot show configuratons: %v", err)
}
} else if listAllConfigurations {
if err := command.PrintOutputWithPatch(cfgStatus.DaemonConfigurationMap, cfgStatus.Realized); err != nil {
Fatalf("Cannot show configuratons: %v", err)
}
} else {
if err := command.PrintOutput(cfgStatus.Realized.Options); err != nil {
Fatalf("Cannot show configuratons: %v", err)
Expand All @@ -120,6 +126,9 @@ func printConfigurations(cfgStatus *models.DaemonConfigurationStatus) {
}
if listReadOnlyConfigurations {
dumpReadOnlyConfigs(cfgStatus)
} else if listAllConfigurations {
dumpReadOnlyConfigs(cfgStatus)
dumpReadWriteConfigs(cfgStatus)
} else {
dumpReadWriteConfigs(cfgStatus)
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,61 @@ func PrintOutput(data interface{}) error {
return PrintOutputWithType(data, outputOpt)
}

//PrintOutputWithPatch merges data with patch and dump the data using the --output flag.
func PrintOutputWithPatch(data interface{}, patch interface{}) error {
mergedInterface, err := mergeInterfaces(data, patch)
if err != nil {
return fmt.Errorf("Unable to merge Interfaces:%v", err)
}
return PrintOutputWithType(mergedInterface, outputOpt)
}

func mergeInterfaces(data, patch interface{}) (interface{}, error) {
var i1, i2 interface{}

data1, err := json.Marshal(data)
if err != nil {
return nil, err
}
data2, err := json.Marshal(patch)
if err != nil {
return nil, err
}
err = json.Unmarshal(data1, &i1)
if err != nil {
return nil, err
}

err = json.Unmarshal(data2, &i2)
if err != nil {
return nil, err
}
return recursiveMerge(i1, i2), nil
}

func recursiveMerge(i1, i2 interface{}) interface{} {
switch i1 := i1.(type) {
case map[string]interface{}:
i2, ok := i2.(map[string]interface{})
if !ok {
return i1
}
for k, v2 := range i2 {
if v1, ok := i1[k]; ok {
i1[k] = recursiveMerge(v1, v2)
} else {
i1[k] = v2
}
}
case nil:
i2, ok := i2.(map[string]interface{})
if ok {
return i2
}
}
return i1
}

//PrintOutputWithType receives an interface and dump the data using the --output flag.
//ATM only json or jsonpath. In the future yaml
func PrintOutputWithType(data interface{}, outputType string) error {
Expand Down

0 comments on commit 85ae42e

Please sign in to comment.