k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/controlplane/apiserver/peer.go (about) 1 /* 2 Copyright 2024 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 apiserver 18 19 import ( 20 "fmt" 21 "net" 22 "strconv" 23 "time" 24 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apiserver/pkg/reconcilers" 27 genericapiserver "k8s.io/apiserver/pkg/server" 28 serverstorage "k8s.io/apiserver/pkg/server/storage" 29 "k8s.io/apiserver/pkg/storageversion" 30 utilpeerproxy "k8s.io/apiserver/pkg/util/peerproxy" 31 clientgoinformers "k8s.io/client-go/informers" 32 "k8s.io/client-go/transport" 33 "k8s.io/klog/v2" 34 api "k8s.io/kubernetes/pkg/apis/core" 35 ) 36 37 const ( 38 // DefaultPeerEndpointReconcileInterval is the default amount of time for how often 39 // the peer endpoint leases are reconciled. 40 DefaultPeerEndpointReconcileInterval = 10 * time.Second 41 // DefaultPeerEndpointReconcilerTTL is the default TTL timeout for peer endpoint 42 // leases on the storage layer 43 DefaultPeerEndpointReconcilerTTL = 15 * time.Second 44 ) 45 46 func BuildPeerProxy(versionedInformer clientgoinformers.SharedInformerFactory, svm storageversion.Manager, 47 proxyClientCertFile string, proxyClientKeyFile string, peerCAFile string, peerAdvertiseAddress reconcilers.PeerAdvertiseAddress, 48 apiServerID string, reconciler reconcilers.PeerEndpointLeaseReconciler, serializer runtime.NegotiatedSerializer) (utilpeerproxy.Interface, error) { 49 if proxyClientCertFile == "" { 50 return nil, fmt.Errorf("error building peer proxy handler, proxy-cert-file not specified") 51 } 52 if proxyClientKeyFile == "" { 53 return nil, fmt.Errorf("error building peer proxy handler, proxy-key-file not specified") 54 } 55 // create proxy client config 56 clientConfig := &transport.Config{ 57 TLS: transport.TLSConfig{ 58 Insecure: false, 59 CertFile: proxyClientCertFile, 60 KeyFile: proxyClientKeyFile, 61 CAFile: peerCAFile, 62 ServerName: "kubernetes.default.svc", 63 }} 64 65 // build proxy transport 66 proxyRoundTripper, transportBuildingError := transport.New(clientConfig) 67 if transportBuildingError != nil { 68 klog.Error(transportBuildingError.Error()) 69 return nil, transportBuildingError 70 } 71 return utilpeerproxy.NewPeerProxyHandler( 72 versionedInformer, 73 svm, 74 proxyRoundTripper, 75 apiServerID, 76 reconciler, 77 serializer, 78 ), nil 79 } 80 81 // CreatePeerEndpointLeaseReconciler creates a apiserver endpoint lease reconciliation loop 82 // The peer endpoint leases are used to find network locations of apiservers for peer proxy 83 func CreatePeerEndpointLeaseReconciler(c genericapiserver.Config, storageFactory serverstorage.StorageFactory) (reconcilers.PeerEndpointLeaseReconciler, error) { 84 ttl := DefaultPeerEndpointReconcilerTTL 85 config, err := storageFactory.NewConfig(api.Resource("apiServerPeerIPInfo")) 86 if err != nil { 87 return nil, fmt.Errorf("error creating storage factory config: %w", err) 88 } 89 reconciler, err := reconcilers.NewPeerEndpointLeaseReconciler(config, "/peerserverleases/", ttl) 90 return reconciler, err 91 } 92 93 // utility function to get the apiserver address that is used by peer apiservers to proxy 94 // requests to this apiserver in case the peer is incapable of serving the request 95 func getPeerAddress(peerAdvertiseAddress reconcilers.PeerAdvertiseAddress, publicAddress net.IP, publicServicePort int) string { 96 if peerAdvertiseAddress.PeerAdvertiseIP != "" && peerAdvertiseAddress.PeerAdvertisePort != "" { 97 return net.JoinHostPort(peerAdvertiseAddress.PeerAdvertiseIP, peerAdvertiseAddress.PeerAdvertisePort) 98 } else { 99 return net.JoinHostPort(publicAddress.String(), strconv.Itoa(publicServicePort)) 100 } 101 }