Skip to content

Commit

Permalink
feat: render compose file as a Go template
Browse files Browse the repository at this point in the history
  • Loading branch information
m-adawi committed Jul 24, 2024
1 parent 98784ac commit 66ac8ac
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
10 changes: 9 additions & 1 deletion docs/stacks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ stack-name:
branch: main
# The path to the docker compose file where stack
# is defined
compose_file: compose.yaml
compose_file: /path/to/compose.yaml
# Path to values file to use when rendering
# compose file as a Go template. If empty, compose
# file will be treated as a regular compose file
values_file: /path/to/values.yaml
# Paths to files encrypted using sops to decrypt
# before updating stack
sops_files:
- path/to/sops/encrypted/file

2 changes: 1 addition & 1 deletion swarmcd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func initStacks() error {
if !ok {
return fmt.Errorf("error initializing %s stack, no such repo: %s", stack, stackConfig.Repo)
}
swarmStack := newSwarmStack(stack, stackRepo, stackConfig.Branch, stackConfig.ComposeFile, stackConfig.SopsFiles)
swarmStack := newSwarmStack(stack, stackRepo, stackConfig.Branch, stackConfig.ComposeFile, stackConfig.SopsFiles, stackConfig.ValuesFile)
stacks = append(stacks, swarmStack)
stackStatus[stack] = &StackStatus{}
stackStatus[stack].RepoURL = stackRepo.url
Expand Down
37 changes: 34 additions & 3 deletions swarmcd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"text/template"

"github.com/docker/cli/cli/command/stack"
"github.com/go-git/go-git/v5"
Expand All @@ -18,17 +19,17 @@ type swarmStack struct {
branch string
composePath string
sopsFiles []string
status *StackStatus
valuesFile string
}

func newSwarmStack(name string, repo *stackRepo, branch string, composePath string, sopsFiles []string) *swarmStack {
func newSwarmStack(name string, repo *stackRepo, branch string, composePath string, sopsFiles []string, valuesFile string) *swarmStack {
return &swarmStack{
name: name,
repo: repo,
branch: branch,
composePath: composePath,
sopsFiles: sopsFiles,
status: &StackStatus{},
valuesFile: valuesFile,
}
}

Expand All @@ -44,6 +45,13 @@ func (swarmStack *swarmStack) updateStack() (revision string, err error) {
return "", fmt.Errorf("failed to decrypt one or more sops files for %s stack: %w", swarmStack.name, err)
}

if swarmStack.valuesFile != "" {
err = swarmStack.renderComposeTemplate()
if err != nil {
return
}
}

err = swarmStack.rotateConfigsAndSecrets()
if err != nil {
return
Expand Down Expand Up @@ -140,3 +148,26 @@ func (swarmStack *swarmStack) rotateObjects(objects map[string]any) error {
return nil
}

func (swarmStack *swarmStack) renderComposeTemplate() error {
composeFile := path.Join(config.ReposPath, swarmStack.repo.path, swarmStack.composePath)
valuesFile := path.Join(config.ReposPath, swarmStack.repo.path, swarmStack.valuesFile)
valuesBytes, err := os.ReadFile(valuesFile)
if err != nil {
return fmt.Errorf("could not read %s stack values file: %w", swarmStack.name, err)
}
var valuesMap map[string]any
yaml.Unmarshal(valuesBytes, &valuesMap)
templ, err := template.New(path.Base(composeFile)).ParseFiles(composeFile)
if err != nil {
return fmt.Errorf("could not parse %s stack compose file as a Go template: %w", swarmStack.name, err)
}
composeFileWriter, err := os.Create(composeFile)
if err != nil {
return fmt.Errorf("could not open %s stack compose file: %w", swarmStack.name, err)
}
err = templ.Execute(composeFileWriter, map[string]map[string]any{"Values": valuesMap})
if err != nil {
return fmt.Errorf("error rending %s stack compose template: %w", swarmStack.name, err)
}
return nil
}
1 change: 1 addition & 0 deletions swarmcd/swarmcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func updateStackThread(swarmStack *swarmStack, waitGroup *sync.WaitGroup) {
}



func GetStackStatus() map[string]*StackStatus {
return stackStatus
}
1 change: 1 addition & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type StackConfig struct {
Repo string
Branch string
ComposeFile string `mapstructure:"compose_file"`
ValuesFile string `mapstructure:"values_file"`
SopsFiles []string `mapstructure:"sops_files"`
}

Expand Down

0 comments on commit 66ac8ac

Please sign in to comment.