1 /* 2 Copyright 2017 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 proxy 18 19 import ( 20 "net" 21 "strconv" 22 23 "k8s.io/apimachinery/pkg/util/sets" 24 ) 25 26 // Endpoint in an interface which abstracts information about an endpoint. 27 type Endpoint interface { 28 // String returns endpoint string. An example format can be: `IP:Port`. 29 // We take the returned value as ServiceEndpoint.Endpoint. 30 String() string 31 // IP returns IP part of the endpoint. 32 IP() string 33 // Port returns the Port part of the endpoint. 34 Port() int 35 36 // IsLocal returns true if the endpoint is running on the same host as kube-proxy. 37 IsLocal() bool 38 // IsReady returns true if an endpoint is ready and not terminating, or 39 // if PublishNotReadyAddresses is set on the service. 40 IsReady() bool 41 // IsServing returns true if an endpoint is ready. It does not account 42 // for terminating state. 43 IsServing() bool 44 // IsTerminating returns true if an endpoint is terminating. For pods, 45 // that is any pod with a deletion timestamp. 46 IsTerminating() bool 47 48 // ZoneHints returns the zone hint for the endpoint. This is based on 49 // endpoint.hints.forZones[0].name in the EndpointSlice API. 50 ZoneHints() sets.Set[string] 51 } 52 53 // BaseEndpointInfo contains base information that defines an endpoint. 54 // This could be used directly by proxier while processing endpoints, 55 // or can be used for constructing a more specific EndpointInfo struct 56 // defined by the proxier if needed. 57 type BaseEndpointInfo struct { 58 // Cache this values to improve performance 59 ip string 60 port int 61 // endpoint is the same as net.JoinHostPort(ip,port) 62 endpoint string 63 64 // isLocal indicates whether the endpoint is running on same host as kube-proxy. 65 isLocal bool 66 67 // ready indicates whether this endpoint is ready and NOT terminating, unless 68 // PublishNotReadyAddresses is set on the service, in which case it will just 69 // always be true. 70 ready bool 71 // serving indicates whether this endpoint is ready regardless of its terminating state. 72 // For pods this is true if it has a ready status regardless of its deletion timestamp. 73 serving bool 74 // terminating indicates whether this endpoint is terminating. 75 // For pods this is true if it has a non-nil deletion timestamp. 76 terminating bool 77 78 // zoneHints represent the zone hints for the endpoint. This is based on 79 // endpoint.hints.forZones[*].name in the EndpointSlice API. 80 zoneHints sets.Set[string] 81 } 82 83 var _ Endpoint = &BaseEndpointInfo{} 84 85 // String is part of proxy.Endpoint interface. 86 func (info *BaseEndpointInfo) String() string { 87 return info.endpoint 88 } 89 90 // IP returns just the IP part of the endpoint, it's a part of proxy.Endpoint interface. 91 func (info *BaseEndpointInfo) IP() string { 92 return info.ip 93 } 94 95 // Port returns just the Port part of the endpoint. 96 func (info *BaseEndpointInfo) Port() int { 97 return info.port 98 } 99 100 // IsLocal is part of proxy.Endpoint interface. 101 func (info *BaseEndpointInfo) IsLocal() bool { 102 return info.isLocal 103 } 104 105 // IsReady returns true if an endpoint is ready and not terminating. 106 func (info *BaseEndpointInfo) IsReady() bool { 107 return info.ready 108 } 109 110 // IsServing returns true if an endpoint is ready, regardless of if the 111 // endpoint is terminating. 112 func (info *BaseEndpointInfo) IsServing() bool { 113 return info.serving 114 } 115 116 // IsTerminating retruns true if an endpoint is terminating. For pods, 117 // that is any pod with a deletion timestamp. 118 func (info *BaseEndpointInfo) IsTerminating() bool { 119 return info.terminating 120 } 121 122 // ZoneHints returns the zone hint for the endpoint. 123 func (info *BaseEndpointInfo) ZoneHints() sets.Set[string] { 124 return info.zoneHints 125 } 126 127 func newBaseEndpointInfo(ip string, port int, isLocal, ready, serving, terminating bool, zoneHints sets.Set[string]) *BaseEndpointInfo { 128 return &BaseEndpointInfo{ 129 ip: ip, 130 port: port, 131 endpoint: net.JoinHostPort(ip, strconv.Itoa(port)), 132 isLocal: isLocal, 133 ready: ready, 134 serving: serving, 135 terminating: terminating, 136 zoneHints: zoneHints, 137 } 138 } 139