k8s.io/kubernetes@v1.29.3/pkg/kubelet/prober/results/results_manager.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 results 18 19 import ( 20 "sync" 21 22 v1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/types" 24 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 25 ) 26 27 // Manager provides a probe results cache and channel of updates. 28 type Manager interface { 29 // Get returns the cached result for the container with the given ID. 30 Get(kubecontainer.ContainerID) (Result, bool) 31 // Set sets the cached result for the container with the given ID. 32 // The pod is only included to be sent with the update. 33 Set(kubecontainer.ContainerID, Result, *v1.Pod) 34 // Remove clears the cached result for the container with the given ID. 35 Remove(kubecontainer.ContainerID) 36 // Updates creates a channel that receives an Update whenever its result changes (but not 37 // removed). 38 // NOTE: The current implementation only supports a single updates channel. 39 Updates() <-chan Update 40 } 41 42 // Result is the type for probe results. 43 type Result int 44 45 const ( 46 // Unknown is encoded as -1 (type Result) 47 Unknown Result = iota - 1 48 49 // Success is encoded as 0 (type Result) 50 Success 51 52 // Failure is encoded as 1 (type Result) 53 Failure 54 ) 55 56 func (r Result) String() string { 57 switch r { 58 case Success: 59 return "Success" 60 case Failure: 61 return "Failure" 62 default: 63 return "UNKNOWN" 64 } 65 } 66 67 // ToPrometheusType translates a Result to a form which is better understood by prometheus. 68 func (r Result) ToPrometheusType() float64 { 69 switch r { 70 case Success: 71 return 0 72 case Failure: 73 return 1 74 default: 75 return -1 76 } 77 } 78 79 // Update is an enum of the types of updates sent over the Updates channel. 80 type Update struct { 81 ContainerID kubecontainer.ContainerID 82 Result Result 83 PodUID types.UID 84 } 85 86 // Manager implementation. 87 type manager struct { 88 // guards the cache 89 sync.RWMutex 90 // map of container ID -> probe Result 91 cache map[kubecontainer.ContainerID]Result 92 // channel of updates 93 updates chan Update 94 } 95 96 var _ Manager = &manager{} 97 98 // NewManager creates and returns an empty results manager. 99 func NewManager() Manager { 100 return &manager{ 101 cache: make(map[kubecontainer.ContainerID]Result), 102 updates: make(chan Update, 20), 103 } 104 } 105 106 func (m *manager) Get(id kubecontainer.ContainerID) (Result, bool) { 107 m.RLock() 108 defer m.RUnlock() 109 result, found := m.cache[id] 110 return result, found 111 } 112 113 func (m *manager) Set(id kubecontainer.ContainerID, result Result, pod *v1.Pod) { 114 if m.setInternal(id, result) { 115 m.updates <- Update{id, result, pod.UID} 116 } 117 } 118 119 // Internal helper for locked portion of set. Returns whether an update should be sent. 120 func (m *manager) setInternal(id kubecontainer.ContainerID, result Result) bool { 121 m.Lock() 122 defer m.Unlock() 123 prev, exists := m.cache[id] 124 if !exists || prev != result { 125 m.cache[id] = result 126 return true 127 } 128 return false 129 } 130 131 func (m *manager) Remove(id kubecontainer.ContainerID) { 132 m.Lock() 133 defer m.Unlock() 134 delete(m.cache, id) 135 } 136 137 func (m *manager) Updates() <-chan Update { 138 return m.updates 139 }