istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/legacy/util/kuberesource/resources.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 kuberesource 16 17 import ( 18 "fmt" 19 20 "istio.io/istio/pkg/config" 21 "istio.io/istio/pkg/config/schema/collection" 22 "istio.io/istio/pkg/config/schema/collections" 23 "istio.io/istio/pkg/config/schema/resource" 24 ) 25 26 func ConvertInputsToSchemas(inputs []config.GroupVersionKind) collection.Schemas { 27 resultBuilder := collection.NewSchemasBuilder() 28 for _, gv := range inputs { 29 s, f := collections.All.FindByGroupVersionKind(gv) 30 if !f { 31 continue 32 } 33 _ = resultBuilder.Add(s) 34 } 35 36 return resultBuilder.Build() 37 } 38 39 func DefaultExcludedSchemas() collection.Schemas { 40 resultBuilder := collection.NewSchemasBuilder() 41 for _, r := range collections.Kube.All() { 42 if IsDefaultExcluded(r) { 43 _ = resultBuilder.Add(r) 44 } 45 } 46 47 return resultBuilder.Build() 48 } 49 50 func DefaultRemoteClusterExcludedSchemas() collection.Schemas { 51 resultBuilder := collection.NewSchemasBuilder() 52 // Pods are the most common resource type exists in the remote cluster. 53 _ = resultBuilder.Add(collections.Deployment) 54 // We don't need to analyze injection config in the multi-cluster scenario. 55 _ = resultBuilder.Add(collections.ConfigMap) 56 _ = resultBuilder.Add(collections.ValidatingWebhookConfiguration) 57 _ = resultBuilder.Add(collections.MutatingWebhookConfiguration) 58 return resultBuilder.Build() 59 } 60 61 // the following code minimally duplicates logic from galley/pkg/config/source/kube/rt/known.go 62 // without propagating the many dependencies it comes with. 63 64 var knownTypes = map[string]struct{}{ 65 asTypesKey("", "Service"): {}, 66 asTypesKey("", "Namespace"): {}, 67 asTypesKey("", "Node"): {}, 68 asTypesKey("", "Pod"): {}, 69 asTypesKey("", "Secret"): {}, 70 } 71 72 func asTypesKey(group, kind string) string { 73 if group == "" { 74 return kind 75 } 76 return fmt.Sprintf("%s/%s", group, kind) 77 } 78 79 func IsDefaultExcluded(res resource.Schema) bool { 80 key := asTypesKey(res.Group(), res.Kind()) 81 _, ok := knownTypes[key] 82 return ok 83 }