Skip to content

Commit

Permalink
feat: running multiple service stack
Browse files Browse the repository at this point in the history
Signed-off-by: Kunal Singh <[email protected]>
  • Loading branch information
KunalSin9h committed Dec 17, 2023
1 parent 7c82f2e commit 0770895
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
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)

Check warning on line 256 in internal/core/application/app.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var networkId should be networkID (revive)
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
13 changes: 12 additions & 1 deletion spec/dockerSwarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Volume struct {
Options map[string]string `yaml:"options"`
}

func (d *DockerSwarm) GetServiceSpec(appName string) ([]swarm.ServiceSpec, error) {
func (d *DockerSwarm) GetServiceSpec(appName string, networkID string) ([]swarm.ServiceSpec, error) {
log.Info("Getting service spec for app", "app name", appName)

specs := make([]swarm.ServiceSpec, 0)
Expand All @@ -91,9 +91,20 @@ func (d *DockerSwarm) GetServiceSpec(appName string) ([]swarm.ServiceSpec, error
targetSpec.TaskTemplate = swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: spec.Image,
Labels: map[string]string{
"com.docker.stack.namespace": appName,
},
},
}

// Connection the service with the network
targetSpec.TaskTemplate.Networks = append(targetSpec.TaskTemplate.Networks, swarm.NetworkAttachmentConfig{
Target: networkID,
Aliases: []string{
serviceName,
},
})

for _, envFile := range spec.EnvFile {
log.Info("Using environment variable from files", "file", envFile)

Expand Down

0 comments on commit 0770895

Please sign in to comment.