Skip to content

Commit

Permalink
Merge pull request #47 from meltred/namespace
Browse files Browse the repository at this point in the history
feat: running multiple service stack
  • Loading branch information
KunalSin9h authored Dec 17, 2023
2 parents e11ea91 + 2ec05d1 commit d8828d7
Show file tree
Hide file tree
Showing 22 changed files with 546 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ run:
@swag init --output ./docs/swagger
@go run main.go serve --verbose

.PHONY: fontend
fontend:
@pnpm --prefix ./ui run build --emptyOutDir


.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/meltred/meltcd

go 1.21.0
go 1.21

require (
github.com/docker/docker v24.0.7+incompatible
Expand Down
47 changes: 45 additions & 2 deletions internal/core/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (app *Application) Apply(targetState string) error {
for volName, volOpts := range swarmSpec.Volumes {
labels := make(map[string]string)
for _, l := range volOpts.Labels {
tokens := strings.Split(l, "=")
tokens := strings.SplitN(l, "=", 2)
if len(tokens) != 2 {
return errors.New("invalid labels in volume")
}
Expand All @@ -253,7 +253,12 @@ func (app *Application) Apply(targetState string) error {
})
}

services, err := swarmSpec.GetServiceSpec(app.Name)
networkID, err := createNetwork(cli, app.Name)
if err != nil {
return err
}

services, err := swarmSpec.GetServiceSpec(app.Name, networkID)
if err != nil {
return err
}
Expand Down Expand Up @@ -282,6 +287,7 @@ func (app *Application) Apply(targetState string) error {
app.LastSyncedAt = time.Now()
continue
}

log.Info("Creating new service")
res, err := cli.ServiceCreate(context.Background(), service, types.ServiceCreateOptions{})
if err != nil {
Expand Down Expand Up @@ -317,3 +323,40 @@ func checkServiceAlreadyExist(serviceName string, allServices *[]swarm.Service)
}
return swarm.Service{}, false
}

func createNetwork(cli *client.Client, appName string) (string, error) {
log.Info("Creating network")
networkName := appName + "_default"

nets, err := cli.NetworkList(context.Background(), types.NetworkListOptions{})
if err != nil {
return "", err
}

for _, network := range nets {
if network.Name == networkName {
log.Info("Network already exists")
return network.ID, nil
}
}

net, err := cli.NetworkCreate(context.Background(), networkName, types.NetworkCreate{
Scope: "swarm",
Labels: map[string]string{
"com.docker.stack.namespace": appName,
},
Driver: "overlay",
})

log.Info("Created network", "id", net.ID)

if err != nil {
return "", err
}

if net.Warning != "" {
log.Warn(net.Warning)
}

return net.ID, nil
}
19 changes: 17 additions & 2 deletions internal/core/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -176,11 +175,27 @@ func RemoveApplication(appName string) error {
return err
}

// set (unique) of network to remove
networksToRemove := map[string]bool{}

for _, svc := range runningService {
if strings.HasPrefix(svc.Spec.Name, appName) {
name := svc.Spec.Labels["com.docker.stack.namespace"]

if name == appName {
if err := cli.ServiceRemove(context.Background(), svc.ID); err != nil {
return err
}

for _, nets := range svc.Spec.TaskTemplate.Networks {
// nets.Target is the network ID
networksToRemove[nets.Target] = true
}
}
}

for networkID := range networksToRemove {
if err := cli.NetworkRemove(context.Background(), networkID); err != nil {
return err
}
}

Expand Down
1 change: 1 addition & 0 deletions server/static/assets/index-2d45d31a.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d8828d7

Please sign in to comment.