/* Copyright 2020 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package server import ( "context" "fmt" "io" "math/rand" "slices" "sync" "sync/atomic" "time" "google.golang.org/grpc/metadata" "k8s.io/klog/v2" commonmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics" client "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client" "sigs.k8s.io/apiserver-network-proxy/pkg/server/metrics" "sigs.k8s.io/apiserver-network-proxy/pkg/server/proxystrategies" "sigs.k8s.io/apiserver-network-proxy/proto/agent" "sigs.k8s.io/apiserver-network-proxy/proto/header" ) // Backend abstracts a connected Konnectivity agent. // // In the only currently supported case (gRPC), it wraps an // agent.AgentService_ConnectServer, provides synchronization and // emits common stream metrics. type Backend struct { sendLock sync.Mutex recvLock sync.Mutex retireOnce sync.Once conn agent.AgentService_ConnectServer // cached from conn.Context() id string idents header.Identifiers done chan struct{} // draining indicates if this backend is draining and should not accept new connections draining atomic.Bool } // IsDraining returns true if the backend is draining func (b *Backend) IsDraining() bool { return b.draining.Load() } // SetDraining marks the backend as draining func (b *Backend) SetDraining() { b.draining.Store(true) } func (b *Backend) Retire() { b.SetDraining() b.retireOnce.Do(func() { close(b.done) }) } func (b *Backend) Done() <-chan struct{} { return b.done } func (b *Backend) Send(p *client.Packet) error { b.sendLock.Lock() defer b.sendLock.Unlock() const segment = commonmetrics.SegmentToAgent metrics.Metrics.ObservePacket(segment, p.Type) err := b.conn.Send(p) if err != nil && err != io.EOF { metrics.Metrics.ObserveStreamError(segment, err, p.Type) } return err } func (b *Backend) Recv() (*client.Packet, error) { b.recvLock.Lock() defer b.recvLock.Unlock() const segment = commonmetrics.SegmentFromAgent pkt, err := b.conn.Recv() if err != nil { if err != io.EOF { metrics.Metrics.ObserveStreamErrorNoPacket(segment, err) } return nil, err } metrics.Metrics.ObservePacket(segment, pkt.Type) return pkt, nil } func (b *Backend) Context() context.Context { // TODO: does Context require lock protection? return b.conn.Context() } func (b *Backend) GetAgentID() string { return b.id } func (b *Backend) GetAgentIdentifiers() header.Identifiers { return b.idents } func getAgentID(stream agent.AgentService_ConnectServer) (string, error) { md, ok := metadata.FromIncomingContext(stream.Context()) if !ok { return "", fmt.Errorf("failed to get context") } agentIDs := md.Get(header.AgentID) if len(agentIDs) != 1 { return "", fmt.Errorf("expected one agent ID in the context, got %v", agentIDs) } return agentIDs[0], nil } func getAgentIdentifiers(conn agent.AgentService_ConnectServer) (header.Identifiers, error) { var agentIdentifiers header.Identifiers md, ok := metadata.FromIncomingContext(conn.Context()) if !ok { return agentIdentifiers, fmt.Errorf("failed to get metadata from context") } agentIdent := md.Get(header.AgentIdentifiers) if len(agentIdent) > 1 { return agentIdentifiers, fmt.Errorf("expected at most one set of agent identifiers in the context, got %v", agentIdent) } if len(agentIdent) == 0 { return agentIdentifiers, nil } return header.GenAgentIdentifiers(agentIdent[0]) } func NewBackend(conn agent.AgentService_ConnectServer) (*Backend, error) { agentID, err := getAgentID(conn) if err != nil { return nil, err } agentIdentifiers, err := getAgentIdentifiers(conn) if err != nil { return nil, err } return &Backend{conn: conn, id: agentID, idents: agentIdentifiers, done: make(chan struct{})}, nil } // BackendStorage is an interface to manage the storage of the backend // connections, i.e., get, add and remove type BackendStorage interface { // addBackend adds a backend. addBackend(identifier string, idType header.IdentifierType, backend *Backend) // removeBackend removes a backend. removeBackend(identifier string, idType header.IdentifierType, backend *Backend) // NumBackends returns the number of backends. NumBackends() int } // BackendManager is an interface to manage backend connections, i.e., // connection to the proxy agents. type BackendManager interface { // Backend returns a single backend. // WARNING: the context passed to the function should be a session-scoped // context instead of a request-scoped context, as the backend manager will // pick a backend for every tunnel session and each tunnel session may // contains multiple requests. Backend(ctx context.Context) (*Backend, error) // AddBackend adds a backend. AddBackend(backend *Backend) // RemoveBackend adds a backend. RemoveBackend(backend *Backend) BackendStorage ReadinessManager } var _ BackendManager = &DefaultBackendManager{} // DefaultBackendManager is the default backend manager. type DefaultBackendManager struct { *DefaultBackendStorage } func (dbm *DefaultBackendManager) Backend(_ context.Context) (*Backend, error) { klog.V(5).InfoS("Get a random backend through the DefaultBackendManager") return dbm.DefaultBackendStorage.GetRandomBackend() } func (dbm *DefaultBackendManager) AddBackend(backend *Backend) { agentID := backend.GetAgentID() klog.V(5).InfoS("Add the agent to DefaultBackendManager", "agentID", agentID) dbm.addBackend(agentID, header.UID, backend) } func (dbm *DefaultBackendManager) RemoveBackend(backend *Backend) { agentID := backend.GetAgentID() klog.V(5).InfoS("Remove the agent from the DefaultBackendManager", "agentID", agentID) dbm.removeBackend(agentID, header.UID, backend) } // DefaultBackendStorage is the default backend storage. type DefaultBackendStorage struct { mu sync.RWMutex //protects the following // A map between agentID and its grpc connections. // For a given agent, ProxyServer prefers backends[agentID][0] to send // traffic, because backends[agentID][1:] are more likely to be closed // by the agent to deduplicate connections to the same server. // // TODO: fix documentation. This is not always agentID, e.g. in // the case of DestHostBackendManager. backends map[string][]*Backend // agentID is tracked in this slice to enable randomly picking an // agentID in the Backend() method. There is no reliable way to // randomly pick a key from a map (in this case, the backends) in // Golang. agentIDs []string random *rand.Rand // idTypes contains the valid identifier types for this // DefaultBackendStorage. The DefaultBackendStorage may only tolerate certain // types of identifiers when associating to a specific BackendManager, // e.g., when associating to the DestHostBackendManager, it can only use the // identifiers of types, IPv4, IPv6 and Host. idTypes []header.IdentifierType // proxyStrategy is the proxy strategy of the backend manager this storage // belongs to. // It is used to record metrics. proxyStrategy proxystrategies.ProxyStrategy } // NewDefaultBackendManager returns a DefaultBackendManager. func NewDefaultBackendManager() *DefaultBackendManager { return &DefaultBackendManager{ DefaultBackendStorage: NewDefaultBackendStorage( []header.IdentifierType{header.UID}, proxystrategies.ProxyStrategyDefault)} } // NewDefaultBackendStorage returns a DefaultBackendStorage func NewDefaultBackendStorage(idTypes []header.IdentifierType, proxyStrategy proxystrategies.ProxyStrategy) *DefaultBackendStorage { // Set an explicit value, so that the metric is emitted even when // no agent ever successfully connects. metrics.Metrics.SetBackendCountDeprecated(0) metrics.Metrics.SetTotalBackendCount(proxyStrategy, 0) return &DefaultBackendStorage{ backends: make(map[string][]*Backend), random: rand.New(rand.NewSource(time.Now().UnixNano())), /* #nosec G404 */ idTypes: idTypes, proxyStrategy: proxyStrategy, } } func containIDType(idTypes []header.IdentifierType, idType header.IdentifierType) bool { return slices.Contains(idTypes, idType) } // addBackend adds a backend. func (s *DefaultBackendStorage) addBackend(identifier string, idType header.IdentifierType, backend *Backend) { if !containIDType(s.idTypes, idType) { klog.V(3).InfoS("fail to add backend", "backend", identifier, "error", &ErrWrongIDType{idType, s.idTypes}) return } klog.V(2).InfoS("Register backend for agent", "agentID", identifier) s.mu.Lock() defer s.mu.Unlock() _, ok := s.backends[identifier] if ok { for _, b := range s.backends[identifier] { if b == backend { klog.V(1).InfoS("This should not happen. Adding existing backend for agent", "agentID", identifier) return } } s.backends[identifier] = append(s.backends[identifier], backend) return } s.backends[identifier] = []*Backend{backend} metrics.Metrics.SetBackendCountDeprecated(len(s.backends)) metrics.Metrics.SetTotalBackendCount(s.proxyStrategy, len(s.backends)) s.agentIDs = append(s.agentIDs, identifier) } // removeBackend removes a backend. func (s *DefaultBackendStorage) removeBackend(identifier string, idType header.IdentifierType, backend *Backend) { if !containIDType(s.idTypes, idType) { klog.ErrorS(&ErrWrongIDType{idType, s.idTypes}, "fail to remove backend") return } klog.V(2).InfoS("Remove connection for agent", "agentID", identifier) s.mu.Lock() defer s.mu.Unlock() backends, ok := s.backends[identifier] if !ok { klog.V(1).InfoS("Cannot find agent in backends", "identifier", identifier) return } var found bool for i, b := range backends { if b == backend { s.backends[identifier] = append(s.backends[identifier][:i], s.backends[identifier][i+1:]...) if i == 0 && len(s.backends[identifier]) != 0 { klog.V(1).InfoS("This should not happen. Removed connection that is not the first connection", "agentID", identifier) } found = true } } if len(s.backends[identifier]) == 0 { delete(s.backends, identifier) for i := range s.agentIDs { if s.agentIDs[i] == identifier { s.agentIDs[i] = s.agentIDs[len(s.agentIDs)-1] s.agentIDs = s.agentIDs[:len(s.agentIDs)-1] break } } } if !found { klog.V(1).InfoS("Could not find connection matching identifier to remove", "agentID", identifier, "idType", idType) } metrics.Metrics.SetBackendCountDeprecated(len(s.backends)) metrics.Metrics.SetTotalBackendCount(s.proxyStrategy, len(s.backends)) } // NumBackends resturns the number of available backends func (s *DefaultBackendStorage) NumBackends() int { s.mu.RLock() defer s.mu.RUnlock() return len(s.backends) } // ErrNotFound indicates that no backend can be found. type ErrNotFound struct{} // Error returns the error message. func (e *ErrNotFound) Error() string { return "No agent available" } type ErrWrongIDType struct { got header.IdentifierType expect []header.IdentifierType } func (e *ErrWrongIDType) Error() string { return fmt.Sprintf("incorrect id type: got %s, expect %s", e.got, e.expect) } func ignoreNotFound(err error) error { if _, ok := err.(*ErrNotFound); ok { return nil } return err } // GetRandomBackend returns a random backend connection from all connected agents. func (s *DefaultBackendStorage) GetRandomBackend() (*Backend, error) { s.mu.Lock() defer s.mu.Unlock() if len(s.backends) == 0 { return nil, &ErrNotFound{} } var firstDrainingBackend *Backend // Start at a random agent and check each agent in sequence startIdx := s.random.Intn(len(s.agentIDs)) for i := 0; i < len(s.agentIDs); i++ { // Wrap around using modulo currentIdx := (startIdx + i) % len(s.agentIDs) agentID := s.agentIDs[currentIdx] // always return the first connection to an agent, because the agent // will close later connections if there are multiple. backend := s.backends[agentID][0] if !backend.IsDraining() { klog.V(3).InfoS("Pick agent as backend", "agentID", agentID) return backend, nil } // Keep track of first draining backend as fallback if firstDrainingBackend == nil { firstDrainingBackend = backend } } // All agents are draining, use one as fallback if firstDrainingBackend != nil { agentID := firstDrainingBackend.id klog.V(3).InfoS("No non-draining backends available, using draining backend as fallback", "agentID", agentID) return firstDrainingBackend, nil } return nil, &ErrNotFound{} }