Skip to content

Commit

Permalink
Merge pull request #245 from shalb/tainted-graph-ref
Browse files Browse the repository at this point in the history
Tainted units, graph refactoring, fixes
  • Loading branch information
romanprog authored Dec 18, 2023
2 parents 46444aa + 70d4822 commit 39d1c49
Show file tree
Hide file tree
Showing 31 changed files with 1,165 additions and 657 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
git fetch --prune --unshallow --tags && make && cp ./bin/linux-amd64/cdev /usr/local/bin/
- name: Run tests
run: cd tests/test-project/ && cdev plan --tf-plan -l debug && cdev apply --force -l debug && cdev destroy --force -l debug
run: cd tests/test-project/ && cdev apply --force -l debug && cdev destroy --force -l debug
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Binary file added docs/images/cdev-tainted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (
require (
github.com/Azure/azure-pipeline-go v0.2.3 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/mattn/go-ieproxy v0.0.1 // indirect
)

Expand Down Expand Up @@ -94,7 +95,6 @@ require (
github.com/fatih/color v1.15.0 // indirect
github.com/getsops/gopgagent v0.0.0-20170926210634-4d7ea76ff71a // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/cdev/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var applyCmd = &cobra.Command{
Short: "Deploys or updates infrastructure according to project configuration",
RunE: func(cmd *cobra.Command, args []string) error {
project, err := project.LoadProjectFull()
if utils.GetEnv("CDEV_COLLECT_USAGE_STATS", "false") == "true" {
if utils.GetEnv("CDEV_COLLECT_USAGE_STATS", "true") != "false" {
log.Infof("Sending usage statistic. To disable statistics collection, export the CDEV_COLLECT_USAGE_STATS=false environment variable")
}
if err != nil {
Expand All @@ -31,11 +31,11 @@ var applyCmd = &cobra.Command{
if err != nil {
return NewCmdErr(project, "apply", err)
}
log.Info("The project was successfully applied")
err = project.PrintOutputs()
if err != nil {
return NewCmdErr(project, "apply", err)
}
project.UnLockState()
return NewCmdErr(project, "apply", nil)
},
}
Expand All @@ -44,4 +44,5 @@ func init() {
rootCmd.AddCommand(applyCmd)
applyCmd.Flags().BoolVar(&config.Global.IgnoreState, "ignore-state", false, "Apply even if the state has not changed.")
applyCmd.Flags().BoolVar(&config.Global.Force, "force", false, "Skip interactive approval.")
applyCmd.Flags().StringArrayVarP(&config.Global.Targets, "target", "t", []string{}, "Units and stack that will be applied. All others will not apply.")
}
3 changes: 3 additions & 0 deletions pkg/cmd/cdev/destroy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cdev

import (
"github.com/apex/log"
"github.com/shalb/cluster.dev/pkg/config"
"github.com/shalb/cluster.dev/pkg/project"
"github.com/spf13/cobra"
Expand All @@ -26,6 +27,7 @@ var destroyCmd = &cobra.Command{
project.UnLockState()
return NewCmdErr(project, "destroy", err)
}
log.Info("The project was successfully destroyed")
project.UnLockState()
return NewCmdErr(project, "destroy", nil)
},
Expand All @@ -35,4 +37,5 @@ func init() {
rootCmd.AddCommand(destroyCmd)
destroyCmd.Flags().BoolVar(&config.Global.IgnoreState, "ignore-state", false, "Destroy current configuration and ignore state.")
destroyCmd.Flags().BoolVar(&config.Global.Force, "force", false, "Skip interactive approval.")
destroyCmd.Flags().StringArrayVarP(&config.Global.Targets, "target", "t", []string{}, "Units and stack that will be destroyed. All others will not destroy.")
}
7 changes: 1 addition & 6 deletions pkg/cmd/cdev/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ var outputCmd = &cobra.Command{
if err != nil {
log.Fatalf("Fatal error: outputs: lock state: %v", err.Error())
}
stProject, err := project.LoadState()
if err != nil {
project.UnLockState()
log.Fatalf("Fatal error: outputs: load state: %v", err.Error())
}
err = stProject.PrintOutputs()
err = project.OwnState.PrintOutputs()
if err != nil {
log.Fatalf("Fatal error: outputs: print %v", err.Error())
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/cmd/cdev/plan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cdev

import (
"fmt"

"github.com/apex/log"
"github.com/shalb/cluster.dev/pkg/config"
"github.com/shalb/cluster.dev/pkg/project"
Expand All @@ -16,19 +18,19 @@ var planCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
project, err := project.LoadProjectFull()
if err != nil {
return NewCmdErr(nil, "plan", err)
return NewCmdErr(nil, "plan", fmt.Errorf("load project configuration: %w", err))
}
log.Info("Planning...")
_, err = project.Plan()
if err != nil {
return NewCmdErr(project, "plan", err)
return NewCmdErr(project, "plan", fmt.Errorf("build plan: %w", err))
}
return NewCmdErr(project, "plan", nil)
},
}

func init() {
rootCmd.AddCommand(planCmd)
planCmd.Flags().BoolVar(&config.Global.ShowTerraformPlan, "tf-plan", false, "Also show units terraform plan if possible.")
// planCmd.Flags().BoolVar(&config.Global.ShowTerraformPlan, "tf-plan", false, "Also show units terraform plan if possible.")
planCmd.Flags().BoolVar(&config.Global.IgnoreState, "force", false, "Show plan (if set tf-plan) even if the state has not changed.")
}
2 changes: 1 addition & 1 deletion pkg/cmd/cdev/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var projectLs = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
p, err := project.LoadProjectFull()
if err != nil {
log.Errorf("No project found in the current directory. Configuration error: %v", err.Error())
log.Errorf("Project configuration error: %v", err.Error())
return
}
log.Info("Project info:")
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/cdev/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var stateUnlockCmd = &cobra.Command{
Use: "unlock",
Short: "Unlock state force",
Run: func(cmd *cobra.Command, args []string) {
config.Global.IgnoreState = true
project, err := project.LoadProjectFull()
if err != nil {
log.Fatalf("Fatal error: state unlock: %v", err.Error())
Expand All @@ -35,7 +36,7 @@ var stateUpdateCmd = &cobra.Command{
Use: "update",
Short: "Updates the state of the current project to version %v. Make sure that the state of the project is consistent (run cdev apply with the old version before)",
Run: func(cmd *cobra.Command, args []string) {
config.Global.NotLoadState = true
config.Global.IgnoreState = true
project, err := project.LoadProjectFull()
if err != nil {
log.Fatalf("Fatal error: state update: %v", err.Error())
Expand All @@ -61,7 +62,7 @@ var statePullCmd = &cobra.Command{
Use: "pull",
Short: "Downloads the remote state",
Run: func(cmd *cobra.Command, args []string) {
config.Global.NotLoadState = true
config.Global.IgnoreState = true
project, err := project.LoadProjectFull()
if err != nil {
log.Fatalf("Fatal error: state pull: %v", err.Error())
Expand Down
4 changes: 4 additions & 0 deletions pkg/colors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
LightWhiteBold
LightRedBold
PurpleBold
Orange
OrangeBold
)

var colored = true
Expand All @@ -65,6 +67,8 @@ var colorsMap map[Color]ColoredFmt = map[Color]ColoredFmt{
WhiteBold: color.New(color.FgWhite, color.BgDefault, color.OpBold),
Yellow: color.New(color.FgYellow, color.BgDefault),
YellowBold: color.New(color.FgYellow, color.BgDefault, color.OpBold),
Orange: color.RGB(255, 153, 51),
OrangeBold: color.NewRGBStyle(color.RGB(255, 153, 51)).SetOpts(color.Opts{color.OpBold}),
}

// SetColored set all colors to default, if colored == false.
Expand Down
29 changes: 12 additions & 17 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ type ConfSpec struct {
UseCache bool
OptFooTest bool
IgnoreState bool
NotLoadState bool
ShowTerraformPlan bool
StateCacheDir string
TemplatesCacheDir string
CacheDir string
NoColor bool
Force bool
Interactive bool
OutputJSON bool
// ShowTerraformPlan bool
StateCacheDir string
TemplatesCacheDir string
CacheDir string
NoColor bool
Force bool
Interactive bool
OutputJSON bool
Targets []string
}

// Global config for executor.
Expand Down Expand Up @@ -102,13 +102,8 @@ func InitConfig() {
log.Fatal(err.Error())
}
}
Interrupted = false
}

// getEnv Helper for args parse.
func getEnv(key string, defaultVal string) string {
if envVal, ok := os.LookupEnv(key); ok {
return envVal
if Global.MaxParallel == 0 {
log.Fatal("Parallelism should be greater then 0.")
}
return defaultVal
Interrupted = false
}
33 changes: 33 additions & 0 deletions pkg/config/targetcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package config

import "strings"

type TargetUnits []string

func NewTargetsChecker(tg []string) *TargetUnits {
res := TargetUnits{}
res = append(res, tg...)
return &res
}

func (t *TargetUnits) Check(unitKey string) bool {
for _, target := range *t {
tgSplitted := strings.Split(target, ".")
uKeySplitted := strings.Split(unitKey, ".")
if len(tgSplitted) == 0 || len(tgSplitted) > 2 || len(uKeySplitted) != 2 {
return false
}
if len(tgSplitted) == 1 {
// Target is whole stack, check unit stack name only.
if uKeySplitted[0] == tgSplitted[0] {
return true
}
continue
}
// The target is unit, compare unit name and stack name.
if uKeySplitted[0] == tgSplitted[0] && uKeySplitted[1] == tgSplitted[1] {
return false
}
}
return false
}
Loading

0 comments on commit 39d1c49

Please sign in to comment.