istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/legacy/source/kube/origin.go (about) 1 /* 2 Copyright Istio Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package kube 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "strings" 23 24 "istio.io/istio/pkg/cluster" 25 "istio.io/istio/pkg/config" 26 "istio.io/istio/pkg/config/resource" 27 "istio.io/istio/pkg/config/schema/gvk" 28 ) 29 30 // Origin is a K8s specific implementation of resource.Origin 31 type Origin struct { 32 Type config.GroupVersionKind 33 FullName resource.FullName 34 ResourceVersion resource.Version 35 Ref resource.Reference 36 FieldsMap map[string]int 37 Cluster cluster.ID 38 } 39 40 var ( 41 _ resource.Origin = &Origin{} 42 _ resource.Reference = &Position{} 43 ) 44 45 // FriendlyName implements resource.Origin 46 func (o *Origin) FriendlyName() string { 47 parts := strings.Split(o.FullName.String(), "/") 48 if len(parts) == 2 { 49 // The istioctl convention is <type> [<namespace>/]<name>. 50 // This code has no notion of a default and always shows the namespace. 51 return fmt.Sprintf("%s %s/%s", o.Type.Kind, parts[0], parts[1]) 52 } 53 return fmt.Sprintf("%s %s", o.Type.Kind, o.FullName.String()) 54 } 55 56 func (o *Origin) Comparator() string { 57 return o.Type.Kind + "/" + o.FullName.Name.String() + "/" + o.FullName.Namespace.String() 58 } 59 60 // Namespace implements resource.Origin 61 func (o *Origin) Namespace() resource.Namespace { 62 // Special case: the namespace of a namespace resource is its own name 63 if o.Type == gvk.Namespace { 64 return resource.Namespace(o.FullName.Name) 65 } 66 67 return o.FullName.Namespace 68 } 69 70 // Reference implements resource.Origin 71 func (o *Origin) Reference() resource.Reference { 72 return o.Ref 73 } 74 75 // FieldMap implements resource.Origin 76 func (o *Origin) FieldMap() map[string]int { 77 return o.FieldsMap 78 } 79 80 // ClusterName implements resource.Origin 81 func (o *Origin) ClusterName() cluster.ID { 82 return o.Cluster 83 } 84 85 // Position is a representation of the location of a source. 86 type Position struct { 87 Filename string // filename, if any 88 Line int // line number, starting at 1 89 } 90 91 // String outputs the string representation of the position. 92 func (p *Position) String() string { 93 s := p.Filename 94 // TODO: support json file position. 95 if p.isValid() && filepath.Ext(p.Filename) != ".json" { 96 if s != "" { 97 s += ":" 98 } 99 s += fmt.Sprintf("%d", p.Line) 100 } 101 return s 102 } 103 104 func (p *Position) isValid() bool { 105 return p.Line > 0 && p.Filename != "" 106 }