| 1 |
/* |
| 2 |
Copyright 2016 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 discovery |
| 18 |
|
| 19 |
import ( |
| 20 |
"net" |
| 21 |
|
| 22 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 |
) |
| 24 |
|
| 25 |
type Addresses interface { |
| 26 |
ServerAddressByClientCIDRs(net.IP) []metav1.ServerAddressByClientCIDR |
| 27 |
} |
| 28 |
|
| 29 |
// DefaultAddresses is a default implementation of Addresses that will work in most cases |
| 30 |
type DefaultAddresses struct { |
| 31 |
// CIDRRules is a list of CIDRs and Addresses to use if a client is in the range |
| 32 |
CIDRRules []CIDRRule |
| 33 |
|
| 34 |
// DefaultAddress is the address (hostname or IP and port) that should be used in |
| 35 |
// if no CIDR matches more specifically. |
| 36 |
DefaultAddress string |
| 37 |
} |
| 38 |
|
| 39 |
// CIDRRule is a rule for adding an alternate path to the master based on matching CIDR |
| 40 |
type CIDRRule struct { |
| 41 |
IPRange net.IPNet |
| 42 |
|
| 43 |
// Address is the address (hostname or IP and port) that should be used in |
| 44 |
// if this CIDR matches |
| 45 |
Address string |
| 46 |
} |
| 47 |
|
| 48 |
func (d DefaultAddresses) ServerAddressByClientCIDRs(clientIP net.IP) []metav1.ServerAddressByClientCIDR { |
| 49 |
addressCIDRMap := []metav1.ServerAddressByClientCIDR{ |
| 50 |
{ |
| 51 |
ClientCIDR: "0.0.0.0/0", |
| 52 |
ServerAddress: d.DefaultAddress, |
| 53 |
}, |
| 54 |
} |
| 55 |
|
| 56 |
for _, rule := range d.CIDRRules { |
| 57 |
addressCIDRMap = append(addressCIDRMap, rule.ServerAddressByClientCIDRs(clientIP)...) |
| 58 |
} |
| 59 |
return addressCIDRMap |
| 60 |
} |
| 61 |
|
| 62 |
func (d CIDRRule) ServerAddressByClientCIDRs(clientIP net.IP) []metav1.ServerAddressByClientCIDR { |
| 63 |
addressCIDRMap := []metav1.ServerAddressByClientCIDR{} |
| 64 |
|
| 65 |
if d.IPRange.Contains(clientIP) { |
| 66 |
addressCIDRMap = append(addressCIDRMap, metav1.ServerAddressByClientCIDR{ |
| 67 |
ClientCIDR: d.IPRange.String(), |
| 68 |
ServerAddress: d.Address, |
| 69 |
}) |
| 70 |
} |
| 71 |
return addressCIDRMap |
| 72 |
} |