github.com/fafucoder/cilium@v1.6.11/cilium/cmd/fqdn.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 "strings" 21 "text/tabwriter" 22 23 "github.com/spf13/cobra" 24 25 "github.com/cilium/cilium/api/v1/client/policy" 26 "github.com/cilium/cilium/api/v1/models" 27 "github.com/cilium/cilium/pkg/command" 28 ) 29 30 var fqdnCmd = &cobra.Command{ 31 Use: "fqdn", 32 Short: "Manage fqdn proxy", 33 Run: func(cmd *cobra.Command, args []string) { 34 cmd.Help() 35 }, 36 } 37 38 var fqdnCacheCmd = &cobra.Command{ 39 Use: "cache", 40 Short: "Manage fqdn proxy cache", 41 Run: func(cmd *cobra.Command, args []string) { 42 cmd.Help() 43 }, 44 } 45 46 var fqdnCleanCacheCmd = &cobra.Command{ 47 Use: "clean", 48 Short: "Clean fqdn cache", 49 Run: func(cmd *cobra.Command, args []string) { 50 cleanFQDNCache() 51 }, 52 } 53 54 var fqdnListCacheCmd = &cobra.Command{ 55 Use: "list", 56 Short: "List fqdn cache contents", 57 Run: func(cmd *cobra.Command, args []string) { 58 listFQDNCache() 59 }, 60 } 61 62 var fqdnCacheMatchPattern string 63 64 func init() { 65 fqdnCacheCmd.AddCommand(fqdnListCacheCmd) 66 fqdnCacheCmd.AddCommand(fqdnCleanCacheCmd) 67 fqdnCmd.AddCommand(fqdnCacheCmd) 68 rootCmd.AddCommand(fqdnCmd) 69 70 fqdnCleanCacheCmd.Flags().BoolVarP(&force, "force", "f", false, "Skip confirmation") 71 fqdnCleanCacheCmd.Flags().StringVarP(&fqdnCacheMatchPattern, "matchpattern", "p", "", "Delete cache entries with FQDNs that match matchpattern") 72 73 fqdnListCacheCmd.Flags().StringVarP(&fqdnCacheMatchPattern, "matchpattern", "p", "", "List cache entries with FQDN that match matchpattern") 74 command.AddJSONOutput(fqdnListCacheCmd) 75 } 76 77 func cleanFQDNCache() { 78 if !force { 79 fmt.Println("Following cache entries are going to be deleted:") 80 listFQDNCache() 81 if !confirmCleanup() { 82 return 83 } 84 } 85 86 params := policy.NewDeleteFqdnCacheParams() 87 88 if fqdnCacheMatchPattern != "" { 89 params.SetMatchpattern(&fqdnCacheMatchPattern) 90 } 91 92 _, err := client.Policy.DeleteFqdnCache(params) 93 if err != nil { 94 Fatalf("Error: %s\n", err) 95 } 96 fmt.Println("FQDN proxy cache cleared") 97 } 98 99 func listFQDNCache() { 100 params := policy.NewGetFqdnCacheParams() 101 102 if fqdnCacheMatchPattern != "" { 103 params.SetMatchpattern(&fqdnCacheMatchPattern) 104 } 105 106 var lookups []*models.DNSLookup = []*models.DNSLookup{} 107 108 result, err := client.Policy.GetFqdnCache(params) 109 if err != nil { 110 switch err := err.(type) { 111 case *policy.GetFqdnCacheNotFound: 112 // print out empty lookups slice 113 default: 114 Fatalf("Error: %s\n", err) 115 } 116 } else { 117 lookups = result.Payload 118 } 119 120 if command.OutputJSON() { 121 if err := command.PrintOutput(lookups); err != nil { 122 Fatalf("Unable to provide JSON output: %s", err) 123 } 124 return 125 } 126 127 w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0) 128 fmt.Fprintln(w, "Endpoint\tFQDN\tTTL\tExpirationTime\tIPs\t") 129 for _, lookup := range lookups { 130 fmt.Fprintf(w, "%d\t%s\t%d\t%s\t%s\t\n", 131 lookup.EndpointID, 132 lookup.Fqdn, 133 lookup.TTL, 134 lookup.ExpirationTime.String(), 135 strings.Join(lookup.Ips, ",")) 136 } 137 w.Flush() 138 }