blob: d175d15fe056e420cba7afa5beaaa201096b4f60 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package discovery
18
19import (
20 "net"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23)
24
25type Addresses interface {
26 ServerAddressByClientCIDRs(net.IP) []metav1.ServerAddressByClientCIDR
27}
28
29// DefaultAddresses is a default implementation of Addresses that will work in most cases
30type 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
40type 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
48func (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
62func (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}