|
| 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 | + "fmt" |
| 21 | + |
| 22 | + "k8s.io/api/core/v1" |
| 23 | + "k8s.io/klog" |
| 24 | + "k8s.io/kubernetes/pkg/proxy" |
| 25 | + utilnet "k8s.io/utils/net" |
| 26 | + |
| 27 | + discovery "k8s.io/api/discovery/v1alpha1" |
| 28 | +) |
| 29 | + |
| 30 | +type metaProxier struct { |
| 31 | + ipv4Proxier proxy.Provider |
| 32 | + ipv6Proxier proxy.Provider |
| 33 | +} |
| 34 | + |
| 35 | +// NewMetaProxier returns a dual-stack "meta-proxier". Proxier API |
| 36 | +// calls will be dispatched to the ProxyProvider instances depending |
| 37 | +// on address family. |
| 38 | +func NewMetaProxier(ipv4Proxier, ipv6Proxier proxy.Provider) proxy.Provider { |
| 39 | + return proxy.Provider(&metaProxier{ |
| 40 | + ipv4Proxier: ipv4Proxier, |
| 41 | + ipv6Proxier: ipv6Proxier, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +// Sync immediately synchronizes the ProxyProvider's current state to |
| 46 | +// proxy rules. |
| 47 | +func (proxier *metaProxier) Sync() { |
| 48 | + proxier.ipv4Proxier.Sync() |
| 49 | + proxier.ipv6Proxier.Sync() |
| 50 | +} |
| 51 | + |
| 52 | +// SyncLoop runs periodic work. This is expected to run as a |
| 53 | +// goroutine or as the main loop of the app. It does not return. |
| 54 | +func (proxier *metaProxier) SyncLoop() { |
| 55 | + go proxier.ipv6Proxier.SyncLoop() // Use go-routine here! |
| 56 | + proxier.ipv4Proxier.SyncLoop() // never returns |
| 57 | +} |
| 58 | + |
| 59 | +// OnServiceAdd is called whenever creation of new service object is observed. |
| 60 | +func (proxier *metaProxier) OnServiceAdd(service *v1.Service) { |
| 61 | + if *(service.Spec.IPFamily) == v1.IPv4Protocol { |
| 62 | + proxier.ipv4Proxier.OnServiceAdd(service) |
| 63 | + return |
| 64 | + } |
| 65 | + proxier.ipv6Proxier.OnServiceAdd(service) |
| 66 | +} |
| 67 | + |
| 68 | +// OnServiceUpdate is called whenever modification of an existing |
| 69 | +// service object is observed. |
| 70 | +func (proxier *metaProxier) OnServiceUpdate(oldService, service *v1.Service) { |
| 71 | + // IPFamily is immutable, hence we only need to check on the new service |
| 72 | + if *(service.Spec.IPFamily) == v1.IPv4Protocol { |
| 73 | + proxier.ipv4Proxier.OnServiceUpdate(oldService, service) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + proxier.ipv6Proxier.OnServiceUpdate(oldService, service) |
| 78 | +} |
| 79 | + |
| 80 | +// OnServiceDelete is called whenever deletion of an existing service |
| 81 | +// object is observed. |
| 82 | +func (proxier *metaProxier) OnServiceDelete(service *v1.Service) { |
| 83 | + if *(service.Spec.IPFamily) == v1.IPv4Protocol { |
| 84 | + proxier.ipv4Proxier.OnServiceDelete(service) |
| 85 | + return |
| 86 | + } |
| 87 | + proxier.ipv6Proxier.OnServiceDelete(service) |
| 88 | +} |
| 89 | + |
| 90 | +// OnServiceSynced is called once all the initial event handlers were |
| 91 | +// called and the state is fully propagated to local cache. |
| 92 | +func (proxier *metaProxier) OnServiceSynced() { |
| 93 | + proxier.ipv4Proxier.OnServiceSynced() |
| 94 | + proxier.ipv6Proxier.OnServiceSynced() |
| 95 | +} |
| 96 | + |
| 97 | +// OnEndpointsAdd is called whenever creation of new endpoints object |
| 98 | +// is observed. |
| 99 | +func (proxier *metaProxier) OnEndpointsAdd(endpoints *v1.Endpoints) { |
| 100 | + ipFamily, err := endpointsIPFamily(endpoints) |
| 101 | + if err != nil { |
| 102 | + klog.Warningf("failed to add endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) |
| 103 | + return |
| 104 | + } |
| 105 | + if *ipFamily == v1.IPv4Protocol { |
| 106 | + proxier.ipv4Proxier.OnEndpointsAdd(endpoints) |
| 107 | + return |
| 108 | + } |
| 109 | + proxier.ipv6Proxier.OnEndpointsAdd(endpoints) |
| 110 | +} |
| 111 | + |
| 112 | +// OnEndpointsUpdate is called whenever modification of an existing |
| 113 | +// endpoints object is observed. |
| 114 | +func (proxier *metaProxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) { |
| 115 | + ipFamily, err := endpointsIPFamily(endpoints) |
| 116 | + if err != nil { |
| 117 | + klog.Warningf("failed to update endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + if *ipFamily == v1.IPv4Protocol { |
| 122 | + proxier.ipv4Proxier.OnEndpointsUpdate(oldEndpoints, endpoints) |
| 123 | + return |
| 124 | + } |
| 125 | + proxier.ipv6Proxier.OnEndpointsUpdate(oldEndpoints, endpoints) |
| 126 | +} |
| 127 | + |
| 128 | +// OnEndpointsDelete is called whenever deletion of an existing |
| 129 | +// endpoints object is observed. |
| 130 | +func (proxier *metaProxier) OnEndpointsDelete(endpoints *v1.Endpoints) { |
| 131 | + ipFamily, err := endpointsIPFamily(endpoints) |
| 132 | + if err != nil { |
| 133 | + klog.Warningf("failed to delete endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + if *ipFamily == v1.IPv4Protocol { |
| 138 | + proxier.ipv4Proxier.OnEndpointsDelete(endpoints) |
| 139 | + return |
| 140 | + } |
| 141 | + proxier.ipv6Proxier.OnEndpointsDelete(endpoints) |
| 142 | +} |
| 143 | + |
| 144 | +// OnEndpointsSynced is called once all the initial event handlers |
| 145 | +// were called and the state is fully propagated to local cache. |
| 146 | +func (proxier *metaProxier) OnEndpointsSynced() { |
| 147 | + proxier.ipv4Proxier.OnEndpointsSynced() |
| 148 | + proxier.ipv6Proxier.OnEndpointsSynced() |
| 149 | +} |
| 150 | + |
| 151 | +// TODO: (khenidak) implement EndpointSlice handling |
| 152 | + |
| 153 | +// OnEndpointSliceAdd is called whenever creation of a new endpoint slice object |
| 154 | +// is observed. |
| 155 | +func (proxier *metaProxier) OnEndpointSliceAdd(endpointSlice *discovery.EndpointSlice) { |
| 156 | + // noop |
| 157 | +} |
| 158 | + |
| 159 | +// OnEndpointSliceUpdate is called whenever modification of an existing endpoint |
| 160 | +// slice object is observed. |
| 161 | +func (proxier *metaProxier) OnEndpointSliceUpdate(_, endpointSlice *discovery.EndpointSlice) { |
| 162 | + //noop |
| 163 | +} |
| 164 | + |
| 165 | +// OnEndpointSliceDelete is called whenever deletion of an existing endpoint slice |
| 166 | +// object is observed. |
| 167 | +func (proxier *metaProxier) OnEndpointSliceDelete(endpointSlice *discovery.EndpointSlice) { |
| 168 | + //noop |
| 169 | +} |
| 170 | + |
| 171 | +// OnEndpointSlicesSynced is called once all the initial event handlers were |
| 172 | +// called and the state is fully propagated to local cache. |
| 173 | +func (proxier *metaProxier) OnEndpointSlicesSynced() { |
| 174 | + //noop |
| 175 | +} |
| 176 | + |
| 177 | +// endpointsIPFamily that returns IPFamily of endpoints or error if |
| 178 | +// failed to identify the IP family. |
| 179 | +func endpointsIPFamily(endpoints *v1.Endpoints) (*v1.IPFamily, error) { |
| 180 | + if len(endpoints.Subsets) == 0 { |
| 181 | + return nil, fmt.Errorf("failed to identify ipfamily for endpoints (no subsets)") |
| 182 | + } |
| 183 | + |
| 184 | + // we only need to work with subset [0],endpoint controller |
| 185 | + // ensures that endpoints selected are of the same family. |
| 186 | + subset := endpoints.Subsets[0] |
| 187 | + if len(subset.Addresses) == 0 { |
| 188 | + return nil, fmt.Errorf("failed to identify ipfamily for endpoints (no addresses)") |
| 189 | + } |
| 190 | + // same apply on addresses |
| 191 | + address := subset.Addresses[0] |
| 192 | + if len(address.IP) == 0 { |
| 193 | + return nil, fmt.Errorf("failed to identify ipfamily for endpoints (address has no ip)") |
| 194 | + } |
| 195 | + |
| 196 | + ipv4 := v1.IPv4Protocol |
| 197 | + ipv6 := v1.IPv6Protocol |
| 198 | + if utilnet.IsIPv6String(address.IP) { |
| 199 | + return &ipv6, nil |
| 200 | + } |
| 201 | + |
| 202 | + return &ipv4, nil |
| 203 | +} |
0 commit comments