istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/compare/cluster.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 21 "github.com/pmezard/go-difflib/difflib" 22 23 "istio.io/istio/pkg/util/protomarshal" 24 ) 25 26 // ClusterDiff prints a diff between Istiod and Envoy clusters to the passed writer 27 func (c *Comparator) ClusterDiff() error { 28 envoyBytes, istiodBytes := &bytes.Buffer{}, &bytes.Buffer{} 29 envoyClusterDump, err := c.envoy.GetDynamicClusterDump(true) 30 if err != nil { 31 envoyBytes.WriteString(err.Error()) 32 } else { 33 envoy, err := protomarshal.ToJSONWithAnyResolver(envoyClusterDump, " ", &envoyResolver) 34 if err != nil { 35 return err 36 } 37 envoyBytes.WriteString(envoy) 38 } 39 istiodClusterDump, err := c.istiod.GetDynamicClusterDump(true) 40 if err != nil { 41 istiodBytes.WriteString(err.Error()) 42 } else { 43 istiod, err := protomarshal.ToJSONWithAnyResolver(istiodClusterDump, " ", &envoyResolver) 44 if err != nil { 45 return err 46 } 47 istiodBytes.WriteString(istiod) 48 } 49 diff := difflib.UnifiedDiff{ 50 FromFile: "Istiod Clusters", 51 A: difflib.SplitLines(istiodBytes.String()), 52 ToFile: "Envoy Clusters", 53 B: difflib.SplitLines(envoyBytes.String()), 54 Context: c.context, 55 } 56 text, err := difflib.GetUnifiedDiffString(diff) 57 if err != nil { 58 return err 59 } 60 if text != "" { 61 fmt.Fprintln(c.w, "Clusters Don't Match") 62 fmt.Fprintln(c.w, text) 63 } else { 64 fmt.Fprintln(c.w, "Clusters Match") 65 } 66 return nil 67 }