Skip to content

Commit

Permalink
[wip] detect volume changes
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <[email protected]>
  • Loading branch information
jhrotko committed Oct 22, 2024
1 parent 24be044 commit 4032d1b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
56 changes: 51 additions & 5 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,

sort.Slice(containers, func(i, j int) bool {
// select obsolete containers first, so they get removed as we scale down
if obsolete, _ := mustRecreate(service, containers[i], recreate); obsolete {
if obsolete, _ := mustRecreate(project, service, containers[i], recreate); obsolete {
// i is obsolete, so must be first in the list
return true
}
if obsolete, _ := mustRecreate(service, containers[j], recreate); obsolete {
if obsolete, _ := mustRecreate(project, service, containers[j], recreate); obsolete {
// j is obsolete, so must be first in the list
return false
}
Expand All @@ -154,7 +154,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
continue
}

mustRecreate, err := mustRecreate(service, container, recreate)
mustRecreate, err := mustRecreate(project, service, container, recreate)
if err != nil {
return err
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func (c *convergence) resolveSharedNamespaces(service *types.ServiceConfig) erro
return nil
}

func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy string) (bool, error) {
func mustRecreate(project *types.Project, expected types.ServiceConfig, actual moby.Container, policy string) (bool, error) {
if policy == api.RecreateNever {
return false, nil
}
Expand All @@ -325,7 +325,53 @@ func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy st
}
configChanged := actual.Labels[api.ConfigHashLabel] != configHash
imageUpdated := actual.Labels[api.ImageDigestLabel] != expected.CustomLabels[api.ImageDigestLabel]
return configChanged || imageUpdated, nil
volumesUpdated := compareVolumeMount(project, expected, actual)
fmt.Printf("volumesUpdated %+v\n", volumesUpdated)
// fmt.Printf("configChanged %+v\n", configChanged)

return configChanged || imageUpdated || volumesUpdated, nil
}

func compareVolumeMount(project *types.Project, expected types.ServiceConfig, actual moby.Container) bool {
// fmt.Printf("service Volumes %+v\n", expected.Volumes)
// fmt.Printf("Volumes %+v\n", project.Volumes)
// fmt.Printf("Mounts %+v\n", actual.Mounts)

var needsUpdate = false
for _, v := range expected.Volumes {
var mount *moby.MountPoint = nil
composeVolume := project.Volumes[v.Source]
// Find in container the named mount
for _, m := range actual.Mounts {
if composeVolume.Name == m.Name {
mount = &m
break
}
}
fmt.Printf("composeVolume -- %+v\n", composeVolume)
fmt.Printf("mount -- %+v\n", mount)
fmt.Printf("v.Type -- %+v\n", v.Type)
fmt.Printf("v.Source -- %+v\n", v.Source)
fmt.Printf("v.Target -- %+v\n", v.Target)
fmt.Printf("v.ReadOnly -- %+v\n", v.ReadOnly)
if mount == nil {
// volume name field has changed
return true
}
composeVolumeDriver := composeVolume.Driver
if composeVolume.Driver == "" {
composeVolumeDriver = "local"
}
fmt.Printf("v.Type != string(mount.Type) -- %v\n", v.Type != string(mount.Type))
fmt.Printf("strings.Contains(mount.Source, fmt.Sprintf('/ss/', v.Source)) -- %v\n", strings.Contains(mount.Source, fmt.Sprintf("/%s/", v.Source)))
fmt.Printf("v.Target != mount.Destination -- %v\n", v.Target != mount.Destination)
fmt.Printf("(composeVolumeDriver != mount.Driver) -- %v\n", composeVolumeDriver != mount.Driver)
fmt.Printf("v.ReadOnly == mount.RW -- %v\n", v.ReadOnly == mount.RW)

needsUpdate = needsUpdate || v.Type != string(mount.Type) || strings.Contains(mount.Source, fmt.Sprintf("/%s/", v.Source)) || v.Target != mount.Destination || (composeVolumeDriver != mount.Driver) || !v.ReadOnly != mount.RW
fmt.Println("==================")
}
return needsUpdate
}

func getContainerName(projectName string, service types.ServiceConfig, number int) string {
Expand Down
1 change: 0 additions & 1 deletion pkg/compose/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func ServiceHash(o types.ServiceConfig) (string, error) {
o.Deploy.Replicas = nil
}
o.DependsOn = nil
o.Volumes = nil

bytes, err := json.Marshal(o)
if err != nil {
Expand Down

0 comments on commit 4032d1b

Please sign in to comment.