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

     1  // Copyright 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  	"text/tabwriter"
    21  
    22  	"github.com/cilium/cilium/pkg/command"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  // policyGetCmd represents the policy_get command
    27  var policyCacheGetCmd = &cobra.Command{
    28  	Use:   "selectors",
    29  	Short: "Display cached information about selectors",
    30  	Run: func(cmd *cobra.Command, args []string) {
    31  		if resp, err := client.PolicyCacheGet(); err != nil {
    32  			Fatalf("Cannot get policy: %s\n", err)
    33  		} else if command.OutputJSON() {
    34  			if err := command.PrintOutput(resp); err != nil {
    35  				os.Exit(1)
    36  			}
    37  		} else if resp != nil {
    38  			w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)
    39  
    40  			fmt.Fprintf(w, "SELECTOR\tUSERS\tIDENTITIES\n")
    41  			for _, mapping := range resp {
    42  				first := true
    43  				fmt.Fprintf(w, "%s", mapping.Selector)
    44  				fmt.Fprintf(w, "\t%d", mapping.Users)
    45  				if len(mapping.Identities) == 0 {
    46  					fmt.Fprintf(w, "\t\n")
    47  				}
    48  				for _, idty := range mapping.Identities {
    49  					if first {
    50  						fmt.Fprintf(w, "\t%d\t\n", idty)
    51  						first = false
    52  					} else {
    53  						fmt.Fprintf(w, "\t\t%d\t\n", idty)
    54  					}
    55  				}
    56  			}
    57  
    58  			w.Flush()
    59  		}
    60  	},
    61  }
    62  
    63  func init() {
    64  	policyCmd.AddCommand(policyCacheGetCmd)
    65  	command.AddJSONOutput(policyCacheGetCmd)
    66  }