Files
kata-containers/virtcontainers/experimental/experimental.go
Wei Zhang da80c70c0c config: enhance Feature structure
Fixes #1226

Add more fields to better describe an experimental feature.

Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
2019-03-10 22:44:41 +08:00

64 lines
1.3 KiB
Go

// Copyright (c) 2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package experimental
import (
"fmt"
"regexp"
)
const (
nameRegStr = "^[a-z][a-z0-9_]*$"
)
// Feature to be experimental
type Feature struct {
Name string
Description string
// the expected release version to move out from experimental
ExpRelease string
}
var (
supportedFeatures = make(map[string]Feature)
)
// Register register a new experimental feature
func Register(feature Feature) error {
if err := validateFeature(feature); err != nil {
return err
}
if _, ok := supportedFeatures[feature.Name]; ok {
return fmt.Errorf("Feature %q had been registered before", feature.Name)
}
supportedFeatures[feature.Name] = feature
return nil
}
// Get returns Feature with requested name
func Get(name string) *Feature {
if f, ok := supportedFeatures[name]; ok {
return &f
}
return nil
}
func validateFeature(feature Feature) error {
if len(feature.Name) == 0 ||
len(feature.Description) == 0 ||
len(feature.ExpRelease) == 0 {
return fmt.Errorf("experimental feature must have valid name, description and expected release")
}
reg := regexp.MustCompile(nameRegStr)
if !reg.MatchString(feature.Name) {
return fmt.Errorf("feature name must in the format %q", nameRegStr)
}
return nil
}