istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/compare/route.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 compare 16 17 import ( 18 "bytes" 19 "fmt" 20 "time" 21 22 "github.com/pmezard/go-difflib/difflib" 23 24 "istio.io/istio/pkg/util/protomarshal" 25 ) 26 27 // RouteDiff prints a diff between Istiod and Envoy routes to the passed writer 28 func (c *Comparator) RouteDiff() error { 29 envoyBytes, istiodBytes := &bytes.Buffer{}, &bytes.Buffer{} 30 envoyRouteDump, err := c.envoy.GetDynamicRouteDump(true) 31 if err != nil { 32 envoyBytes.WriteString(err.Error()) 33 } else { 34 envoy, err := protomarshal.ToJSONWithAnyResolver(envoyRouteDump, " ", &envoyResolver) 35 if err != nil { 36 return err 37 } 38 envoyBytes.WriteString(envoy) 39 } 40 istiodRouteDump, err := c.istiod.GetDynamicRouteDump(true) 41 if err != nil { 42 istiodBytes.WriteString(err.Error()) 43 } else { 44 istiod, err := protomarshal.ToJSONWithAnyResolver(istiodRouteDump, " ", &envoyResolver) 45 if err != nil { 46 return err 47 } 48 istiodBytes.WriteString(istiod) 49 } 50 diff := difflib.UnifiedDiff{ 51 FromFile: "Istiod Routes", 52 A: difflib.SplitLines(istiodBytes.String()), 53 ToFile: "Envoy Routes", 54 B: difflib.SplitLines(envoyBytes.String()), 55 Context: c.context, 56 } 57 text, err := difflib.GetUnifiedDiffString(diff) 58 if err != nil { 59 return err 60 } 61 lastUpdatedStr := "" 62 if lastUpdated, err := c.envoy.GetLastUpdatedDynamicRouteTime(); err != nil { 63 return err 64 } else if lastUpdated != nil { 65 loc, err := time.LoadLocation(c.location) 66 if err != nil { 67 loc, _ = time.LoadLocation("UTC") 68 } 69 lastUpdatedStr = fmt.Sprintf(" (RDS last loaded at %s)", lastUpdated.In(loc).Format(time.RFC1123)) 70 } 71 if text != "" { 72 fmt.Fprintf(c.w, "Routes Don't Match%s\n", lastUpdatedStr) 73 fmt.Fprintln(c.w, text) 74 } else { 75 fmt.Fprintf(c.w, "Routes Match%s\n", lastUpdatedStr) 76 } 77 return nil 78 }