Skip to content

Commit

Permalink
Made cluster default expiry configurable.
Browse files Browse the repository at this point in the history
It is now 0 for most init's and 1hr in CI (based on use of
--auto command, overridable with --noci).
  • Loading branch information
brett19 committed Dec 6, 2023
1 parent 5fe70f4 commit affe6de
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
11 changes: 9 additions & 2 deletions cbdcconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"os"
"path"
"time"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

const Version = 5
const Version = 6

type StringBool string

Expand Down Expand Up @@ -50,7 +51,8 @@ type Config struct {
Azure Config_Azure `yaml:"azure"`
Capella Config_Capella `yaml:"capella"`

DefaultDeployer string `yaml:"default-deployer"`
DefaultDeployer string `yaml:"default-deployer"`
DefaultExpiry time.Duration `yaml:"default-expiry"`

_DefaultCloud string `yaml:"default-cloud"`
}
Expand Down Expand Up @@ -142,6 +144,11 @@ func Upgrade(config *Config) *Config {
config.Version = 5
}

if config.Version < 6 {
config.DefaultExpiry = 0
config.Version = 6
}

return config
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"time"

"github.com/couchbaselabs/cbdinocluster/clusterdef"
"github.com/couchbaselabs/cbdinocluster/deployment"
Expand Down Expand Up @@ -69,11 +68,13 @@ var allocateCmd = &cobra.Command{
helper := CmdHelper{}
logger := helper.GetLogger()
ctx := helper.GetContext()
config := helper.GetConfig(ctx)

defStr, _ := cmd.Flags().GetString("def")
defFile, _ := cmd.Flags().GetString("def-file")
purpose, _ := cmd.Flags().GetString("purpose")
expiry, _ := cmd.Flags().GetDuration("expiry")
expiryIsSet := cmd.Flags().Changed("expiry")
deployerName, _ := cmd.Flags().GetString("deployer")
cloudProvider, _ := cmd.Flags().GetString("cloud-provider")

Expand All @@ -92,8 +93,10 @@ var allocateCmd = &cobra.Command{
if purpose != "" {
def.Purpose = purpose
}
if expiry > 0 {
if expiryIsSet {
def.Expiry = expiry
} else if def.Expiry == 0 {
def.Expiry = config.DefaultExpiry
}
if deployerName != "" {
def.Deployer = deployerName
Expand Down Expand Up @@ -134,7 +137,7 @@ func init() {
allocateCmd.Flags().String("def", "", "The cluster definition you wish to provision.")
allocateCmd.Flags().String("def-file", "", "The path to a file containing a cluster definition to provision.")
allocateCmd.Flags().String("purpose", "", "The purpose for allocating this cluster")
allocateCmd.Flags().Duration("expiry", 1*time.Hour, "The time to keep this cluster allocated for")
allocateCmd.Flags().Duration("expiry", 0, "The time to keep this cluster allocated for")
allocateCmd.Flags().String("deployer", "", "The name of the deployer to use")
allocateCmd.Flags().String("cloud-provider", "", "The cloud provider to use for this cluster")
}
31 changes: 31 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var initCmd = &cobra.Command{
ctx := context.Background()

autoConfig, _ := cmd.Flags().GetBool("auto")
nociConfig, _ := cmd.Flags().GetBool("noci")
ciConfig := autoConfig && !nociConfig

userHomePath, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -99,6 +101,20 @@ var initCmd = &cobra.Command{
}
}

readDuration := func(q string, defaultValue time.Duration) time.Duration {
for {
str := readString(q, defaultValue.String(), false)

dura, err := time.ParseDuration(str)
if err != nil {
fmt.Printf("Invalid entry, try again...\n")
continue
}

return dura
}
}

checkFileExists := func(path string) bool {
stat, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -939,6 +955,7 @@ var initCmd = &cobra.Command{

printBaseConfig := func() {
fmt.Printf(" Default Deployer: %s\n", curConfig.DefaultDeployer)
fmt.Printf(" Default Expiry: %s\n", curConfig.DefaultExpiry.String())
}
{
fmt.Printf("-- Base Configuration\n")
Expand All @@ -959,6 +976,19 @@ var initCmd = &cobra.Command{
curConfig.DefaultDeployer = defaultDeployer
}

{
var defaultExpiry time.Duration
if ciConfig {
defaultExpiry = 1 * time.Hour
}

defaultExpiry = readDuration(
"What cluster expiry should we use by default?",
defaultExpiry)

curConfig.DefaultExpiry = defaultExpiry
}

saveConfig()
}

Expand Down Expand Up @@ -991,6 +1021,7 @@ func init() {
rootCmd.AddCommand(initCmd)

initCmd.Flags().Bool("auto", false, "Automatically setup without any interactivity")
initCmd.Flags().Bool("noci", false, "Disable CI mode when using --auto")
initCmd.Flags().Bool("disable-docker", false, "Disable Docker")
initCmd.Flags().String("docker-host", "", "Docker host address to use")
initCmd.Flags().String("docker-network", "", "Docker network to use")
Expand Down
8 changes: 7 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ var listCmd = &cobra.Command{
deployerName := clusterInfo.DeployerName
cluster := clusterInfo.Info

expiry := cluster.GetExpiry()
expiryStr := "none"
if !expiry.IsZero() {
expiryStr = time.Until(cluster.GetExpiry()).Round(time.Second).String()
}

fmt.Printf(" %s [State: %s, Timeout: %s, Deployer: %s]\n",
cluster.GetID(),
cluster.GetState(),
time.Until(cluster.GetExpiry()).Round(time.Second),
expiryStr,
deployerName)
for _, node := range cluster.GetNodes() {
fmt.Printf(" %-16s %-20s %-20s %s\n",
Expand Down
7 changes: 6 additions & 1 deletion cmd/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ var refreshCmd = &cobra.Command{
logger.Fatal("failed to parse expiry time", zap.Error(err))
}

newExpiryTime := time.Time{}
if newExpiryDura > 0 {
newExpiryTime = time.Now().Add(newExpiryDura)
}

err = deployer.UpdateClusterExpiry(
ctx,
cluster.GetID(),
time.Now().Add(newExpiryDura))
newExpiryTime)
if err != nil {
logger.Fatal("failed to remove cluster", zap.Error(err))
}
Expand Down
9 changes: 7 additions & 2 deletions deployment/clouddeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,14 @@ func (p *Deployer) NewCluster(ctx context.Context, def *clusterdef.Cluster) (dep

clusterID := cbdcuuid.New()

expiryTime := time.Time{}
if def.Expiry > 0 {
expiryTime = time.Now().Add(def.Expiry)
}

metaData := stringclustermeta.MetaData{
ID: clusterID,
Expiry: time.Now().Add(def.Expiry),
Expiry: expiryTime,
}
projectName := metaData.String()

Expand Down Expand Up @@ -919,7 +924,7 @@ func (p *Deployer) Cleanup(ctx context.Context) error {

curTime := time.Now()
for _, cluster := range clusters {
if !cluster.Meta.Expiry.After(curTime) {
if !cluster.Meta.Expiry.IsZero() && !cluster.Meta.Expiry.After(curTime) {
p.logger.Info("removing cluster",
zap.String("cluster-id", cluster.Meta.ID.String()))

Expand Down
5 changes: 4 additions & 1 deletion deployment/dockerdeploy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ func (c *Controller) DeployNode(ctx context.Context, def *DeployNodeOptions) (*N
return nil, errors.Wrap(err, "failed to start container")
}

expiryTime := time.Now().Add(def.Expiry)
expiryTime := time.Time{}
if def.Expiry > 0 {
expiryTime = time.Now().Add(def.Expiry)
}

err = c.WriteNodeState(ctx, containerID, &DockerNodeState{
Expiry: expiryTime,
Expand Down
4 changes: 2 additions & 2 deletions deployment/dockerdeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (d *Deployer) listClusters(ctx context.Context) ([]*ClusterInfo, error) {
cluster.Creator = node.Creator
cluster.Owner = node.Owner
cluster.Purpose = node.Purpose
if node.Expiry.After(cluster.Expiry) {
if !node.Expiry.IsZero() && node.Expiry.After(cluster.Expiry) {
cluster.Expiry = node.Expiry
}
cluster.Nodes = append(cluster.Nodes, &ClusterNodeInfo{
Expand Down Expand Up @@ -680,7 +680,7 @@ func (d *Deployer) Cleanup(ctx context.Context) error {

curTime := time.Now()
for _, node := range nodes {
if !node.Expiry.After(curTime) {
if !node.Expiry.IsZero() && !node.Expiry.After(curTime) {
d.logger.Info("removing node",
zap.String("id", node.NodeID),
zap.String("container", node.ContainerID))
Expand Down

0 comments on commit affe6de

Please sign in to comment.