istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/compare/comparator.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 "encoding/json" 19 "fmt" 20 "io" 21 "strings" 22 23 admin "github.com/envoyproxy/go-control-plane/envoy/admin/v3" 24 discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" 25 legacyproto "github.com/golang/protobuf/proto" // nolint: staticcheck 26 exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" 27 "google.golang.org/protobuf/reflect/protoreflect" 28 "google.golang.org/protobuf/reflect/protoregistry" 29 "google.golang.org/protobuf/types/known/emptypb" 30 31 "istio.io/istio/istioctl/pkg/util/configdump" 32 ) 33 34 // Comparator diffs between a config dump from Istiod and one from Envoy 35 type Comparator struct { 36 envoy, istiod *configdump.Wrapper 37 w io.Writer 38 context int 39 location string 40 } 41 42 // NewComparator is a comparator constructor 43 func NewComparator(w io.Writer, istiodResponses map[string][]byte, envoyResponse []byte) (*Comparator, error) { 44 c := &Comparator{} 45 for _, resp := range istiodResponses { 46 istiodDump := &configdump.Wrapper{} 47 err := json.Unmarshal(resp, istiodDump) 48 if err != nil { 49 continue 50 } 51 c.istiod = istiodDump 52 break 53 } 54 if c.istiod == nil { 55 return nil, fmt.Errorf("unable to find config dump in Istiod responses") 56 } 57 envoyDump := &configdump.Wrapper{} 58 err := json.Unmarshal(envoyResponse, envoyDump) 59 if err != nil { 60 return nil, err 61 } 62 c.envoy = envoyDump 63 c.w = w 64 c.context = 7 65 c.location = "Local" // the time.Location for formatting time.Time instances 66 return c, nil 67 } 68 69 // NewXdsComparator is a comparator constructor 70 func NewXdsComparator(w io.Writer, istiodResponses map[string]*discovery.DiscoveryResponse, envoyResponse []byte) (*Comparator, error) { 71 c := &Comparator{} 72 for _, resp := range istiodResponses { 73 if len(resp.Resources) > 0 { 74 c.istiod = &configdump.Wrapper{ 75 ConfigDump: &admin.ConfigDump{ 76 Configs: resp.Resources, 77 }, 78 } 79 break 80 } 81 } 82 if c.istiod == nil { 83 return nil, fmt.Errorf("unable to find config dump in Istiod responses") 84 } 85 envoyDump := &configdump.Wrapper{} 86 err := json.Unmarshal(envoyResponse, envoyDump) 87 if err != nil { 88 return nil, err 89 } 90 c.envoy = envoyDump 91 c.w = w 92 c.context = 7 93 c.location = "Local" // the time.Location for formatting time.Time instances 94 return c, nil 95 } 96 97 // Diff prints a diff between Istiod and Envoy to the passed writer 98 func (c *Comparator) Diff() error { 99 if err := c.ClusterDiff(); err != nil { 100 return err 101 } 102 if err := c.ListenerDiff(); err != nil { 103 return err 104 } 105 return c.RouteDiff() 106 } 107 108 // nonstrictResolver is an AnyResolver that ignores unknown proto messages 109 type nonstrictResolver struct{} 110 111 var envoyResolver nonstrictResolver 112 113 func (m *nonstrictResolver) Resolve(typeURL string) (legacyproto.Message, error) { 114 // See https://github.com/golang/protobuf/issues/747#issuecomment-437463120 115 mname := typeURL 116 if slash := strings.LastIndex(typeURL, "/"); slash >= 0 { 117 mname = mname[slash+1:] 118 } 119 mt, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(mname)) 120 if err != nil { 121 // istioctl should keep going if it encounters new Envoy versions; ignore unknown types 122 return &exprpb.Type{TypeKind: &exprpb.Type_Dyn{Dyn: &emptypb.Empty{}}}, nil 123 } 124 return legacyproto.MessageV1(mt.New().Interface()), nil 125 }