k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/proxy/conntrack/fake.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2016 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 conntrack 21 22 import ( 23 "fmt" 24 25 v1 "k8s.io/api/core/v1" 26 "k8s.io/apimachinery/pkg/util/sets" 27 ) 28 29 // FakeInterface implements Interface by just recording entries that have been cleared. 30 type FakeInterface struct { 31 ClearedIPs sets.Set[string] 32 ClearedPorts sets.Set[int] 33 ClearedNATs map[string]string // origin -> dest 34 ClearedPortNATs map[int]string // port -> dest 35 } 36 37 var _ Interface = &FakeInterface{} 38 39 // NewFake creates a new FakeInterface 40 func NewFake() *FakeInterface { 41 fake := &FakeInterface{} 42 fake.Reset() 43 return fake 44 } 45 46 // Reset clears fake's sets/maps 47 func (fake *FakeInterface) Reset() { 48 fake.ClearedIPs = sets.New[string]() 49 fake.ClearedPorts = sets.New[int]() 50 fake.ClearedNATs = make(map[string]string) 51 fake.ClearedPortNATs = make(map[int]string) 52 } 53 54 // ClearEntriesForIP is part of Interface 55 func (fake *FakeInterface) ClearEntriesForIP(ip string, protocol v1.Protocol) error { 56 if protocol != v1.ProtocolUDP { 57 return fmt.Errorf("FakeInterface currently only supports UDP") 58 } 59 60 fake.ClearedIPs.Insert(ip) 61 return nil 62 } 63 64 // ClearEntriesForPort is part of Interface 65 func (fake *FakeInterface) ClearEntriesForPort(port int, isIPv6 bool, protocol v1.Protocol) error { 66 if protocol != v1.ProtocolUDP { 67 return fmt.Errorf("FakeInterface currently only supports UDP") 68 } 69 70 fake.ClearedPorts.Insert(port) 71 return nil 72 } 73 74 // ClearEntriesForNAT is part of Interface 75 func (fake *FakeInterface) ClearEntriesForNAT(origin, dest string, protocol v1.Protocol) error { 76 if protocol != v1.ProtocolUDP { 77 return fmt.Errorf("FakeInterface currently only supports UDP") 78 } 79 if previous, exists := fake.ClearedNATs[origin]; exists && previous != dest { 80 return fmt.Errorf("ClearEntriesForNAT called with same origin (%s), different destination (%s / %s)", origin, previous, dest) 81 } 82 83 fake.ClearedNATs[origin] = dest 84 return nil 85 } 86 87 // ClearEntriesForPortNAT is part of Interface 88 func (fake *FakeInterface) ClearEntriesForPortNAT(dest string, port int, protocol v1.Protocol) error { 89 if protocol != v1.ProtocolUDP { 90 return fmt.Errorf("FakeInterface currently only supports UDP") 91 } 92 if previous, exists := fake.ClearedPortNATs[port]; exists && previous != dest { 93 return fmt.Errorf("ClearEntriesForPortNAT called with same port (%d), different destination (%s / %s)", port, previous, dest) 94 } 95 96 fake.ClearedPortNATs[port] = dest 97 return nil 98 }