Skip to content

Commit b094157

Browse files
committed
dynamically add new stacks
1 parent c40c9e5 commit b094157

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

swarmcd/init.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,32 @@ func Init() (err error) {
4444
}
4545

4646
func initRepos() error {
47+
var newRepos = map[string]*stackRepo{}
48+
4749
for repoName, repoConfig := range config.RepoConfigs {
50+
if repo, exists := repos[repoName]; exists {
51+
newRepos[repoName] = repo
52+
delete(repos, repoName)
53+
continue
54+
}
55+
4856
repoPath := path.Join(config.ReposPath, repoName)
4957
auth, err := createHTTPBasicAuth(repoName)
5058
if err != nil {
5159
return err
5260
}
53-
repos[repoName], err = newStackRepo(repoName, repoPath, repoConfig.Url, auth)
61+
newRepos[repoName], err = newStackRepo(repoName, repoPath, repoConfig.Url, auth)
5462
if err != nil {
5563
return err
5664
}
5765
}
66+
67+
if len(repos) != 0 {
68+
logger.Info("Some repos were removed from the stack: %v", repos)
69+
}
70+
71+
repos = newRepos
72+
5873
return nil
5974
}
6075

@@ -92,17 +107,38 @@ func createHTTPBasicAuth(repoName string) (*http.BasicAuth, error) {
92107
}
93108

94109
func initStacks() error {
110+
var newStacks = map[string]*swarmStack{}
111+
var newStackStatus = map[string]*StackStatus{}
112+
95113
for stack, stackConfig := range config.StackConfigs {
96114
stackRepo, ok := repos[stackConfig.Repo]
97115
if !ok {
98116
return fmt.Errorf("error initializing %s stack, no such repo: %s", stack, stackConfig.Repo)
99117
}
118+
100119
discoverSecrets := config.SopsSecretsDiscovery || stackConfig.SopsSecretsDiscovery
101120
swarmStack := newSwarmStack(stack, stackRepo, stackConfig.Branch, stackConfig.ComposeFile, stackConfig.SopsFiles, stackConfig.ValuesFile, discoverSecrets)
102-
stacks = append(stacks, swarmStack)
103-
stackStatus[stack] = &StackStatus{}
104-
stackStatus[stack].RepoURL = stackRepo.url
121+
122+
newStacks[stack] = swarmStack
123+
if _, exists := stacks[stack]; exists {
124+
delete(stacks, stack)
125+
}
126+
127+
newStackStatus[stack] = &StackStatus{}
128+
newStackStatus[stack].RepoURL = stackRepo.url
129+
if _, exists := stackStatus[stack]; exists {
130+
delete(stacks, stack)
131+
}
105132
}
133+
134+
if len(stacks) != 0 {
135+
logger.Info("Some stacks were removed: %v", stacks)
136+
// Todo: do we need to do something for this.
137+
}
138+
139+
stacks = newStacks
140+
stackStatus = newStackStatus
141+
106142
return nil
107143
}
108144

swarmcd/swarmcd.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,48 @@ package swarmcd
22

33
import (
44
"fmt"
5+
"github.com/m-adawi/swarm-cd/util"
56
"sync"
67
"time"
78
)
89

9-
var stackStatus map[string]*StackStatus = map[string]*StackStatus{}
10-
var stacks []*swarmStack
10+
var stackStatus = map[string]*StackStatus{}
11+
var stacks = map[string]*swarmStack{}
1112

1213
func Run() {
1314
logger.Info("starting SwarmCD")
1415
for {
1516
var waitGroup sync.WaitGroup
1617
logger.Info("updating stacks...")
1718
for _, swarmStack := range stacks {
19+
logger.Info("Starting go routine for %v", swarmStack.name)
20+
1821
waitGroup.Add(1)
1922
go updateStackThread(swarmStack, &waitGroup)
2023
}
2124
waitGroup.Wait()
2225
logger.Info("waiting for the update interval")
2326
time.Sleep(time.Duration(config.UpdateInterval) * time.Second)
27+
28+
logger.Info("check if new repos or new stacks are available")
29+
updateStackConfigs()
30+
}
31+
}
32+
33+
func updateStackConfigs() {
34+
err := util.LoadConfigs()
35+
if err != nil {
36+
logger.Info("Error calling loadConfig again: %v", err)
37+
}
38+
39+
err = initRepos()
40+
if err != nil {
41+
logger.Info("Error calling initRepos again: %v", err)
42+
}
43+
44+
err = initStacks()
45+
if err != nil {
46+
logger.Info("Error calling initStacks again: %v", err)
2447
}
2548
}
2649

util/config.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ func LoadConfigs() (err error) {
4040
if err != nil {
4141
return fmt.Errorf("could not read configuration file: %w", err)
4242
}
43-
if Configs.RepoConfigs == nil {
44-
err = readRepoConfigs()
45-
if err != nil {
46-
return fmt.Errorf("could not read repos file: %w", err)
47-
}
43+
44+
err = readRepoConfigs()
45+
if err != nil {
46+
return fmt.Errorf("could not read repos file: %w", err)
4847
}
49-
if Configs.StackConfigs == nil {
50-
err = readStackConfigs()
51-
if err != nil {
52-
return fmt.Errorf("could not load stacks file: %w", err)
53-
}
48+
49+
err = readStackConfigs()
50+
if err != nil {
51+
return fmt.Errorf("could not load stacks file: %w", err)
5452
}
5553
return
5654
}

0 commit comments

Comments
 (0)