k8s.io/kubernetes@v1.29.3/pkg/kubelet/util/util.go (about) 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 util 18 19 import ( 20 "fmt" 21 22 v1 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/kubernetes/pkg/util/filesystem" 25 ) 26 27 // FromApiserverCache modifies <opts> so that the GET request will 28 // be served from apiserver cache instead of from etcd. 29 func FromApiserverCache(opts *metav1.GetOptions) { 30 opts.ResourceVersion = "0" 31 } 32 33 var IsUnixDomainSocket = filesystem.IsUnixDomainSocket 34 35 // GetNodenameForKernel gets hostname value to set in the hostname field (the nodename field of struct utsname) of the pod. 36 func GetNodenameForKernel(hostname string, hostDomainName string, setHostnameAsFQDN *bool) (string, error) { 37 kernelHostname := hostname 38 // FQDN has to be 64 chars to fit in the Linux nodename kernel field (specification 64 chars and the null terminating char). 39 const fqdnMaxLen = 64 40 if len(hostDomainName) > 0 && setHostnameAsFQDN != nil && *setHostnameAsFQDN { 41 fqdn := fmt.Sprintf("%s.%s", hostname, hostDomainName) 42 // FQDN has to be shorter than hostnameMaxLen characters. 43 if len(fqdn) > fqdnMaxLen { 44 return "", fmt.Errorf("failed to construct FQDN from pod hostname and cluster domain, FQDN %s is too long (%d characters is the max, %d characters requested)", fqdn, fqdnMaxLen, len(fqdn)) 45 } 46 kernelHostname = fqdn 47 } 48 return kernelHostname, nil 49 } 50 51 // GetContainerByIndex validates and extracts the container at index "idx" from 52 // "containers" with respect to "statuses". 53 // It returns true if the container is valid, else returns false. 54 func GetContainerByIndex(containers []v1.Container, statuses []v1.ContainerStatus, idx int) (v1.Container, bool) { 55 if idx < 0 || idx >= len(containers) || idx >= len(statuses) { 56 return v1.Container{}, false 57 } 58 if statuses[idx].Name != containers[idx].Name { 59 return v1.Container{}, false 60 } 61 return containers[idx], true 62 }