From f77b5d75cc73bd2bbbf31333f132e9ca3cb2467d Mon Sep 17 00:00:00 2001 From: marmijo Date: Wed, 1 Jun 2022 11:34:28 -0400 Subject: [PATCH] exec/files: sort unit preset in alphabetical order The 20-ignition.preset file is used in CoreOS layering. The contents of this file will be sorted in alphabetical order by unit name to ensure the file is written in a consistent order across multiple runs. Fixes https://github.com/coreos/ignition/issues/1339 --- internal/exec/stages/files/units.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/exec/stages/files/units.go b/internal/exec/stages/files/units.go index b7f93c8032..60aeedf8be 100644 --- a/internal/exec/stages/files/units.go +++ b/internal/exec/stages/files/units.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "path/filepath" + "sort" "strings" "github.com/coreos/ignition/v2/config/shared/errors" @@ -151,7 +152,16 @@ func (s *stage) createSystemdPresetFile(presets map[string]*Preset) error { return err } hasInstanceUnit := false - for _, value := range presets { + + // sort the units before writing to the systemd presets file + unitNames := make([]string, 0, len(presets)) + for unit := range presets { + unitNames = append(unitNames, unit) + } + sort.Strings(unitNames) + + for i := range unitNames { + value := presets[unitNames[i]] unitString := value.unit if value.instantiatable { hasInstanceUnit = true