Skip to content

Commit

Permalink
Merge pull request containers#23449 from ygalblum/quadlet-override-se…
Browse files Browse the repository at this point in the history
…rvice-name

Quadlet override service name
  • Loading branch information
openshift-merge-bot[bot] authored Aug 12, 2024
2 parents 7acaf71 + 795851e commit 708d6c5
Show file tree
Hide file tree
Showing 25 changed files with 406 additions and 168 deletions.
84 changes: 42 additions & 42 deletions cmd/quadlet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,34 +496,47 @@ func warnIfAmbiguousName(unit *parser.UnitFile, group string) {
}
}

func generatePodsInfoMap(units []*parser.UnitFile) map[string]*quadlet.PodInfo {
podsInfoMap := make(map[string]*quadlet.PodInfo)
func generateUnitsInfoMap(units []*parser.UnitFile) map[string]*quadlet.UnitInfo {
unitsInfoMap := make(map[string]*quadlet.UnitInfo)
for _, unit := range units {
if !strings.HasSuffix(unit.Filename, ".pod") {
continue
}

serviceName := quadlet.GetPodServiceName(unit)
podsInfoMap[unit.Filename] = &quadlet.PodInfo{
ServiceName: serviceName,
Containers: make([]string, 0),
}
}
var serviceName string
var containers []string
var resourceName string

return podsInfoMap
}

func prefillBuiltImageNames(units []*parser.UnitFile, resourceNames map[string]string) {
for _, unit := range units {
if !strings.HasSuffix(unit.Filename, ".build") {
switch {
case strings.HasSuffix(unit.Filename, ".container"):
serviceName = quadlet.GetContainerServiceName(unit)
case strings.HasSuffix(unit.Filename, ".volume"):
serviceName = quadlet.GetVolumeServiceName(unit)
case strings.HasSuffix(unit.Filename, ".kube"):
serviceName = quadlet.GetKubeServiceName(unit)
case strings.HasSuffix(unit.Filename, ".network"):
serviceName = quadlet.GetNetworkServiceName(unit)
case strings.HasSuffix(unit.Filename, ".image"):
serviceName = quadlet.GetImageServiceName(unit)
case strings.HasSuffix(unit.Filename, ".build"):
serviceName = quadlet.GetBuildServiceName(unit)
// Prefill resouceNames for .build files. This is significantly less complex than
// pre-computing all resourceNames for all Quadlet types (which is rather complex for a few
// types), but still breaks the dependency cycle between .volume and .build ([Volume] can
// have Image=some.build, and [Build] can have Volume=some.volume:/some-volume)
resourceName = quadlet.GetBuiltImageName(unit)
case strings.HasSuffix(unit.Filename, ".pod"):
serviceName = quadlet.GetPodServiceName(unit)
containers = make([]string, 0)
default:
Logf("Unsupported file type %q", unit.Filename)
continue
}

imageName := quadlet.GetBuiltImageName(unit)
if len(imageName) > 0 {
resourceNames[unit.Filename] = imageName
unitsInfoMap[unit.Filename] = &quadlet.UnitInfo{
ServiceName: serviceName,
Containers: containers,
ResourceName: resourceName,
}
}

return unitsInfoMap
}

func main() {
Expand Down Expand Up @@ -622,40 +635,30 @@ func process() error {
})

// Generate the PodsInfoMap to allow containers to link to their pods and add themselves to the pod's containers list
podsInfoMap := generatePodsInfoMap(units)

// A map of network/volume unit file-names, against their calculated names, as needed by Podman.
var resourceNames = make(map[string]string)

// Prefill resouceNames for .build files. This is significantly less complex than
// pre-computing all resourceNames for all Quadlet types (which is rather complex for a few
// types), but still breaks the dependency cycle between .volume and .build ([Volume] can
// have Image=some.build, and [Build] can have Volume=some.volume:/some-volume)
prefillBuiltImageNames(units, resourceNames)
unitsInfoMap := generateUnitsInfoMap(units)

for _, unit := range units {
var service *parser.UnitFile
var name string
var err error

switch {
case strings.HasSuffix(unit.Filename, ".container"):
warnIfAmbiguousName(unit, quadlet.ContainerGroup)
service, err = quadlet.ConvertContainer(unit, resourceNames, isUserFlag, podsInfoMap)
service, err = quadlet.ConvertContainer(unit, isUserFlag, unitsInfoMap)
case strings.HasSuffix(unit.Filename, ".volume"):
warnIfAmbiguousName(unit, quadlet.VolumeGroup)
service, name, err = quadlet.ConvertVolume(unit, unit.Filename, resourceNames)
service, err = quadlet.ConvertVolume(unit, unit.Filename, unitsInfoMap)
case strings.HasSuffix(unit.Filename, ".kube"):
service, err = quadlet.ConvertKube(unit, resourceNames, isUserFlag)
service, err = quadlet.ConvertKube(unit, unitsInfoMap, isUserFlag)
case strings.HasSuffix(unit.Filename, ".network"):
service, name, err = quadlet.ConvertNetwork(unit, unit.Filename)
service, err = quadlet.ConvertNetwork(unit, unit.Filename, unitsInfoMap)
case strings.HasSuffix(unit.Filename, ".image"):
warnIfAmbiguousName(unit, quadlet.ImageGroup)
service, name, err = quadlet.ConvertImage(unit)
service, err = quadlet.ConvertImage(unit, unitsInfoMap)
case strings.HasSuffix(unit.Filename, ".build"):
service, name, err = quadlet.ConvertBuild(unit, resourceNames)
service, err = quadlet.ConvertBuild(unit, unitsInfoMap)
case strings.HasSuffix(unit.Filename, ".pod"):
service, err = quadlet.ConvertPod(unit, unit.Filename, podsInfoMap, resourceNames)
service, err = quadlet.ConvertPod(unit, unit.Filename, unitsInfoMap)
default:
Logf("Unsupported file type %q", unit.Filename)
continue
Expand All @@ -666,9 +669,6 @@ func process() error {
continue
}

if name != "" {
resourceNames[unit.Filename] = name
}
service.Path = path.Join(outputPath, service.Filename)

if dryRunFlag {
Expand Down
Loading

0 comments on commit 708d6c5

Please sign in to comment.