Skip to content

Commit

Permalink
https://github.com/shalb/cluster.dev/issues/280
Browse files Browse the repository at this point in the history
small fixes
  • Loading branch information
arti-shalb committed Sep 10, 2024
1 parent 2e811eb commit 86de7ca
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 30 deletions.
3 changes: 2 additions & 1 deletion internal/cmd/cdev/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ 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.")
// applyCmd.Flags().StringArrayVarP(&config.Global.Targets, "target", "t", []string{}, "Units and stack that will be applied. All others will not apply.")
// applyCmd.Flags().StringArrayVarP(&config.Global.Targets, "target-except", "", []string{}, "Units and stack that will be applied. All others will not apply.")
}
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type ConfSpec struct {
Interactive bool
OutputJSON bool
Targets []string
TargetsExclude []string
}

// Global config for executor.
Expand Down Expand Up @@ -105,5 +106,8 @@ func InitConfig() {
if Global.MaxParallel == 0 {
log.Fatal("Parallelism should be greater then 0.")
}
if len(Global.Targets) > 0 && len(Global.TargetsExclude) > 0 {
log.Fatalf("Option conflict: both options --target and --target-exclude are set, use one: %s", err.Error())
}
Interrupted = false
}
34 changes: 34 additions & 0 deletions internal/project/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,37 @@ func dependenciesRecursiveIterateDepth(u Unit, f func(Unit) error, depth int) er
}
return nil
}

func checkUnitPath(u Unit, path string) bool {
pathSpl := strings.Split(path, ".")
if len(pathSpl) == 1 { // path is only stack name, so it include all units of stack
return u.Stack().Name == pathSpl[0]
}
if len(pathSpl) == 2 {
return (u.Stack().Name == pathSpl[0] && u.Name() == pathSpl[1])
}

return false
}

func IsUnitExcludedByTarget(u Unit) bool {
if len(config.Global.Targets) == 0 && len(config.Global.TargetsExclude) == 0 {
return false
}
// Target is set
if len(config.Global.Targets) > 0 {
for _, uPath := range config.Global.Targets {
if checkUnitPath(u, uPath) {
return false
}
}
return true
}
// Exclude-target is set
for _, uPath := range config.Global.TargetsExclude {
if checkUnitPath(u, uPath) {
return true
}
}
return false
}
2 changes: 2 additions & 0 deletions internal/project/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (p *Project) readStackObj(stackSpec ObjectData) error {
return fmt.Errorf("duplicate stack name '%s'", name)
}

stackSpec.data["project"] = p.configData["project"]

stack := Stack{
ProjectPtr: p,
ConfigData: stackSpec.data,
Expand Down
2 changes: 1 addition & 1 deletion internal/project/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func templateMust(data []byte, values interface{}, p *Project, s *Stack, fileNam
return tmplWithMissingKey(data, values, "error", p, s, fileName)
}

// templateMust apply values to template data, considering template file path (if empty will be used project path).
// templateTry apply values to template data, considering template file path (if empty will be used project path).
// If template has unresolved variables - warn will be set to true.
func templateTry(data []byte, values interface{}, p *Project, s *Stack, fileName string) (res []byte, warn bool, err error) {
res, err = tmplWithMissingKey(data, values, "default", p, s, fileName)
Expand Down
11 changes: 0 additions & 11 deletions internal/project/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ type Unit interface {
Key() string
GetState() Unit
GetDiffData() interface{}
GetStateDiffData() interface{}
LoadState(interface{}, string, *StateProject) error
KindKey() string
CodeDir() string
UpdateProjectRuntimeData(p *Project) error
WasApplied() bool
ForceApply() bool
Expand Down Expand Up @@ -101,14 +98,6 @@ func NewUnitFromState(state map[string]interface{}, name string, p *StateProject
return nil, fmt.Errorf("internal error: bad unit type in state '%v'", mType)
}
}
// stateUnit, err := modDrv.NewFromState(state, name, p)
// if err != nil {
// return nil, err
// }
// curUnit := p.LoaderProjectPtr.Units[stateUnit.Key()]
// if curUnit != nil {
// curUnit.SetTainted()
// }
return modDrv.NewFromState(state, name, p)
}

Expand Down
3 changes: 3 additions & 0 deletions internal/units/shell/common/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func (u *Unit) Dependencies() *project.UnitLinksT {
// Init runs init procedure for unit.
func (u *Unit) Init() error {
_, err := u.runCommands(*u.InitConf, "init")
if err != nil {
u.SetTainted(true, err)
}
return err
}

Expand Down
42 changes: 25 additions & 17 deletions internal/units/shell/terraform/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (

type Unit struct {
base.Unit
Source string `yaml:"-" json:"source"`
Kubeconfig string `yaml:"-" json:"kubeconfig"`
Inputs map[string]interface{} `yaml:"-" json:"inputs"`
// providerVersion string `yaml:"-" json:"-"`
ProviderConf types.ProviderConfigSpec `yaml:"provider_conf" json:"provider_conf"`
UnitKind string `yaml:"-" json:"type"`
StateData project.Unit `yaml:"-" json:"-"`
Source string `yaml:"-" json:"source"`
Kubeconfig string `yaml:"-" json:"kubeconfig"`
Inputs map[string]interface{} `yaml:"-" json:"inputs"`
ApplyTemplate bool `yaml:"apply_template" json:"-"`
ProviderConf types.ProviderConfigSpec `yaml:"provider_conf" json:"provider_conf"`
UnitKind string `yaml:"-" json:"type"`
StateData project.Unit `yaml:"-" json:"-"`
}

func (u *Unit) KindKey() string {
Expand Down Expand Up @@ -68,6 +68,11 @@ func (u *Unit) genMainCodeBlock() ([]byte, error) {
}

func (u *Unit) ReadConfig(spec map[string]interface{}, stack *project.Stack) error {
u.ApplyTemplate = true
err := utils.YAMLInterfaceToType(spec, u)
if err != nil {
return err
}
source, ok := spec["source"].(string)
if !ok {
return fmt.Errorf("reading kubernetes unit '%v': malformed unit source", u.Key())
Expand Down Expand Up @@ -107,14 +112,21 @@ func (u *Unit) ReadConfig(spec map[string]interface{}, stack *project.Stack) err
if err != nil {
return fmt.Errorf("reading kubernetes unit '%v': read manifest from '%v': %v", u.Key(), source, err.Error())
}
manifest, errIsWarn, err := u.Stack().TemplateTry(file, fileName)
if err != nil {
if errIsWarn {
log.Warnf("File %v has unresolved template key: \n%v", fileName, err.Error())
} else {
return err
var manifest []byte
if u.ApplyTemplate {
var errIsWarn bool
manifest, errIsWarn, err = u.Stack().TemplateTry(file, fileName)
if err != nil {
if errIsWarn {
log.Warnf("File %v has unresolved template key: \n%v", fileName, err.Error())
} else {
return err
}
}
} else {
manifest = file
}

manifests, err := utils.ReadYAMLObjects(manifest)
if err != nil {
return fmt.Errorf("reading kubernetes unit '%v': reading kubernetes manifests form source '%v': %v", u.Key(), source, err.Error())
Expand All @@ -130,10 +142,6 @@ func (u *Unit) ReadConfig(spec map[string]interface{}, stack *project.Stack) err
return fmt.Errorf("the kubernetes unit must contain at least one manifest")
}

err := utils.YAMLInterfaceToType(spec, u)
if err != nil {
return err
}
kubeconfig, ok := spec["kubeconfig"].(string)
if ok && u.ProviderConf.ConfigPath == "" {
u.ProviderConf.ConfigPath = kubeconfig
Expand Down
1 change: 1 addition & 0 deletions tests/test-project/graph-test/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ units:
commands:
- echo "Waiting..."
- sleep 1
- echo region {{ .project.variables.region }}
-
name: force_apply_unit
type: shell
Expand Down

0 comments on commit 86de7ca

Please sign in to comment.