Debian Codegraph @

pkg/server/desthost_backend_manager.go

raw
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/pkg/server/proxystrategies"
24 "sigs.k8s.io/apiserver-network-proxy/proto/header"
25 )
26
27 type DestHostBackendManager struct {
28 *DefaultBackendStorage
29 }
30
31 var _ BackendManager = &DestHostBackendManager{}
32
33 func NewDestHostBackendManager() *DestHostBackendManager {
34 return &DestHostBackendManager{
35 DefaultBackendStorage: NewDefaultBackendStorage(
36 []header.IdentifierType{header.IPv4, header.IPv6, header.Host}, proxystrategies.ProxyStrategyDestHost)}
37 }
38
39 func (dibm *DestHostBackendManager) AddBackend(backend *Backend) {
40 agentIdentifiers := backend.GetAgentIdentifiers()
41 for _, ipv4 := range agentIdentifiers.IPv4 {
42 klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", ipv4)
43 dibm.addBackend(ipv4, header.IPv4, backend)
44 }
45 for _, ipv6 := range agentIdentifiers.IPv6 {
46 klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", ipv6)
47 dibm.addBackend(ipv6, header.IPv6, backend)
48 }
49 for _, host := range agentIdentifiers.Host {
50 klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", host)
51 dibm.addBackend(host, header.Host, backend)
52 }
53 }
54
55 func (dibm *DestHostBackendManager) RemoveBackend(backend *Backend) {
56 agentIdentifiers := backend.GetAgentIdentifiers()
57 for _, ipv4 := range agentIdentifiers.IPv4 {
58 klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", ipv4)
59 dibm.removeBackend(ipv4, header.IPv4, backend)
60 }
61 for _, ipv6 := range agentIdentifiers.IPv6 {
62 klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", ipv6)
63 dibm.removeBackend(ipv6, header.IPv6, backend)
64 }
65 for _, host := range agentIdentifiers.Host {
66 klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", host)
67 dibm.removeBackend(host, header.Host, backend)
68 }
69 }
70
71 // Backend tries to get a backend associating to the request destination host.
72 func (dibm *DestHostBackendManager) Backend(ctx context.Context) (*Backend, error) {
73 dibm.mu.RLock()
74 defer dibm.mu.RUnlock()
75 if len(dibm.backends) == 0 {
76 return nil, &ErrNotFound{}
77 }
78 destHost := ctx.Value(destHostKey).(string)
79 if destHost != "" {
80 bes, exist := dibm.backends[destHost]
81 if exist && len(bes) > 0 {
82 var firstDrainingBackend *Backend
83
84 // Find a non-draining backend for this destination host
85 for _, backend := range bes {
86 if !backend.IsDraining() {
87 klog.V(5).InfoS("Get the backend through the DestHostBackendManager", "destHost", destHost)
88 return backend, nil
89 }
90 // Keep track of first draining backend as fallback
91 if firstDrainingBackend == nil {
92 firstDrainingBackend = backend
93 }
94 }
95
96 // All backends for this destination are draining, use one as fallback
97 if firstDrainingBackend != nil {
98 klog.V(3).InfoS("All backends for destination host are draining, using one as fallback", "destHost", destHost)
99 return firstDrainingBackend, nil
100 }
101 }
102 }
103 return nil, &ErrNotFound{}
104 }