ceph-csi/vendor/k8s.io/kubernetes/pkg/proxy/ipvs/ipset.go

183 lines
6.2 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ipvs
import (
"k8s.io/apimachinery/pkg/util/sets"
2018-11-26 18:23:56 +00:00
utilversion "k8s.io/apimachinery/pkg/util/version"
2018-01-09 18:57:14 +00:00
utilipset "k8s.io/kubernetes/pkg/util/ipset"
2018-07-18 14:47:22 +00:00
"fmt"
2018-11-26 18:23:56 +00:00
"k8s.io/klog"
2018-01-09 18:57:14 +00:00
)
const (
// MinIPSetCheckVersion is the min ipset version we need. IPv6 is supported in ipset 6.x
MinIPSetCheckVersion = "6.0"
2018-07-18 14:47:22 +00:00
kubeLoopBackIPSetComment = "Kubernetes endpoints dst ip:port, source ip for solving hairpin purpose"
kubeLoopBackIPSet = "KUBE-LOOP-BACK"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeClusterIPSetComment = "Kubernetes service cluster ip + port for masquerade purpose"
kubeClusterIPSet = "KUBE-CLUSTER-IP"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeExternalIPSetComment = "Kubernetes service external ip + port for masquerade and filter purpose"
kubeExternalIPSet = "KUBE-EXTERNAL-IP"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeLoadBalancerSetComment = "Kubernetes service lb portal"
kubeLoadBalancerSet = "KUBE-LOAD-BALANCER"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeLoadBalancerLocalSetComment = "Kubernetes service load balancer ip + port with externalTrafficPolicy=local"
kubeLoadBalancerLocalSet = "KUBE-LOAD-BALANCER-LOCAL"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeLoadbalancerFWSetComment = "Kubernetes service load balancer ip + port for load balancer with sourceRange"
kubeLoadbalancerFWSet = "KUBE-LOAD-BALANCER-FW"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeLoadBalancerSourceIPSetComment = "Kubernetes service load balancer ip + port + source IP for packet filter purpose"
kubeLoadBalancerSourceIPSet = "KUBE-LOAD-BALANCER-SOURCE-IP"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeLoadBalancerSourceCIDRSetComment = "Kubernetes service load balancer ip + port + source cidr for packet filter purpose"
kubeLoadBalancerSourceCIDRSet = "KUBE-LOAD-BALANCER-SOURCE-CIDR"
2018-01-09 18:57:14 +00:00
2018-07-18 14:47:22 +00:00
kubeNodePortSetTCPComment = "Kubernetes nodeport TCP port for masquerade purpose"
kubeNodePortSetTCP = "KUBE-NODE-PORT-TCP"
kubeNodePortLocalSetTCPComment = "Kubernetes nodeport TCP port with externalTrafficPolicy=local"
kubeNodePortLocalSetTCP = "KUBE-NODE-PORT-LOCAL-TCP"
kubeNodePortSetUDPComment = "Kubernetes nodeport UDP port for masquerade purpose"
kubeNodePortSetUDP = "KUBE-NODE-PORT-UDP"
kubeNodePortLocalSetUDPComment = "Kubernetes nodeport UDP port with externalTrafficPolicy=local"
kubeNodePortLocalSetUDP = "KUBE-NODE-PORT-LOCAL-UDP"
2018-11-26 18:23:56 +00:00
kubeNodePortSetSCTPComment = "Kubernetes nodeport SCTP port for masquerade purpose"
kubeNodePortSetSCTP = "KUBE-NODE-PORT-SCTP"
kubeNodePortLocalSetSCTPComment = "Kubernetes nodeport SCTP port with externalTrafficPolicy=local"
kubeNodePortLocalSetSCTP = "KUBE-NODE-PORT-LOCAL-SCTP"
2018-01-09 18:57:14 +00:00
)
// IPSetVersioner can query the current ipset version.
type IPSetVersioner interface {
// returns "X.Y"
GetVersion() (string, error)
}
// IPSet wraps util/ipset which is used by IPVS proxier.
type IPSet struct {
utilipset.IPSet
// activeEntries is the current active entries of the ipset.
activeEntries sets.String
// handle is the util ipset interface handle.
handle utilipset.Interface
}
// NewIPSet initialize a new IPSet struct
2018-07-18 14:47:22 +00:00
func NewIPSet(handle utilipset.Interface, name string, setType utilipset.Type, isIPv6 bool, comment string) *IPSet {
2018-01-09 18:57:14 +00:00
hashFamily := utilipset.ProtocolFamilyIPV4
if isIPv6 {
hashFamily = utilipset.ProtocolFamilyIPV6
}
set := &IPSet{
IPSet: utilipset.IPSet{
Name: name,
SetType: setType,
HashFamily: hashFamily,
2018-07-18 14:47:22 +00:00
Comment: comment,
2018-01-09 18:57:14 +00:00
},
activeEntries: sets.NewString(),
handle: handle,
}
return set
}
2018-03-06 22:33:18 +00:00
func (set *IPSet) validateEntry(entry *utilipset.Entry) bool {
return entry.Validate(&set.IPSet)
}
2018-01-09 18:57:14 +00:00
func (set *IPSet) isEmpty() bool {
return len(set.activeEntries.UnsortedList()) == 0
}
2018-07-18 14:47:22 +00:00
func (set *IPSet) getComment() string {
return fmt.Sprintf("\"%s\"", set.Comment)
}
2018-01-09 18:57:14 +00:00
func (set *IPSet) resetEntries() {
set.activeEntries = sets.NewString()
}
func (set *IPSet) syncIPSetEntries() {
appliedEntries, err := set.handle.ListEntries(set.Name)
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Errorf("Failed to list ip set entries, error: %v", err)
2018-01-09 18:57:14 +00:00
return
}
// currentIPSetEntries represents Endpoints watched from API Server.
currentIPSetEntries := sets.NewString()
for _, appliedEntry := range appliedEntries {
currentIPSetEntries.Insert(appliedEntry)
}
if !set.activeEntries.Equal(currentIPSetEntries) {
// Clean legacy entries
for _, entry := range currentIPSetEntries.Difference(set.activeEntries).List() {
if err := set.handle.DelEntry(entry, set.Name); err != nil {
2018-03-06 22:33:18 +00:00
if !utilipset.IsNotFoundError(err) {
2018-11-26 18:23:56 +00:00
klog.Errorf("Failed to delete ip set entry: %s from ip set: %s, error: %v", entry, set.Name, err)
2018-03-06 22:33:18 +00:00
}
2018-01-09 18:57:14 +00:00
} else {
2018-11-26 18:23:56 +00:00
klog.V(3).Infof("Successfully delete legacy ip set entry: %s from ip set: %s", entry, set.Name)
2018-01-09 18:57:14 +00:00
}
}
// Create active entries
for _, entry := range set.activeEntries.Difference(currentIPSetEntries).List() {
2018-03-06 22:33:18 +00:00
if err := set.handle.AddEntry(entry, &set.IPSet, true); err != nil {
2018-11-26 18:23:56 +00:00
klog.Errorf("Failed to add entry: %v to ip set: %s, error: %v", entry, set.Name, err)
2018-01-09 18:57:14 +00:00
} else {
2018-11-26 18:23:56 +00:00
klog.V(3).Infof("Successfully add entry: %v to ip set: %s", entry, set.Name)
2018-01-09 18:57:14 +00:00
}
}
}
}
2018-07-18 14:47:22 +00:00
func ensureIPSet(set *IPSet) error {
if err := set.handle.CreateSet(&set.IPSet, true); err != nil {
2018-11-26 18:23:56 +00:00
klog.Errorf("Failed to make sure ip set: %v exist, error: %v", set, err)
2018-07-18 14:47:22 +00:00
return err
2018-01-09 18:57:14 +00:00
}
return nil
}
// checkMinVersion checks if ipset current version satisfies required min version
func checkMinVersion(vstring string) bool {
version, err := utilversion.ParseGeneric(vstring)
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Errorf("vstring (%s) is not a valid version string: %v", vstring, err)
2018-01-09 18:57:14 +00:00
return false
}
minVersion, err := utilversion.ParseGeneric(MinIPSetCheckVersion)
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Errorf("MinCheckVersion (%s) is not a valid version string: %v", MinIPSetCheckVersion, err)
2018-01-09 18:57:14 +00:00
return false
}
return !version.LessThan(minVersion)
}