kubelet: change map[string]RuntimeHandler to []RuntimeHandler

The map is changed to an array so as to retain the order of the original array
propagated from the CRI runtime.

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2024-03-09 09:48:07 +09:00
parent d3d06c3c7e
commit 27f24a62e3
No known key found for this signature in database
GPG Key ID: 49524C6F9F638F1A
5 changed files with 17 additions and 16 deletions

View File

@ -530,8 +530,8 @@ const (
type RuntimeStatus struct {
// Conditions is an array of current observed runtime conditions.
Conditions []RuntimeCondition
// Handlers is a map of current available handlers
Handlers map[string]RuntimeHandler
// Handlers is an array of current available handlers
Handlers []RuntimeHandler
}
// GetRuntimeCondition gets a specified runtime condition from the runtime status.

View File

@ -123,11 +123,12 @@ func (kl *Kubelet) HandlerSupportsUserNamespaces(rtHandler string) (bool, error)
if rtHandlers == nil {
return false, fmt.Errorf("runtime handlers are not set")
}
h, found := rtHandlers[rtHandler]
if !found {
return false, fmt.Errorf("the handler %q is not known", rtHandler)
for _, h := range rtHandlers {
if h.Name == rtHandler {
return h.SupportsUserNamespaces, nil
}
}
return h.SupportsUserNamespaces, nil
return false, fmt.Errorf("the handler %q is not known", rtHandler)
}
// GetKubeletMappings gets the additional IDs allocated for the Kubelet.

View File

@ -109,13 +109,13 @@ func TestHandlerSupportsUserNamespaces(t *testing.T) {
defer testKubelet.Cleanup()
kubelet := testKubelet.kubelet
kubelet.runtimeState.setRuntimeHandlers(map[string]kubecontainer.RuntimeHandler{
"has-support": {
kubelet.runtimeState.setRuntimeHandlers([]kubecontainer.RuntimeHandler{
{
Name: "has-support",
SupportsUserNamespaces: true,
},
"has-no-support": {
Name: "has-support",
{
Name: "has-no-support",
SupportsUserNamespaces: false,
},
})

View File

@ -219,13 +219,13 @@ func toKubeRuntimeStatus(status *runtimeapi.RuntimeStatus, handlers []*runtimeap
Message: c.Message,
})
}
retHandlers := make(map[string]kubecontainer.RuntimeHandler)
for _, h := range handlers {
retHandlers := make([]kubecontainer.RuntimeHandler, len(handlers))
for i, h := range handlers {
supportsUserns := false
if h.Features != nil {
supportsUserns = h.Features.UserNamespaces
}
retHandlers[h.Name] = kubecontainer.RuntimeHandler{
retHandlers[i] = kubecontainer.RuntimeHandler{
Name: h.Name,
SupportsUserNamespaces: supportsUserns,
}

View File

@ -35,7 +35,7 @@ type runtimeState struct {
storageError error
cidr string
healthChecks []*healthCheck
rtHandlers map[string]kubecontainer.RuntimeHandler
rtHandlers []kubecontainer.RuntimeHandler
}
// A health check function should be efficient and not rely on external
@ -71,13 +71,13 @@ func (s *runtimeState) setRuntimeState(err error) {
s.runtimeError = err
}
func (s *runtimeState) setRuntimeHandlers(rtHandlers map[string]kubecontainer.RuntimeHandler) {
func (s *runtimeState) setRuntimeHandlers(rtHandlers []kubecontainer.RuntimeHandler) {
s.Lock()
defer s.Unlock()
s.rtHandlers = rtHandlers
}
func (s *runtimeState) runtimeHandlers() map[string]kubecontainer.RuntimeHandler {
func (s *runtimeState) runtimeHandlers() []kubecontainer.RuntimeHandler {
s.RLock()
defer s.RUnlock()
return s.rtHandlers