| 1 | /* |
| 2 | Copyright 2020 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 server |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | |
| 22 | "k8s.io/klog/v2" |
| 23 | "sigs.k8s.io/apiserver-network-proxy/proto/header" |
| 24 | ) |
| 25 | |
| 26 | type DestHostBackendManager struct { |
| 27 | *DefaultBackendStorage |
| 28 | } |
| 29 | |
| 30 | var _ BackendManager = &DestHostBackendManager{} |
| 31 | |
| 32 | func NewDestHostBackendManager() *DestHostBackendManager { |
| 33 | return &DestHostBackendManager{ |
| 34 | DefaultBackendStorage: NewDefaultBackendStorage( |
| 35 | []header.IdentifierType{header.IPv4, header.IPv6, header.Host})} |
| 36 | } |
| 37 | |
| 38 | func (dibm *DestHostBackendManager) AddBackend(backend *Backend) { |
| 39 | agentIdentifiers := backend.GetAgentIdentifiers() |
| 40 | for _, ipv4 := range agentIdentifiers.IPv4 { |
| 41 | klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", ipv4) |
| 42 | dibm.addBackend(ipv4, header.IPv4, backend) |
| 43 | } |
| 44 | for _, ipv6 := range agentIdentifiers.IPv6 { |
| 45 | klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", ipv6) |
| 46 | dibm.addBackend(ipv6, header.IPv6, backend) |
| 47 | } |
| 48 | for _, host := range agentIdentifiers.Host { |
| 49 | klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", host) |
| 50 | dibm.addBackend(host, header.Host, backend) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (dibm *DestHostBackendManager) RemoveBackend(backend *Backend) { |
| 55 | agentIdentifiers := backend.GetAgentIdentifiers() |
| 56 | for _, ipv4 := range agentIdentifiers.IPv4 { |
| 57 | klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", ipv4) |
| 58 | dibm.removeBackend(ipv4, header.IPv4, backend) |
| 59 | } |
| 60 | for _, ipv6 := range agentIdentifiers.IPv6 { |
| 61 | klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", ipv6) |
| 62 | dibm.removeBackend(ipv6, header.IPv6, backend) |
| 63 | } |
| 64 | for _, host := range agentIdentifiers.Host { |
| 65 | klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", host) |
| 66 | dibm.removeBackend(host, header.Host, backend) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Backend tries to get a backend associating to the request destination host. |
| 71 | func (dibm *DestHostBackendManager) Backend(ctx context.Context) (*Backend, error) { |
| 72 | dibm.mu.RLock() |
| 73 | defer dibm.mu.RUnlock() |
| 74 | if len(dibm.backends) == 0 { |
| 75 | return nil, &ErrNotFound{} |
| 76 | } |
| 77 | destHost := ctx.Value(destHostKey).(string) |
| 78 | if destHost != "" { |
| 79 | bes, exist := dibm.backends[destHost] |
| 80 | if exist && len(bes) > 0 { |
| 81 | klog.V(5).InfoS("Get the backend through the DestHostBackendManager", "destHost", destHost) |
| 82 | return dibm.backends[destHost][0], nil |
| 83 | } |
| 84 | } |
| 85 | return nil, &ErrNotFound{} |
| 86 | } |