Skip to content

Commit

Permalink
refactor: dont create volumes that arent referenced by a service
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jul 26, 2024
1 parent f178aca commit 91b97d9
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 78 deletions.
5 changes: 3 additions & 2 deletions internal/generator/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ type ImageCacheBuildArguments struct {
}

type ComposeVolume struct {
Name string `json:"name" description:"name is the name the volume, when creating in kubernetes will have a prefix"`
Size string `json:"size" description:"the size of the volume to request if the system enforces it"`
Name string `json:"name" description:"name is the name the volume, when creating in kubernetes will have a prefix"`
Size string `json:"size" description:"the size of the volume to request if the system enforces it"`
Create bool `json:"unused" description:"flag to determine if this volume is to be created or not"`
}

type ServiceVolume struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func composeToServiceValues(

// calculate if this service needs any additional volumes attached from the calculated build volumes
// additional volumes can only be attached to certain
serviceVolumes, err := calculateServiceVolumes(buildValues.Volumes, lagoonType, servicePersistentName, composeServiceValues.Labels)
serviceVolumes, err := calculateServiceVolumes(buildValues, lagoonType, servicePersistentName, composeServiceValues.Labels)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions internal/generator/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ func composeToVolumeValues(

// calculateServiceVolumes checks if a service type is allowed to have additional volumes attached, and if any volumes from docker compose
// are to be attached to this service or not
func calculateServiceVolumes(buildVolumes []ComposeVolume, lagoonType, servicePersistentName string, serviceLabels composetypes.Labels) ([]ServiceVolume, error) {
func calculateServiceVolumes(buildValues *BuildValues, lagoonType, servicePersistentName string, serviceLabels composetypes.Labels) ([]ServiceVolume, error) {
serviceVolumes := []ServiceVolume{}
if val, ok := servicetypes.ServiceTypes[lagoonType]; ok {
for _, vol := range buildVolumes {
for i := 0; i < len(buildValues.Volumes); i++ {
vol := &buildValues.Volumes[i]
volName := lagoon.GetVolumeNameFromLagoonVolume(vol.Name)
// if there is a `lagoon.volumes.<xyx>.path` for a custom volume that matches the default persistent volume
// don't add it again as a service volume
Expand All @@ -100,9 +101,11 @@ func calculateServiceVolumes(buildVolumes []ComposeVolume, lagoonType, servicePe
if volumePath != "" {
if val.AllowAdditionalVolumes {
sVol := ServiceVolume{
ComposeVolume: vol,
ComposeVolume: *vol,
Path: volumePath,
}
// if the volume is attached to a service, then add the flag to create the persistent volume claim
vol.Create = true
serviceVolumes = append(serviceVolumes, sVol)
} else {
// if the service type is not allowed additional volumes, return an error
Expand Down
26 changes: 14 additions & 12 deletions internal/templating/services/templates_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,22 @@ func GeneratePVCTemplate(
}
// check for any additional volumes
for _, vol := range buildValues.Volumes {
exists := false
// check if an existing pvc that will be created as a default persistent volume hasn't also been defined as an additional volume
// this will prevent creating another volume named the same
for _, cpvc := range result {
if lagoon.GetLagoonVolumeName(cpvc.Name) == vol.Name {
exists = true
if vol.Create {
exists := false
// check if an existing pvc that will be created as a default persistent volume hasn't also been defined as an additional volume
// this will prevent creating another volume named the same
for _, cpvc := range result {
if lagoon.GetLagoonVolumeName(cpvc.Name) == vol.Name {
exists = true
}
}
}
if !exists {
pvc, err := generateAdditionalPVC(buildValues, vol, labels, annotations)
if err != nil {
return nil, err
if !exists {
pvc, err := generateAdditionalPVC(buildValues, vol, labels, annotations)
if err != nil {
return nil, err
}
result = append(result, *pvc)
}
result = append(result, *pvc)
}
}
return result, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ volumes:
lagoon.type: none
notused:
labels:
# will create a pvc, but as there is no `lagoon.volumes.notused.path` defined anywhere, it will not be mounted
# as there is no `lagoon.volumes.notused.path` defined anywhere, it will not be created or mounted
lagoon.type: persistent
logs:
{}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ volumes:
lagoon.type: none
notused:
labels:
# will create a pvc, but as there is no `lagoon.volumes.notused.path` defined anywhere, it will not be mounted
# as there is no `lagoon.volumes.notused.path` defined anywhere, it will not be created or mounted
lagoon.type: persistent
logs:
{}

This file was deleted.

This file was deleted.

0 comments on commit 91b97d9

Please sign in to comment.