istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/ztunnel/configdump/services.go (about) 1 // Copyright Istio Authors 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 configdump 16 17 import ( 18 "cmp" 19 "encoding/json" 20 "fmt" 21 "strings" 22 23 "sigs.k8s.io/yaml" 24 25 "istio.io/istio/pkg/maps" 26 "istio.io/istio/pkg/slices" 27 ) 28 29 // ServiceFilter is used to pass filter information into service based config writer print functions 30 type ServiceFilter struct { 31 Namespace string 32 } 33 34 // Verify returns true if the passed workload matches the filter fields 35 func (wf *ServiceFilter) Verify(svc *ZtunnelService) bool { 36 if wf.Namespace != "" { 37 if !strings.EqualFold(svc.Namespace, wf.Namespace) { 38 return false 39 } 40 } 41 42 return true 43 } 44 45 // PrintServiceSummary prints a summary of the relevant services in the config dump to the ConfigWriter stdout 46 func (c *ConfigWriter) PrintServiceSummary(filter ServiceFilter) error { 47 w := c.tabwriter() 48 zDump := c.ztunnelDump 49 50 svcs := slices.Filter(maps.Values(zDump.Services), filter.Verify) 51 slices.SortFunc(svcs, func(a, b *ZtunnelService) int { 52 if r := cmp.Compare(a.Namespace, b.Namespace); r != 0 { 53 return r 54 } 55 if r := cmp.Compare(a.Name, b.Name); r != 0 { 56 return r 57 } 58 return cmp.Compare(a.Hostname, b.Hostname) 59 }) 60 fmt.Fprintln(w, "NAMESPACE\tSERVICE NAME\tSERVICE VIP\tWAYPOINT") 61 62 for _, svc := range svcs { 63 var ip string 64 if len(svc.Addresses) > 0 { 65 _, ip, _ = strings.Cut(svc.Addresses[0], "/") 66 } 67 waypoint := serviceWaypointName(svc, zDump.Services) 68 fmt.Fprintf(w, "%v\t%v\t%v\t%v\n", 69 svc.Namespace, svc.Name, ip, waypoint) 70 } 71 return w.Flush() 72 } 73 74 // PrintServiceDump prints the relevant services in the config dump to the ConfigWriter stdout 75 func (c *ConfigWriter) PrintServiceDump(filter ServiceFilter, outputFormat string) error { 76 zDump := c.ztunnelDump 77 svcs := slices.Filter(maps.Values(zDump.Services), filter.Verify) 78 slices.SortFunc(svcs, func(a, b *ZtunnelService) int { 79 if r := cmp.Compare(a.Namespace, b.Namespace); r != 0 { 80 return r 81 } 82 if r := cmp.Compare(a.Name, b.Name); r != 0 { 83 return r 84 } 85 return cmp.Compare(a.Hostname, b.Hostname) 86 }) 87 out, err := json.MarshalIndent(svcs, "", " ") 88 if err != nil { 89 return fmt.Errorf("failed to marshal workloads: %v", err) 90 } 91 if outputFormat == "yaml" { 92 if out, err = yaml.JSONToYAML(out); err != nil { 93 return err 94 } 95 } 96 fmt.Fprintln(c.Stdout, string(out)) 97 return nil 98 }