From 2fff7251c3c473a3d3412d873eb14e65e92c9b00 Mon Sep 17 00:00:00 2001 From: Graceson Aufderheide Date: Sat, 19 Oct 2024 19:04:37 -0700 Subject: [PATCH] Fixes: #23544 a bug where podman machine init does not create all the necessary machine files when ignition-path is used. Signed-off-by: Graceson Aufderheide --- pkg/machine/e2e/config_init_test.go | 5 +++ pkg/machine/e2e/init_test.go | 44 +++++++++++++++++++++++++ pkg/machine/shim/host.go | 51 +++++++++++++++-------------- 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/pkg/machine/e2e/config_init_test.go b/pkg/machine/e2e/config_init_test.go index 9d33e2d3d2..0423010477 100644 --- a/pkg/machine/e2e/config_init_test.go +++ b/pkg/machine/e2e/config_init_test.go @@ -90,6 +90,11 @@ func (i *initMachine) buildCmd(m *machineTestBuilder) []string { if strings.Contains(session.errorToString(), "VM does not exist") { return } + + // FIXME:#24344 work-around for custom ignition removal + if strings.Contains(session.errorToString(), "failed to remove machines files: unable to find connection named") { + return + } } Expect(session).To(Exit(0)) }) diff --git a/pkg/machine/e2e/init_test.go b/pkg/machine/e2e/init_test.go index 58fa3f84ff..7b5e35e96a 100644 --- a/pkg/machine/e2e/init_test.go +++ b/pkg/machine/e2e/init_test.go @@ -224,6 +224,50 @@ var _ = Describe("podman machine init", func() { Expect(sshSession.outputToString()).To(ContainSubstring("example")) }) + It("machine init with ignition path", func() { + skipIfWSL("Ignition is not compatible with WSL machines since they are not based on Fedora CoreOS") + + tmpDir, err := os.MkdirTemp("", "") + defer func() { _ = utils.GuardedRemoveAll(tmpDir) }() + Expect(err).ToNot(HaveOccurred()) + + tmpFile, err := os.CreateTemp(tmpDir, "test-ignition-*.ign") + Expect(err).ToNot(HaveOccurred()) + + mockIgnitionContent := `{"ignition":{"version":"3.4.0"},"passwd":{"users":[{"name":"core"}]}}` + + _, err = tmpFile.WriteString(mockIgnitionContent) + Expect(err).ToNot(HaveOccurred()) + + err = tmpFile.Close() + Expect(err).ToNot(HaveOccurred()) + + name := randomString() + i := new(initMachine) + session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withIgnitionPath(tmpFile.Name())).run() + Expect(err).ToNot(HaveOccurred()) + Expect(session).To(Exit(0)) + + configDir := filepath.Join(testDir, ".config", "containers", "podman", "machine", testProvider.VMType().String()) + + // test that all required machine files are created + fileExtensions := []string{".lock", ".json", ".ign"} + for _, ext := range fileExtensions { + filename := filepath.Join(configDir, fmt.Sprintf("%s%s", name, ext)) + + _, err := os.Stat(filename) + Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("file %v does not exist", filename)) + } + + // enforce that the raw ignition is copied over verbatim + createdIgn := filepath.Join(configDir, fmt.Sprintf("%s%s", name, ".ign")) + contentWanted, err := os.ReadFile(tmpFile.Name()) + Expect(err).ToNot(HaveOccurred()) + contentGot, err := os.ReadFile(createdIgn) + Expect(err).ToNot(HaveOccurred()) + Expect(contentWanted).To(Equal(contentGot), "The ignition file provided and the ignition file created do not match") + }) + It("machine init rootless docker.sock check", func() { i := initMachine{} name := randomString() diff --git a/pkg/machine/shim/host.go b/pkg/machine/shim/host.go index 948ebe11ab..9456683ece 100644 --- a/pkg/machine/shim/host.go +++ b/pkg/machine/shim/host.go @@ -196,30 +196,38 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error { // copy it into the conf dir if len(opts.IgnitionPath) > 0 { err = ignBuilder.BuildWithIgnitionFile(opts.IgnitionPath) - return err - } - err = ignBuilder.GenerateIgnitionConfig() - if err != nil { - return err - } + if err != nil { + return err + } + } else { + err = ignBuilder.GenerateIgnitionConfig() + if err != nil { + return err + } - readyIgnOpts, err := mp.PrepareIgnition(mc, &ignBuilder) - if err != nil { - return err - } + readyIgnOpts, err := mp.PrepareIgnition(mc, &ignBuilder) + if err != nil { + return err + } - readyUnitFile, err := ignition.CreateReadyUnitFile(mp.VMType(), readyIgnOpts) - if err != nil { - return err - } + readyUnitFile, err := ignition.CreateReadyUnitFile(mp.VMType(), readyIgnOpts) + if err != nil { + return err + } + + readyUnit := ignition.Unit{ + Enabled: ignition.BoolToPtr(true), + Name: "ready.service", + Contents: ignition.StrToPtr(readyUnitFile), + } + ignBuilder.WithUnit(readyUnit) - readyUnit := ignition.Unit{ - Enabled: ignition.BoolToPtr(true), - Name: "ready.service", - Contents: ignition.StrToPtr(readyUnitFile), + err = ignBuilder.Build() + if err != nil { + return err + } } - ignBuilder.WithUnit(readyUnit) // Mounts if mp.VMType() != machineDefine.WSLVirt { @@ -245,11 +253,6 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error { return err } - err = ignBuilder.Build() - if err != nil { - return err - } - return mc.Write() }