Skip to content

Commit

Permalink
Cleaning up. Creating kubevirt.go under k8s
Browse files Browse the repository at this point in the history
Creating function and testing the ssh mechanism

Signed-off-by: Joe Talerico aka rook <[email protected]>
  • Loading branch information
jtaleric committed Aug 28, 2024
1 parent 585c020 commit 137d5e0
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 47 deletions.
52 changes: 5 additions & 47 deletions cmd/k8s-netperf/k8s-netperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
v1 "kubevirt.io/api/core/v1"
)

const index = "k8s-netperf"
Expand Down Expand Up @@ -155,52 +154,11 @@ var rootCmd = &cobra.Command{
if err != nil {
log.Error(err)
}
kclient.VirtualMachineInstances("netperf").Create(context.TODO(), &v1.VirtualMachineInstance{
Spec: v1.VirtualMachineInstanceSpec{
Domain: v1.DomainSpec{
CPU: &v1.CPU{
Sockets: 2,
Cores: 2,
Threads: 1,
},
Devices: v1.Devices{
Disks: []v1.Disk{
v1.Disk{
Name: "disk0",
DiskDevice: v1.DiskDevice{
Disk: &v1.DiskTarget{
Bus: "virtio",
},
},
},
},
},
},
Volumes: []v1.Volume{
v1.Volume{
Name: "disk0",
VolumeSource: v1.VolumeSource{
ContainerDisk: &v1.ContainerDiskSource{
Image: "kubevirt/fedora-cloud-container-disk-demo:latest",
},
},
},
v1.Volume{
Name: "cloudinit",
VolumeSource: v1.VolumeSource{
CloudInitNoCloud: &v1.CloudInitNoCloudSource{
UserData: `#cloud-config
password: fedora
chpasswd: { expire: False }
runcmd:
- dnf install -y uperf iperf3 git ethtool`,
},
},
},
},
},
}, metav1.CreateOptions{})
os.Exit(1)
_, err = k8s.CreateVMServer(kclient, "server")
if err != nil {
log.Error(err)
}
k8s.WaitForVMI(kclient, "server")
}

// Build the SUT (Deployments)
Expand Down
118 changes: 118 additions & 0 deletions pkg/k8s/kubevirt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package k8s

import (
"context"
"fmt"
"os"

b64 "encoding/base64"

kubevirtv1 "github.com/cloud-bulldozer/k8s-netperf/pkg/kubevirt/client-go/clientset/versioned/typed/core/v1"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "kubevirt.io/api/core/v1"
)

func CreateVMServer(client *kubevirtv1.KubevirtV1Client, name string) (*v1.VirtualMachineInstance, error) {
dirname, err := os.UserHomeDir()
if err != nil {
return nil, err
}
ssh, err := os.ReadFile(fmt.Sprintf("%s/.ssh/id_rsa.pub", dirname))
if err != nil {
return nil, err
}
data := fmt.Sprintf(`#cloud-config
runcmd:
- dnf install -y uperf iperf3 git ethtool
users:
- name: fedora
groups: sudo
shell: /bin/bash
ssh_authorized_keys:
- %s
ssh_deletekeys: false
password: fedora
chpasswd: { expire: False }`, string(ssh))
return CreateVMI(client, name, b64.StdEncoding.EncodeToString([]byte(data)))

}

func CreateVMI(client *kubevirtv1.KubevirtV1Client, name string, b64data string) (*v1.VirtualMachineInstance, error) {
vmi, err := client.VirtualMachineInstances(namespace).Create(context.TODO(), &v1.VirtualMachineInstance{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1.VirtualMachineInstanceSpec{
Domain: v1.DomainSpec{
Resources: v1.ResourceRequirements{
Requests: k8sv1.ResourceList{
k8sv1.ResourceMemory: resource.MustParse("4096Mi"),
k8sv1.ResourceCPU: resource.MustParse("500m"),
},
},
CPU: &v1.CPU{
Sockets: 2,
Cores: 2,
Threads: 1,
},
Devices: v1.Devices{
Disks: []v1.Disk{
v1.Disk{
Name: "disk0",
DiskDevice: v1.DiskDevice{
Disk: &v1.DiskTarget{
Bus: "virtio",
},
},
},
},
},
},
Volumes: []v1.Volume{
v1.Volume{
Name: "disk0",
VolumeSource: v1.VolumeSource{
ContainerDisk: &v1.ContainerDiskSource{
Image: "kubevirt/fedora-cloud-container-disk-demo:latest",
},
},
},
v1.Volume{
Name: "cloudinit",
VolumeSource: v1.VolumeSource{
CloudInitNoCloud: &v1.CloudInitNoCloudSource{
UserDataBase64: b64data,
},
},
},
},
},
}, metav1.CreateOptions{})
if err != nil {
return vmi, err
}
return vmi, nil
}

func WaitForVMI(client *kubevirtv1.KubevirtV1Client, name string) error {
vmw, err := client.VirtualMachineInstances(namespace).Watch(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
defer vmw.Stop()
for event := range vmw.ResultChan() {
d, ok := event.Object.(*v1.VirtualMachineInstance)
if !ok {
return fmt.Errorf("Unable to watch VMI %s", name)
}
if d.Name == name {
if d.Status.Phase == "Running" {
return nil
}
}
}
return nil
}

0 comments on commit 137d5e0

Please sign in to comment.