k8s.io/kubernetes@v1.29.3/pkg/proxy/ipvs/ipset/testing/fake.go (about) 1 /* 2 Copyright 2017 The Kubernetes 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 testing 18 19 import ( 20 "fmt" 21 22 "k8s.io/apimachinery/pkg/util/sets" 23 "k8s.io/kubernetes/pkg/proxy/ipvs/ipset" 24 ) 25 26 // FakeIPSet is a no-op implementation of ipset Interface 27 type FakeIPSet struct { 28 // version of ipset util 29 Version string 30 // The key of Sets map is the ip set name 31 Sets map[string]*ipset.IPSet 32 // The key of Entries map is the ip set name where the entries exists 33 Entries map[string]sets.String 34 } 35 36 // NewFake create a new fake ipset interface - it initialize the FakeIPSet. 37 func NewFake(version string) *FakeIPSet { 38 return &FakeIPSet{ 39 Version: version, 40 Sets: make(map[string]*ipset.IPSet), 41 Entries: make(map[string]sets.String), 42 } 43 } 44 45 // GetVersion is part of interface. 46 func (f *FakeIPSet) GetVersion() (string, error) { 47 return f.Version, nil 48 } 49 50 // FlushSet is part of interface. It deletes all entries from a named set but keeps the set itself. 51 func (f *FakeIPSet) FlushSet(set string) error { 52 if f.Entries == nil { 53 return fmt.Errorf("entries map can't be nil") 54 } 55 56 // delete all entry elements 57 for true { 58 if _, has := f.Entries[set].PopAny(); has { 59 continue 60 } 61 break 62 } 63 return nil 64 } 65 66 // DestroySet is part of interface. It deletes both the entries and the set itself. 67 func (f *FakeIPSet) DestroySet(set string) error { 68 delete(f.Sets, set) 69 delete(f.Entries, set) 70 return nil 71 } 72 73 // DestroyAllSets is part of interface. 74 func (f *FakeIPSet) DestroyAllSets() error { 75 f.Sets = nil 76 f.Entries = nil 77 return nil 78 } 79 80 // CreateSet is part of interface. 81 func (f *FakeIPSet) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error { 82 if f.Sets[set.Name] != nil { 83 if !ignoreExistErr { 84 // already exists 85 return fmt.Errorf("Set cannot be created: set with the same name already exists") 86 } 87 return nil 88 } 89 f.Sets[set.Name] = set 90 // initialize entry map 91 f.Entries[set.Name] = sets.NewString() 92 return nil 93 } 94 95 // AddEntry is part of interface. 96 func (f *FakeIPSet) AddEntry(entry string, set *ipset.IPSet, ignoreExistErr bool) error { 97 if f.Entries[set.Name].Has(entry) { 98 if !ignoreExistErr { 99 // already exists 100 return fmt.Errorf("Element cannot be added to the set: it's already added") 101 } 102 return nil 103 } 104 f.Entries[set.Name].Insert(entry) 105 return nil 106 } 107 108 // DelEntry is part of interface. 109 func (f *FakeIPSet) DelEntry(entry string, set string) error { 110 if f.Entries == nil { 111 return fmt.Errorf("entries map can't be nil") 112 } 113 f.Entries[set].Delete(entry) 114 return nil 115 } 116 117 // TestEntry is part of interface. 118 func (f *FakeIPSet) TestEntry(entry string, set string) (bool, error) { 119 if f.Entries == nil { 120 return false, fmt.Errorf("entries map can't be nil") 121 } 122 found := f.Entries[set].Has(entry) 123 return found, nil 124 } 125 126 // ListEntries is part of interface. 127 func (f *FakeIPSet) ListEntries(set string) ([]string, error) { 128 if f.Entries == nil { 129 return nil, fmt.Errorf("entries map can't be nil") 130 } 131 return f.Entries[set].UnsortedList(), nil 132 } 133 134 // ListSets is part of interface. 135 func (f *FakeIPSet) ListSets() ([]string, error) { 136 res := []string{} 137 for set := range f.Sets { 138 res = append(res, set) 139 } 140 return res, nil 141 } 142 143 var _ = ipset.Interface(&FakeIPSet{})