Skip to content

Commit

Permalink
fix: Introduce Source object in deployment projects and render per so…
Browse files Browse the repository at this point in the history
…urce id

This fixes issues where multiple included git projects would render into
the same directory.
  • Loading branch information
codablock committed May 24, 2022
1 parent d158b94 commit 8b2b600
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/kluctl/commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func buildInclusionCompletionFunc(cmdStruct interface{}, forDirs bool) func(cmd
defer mutex.Unlock()
for _, di := range ctx.targetCtx.DeploymentCollection.Deployments {
tags.Merge(di.Tags)
deploymentItemDirs.Set(di.RelToRootItemDir, true)
deploymentItemDirs.Set(di.RelToSourceItemDir, true)
}
return nil
})
Expand Down
18 changes: 9 additions & 9 deletions pkg/deployment/deployment_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type DeploymentItem struct {
Objects []*uo.UnstructuredObject
Tags *utils.OrderedMap

RelToRootItemDir string
RelToSourceItemDir string
RelToProjectItemDir string
relRenderedDir string
renderedDir string
Expand All @@ -60,22 +60,22 @@ func NewDeploymentItem(ctx SharedContext, project *DeploymentProject, collection
di.Tags.SetMultiple(di.Config.Tags, true)

if di.dir != nil {
di.RelToRootItemDir, err = filepath.Rel(di.Project.rootDir, *di.dir)
di.RelToSourceItemDir, err = filepath.Rel(di.Project.source.dir, *di.dir)
if err != nil {
return nil, err
}

di.RelToProjectItemDir, err = filepath.Rel(di.Project.relDir, di.RelToRootItemDir)
di.RelToProjectItemDir, err = filepath.Rel(di.Project.relDir, di.RelToSourceItemDir)
if err != nil {
return nil, err
}

di.relRenderedDir = di.RelToRootItemDir
di.relRenderedDir = di.RelToSourceItemDir
if di.index != 0 {
di.relRenderedDir = fmt.Sprintf("%s-%d", di.relRenderedDir, di.index)
}

di.renderedDir = filepath.Join(collection.ctx.RenderDir, di.relRenderedDir)
di.renderedDir = filepath.Join(collection.ctx.RenderDir, di.Project.source.id, di.relRenderedDir)
di.renderedYamlPath = filepath.Join(di.renderedDir, ".rendered.yml")
}
return di, nil
Expand All @@ -94,7 +94,7 @@ func (di *DeploymentItem) getCommonLabels() map[string]string {
func (di *DeploymentItem) getCommonAnnotations() map[string]string {
// TODO change it to kluctl.io/deployment_dir
a := map[string]string{
"kluctl.io/kustomize_dir": filepath.ToSlash(di.RelToRootItemDir),
"kluctl.io/kustomize_dir": filepath.ToSlash(di.RelToSourceItemDir),
}
if di.Config.SkipDeleteIfTags {
a["kluctl.io/skip-delete-if-tags"] = "true"
Expand Down Expand Up @@ -147,7 +147,7 @@ func (di *DeploymentItem) render(forSeal bool, wp *utils.WorkerPoolWithErrors) e
}

wp.Submit(func() error {
return varsCtx.RenderDirectory(di.Project.rootDir, di.Project.getRenderSearchDirs(), di.Project.relDir, excludePatterns, di.RelToProjectItemDir, di.renderedDir)
return varsCtx.RenderDirectory(di.Project.source.dir, di.Project.getRenderSearchDirs(), di.Project.relDir, excludePatterns, di.RelToProjectItemDir, di.renderedDir)
})

return nil
Expand Down Expand Up @@ -210,7 +210,7 @@ func (di *DeploymentItem) resolveSealedSecrets(subdir string) error {
renderedDir := filepath.Join(di.renderedDir, subdir)

// ensure we're not leaving the project
_, err := securejoin.SecureJoin(di.Project.rootDir, subdir)
err := utils.CheckSubInDir(di.Project.source.dir, subdir)
if err != nil {
return err
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (di *DeploymentItem) buildInclusionEntries() []utils.InclusionEntry {
values = append(values, utils.InclusionEntry{Type: "tag", Value: t})
}
if di.dir != nil {
dir := filepath.ToSlash(di.RelToRootItemDir)
dir := filepath.ToSlash(di.RelToSourceItemDir)
values = append(values, utils.InclusionEntry{Type: "deploymentItemDir", Value: dir})
}
return values
Expand Down
26 changes: 15 additions & 11 deletions pkg/deployment/deployment_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type DeploymentProject struct {

VarsCtx *vars.VarsCtx

rootDir string
relDir string
absDir string
source Source
relDir string
absDir string

Config types.DeploymentProjectConfig

Expand All @@ -29,17 +29,17 @@ type DeploymentProject struct {
parentProjectInclude *types.DeploymentItemConfig
}

func NewDeploymentProject(ctx SharedContext, varsCtx *vars.VarsCtx, rootDir string, relDir string, parentProject *DeploymentProject) (*DeploymentProject, error) {
func NewDeploymentProject(ctx SharedContext, varsCtx *vars.VarsCtx, source Source, relDir string, parentProject *DeploymentProject) (*DeploymentProject, error) {
dp := &DeploymentProject{
ctx: ctx,
VarsCtx: varsCtx.Copy(),
rootDir: rootDir,
source: source,
relDir: relDir,
parentProject: parentProject,
includes: map[int]*DeploymentProject{},
}

dir, err := securejoin.SecureJoin(dp.rootDir, dp.relDir)
dir, err := securejoin.SecureJoin(dp.source.dir, dp.relDir)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (p *DeploymentProject) checkDeploymentDirs() error {
return err
}

if !strings.HasPrefix(diDir, p.rootDir) {
if !strings.HasPrefix(diDir, p.source.dir) {
return fmt.Errorf("path/include is not part of the deployment project: %s", *di.Path)
}

Expand All @@ -150,7 +150,7 @@ func (p *DeploymentProject) loadIncludes() error {
var newProject *DeploymentProject

if inc.Include != nil {
newProject, err = p.loadLocalInclude(p.rootDir, filepath.Join(p.relDir, *inc.Include), inc.Vars)
newProject, err = p.loadLocalInclude(p.source, filepath.Join(p.relDir, *inc.Include), inc.Vars)
if err != nil {
return err
}
Expand All @@ -159,7 +159,7 @@ func (p *DeploymentProject) loadIncludes() error {
if err != nil {
return err
}
newProject, err = p.loadLocalInclude(cloneDir, inc.Git.SubDir, inc.Vars)
newProject, err = p.loadLocalInclude(NewSource(cloneDir), inc.Git.SubDir, inc.Vars)
if err != nil {
return err
}
Expand All @@ -172,14 +172,14 @@ func (p *DeploymentProject) loadIncludes() error {
return nil
}

func (p *DeploymentProject) loadLocalInclude(rootDir string, incDir string, vars []*types.VarsSource) (*DeploymentProject, error) {
func (p *DeploymentProject) loadLocalInclude(source Source, incDir string, vars []*types.VarsSource) (*DeploymentProject, error) {
varsCtx := p.VarsCtx.Copy()
err := p.loadVarsList(varsCtx, vars)
if err != nil {
return nil, err
}

newProject, err := NewDeploymentProject(p.ctx, varsCtx, rootDir, incDir, p)
newProject, err := NewDeploymentProject(p.ctx, varsCtx, source, incDir, p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -237,6 +237,10 @@ func (p *DeploymentProject) getChildren(recursive bool, includeSelf bool) []*Dep
func (p *DeploymentProject) getRenderSearchDirs() []string {
var ret []string
for _, d := range p.getParents() {
if d.p.source != p.source {
// only allow searching inside same source project
continue
}
ret = append(ret, d.p.absDir)
}
return ret
Expand Down
29 changes: 29 additions & 0 deletions pkg/deployment/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package deployment

import (
"fmt"
"sync"
)

type Source struct {
id string
dir string
}

func NewSource(dir string) Source {
return Source{
id: getNextSourceId(),
dir: dir,
}
}

var nextSourceId int
var nextSourceIdMutex sync.Mutex

func getNextSourceId() string {
nextSourceIdMutex.Lock()
defer nextSourceIdMutex.Unlock()
id := fmt.Sprintf("%d", nextSourceId)
nextSourceId += 1
return id
}
2 changes: 1 addition & 1 deletion pkg/kluctl_project/target_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (p *LoadedKluctlProject) NewTargetContext(ctx context.Context, targetName s
DefaultSealedSecretsOutputPattern: targetName,
}

d, err := deployment.NewDeploymentProject(dctx, varsCtx, deploymentDir, ".", nil)
d, err := deployment.NewDeploymentProject(dctx, varsCtx, deployment.NewSource(deploymentDir), ".", nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func CheckInDir(root string, path string) error {
return nil
}

func CheckSubInDir(root string, subDir string) error {
return CheckInDir(root, filepath.Join(root, subDir))
}

func Touch(path string) error {
f, err := os.Create(path)
if err != nil {
Expand Down

0 comments on commit 8b2b600

Please sign in to comment.