k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/proxy/ipvs/ipset/testing/fake.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2017 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package testing 21 22 import ( 23 "fmt" 24 25 "k8s.io/apimachinery/pkg/util/sets" 26 "k8s.io/kubernetes/pkg/proxy/ipvs/ipset" 27 ) 28 29 // FakeIPSet is a no-op implementation of ipset Interface 30 type FakeIPSet struct { 31 // version of ipset util 32 Version string 33 // The key of Sets map is the ip set name 34 Sets map[string]*ipset.IPSet 35 // The key of Entries map is the ip set name where the entries exists 36 Entries map[string]sets.String 37 } 38 39 // NewFake create a new fake ipset interface - it initialize the FakeIPSet. 40 func NewFake(version string) *FakeIPSet { 41 return &FakeIPSet{ 42 Version: version, 43 Sets: make(map[string]*ipset.IPSet), 44 Entries: make(map[string]sets.String), 45 } 46 } 47 48 // GetVersion is part of interface. 49 func (f *FakeIPSet) GetVersion() (string, error) { 50 return f.Version, nil 51 } 52 53 // FlushSet is part of interface. It deletes all entries from a named set but keeps the set itself. 54 func (f *FakeIPSet) FlushSet(set string) error { 55 if f.Entries == nil { 56 return fmt.Errorf("entries map can't be nil") 57 } 58 59 // delete all entry elements 60 for true { 61 if _, has := f.Entries[set].PopAny(); has { 62 continue 63 } 64 break 65 } 66 return nil 67 } 68 69 // DestroySet is part of interface. It deletes both the entries and the set itself. 70 func (f *FakeIPSet) DestroySet(set string) error { 71 delete(f.Sets, set) 72 delete(f.Entries, set) 73 return nil 74 } 75 76 // DestroyAllSets is part of interface. 77 func (f *FakeIPSet) DestroyAllSets() error { 78 f.Sets = nil 79 f.Entries = nil 80 return nil 81 } 82 83 // CreateSet is part of interface. 84 func (f *FakeIPSet) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error { 85 if f.Sets[set.Name] != nil { 86 if !ignoreExistErr { 87 // already exists 88 return fmt.Errorf("Set cannot be created: set with the same name already exists") 89 } 90 return nil 91 } 92 f.Sets[set.Name] = set 93 // initialize entry map 94 f.Entries[set.Name] = sets.NewString() 95 return nil 96 } 97 98 // AddEntry is part of interface. 99 func (f *FakeIPSet) AddEntry(entry string, set *ipset.IPSet, ignoreExistErr bool) error { 100 if f.Entries[set.Name].Has(entry) { 101 if !ignoreExistErr { 102 // already exists 103 return fmt.Errorf("Element cannot be added to the set: it's already added") 104 } 105 return nil 106 } 107 f.Entries[set.Name].Insert(entry) 108 return nil 109 } 110 111 // DelEntry is part of interface. 112 func (f *FakeIPSet) DelEntry(entry string, set string) error { 113 if f.Entries == nil { 114 return fmt.Errorf("entries map can't be nil") 115 } 116 f.Entries[set].Delete(entry) 117 return nil 118 } 119 120 // TestEntry is part of interface. 121 func (f *FakeIPSet) TestEntry(entry string, set string) (bool, error) { 122 if f.Entries == nil { 123 return false, fmt.Errorf("entries map can't be nil") 124 } 125 found := f.Entries[set].Has(entry) 126 return found, nil 127 } 128 129 // ListEntries is part of interface. 130 func (f *FakeIPSet) ListEntries(set string) ([]string, error) { 131 if f.Entries == nil { 132 return nil, fmt.Errorf("entries map can't be nil") 133 } 134 return f.Entries[set].UnsortedList(), nil 135 } 136 137 // ListSets is part of interface. 138 func (f *FakeIPSet) ListSets() ([]string, error) { 139 res := []string{} 140 for set := range f.Sets { 141 res = append(res, set) 142 } 143 return res, nil 144 } 145 146 var _ = ipset.Interface(&FakeIPSet{})