k8s.io/kubernetes@v1.29.3/pkg/proxy/ipvs/safe_ipset.go (about)

     1  /*
     2  Copyright 2019 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 ipvs
    18  
    19  import (
    20  	"sync"
    21  
    22  	"k8s.io/kubernetes/pkg/proxy/ipvs/ipset"
    23  )
    24  
    25  type safeIpset struct {
    26  	ipset ipset.Interface
    27  	mu    sync.Mutex
    28  }
    29  
    30  func newSafeIpset(ipset ipset.Interface) ipset.Interface {
    31  	return &safeIpset{
    32  		ipset: ipset,
    33  	}
    34  }
    35  
    36  // FlushSet deletes all entries from a named set.
    37  func (s *safeIpset) FlushSet(set string) error {
    38  	s.mu.Lock()
    39  	defer s.mu.Unlock()
    40  	return s.ipset.FlushSet(set)
    41  }
    42  
    43  // DestroySet deletes a named set.
    44  func (s *safeIpset) DestroySet(set string) error {
    45  	s.mu.Lock()
    46  	defer s.mu.Unlock()
    47  	return s.ipset.DestroySet(set)
    48  }
    49  
    50  // DestroyAllSets deletes all sets.
    51  func (s *safeIpset) DestroyAllSets() error {
    52  	s.mu.Lock()
    53  	defer s.mu.Unlock()
    54  	return s.ipset.DestroyAllSets()
    55  }
    56  
    57  // CreateSet creates a new set.  It will ignore error when the set already exists if ignoreExistErr=true.
    58  func (s *safeIpset) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error {
    59  	s.mu.Lock()
    60  	defer s.mu.Unlock()
    61  	return s.ipset.CreateSet(set, ignoreExistErr)
    62  }
    63  
    64  // AddEntry adds a new entry to the named set.  It will ignore error when the entry already exists if ignoreExistErr=true.
    65  func (s *safeIpset) AddEntry(entry string, set *ipset.IPSet, ignoreExistErr bool) error {
    66  	s.mu.Lock()
    67  	defer s.mu.Unlock()
    68  	return s.ipset.AddEntry(entry, set, ignoreExistErr)
    69  }
    70  
    71  // DelEntry deletes one entry from the named set
    72  func (s *safeIpset) DelEntry(entry string, set string) error {
    73  	s.mu.Lock()
    74  	defer s.mu.Unlock()
    75  	return s.ipset.DelEntry(entry, set)
    76  }
    77  
    78  // Test test if an entry exists in the named set
    79  func (s *safeIpset) TestEntry(entry string, set string) (bool, error) {
    80  	s.mu.Lock()
    81  	defer s.mu.Unlock()
    82  	return s.ipset.TestEntry(entry, set)
    83  }
    84  
    85  // ListEntries lists all the entries from a named set
    86  func (s *safeIpset) ListEntries(set string) ([]string, error) {
    87  	s.mu.Lock()
    88  	defer s.mu.Unlock()
    89  	return s.ipset.ListEntries(set)
    90  }
    91  
    92  // ListSets list all set names from kernel
    93  func (s *safeIpset) ListSets() ([]string, error) {
    94  	s.mu.Lock()
    95  	defer s.mu.Unlock()
    96  	return s.ipset.ListSets()
    97  }
    98  
    99  // GetVersion returns the "X.Y" version string for ipset.
   100  func (s *safeIpset) GetVersion() (string, error) {
   101  	s.mu.Lock()
   102  	defer s.mu.Unlock()
   103  	return s.ipset.GetVersion()
   104  }