Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for podman machine init not creating necessary JSON file when an ignition-path is passed #24321

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/machine/e2e/config_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
44 changes: 44 additions & 0 deletions pkg/machine/e2e/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 12 additions & 8 deletions pkg/machine/shim/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,15 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error {
// copy it into the conf dir
if len(opts.IgnitionPath) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just create a new function to deal with this given the more complexity here? wdyt @Luap99

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is pretty long already so splitting it up makes sense but I would not force a community contributor to do that work. It only adds one more if/else here so I don't think it is required to rework just for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im cool with that

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)
Expand Down Expand Up @@ -245,9 +248,10 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error {
return err
}

err = ignBuilder.Build()
if err != nil {
return err
if len(opts.IgnitionPath) == 0 {
if err := ignBuilder.Build(); err != nil {
return err
}
}

return mc.Write()
Expand Down