-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Start VM in Harvester #7252
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
Merged
Merged
Start VM in Harvester #7252
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
type NamespaceManifestOptions = { | ||
namespace: string | ||
} | ||
|
||
export function NamespaceManifest({ namespace }: NamespaceManifestOptions) { | ||
return ` | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: ${namespace} | ||
` | ||
} | ||
|
||
type VirtualMachineManifestArguments = { | ||
vmName: string | ||
namespace: string | ||
claimName: string, | ||
userDataSecretName: string | ||
} | ||
|
||
export function VirtualMachineManifest({ vmName, namespace, claimName, userDataSecretName }: VirtualMachineManifestArguments) { | ||
return ` | ||
apiVersion: kubevirt.io/v1 | ||
type: kubevirt.io.virtualmachine | ||
kind: VirtualMachine | ||
metadata: | ||
namespace: ${namespace} | ||
annotations: | ||
harvesterhci.io/volumeClaimTemplates: '[{"metadata":{"name":"${claimName}","annotations":{"harvesterhci.io/imageId":"default/image-cjlm2"}},"spec":{"accessModes":["ReadWriteMany"],"resources":{"requests":{"storage":"10Gi"}},"volumeMode":"Block","storageClassName":"longhorn-image-cjlm2"}}]' | ||
network.harvesterhci.io/ips: "[]" | ||
labels: | ||
harvesterhci.io/creator: harvester | ||
harvesterhci.io/os: ubuntu | ||
name: ${vmName} | ||
spec: | ||
running: true | ||
template: | ||
metadata: | ||
annotations: | ||
harvesterhci.io/sshNames: "[]" | ||
labels: | ||
harvesterhci.io/vmName: ${vmName} | ||
spec: | ||
domain: | ||
hostname: ${vmName} | ||
machine: | ||
type: q35 | ||
cpu: | ||
cores: 1 | ||
sockets: 1 | ||
threads: 1 | ||
devices: | ||
interfaces: | ||
- masquerade: {} | ||
model: virtio | ||
name: default | ||
disks: | ||
- name: system | ||
bootOrder: 1 | ||
disk: | ||
bus: virtio | ||
- name: cloudinitdisk | ||
disk: | ||
bus: virtio | ||
resources: | ||
limits: | ||
memory: 2Gi | ||
cpu: 1 | ||
evictionStrategy: LiveMigrate | ||
networks: | ||
- pod: {} | ||
name: default | ||
volumes: | ||
- name: system | ||
persistentVolumeClaim: | ||
claimName: ${claimName} | ||
- name: cloudinitdisk | ||
cloudInitNoCloud: | ||
networkDataSecretRef: | ||
name: ${userDataSecretName} | ||
secretRef: | ||
name: ${userDataSecretName} | ||
` | ||
} | ||
|
||
type ServiceManifestOptions = { | ||
vmName: string | ||
namespace: string | ||
} | ||
|
||
export function ServiceManifest({ vmName, namespace }: ServiceManifestOptions) { | ||
return ` | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: proxy | ||
namespace: ${namespace} | ||
spec: | ||
ports: | ||
- name: ssh | ||
protocol: TCP | ||
port: 22 | ||
targetPort: 22 | ||
- name: http | ||
protocol: TCP | ||
port: 80 | ||
targetPort: 80 | ||
- name: https | ||
protocol: TCP | ||
port: 443 | ||
targetPort: 443 | ||
- name: kube-api | ||
protocol: TCP | ||
port: 6443 | ||
targetPort: 6443 | ||
selector: | ||
harvesterhci.io/vmName: ${vmName} | ||
type: ClusterIP | ||
` | ||
} | ||
|
||
type UserDataSecretManifestOptions = { | ||
namespace: string, | ||
secretName: string | ||
} | ||
|
||
export function UserDataSecretManifest({ namespace, secretName }: UserDataSecretManifestOptions) { | ||
const userdata = Buffer.from(`#cloud-config | ||
users: | ||
- name: ubuntu | ||
lock_passwd: false | ||
sudo: "ALL=(ALL) NOPASSWD: ALL" | ||
passwd: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"`).toString("base64") | ||
return ` | ||
apiVersion: v1 | ||
type: secret | ||
kind: Secret | ||
data: | ||
networkdata: "" | ||
userdata: ${userdata} | ||
metadata: | ||
name: ${secretName} | ||
namespace: ${namespace} | ||
` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { exec } from '../util/shell'; | ||
|
||
import * as Manifests from './manifests' | ||
|
||
const KUBECONFIG_PATH = '/mnt/secrets/harvester-kubeconfig/harvester-kubeconfig.yml' | ||
|
||
/** | ||
* Convenience function to kubectl apply a manifest from stdin. | ||
*/ | ||
function kubectlApplyManifest(manifest: string, options?: { validate?: boolean }) { | ||
exec(` | ||
cat <<EOF | kubectl --kubeconfig ${KUBECONFIG_PATH} apply --validate=${!!options?.validate} -f - | ||
${manifest} | ||
EOF | ||
`) | ||
} | ||
|
||
/** | ||
* Start a VM | ||
* Does not wait for the VM to be ready. | ||
*/ | ||
export function startVM(options: { name: string }) { | ||
const namespace = `preview-${options.name}` | ||
const userDataSecretName = `userdata-${options.name}` | ||
|
||
kubectlApplyManifest( | ||
Manifests.NamespaceManifest({ | ||
namespace | ||
}) | ||
) | ||
|
||
kubectlApplyManifest( | ||
Manifests.UserDataSecretManifest({ | ||
namespace, | ||
secretName: userDataSecretName, | ||
}) | ||
) | ||
|
||
kubectlApplyManifest( | ||
Manifests.VirtualMachineManifest({ | ||
namespace, | ||
vmName: options.name, | ||
claimName: `${options.name}-${Date.now()}`, | ||
userDataSecretName | ||
}), | ||
{ validate: false } | ||
) | ||
|
||
kubectlApplyManifest( | ||
Manifests.ServiceManifest({ | ||
vmName: options.name, | ||
namespace | ||
}) | ||
) | ||
} | ||
|
||
/** | ||
* Check if a VM with the given name already exists. | ||
* @returns true if the VM already exists | ||
*/ | ||
export function vmExists(options: { name: string }) { | ||
const namespace = `preview-${options.name}` | ||
const status = exec(`kubectl --kubeconfig ${KUBECONFIG_PATH} -n ${namespace} get vmi ${options.name}`, { dontCheckRc: true, silent: true }) | ||
return status.code == 0 | ||
} | ||
|
||
/** | ||
* Wait until the VM Instance reaches the Running status. | ||
* If the VM Instance doesn't reach Running before the timeoutMS it will throw an Error. | ||
*/ | ||
export function waitForVM(options: { name: string, timeoutMS: number }) { | ||
const namespace = `preview-${options.name}` | ||
const startTime = Date.now() | ||
while (true) { | ||
|
||
const status = exec(`kubectl --kubeconfig ${KUBECONFIG_PATH} -n ${namespace} get vmi ${options.name} -o jsonpath="{.status.phase}"`, { silent: true }).stdout.trim() | ||
|
||
if (status == "Running") { | ||
return | ||
} | ||
|
||
const elapsedTimeMs = Date.now() - startTime | ||
if (elapsedTimeMs > options.timeoutMS) { | ||
throw new Error("VM didn reach Running status before the timeout") | ||
} | ||
|
||
console.log(`VM is not yet running. Current status is ${status}. Sleeping 5 seconds`) | ||
exec('sleep 5', { silent: true }) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.