Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add Feature dependency #1357

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
- (Feature) Backup lifetime - remove Backup once its lifetime has been reached
- (Feature) Add Feature dependency

## [1.2.31](https://github.com/arangodb/kube-arangodb/tree/1.2.31) (2023-07-14)
- (Improvement) Block traffic on the services if there is more than 1 active leader in ActiveFailover mode
Expand Down
9 changes: 6 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,12 @@ func executeMain(cmd *cobra.Command, args []string) {

podNameParts := strings.Split(name, "-")
operatorID := podNameParts[len(podNameParts)-1]
logging.Global().RegisterWrappers(func(in *zerolog.Event) *zerolog.Event {
return in.Str("operator-id", operatorID)
})

if operatorID != "" {
logging.Global().RegisterWrappers(func(in *zerolog.Event) *zerolog.Event {
return in.Str("operator-id", operatorID)
})
}

logger.Info("nice to meet you")

Expand Down
40 changes: 40 additions & 0 deletions pkg/deployment/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package features

import (
"sort"

"github.com/arangodb/go-driver"

api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
Expand All @@ -32,11 +34,30 @@ const (
Disabled = "false"
)

type Features []Feature

func (f Features) Get(name string) (Feature, bool) {
for _, feature := range features {
if feature.Name() == name {
return feature, true
}
}

return nil, false
}

func (f Features) Sort() {
sort.Slice(f, func(i, j int) bool {
return f[i].Name() < f[j].Name()
})
}

var _ Feature = &feature{}

type Feature interface {
Name() string
Description() string
Dependencies() []Feature
Version() driver.Version
EnterpriseRequired() bool
OperatorEnterpriseRequired() bool
Expand All @@ -56,6 +77,19 @@ type feature struct {
deprecated string
constValue *bool
hidden bool
dependencies []Feature
}

func (f feature) Dependencies() []Feature {
if len(f.dependencies) == 0 {
return nil
}

q := make([]Feature, len(f.dependencies))

copy(q, f.dependencies)

return q
}

func (f feature) ImageSupported(i *api.ImageInfo) bool {
Expand All @@ -82,6 +116,12 @@ func (f feature) Enabled() bool {
}
}

for _, dep := range f.dependencies {
if !dep.Enabled() {
return false
}
}

if f.constValue != nil {
return *f.constValue
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/deployment/features/graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var optionalGracefulShutdown = &feature{
enterpriseRequired: false,
enabledByDefault: false,
hidden: true,

dependencies: []Feature{gracefulShutdown},
}

func GracefulShutdown() Feature {
Expand Down
31 changes: 22 additions & 9 deletions pkg/deployment/features/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package features
import (
"fmt"
"os"
"strings"
"sync"

"github.com/spf13/cobra"
Expand All @@ -34,23 +35,21 @@ import (

const prefixArg = "deployment.feature"

var features = map[string]Feature{}
var features Features
var featuresLock sync.Mutex
var enableAll = false

func registerFeature(f Feature) {
featuresLock.Lock()
defer featuresLock.Unlock()

if f == nil {
panic("Feature cannot be nil")
}

if _, ok := features[f.Name()]; ok {
if _, ok := features.Get(f.Name()); ok {
panic("Feature already registered")
}

features[f.Name()] = f
features = append(features, f)

features.Sort()
}

var internalCMD = &cobra.Command{
Expand All @@ -67,8 +66,8 @@ func Iterate(iterator Iterator) {
featuresLock.Lock()
defer featuresLock.Unlock()

for name, feature := range features {
iterator(name, feature)
for _, feature := range features {
iterator(feature.Name(), feature)
}
}

Expand Down Expand Up @@ -130,6 +129,13 @@ func cmdRun(_ *cobra.Command, _ []string) {
for _, feature := range features {
println(fmt.Sprintf("Feature: %s", feature.Name()))
println(fmt.Sprintf("Description: %s", feature.Description()))
if deps := feature.Dependencies(); len(deps) > 0 {
names := make([]string, len(deps))
for id := range names {
names[id] = deps[id].Name()
}
println(fmt.Sprintf("Dependencies: %s", strings.Join(names, ", ")))
}
if feature.EnabledByDefault() {
println("Enabled: true")
} else {
Expand All @@ -155,6 +161,7 @@ func cmdRun(_ *cobra.Command, _ []string) {

// Supported returns false when:
// - feature is disabled.
// - any feature dependency is disabled.
// - a given version is lower than minimum feature version.
// - feature expects enterprise but a given enterprise arg is not true.
func Supported(f Feature, v driver.Version, enterprise bool) bool {
Expand All @@ -167,6 +174,12 @@ func Supported(f Feature, v driver.Version, enterprise bool) bool {
return false
}

for _, dependency := range f.Dependencies() {
if !Supported(dependency, v, enterprise) {
return false
}
}

return v.CompareTo(f.Version()) >= 0
}

Expand Down