k8s.io/kubernetes@v1.29.3/pkg/kubelet/types/types.go (about) 1 /* 2 Copyright 2015 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 types 18 19 import ( 20 "net/http" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/types" 25 ) 26 27 // TODO: Reconcile custom types in kubelet/types and this subpackage 28 29 // HTTPDoer encapsulates http.Do functionality 30 type HTTPDoer interface { 31 Do(req *http.Request) (*http.Response, error) 32 } 33 34 // Timestamp wraps around time.Time and offers utilities to format and parse 35 // the time using RFC3339Nano 36 type Timestamp struct { 37 time time.Time 38 } 39 40 // NewTimestamp returns a Timestamp object using the current time. 41 func NewTimestamp() *Timestamp { 42 return &Timestamp{time.Now()} 43 } 44 45 // ConvertToTimestamp takes a string, parses it using the RFC3339NanoLenient layout, 46 // and converts it to a Timestamp object. 47 func ConvertToTimestamp(timeString string) *Timestamp { 48 parsed, _ := time.Parse(RFC3339NanoLenient, timeString) 49 return &Timestamp{parsed} 50 } 51 52 // Get returns the time as time.Time. 53 func (t *Timestamp) Get() time.Time { 54 return t.time 55 } 56 57 // GetString returns the time in the string format using the RFC3339NanoFixed 58 // layout. 59 func (t *Timestamp) GetString() string { 60 return t.time.Format(RFC3339NanoFixed) 61 } 62 63 // SortedContainerStatuses is a type to help sort container statuses based on container names. 64 type SortedContainerStatuses []v1.ContainerStatus 65 66 func (s SortedContainerStatuses) Len() int { return len(s) } 67 func (s SortedContainerStatuses) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 68 69 func (s SortedContainerStatuses) Less(i, j int) bool { 70 return s[i].Name < s[j].Name 71 } 72 73 // SortInitContainerStatuses ensures that statuses are in the order that their 74 // init container appears in the pod spec 75 func SortInitContainerStatuses(p *v1.Pod, statuses []v1.ContainerStatus) { 76 containers := p.Spec.InitContainers 77 current := 0 78 for _, container := range containers { 79 for j := current; j < len(statuses); j++ { 80 if container.Name == statuses[j].Name { 81 statuses[current], statuses[j] = statuses[j], statuses[current] 82 current++ 83 break 84 } 85 } 86 } 87 } 88 89 // SortStatusesOfInitContainers returns the statuses of InitContainers of pod p, 90 // in the order that they appear in its spec. 91 func SortStatusesOfInitContainers(p *v1.Pod, statusMap map[string]*v1.ContainerStatus) []v1.ContainerStatus { 92 containers := p.Spec.InitContainers 93 statuses := []v1.ContainerStatus{} 94 for _, container := range containers { 95 if status, found := statusMap[container.Name]; found { 96 statuses = append(statuses, *status) 97 } 98 } 99 return statuses 100 } 101 102 // Reservation represents reserved resources for non-pod components. 103 type Reservation struct { 104 // System represents resources reserved for non-kubernetes components. 105 System v1.ResourceList 106 // Kubernetes represents resources reserved for kubernetes system components. 107 Kubernetes v1.ResourceList 108 } 109 110 // ResolvedPodUID is a pod UID which has been translated/resolved to the representation known to kubelets. 111 type ResolvedPodUID types.UID 112 113 // MirrorPodUID is a pod UID for a mirror pod. 114 type MirrorPodUID types.UID