-
Notifications
You must be signed in to change notification settings - Fork 119
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
Convert the vm.sh script into a go binary #1164
base: main
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Hi @Kuruyia. Thanks for your PR. PRs from untrusted users cannot be marked as trusted with I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
) | ||
|
||
// Wrapper around the output of the "qemu-img info" command | ||
type QemuImgInfo struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to export this type since it is used only locally
// Creates a virtual disk image | ||
func CreateDisk(path string, format string, size uint64) error { | ||
cmd := exec.Command("qemu-img", "create", "-f", format, path, strconv.FormatUint(size, 10)) | ||
cmd.Stderr = os.Stderr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not also the StdOut?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know if we wanted to show the output of those commands, but in hindsight more verbosity can't hurt.
) | ||
|
||
// The name of the QEMU main executable | ||
const qemuExec = "qemu-system-%s" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is correct for all OSes, in Centos Stream it should be qemu-kvm
. I'm wondering how this can work with the current script, maybe there is a symlink
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This script is actually running in a Fedora 39 container, so this is why it currently works.
Do we want to add a check for the current OS and use qemu-kvm
if available instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. I think we should get rid of fedora in the future. It is up to you, either you can a put a comment with a TODO and we can address this in a second PR or you can add it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be better to address this in a second PR, so this one doesn't get too big
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to get rid of Fedora in this context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need to. But we have converted everything to centos stream. It seems a bit inconsistent to me that we still keep fedora. Aynway, this can be a topic for another PR.
|
||
// Writes the SSH script to connect to the guest | ||
func writeSshFiles(nodeNum int) error { | ||
err := os.WriteFile(sshScriptPath, []byte(fmt.Sprintf(sshScriptContents, nodeNum)), 744) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please inline this function with the error check
return err | ||
} | ||
|
||
err = os.WriteFile(sshReadyPath, []byte("done\n"), 0644) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: same here
_, err = os.Stat(provisionedDiskPath) | ||
|
||
if err == nil { | ||
os.Remove(firstDiskPath) | ||
os.Symlink(provisionedDiskPath, firstDiskPath) | ||
} | ||
|
||
diskFile, diskBackingFile, err := utils.CalcNextDisk(".", nextDiskFlag) | ||
if err != nil { | ||
return fmt.Errorf("Failed to calculate the next disk image path: %v", err) | ||
} | ||
|
||
diskSize, err := qemu.GetDiskSize(diskBackingFile) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get the disk image size of \"%s\": %v", diskBackingFile, err) | ||
} | ||
|
||
if diskSize < defaultDiskSize { | ||
diskSize = defaultDiskSize | ||
} | ||
|
||
fmt.Printf("Creating disk \"%s backed by %s with size %d\"\n", diskFile, diskBackingFile, diskSize) | ||
err = qemu.CreateDiskWithBackingFile(diskFile, "qcow2", diskSize, diskBackingFile, "qcow2") | ||
if err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be encapsulate into a separate function
@Kuruyia thanks for the work! Can you implement a couple of unit tests for the functions? |
@dhiller @brianmcarey do you have any idea how we could test this? |
if rootless { | ||
for _, port := range ports { | ||
// Add DNAT rule for rootless podman (traffic originating from loopback adapter) | ||
toDestinationFlag := fmt.Sprintf("192.168.66.101:%d", port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you used the same constant value in mulitple places, I would define a const for it
Hey, thanks for your review! I'll address your change requests and add the unit tests this week. I guess we're only testing the functions that don't have any side effects for now? It'll be more involved to test functions that execute |
Since we didn't have any tests for the bash script, I would just add some simple ones. Another place where you can add unit tests is type readDirFunc func(n int) ([]os.DirEntry, error)
type DiskUtil struct {
readDirFunc readDir
}
func NewDiskUtil() *DiskUtil {
return &DiskUtil{
readDir = os.ReadDir
}
} and in the tests something like this: type mockDirEntry struct {
name string
}
func newmockDirEntry(name string) os.DirEntry {
return &mockDirEntry{name:name}
}
func (m *mockDirEntry) Name() string {
return m.name
}
[...] // implement the rest of the function for DirEntry
func mockReadDir(n int) ([]os.DirEntry, error) {
return os.DirEntry{
newmockDirEntry("disk0.qcow2"),
newmockDirEntry("disk1.qcow2"),
}, nil
}
diskUtil := &DiskUtil{
readDir = mockReadDir
}
// test
diskUtil.CalcNextDisk(...) |
@Kuruyia thank you for your work here! @alicefr I agree with what you suggested. Since we just didn't (or rather couldn't) unit-test anything here, let's try to increase coverage of testing where we can - anything is better than nothing, as it is currently. |
/cc FWIW #1174 is about to merge and will need to be covered here. I wasn't aware of this PR prior to starting this work sorry otherwise I would've extended it myself. |
No problem, I was working on the unit tests today so I'll work on adding your modifications as well afterwards, thanks for letting me know. |
/lgtm |
/test ? |
@brianmcarey: The following commands are available to trigger required jobs:
The following commands are available to trigger optional jobs:
Use
In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/test check-gocli |
New changes are detected. LGTM label has been removed. |
/retest |
@Kuruyia: Cannot trigger testing until a trusted user reviews the PR and leaves an In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/test check-gocli |
/ok-to-test |
/retest |
@Kuruyia: Cannot trigger testing until a trusted user reviews the PR and leaves an In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/test check-gocli |
/ok-to-test |
Signed-off-by: Alexandre Sollier <[email protected]>
Signed-off-by: Alexandre Sollier <[email protected]>
I fixed the name of the base golang image, and tested cluster provisioning under Fedora 40 with Podman. It built successfully, so hopefully this time's the charm for the CI as well :) /retest |
@Kuruyia: Cannot trigger testing until a trusted user reviews the PR and leaves an In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/retest |
hi @Kuruyia thanks for the persistence. I'm in favor of this change. |
@Kuruyia: The following tests failed, say
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
No problem. It looks like the |
Thanks for your contribution.
Centos9 folder which is the only changed folder doesnt affect kind-X providers. Please consider that check-provision uses podman, but the post submit itself uses docker, worth to check it as well, |
PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
What this PR does / why we need it:
This adds a new Go CLI based on Cobra that will replace the vm.sh script used when provisioning a cluster to set up the VMs that will contain the k8s cluster nodes. This is done in order to increase the maintainability of this script in the future.
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #1160
Special notes for your reviewer:
The CLI flags were kept as-is so this can be used as a drop-in replacement.
I've already started to split the functions into multiple files so it is (hopefully) already a bit cleaner than the current shell script.
Checklist
This checklist is not enforcing, but it's a reminder of items that could be relevant to every PR.
Approvers are expected to review this list.
Release note: