safe_ipset.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ipvs
  14. import (
  15. "sync"
  16. "k8s.io/kubernetes/pkg/util/ipset"
  17. )
  18. type safeIpset struct {
  19. ipset ipset.Interface
  20. mu sync.Mutex
  21. }
  22. func newSafeIpset(ipset ipset.Interface) ipset.Interface {
  23. return &safeIpset{
  24. ipset: ipset,
  25. }
  26. }
  27. // FlushSet deletes all entries from a named set.
  28. func (s *safeIpset) FlushSet(set string) error {
  29. s.mu.Lock()
  30. defer s.mu.Unlock()
  31. return s.ipset.FlushSet(set)
  32. }
  33. // DestroySet deletes a named set.
  34. func (s *safeIpset) DestroySet(set string) error {
  35. s.mu.Lock()
  36. defer s.mu.Unlock()
  37. return s.ipset.DestroySet(set)
  38. }
  39. // DestroyAllSets deletes all sets.
  40. func (s *safeIpset) DestroyAllSets() error {
  41. s.mu.Lock()
  42. defer s.mu.Unlock()
  43. return s.ipset.DestroyAllSets()
  44. }
  45. // CreateSet creates a new set. It will ignore error when the set already exists if ignoreExistErr=true.
  46. func (s *safeIpset) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error {
  47. s.mu.Lock()
  48. defer s.mu.Unlock()
  49. return s.ipset.CreateSet(set, ignoreExistErr)
  50. }
  51. // AddEntry adds a new entry to the named set. It will ignore error when the entry already exists if ignoreExistErr=true.
  52. func (s *safeIpset) AddEntry(entry string, set *ipset.IPSet, ignoreExistErr bool) error {
  53. s.mu.Lock()
  54. defer s.mu.Unlock()
  55. return s.ipset.AddEntry(entry, set, ignoreExistErr)
  56. }
  57. // DelEntry deletes one entry from the named set
  58. func (s *safeIpset) DelEntry(entry string, set string) error {
  59. s.mu.Lock()
  60. defer s.mu.Unlock()
  61. return s.ipset.DelEntry(entry, set)
  62. }
  63. // Test test if an entry exists in the named set
  64. func (s *safeIpset) TestEntry(entry string, set string) (bool, error) {
  65. s.mu.Lock()
  66. defer s.mu.Unlock()
  67. return s.ipset.TestEntry(entry, set)
  68. }
  69. // ListEntries lists all the entries from a named set
  70. func (s *safeIpset) ListEntries(set string) ([]string, error) {
  71. s.mu.Lock()
  72. defer s.mu.Unlock()
  73. return s.ipset.ListEntries(set)
  74. }
  75. // ListSets list all set names from kernel
  76. func (s *safeIpset) ListSets() ([]string, error) {
  77. s.mu.Lock()
  78. defer s.mu.Unlock()
  79. return s.ipset.ListSets()
  80. }
  81. // GetVersion returns the "X.Y" version string for ipset.
  82. func (s *safeIpset) GetVersion() (string, error) {
  83. s.mu.Lock()
  84. defer s.mu.Unlock()
  85. return s.ipset.GetVersion()
  86. }