istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzers/util/find_errorline_utils.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 util 16 17 import ( 18 "fmt" 19 "strings" 20 21 "istio.io/istio/pkg/config/analysis/diag" 22 "istio.io/istio/pkg/config/resource" 23 ) 24 25 const ( 26 // Path templates for different fields with different paths, may edited by future developers if not covered in this list 27 // Use the path template to find the exact line number for the field 28 29 // Path for host in VirtualService. 30 // Required parameters: route rule, route rule index, route index. 31 DestinationHost = "{.spec.%s[%d].route[%d].destination.host}" 32 33 // Path for mirror host in VirtualService. 34 // Required parameters: http index. 35 MirrorHost = "{.spec.http[%d].mirror.host}" 36 37 // Path for mirrors host in VirtualService. 38 // Required parameters: http index, mirror index. 39 MirrorsHost = "{.spec.http[%d].mirrors[%d].host}" 40 41 // Path for VirtualService gateway. 42 // Required parameters: gateway index. 43 VSGateway = "{.spec.gateways[%d]}" 44 45 // Path for regex match of uri, scheme, method and authority. 46 // Required parameters: http index, match index, where to match. 47 URISchemeMethodAuthorityRegexMatch = "{.spec.http[%d].match[%d].%s.regex}" 48 49 // Path for regex match of headers and queryParams. 50 // Required parameters: http index, match index, where to match, match key. 51 HeaderAndQueryParamsRegexMatch = "{.spec.http[%d].match[%d].%s.%s.regex}" 52 53 // Path for regex match of allowOrigins. 54 // Required parameters: http index, allowOrigins index. 55 AllowOriginsRegexMatch = "{.spec.http[%d].corsPolicy.allowOrigins[%d].regex}" 56 57 // Path for workload selector. 58 // Required parameters: selector label. 59 WorkloadSelector = "{.spec.workloadSelector.labels.%s}" 60 61 // Path for port from ports collections. 62 // Required parameters: port index. 63 PortInPorts = "{.spec.ports[%d].port}" 64 65 // Path for fromRegistry in the mesh networks. 66 // Required parameters: network name, endPoint index. 67 FromRegistry = "{.networks.%s.endpoints[%d]}" 68 69 // Path for the image in the container. 70 // Required parameters: container index. 71 ImageInContainer = "{.spec.containers[%d].image}" 72 73 // Path for namespace in metadata. 74 // Required parameters: none. 75 MetadataNamespace = "{.metadata.namespace}" 76 77 // Path for name in metadata. 78 // Required parameters: none. 79 MetadataName = "{.metadata.name}" 80 81 // Path for namespace in authorizationPolicy. 82 // Required parameters: rule index, from index, namespace index. 83 AuthorizationPolicyNameSpace = "{.spec.rules[%d].from[%d].source.namespaces[%d]}" 84 85 // Path for annotation. 86 // Required parameters: annotation name. 87 Annotation = "{.metadata.annotations.%s}" 88 89 // Path for selector in Gateway. 90 // Required parameters: selector label. 91 GatewaySelector = "{.spec.selector.%s}" 92 93 // Path for credentialName. 94 // Required parameters: server index. 95 CredentialName = "{.spec.servers[%d].tls.credentialName}" 96 97 // Path for Port in ServiceEntry. 98 // Required parameters: port index. 99 ServiceEntryPort = "{.spec.ports[%d].name}" 100 101 // Path for DestinationRule tls certificate. 102 // Required parameters: none. 103 DestinationRuleTLSCert = "{.spec.trafficPolicy.tls.caCertificates}" 104 105 // Path for DestinationRule port-level tls certificate. 106 // Required parameters: portLevelSettings index. 107 DestinationRuleTLSPortLevelCert = "{.spec.trafficPolicy.portLevelSettings[%d].tls.caCertificates}" 108 109 // Path for ConfigPatch in envoyFilter 110 // Required parameters: envoyFilter config patch index 111 EnvoyFilterConfigPath = "{.spec.configPatches[%d].patch.value}" 112 113 // Path for selector in telemetry. 114 // Required parameters: selector label. 115 TelemetrySelector = "{.spec.selector.matchLabels.%s}" 116 ) 117 118 // ErrorLine returns the line number of the input path key in the resource 119 func ErrorLine(r *resource.Instance, path string) (line int, found bool) { 120 fieldMap := r.Origin.FieldMap() 121 line, ok := fieldMap[path] 122 if !ok { 123 return 0, false 124 } 125 return line, true 126 } 127 128 // ExtractLabelFromSelectorString returns the label of the match in the k8s labels.Selector 129 func ExtractLabelFromSelectorString(s string) string { 130 equalIndex := strings.Index(s, "=") 131 if equalIndex < 0 { 132 return "" 133 } 134 return s[:equalIndex] 135 } 136 137 func AddLineNumber(r *resource.Instance, ann string, m diag.Message) bool { 138 if line, ok := ErrorLine(r, fmt.Sprintf(Annotation, ann)); ok { 139 m.Line = line 140 return true 141 } 142 return false 143 }