Skip to content

Commit

Permalink
Merge pull request #2284 from zerbitx/ENG-5133
Browse files Browse the repository at this point in the history
feat: Native sleep mode config
  • Loading branch information
lizardruss authored Nov 22, 2024
2 parents f6d72d3 + 1aa16f0 commit ab765d6
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
66 changes: 66 additions & 0 deletions chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@
"type": "object",
"description": "APIServiceService holds the service name and namespace of the host apiservice."
},
"AutoSleepExclusion": {
"properties": {
"selector": {
"$ref": "#/$defs/LabelSelector"
}
},
"additionalProperties": false,
"type": "object",
"description": "AutoSleepExclusion holds conifiguration for excluding workloads from sleeping by label(s)"
},
"AutoWakeup": {
"properties": {
"schedule": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"description": "AutoWakeup holds the cron schedule to wake workloads automatically"
},
"BackingStore": {
"properties": {
"etcd": {
Expand Down Expand Up @@ -2913,6 +2933,48 @@
"additionalProperties": false,
"type": "object"
},
"SleepMode": {
"properties": {
"enabled": {
"type": "boolean",
"description": "Enabled toggles the sleep mode functionality, allowing for disabling sleep mode without removing other config"
},
"timeZone": {
"type": "string",
"description": "Timezone represents the timezone a sleep schedule should run against, defaulting to UTC if unset"
},
"autoSleep": {
"$ref": "#/$defs/SleepModeAutoSleep",
"description": "AutoSleep holds autoSleep details"
}
},
"additionalProperties": false,
"type": "object",
"description": "SleepMode holds configuration for native/workload only sleep mode"
},
"SleepModeAutoSleep": {
"properties": {
"afterInactivity": {
"type": "integer",
"description": "AfterInactivity represents how long a vCluster can be idle before workloads are automaticaly put to sleep"
},
"schedule": {
"type": "string",
"description": "Schedule represents a cron schedule for when to sleep workloads"
},
"wakeup": {
"$ref": "#/$defs/AutoWakeup",
"description": "Wakeup holds configuration for waking the vCluster on a schedule rather than waiting for some activity."
},
"exclude": {
"$ref": "#/$defs/AutoSleepExclusion",
"description": "Exclude holds configuration for labels that, if present, will prevent a workload from going to sleep"
}
},
"additionalProperties": false,
"type": "object",
"description": "SleepModeAutoSleep holds configuration for allowing a vCluster to sleep its workloads automatically"
},
"StatefulSetImage": {
"properties": {
"registry": {
Expand Down Expand Up @@ -3653,6 +3715,10 @@
"$ref": "#/$defs/Plugin"
},
"description": "Plugin specifies which vCluster plugins to enable. Use \"plugins\" instead. Do not use this option anymore."
},
"sleepMode": {
"$ref": "#/$defs/SleepMode",
"description": "SleepMode holds the native sleep mode configuration for Pro clusters"
}
},
"additionalProperties": false,
Expand Down
71 changes: 71 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package config

import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"strings"
"time"

"github.com/invopop/jsonschema"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -74,6 +76,9 @@ type Config struct {

// Plugin specifies which vCluster plugins to enable. Use "plugins" instead. Do not use this option anymore.
Plugin map[string]Plugin `json:"plugin,omitempty"`

// SleepMode holds the native sleep mode configuration for Pro clusters
SleepMode *SleepMode `json:"sleepMode,omitempty"`
}

// Integrations holds config for vCluster integrations with other operators or tools running on the host cluster
Expand Down Expand Up @@ -2315,3 +2320,69 @@ func addProToJSONSchema(base *jsonschema.Schema, t reflect.Type) {
central.Extras["pro"] = true
}
}

// SleepMode holds configuration for native/workload only sleep mode
type SleepMode struct {
// Enabled toggles the sleep mode functionality, allowing for disabling sleep mode without removing other config
Enabled bool `json:"enabled,omitempty"`
// Timezone represents the timezone a sleep schedule should run against, defaulting to UTC if unset
TimeZone string `json:"timeZone,omitempty"`
// AutoSleep holds autoSleep details
AutoSleep SleepModeAutoSleep `json:"autoSleep,omitempty"`
}

// SleepModeAutoSleep holds configuration for allowing a vCluster to sleep its workloads
// automatically
type SleepModeAutoSleep struct {
// AfterInactivity represents how long a vCluster can be idle before workloads are automaticaly put to sleep
AfterInactivity Duration `json:"afterInactivity,omitempty"`

// Schedule represents a cron schedule for when to sleep workloads
Schedule string `json:"schedule,omitempty"`

// Wakeup holds configuration for waking the vCluster on a schedule rather than waiting for some activity.
Wakeup AutoWakeup `json:"wakeup,omitempty"`

// Exclude holds configuration for labels that, if present, will prevent a workload from going to sleep
Exclude AutoSleepExclusion `json:"exclude,omitempty"`
}

// Duration allows for automatic Marshalling from strings like "1m" to a time.Duration
type Duration time.Duration

// MarshalJSON implements Marshaler
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}

// UnmarshalJSON implements Marshaler
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(time.Duration(value))
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(tmp)
return nil
default:
return errors.New("invalid duration")
}
}

// AutoWakeup holds the cron schedule to wake workloads automatically
type AutoWakeup struct {
Schedule string `json:"schedule,omitempty"`
}

// AutoSleepExclusion holds conifiguration for excluding workloads from sleeping by label(s)
type AutoSleepExclusion struct {
Selector LabelSelector `json:"selector,omitempty"`
}

0 comments on commit ab765d6

Please sign in to comment.