Artem Chernyshev 04e267a550 feat: handle unsupported commands being called for docker
Return proper message back to the client in case if called method is not
supported by mode any particular node runs in.

Fixes: https://github.com/talos-systems/talos/issues/2629

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
2020-10-14 13:44:38 -07:00

86 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package runtime
import (
"fmt"
)
// Mode is a runtime mode.
type Mode int
// ModeCapability describes mode capability flags.
type ModeCapability uint64
const (
// ModeCloud is the cloud runtime mode.
ModeCloud Mode = iota
// ModeContainer is the container runtime mode.
ModeContainer
// ModeMetal is the metal runtime mode.
ModeMetal
)
const (
// Reboot node reboot.
Reboot ModeCapability = 1 << iota
// Rollback node rollback.
Rollback
// Shutdown node shutdown.
Shutdown
// Upgrade node upgrade.
Upgrade
)
const (
cloud = "cloud"
container = "container"
metal = "metal"
)
// String returns the string representation of a Mode.
func (m Mode) String() string {
return [...]string{cloud, container, metal}[m]
}
// RequiresInstall implements config.RuntimeMode.
func (m Mode) RequiresInstall() bool {
return m == ModeMetal
}
// Supports returns mode capability.
func (m Mode) Supports(feature ModeCapability) bool {
return (m.capabilities() & uint64(feature)) != 0
}
// ParseMode returns a `Mode` that matches the specified string.
func ParseMode(s string) (mod Mode, err error) {
switch s {
case cloud:
mod = ModeCloud
case container:
mod = ModeContainer
case metal:
mod = ModeMetal
default:
return mod, fmt.Errorf("unknown runtime mode: %q", s)
}
return mod, nil
}
func (m Mode) capabilities() uint64 {
all := ^uint64(0)
return [...]uint64{
// metal
all,
// container
all ^ uint64(Reboot|Shutdown|Upgrade|Rollback),
// cloud
all,
}[m]
}