istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/model/fake_store.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 model 16 17 import ( 18 "errors" 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/maps" 24 ) 25 26 type FakeStore struct { 27 store map[config.GroupVersionKind]map[string]map[string]config.Config 28 } 29 30 func NewFakeStore() *FakeStore { 31 f := FakeStore{ 32 store: make(map[config.GroupVersionKind]map[string]map[string]config.Config), 33 } 34 return &f 35 } 36 37 var _ ConfigStore = (*FakeStore)(nil) 38 39 func (s *FakeStore) Schemas() collection.Schemas { 40 return collections.Pilot 41 } 42 43 func (s *FakeStore) Get(typ config.GroupVersionKind, name, namespace string) *config.Config { 44 nsConfigs := s.store[typ] 45 if nsConfigs == nil { 46 return nil 47 } 48 49 configs := nsConfigs[namespace] 50 if configs == nil { 51 return nil 52 } 53 54 if config, f := configs[name]; f { 55 return &config 56 } 57 58 return nil 59 } 60 61 func (s *FakeStore) List(typ config.GroupVersionKind, namespace string) []config.Config { 62 nsConfigs := s.store[typ] 63 if nsConfigs == nil { 64 return nil 65 } 66 var res []config.Config 67 if namespace == NamespaceAll { 68 for _, configs := range nsConfigs { 69 for _, cfg := range configs { 70 res = append(res, cfg) 71 } 72 } 73 return res 74 } 75 76 return maps.Values(nsConfigs[namespace]) 77 } 78 79 func (s *FakeStore) Create(cfg config.Config) (revision string, err error) { 80 nsConfigs := s.store[cfg.GroupVersionKind] 81 if nsConfigs == nil { 82 nsConfigs = make(map[string]map[string]config.Config) 83 s.store[cfg.GroupVersionKind] = nsConfigs 84 } 85 86 configs := nsConfigs[cfg.Namespace] 87 if configs == nil { 88 configs = make(map[string]config.Config) 89 nsConfigs[cfg.Namespace] = configs 90 } 91 92 configs[cfg.Name] = cfg 93 return "", nil 94 } 95 96 func (s *FakeStore) Update(cfg config.Config) (newRevision string, err error) { 97 nsConfigs := s.store[cfg.GroupVersionKind] 98 if nsConfigs != nil { 99 configs := nsConfigs[cfg.Namespace] 100 if configs != nil { 101 if _, f := configs[cfg.Name]; f { 102 configs[cfg.Name] = cfg 103 return "", nil 104 } 105 } 106 } 107 108 return "", errors.New("config not found") 109 } 110 111 func (*FakeStore) UpdateStatus(config config.Config) (string, error) { return "", nil } 112 113 func (*FakeStore) Patch(orig config.Config, patchFn config.PatchFunc) (string, error) { 114 return "", nil 115 } 116 117 func (s *FakeStore) Delete(typ config.GroupVersionKind, name, namespace string, rv *string) error { 118 nsConfigs := s.store[typ] 119 if nsConfigs == nil { 120 return nil 121 } 122 123 configs := nsConfigs[namespace] 124 if configs == nil { 125 return nil 126 } 127 128 delete(configs, name) 129 return nil 130 }