2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package role
|
|
|
|
|
2022-04-11 04:31:10 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
//go:generate stringer -type=Role
|
|
|
|
|
|
|
|
// Role is a peer's role.
|
|
|
|
type Role uint
|
|
|
|
|
|
|
|
const (
|
2022-11-09 09:57:54 -05:00
|
|
|
// Unknown is the default value for Role and should have no meaning.
|
2022-03-22 11:03:15 -04:00
|
|
|
Unknown Role = iota
|
2022-11-09 09:57:54 -05:00
|
|
|
// ControlPlane declares this node as a Kubernetes control plane node.
|
2022-06-29 09:26:29 -04:00
|
|
|
ControlPlane
|
2022-11-09 09:57:54 -05:00
|
|
|
// Worker declares this node as a Kubernetes worker node.
|
2022-06-29 09:26:29 -04:00
|
|
|
Worker
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
2022-04-11 04:31:10 -04:00
|
|
|
|
|
|
|
// MarshalJSON marshals the Role to JSON string.
|
|
|
|
func (r Role) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the Role from JSON string.
|
|
|
|
func (r *Role) UnmarshalJSON(b []byte) error {
|
|
|
|
var roleString string
|
|
|
|
if err := json.Unmarshal(b, &roleString); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-06 05:57:35 -04:00
|
|
|
*r = FromString(roleString)
|
2022-04-11 04:31:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-12 07:40:38 -04:00
|
|
|
|
2022-10-06 05:57:35 -04:00
|
|
|
// FromString returns the Role for the given string.
|
2022-10-12 07:40:38 -04:00
|
|
|
func FromString(s string) Role {
|
|
|
|
switch strings.ToLower(s) {
|
2022-10-06 05:57:35 -04:00
|
|
|
case "controlplane", "control-plane":
|
2022-10-12 07:40:38 -04:00
|
|
|
return ControlPlane
|
|
|
|
case "worker":
|
|
|
|
return Worker
|
|
|
|
default:
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
}
|