diff --git a/api/proxmox/v1alpha1/virtualmachinetemplate_types.go b/api/proxmox/v1alpha1/virtualmachinetemplate_types.go index 992b3b0..825512c 100644 --- a/api/proxmox/v1alpha1/virtualmachinetemplate_types.go +++ b/api/proxmox/v1alpha1/virtualmachinetemplate_types.go @@ -32,6 +32,8 @@ type VirtualMachineTemplateSpec struct { Name string `json:"name"` // NodeName is the node name NodeName string `json:"node"` + // +kubebuilder:default:=false + DeletionProtection bool `json:"deletionProtection,omitempty"` // VirtualMachineConfig is the configuration of the VM VirtualMachineConfig VirtualMachineConfig `json:"virtualMachineConfig,omitempty"` @@ -108,6 +110,15 @@ type VirtualMachineTemplateStatus struct { //+kubebuilder:object:root=true //+kubebuilder:subresource:status +//+kubebuilder:resource:shortName="vmt" +//+kubebuilder:printcolumn:name="Node",type="string",JSONPath=".spec.node",description="The node name" +//+kubebuilder:printcolumn:name="Cores",type="string",JSONPath=".spec.virtualMachineConfig.cores",description="The number of cores" +//+kubebuilder:printcolumn:name="Memory",type="string",JSONPath=".spec.virtualMachineConfig.memory",description="The amount of memory" +//+kubebuilder:printcolumn:name="Image",type="string",JSONPath=".spec.imageConfig.filename",description="The name of the image" +//+kubebuilder:printcolumn:name="Username",type="string",JSONPath=".spec.cloudInitConfig.user",description="The username" +//+kubebuilder:printcolumn:name="Password",type="string",JSONPath=".spec.cloudInitConfig.password",description="The password" +//+kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.condition[0].type",description="The status of the VM" +//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" // VirtualMachineTemplate is the Schema for the virtualmachinetemplates API type VirtualMachineTemplate struct { diff --git a/charts/kubemox/templates/crds/virtualmachinetemplate.yaml b/charts/kubemox/templates/crds/virtualmachinetemplate.yaml new file mode 100644 index 0000000..9a4d89c --- /dev/null +++ b/charts/kubemox/templates/crds/virtualmachinetemplate.yaml @@ -0,0 +1,269 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + name: virtualmachinetemplates.proxmox.alperen.cloud +spec: + conversion: + strategy: None + group: proxmox.alperen.cloud + names: + kind: VirtualMachineTemplate + listKind: VirtualMachineTemplateList + plural: virtualmachinetemplates + shortNames: + - vmt + singular: virtualmachinetemplate + scope: Namespaced + versions: + - additionalPrinterColumns: + - description: The node name + jsonPath: .spec.node + name: Node + type: string + - description: The number of cores + jsonPath: .spec.virtualMachineConfig.cores + name: Cores + type: string + - description: The amount of memory + jsonPath: .spec.virtualMachineConfig.memory + name: Memory + type: string + - description: The name of the image + jsonPath: .spec.imageConfig.filename + name: Image + type: string + - description: The username + jsonPath: .spec.cloudInitConfig.user + name: Username + type: string + - description: The password + jsonPath: .spec.cloudInitConfig.password + name: Password + type: string + - description: The status of the VM + jsonPath: .status.condition[0].type + name: Status + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 + schema: + openAPIV3Schema: + description: VirtualMachineTemplate is the Schema for the virtualmachinetemplates + API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: VirtualMachineTemplateSpec defines the desired state of VirtualMachineTemplate + properties: + cloudInitConfig: + description: Cloud Init Config + properties: + dnsDomain: + description: DNS Domain + type: string + dnsServers: + description: DNS Servers + items: + type: string + type: array + ipConfig: + description: IPConfig is the IP configuration for the VM + properties: + cidr: + description: Subnet Mask + type: string + gateway: + description: Gateway + type: string + gatewayIPv6: + description: GatewayIPv6 + type: string + ip: + description: IP Address + type: string + ipv6: + description: IPv6 Address + type: string + type: object + password: + description: Password is the password for the template + type: string + sshKeys: + description: SSH Keys -- suppose to be on openSSH format + items: + type: string + type: array + upgradePackages: + default: true + description: Upgrade Packages + type: boolean + user: + description: User is the user name for the template + type: string + type: object + deletionProtection: + default: false + type: boolean + imageConfig: + description: Image config + properties: + checksum: + description: Optional fields + type: string + checksumAlgorithm: + type: string + compression: + type: string + content: + pattern: \b(iso|vztmpl)\b + type: string + filename: + type: string + node: + type: string + storage: + default: local + type: string + url: + type: string + verifyCertificate: + type: boolean + required: + - content + - filename + - node + - url + type: object + name: + description: Name is the name of the VM + type: string + node: + description: NodeName is the node name + type: string + virtualMachineConfig: + description: VirtualMachineConfig is the configuration of the VM + properties: + cores: + default: 2 + description: Cores + type: integer + memory: + default: 2048 + description: Memory as MB + type: integer + network: + properties: + bridge: + default: vmbr0 + type: string + model: + default: virtio + type: string + type: object + sockets: + default: 1 + description: Sockets + type: integer + type: object + required: + - imageConfig + - name + - node + type: object + status: + description: VirtualMachineTemplateStatus defines the observed state of + VirtualMachineTemplate + properties: + condition: + description: 'INSERT ADDITIONAL STATUS FIELD - define observed state + of cluster Important: Run "make" to regenerate code after modifying + this file' + items: + description: "Condition contains details for one aspect of the current + state of this API Resource. --- This struct is intended for direct + use as an array at the field path .status.conditions. For example, + \n type FooStatus struct{ // Represents the observations of a + foo's current state. // Known .status.conditions.type are: \"Available\", + \"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge + // +listType=map // +listMapKey=type Conditions ]metav1.Condition + `json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\" + protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }" + properties: + lastTransitionTime: + description: lastTransitionTime is the last time the condition + transitioned from one status to another. This should be when + the underlying condition changed. If that is not known, then + using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: message is a human readable message indicating + details about the transition. This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: observedGeneration represents the .metadata.generation + that the condition was set based upon. For instance, if .metadata.generation + is currently 12, but the .status.conditions[x].observedGeneration + is 9, the condition is out of date with respect to the current + state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: reason contains a programmatic identifier indicating + the reason for the condition's last transition. Producers + of specific condition types may define expected values and + meanings for this field, and whether the values are considered + a guaranteed API. The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + --- Many .condition.type values are consistent across resources + like Available, but because arbitrary conditions can be useful + (see .node.status.conditions), the ability to deconflict is + important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt) + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + status: + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} \ No newline at end of file diff --git a/config/crd/bases/proxmox.alperen.cloud_virtualmachinetemplates.yaml b/config/crd/bases/proxmox.alperen.cloud_virtualmachinetemplates.yaml index 4dfaae1..374486f 100644 --- a/config/crd/bases/proxmox.alperen.cloud_virtualmachinetemplates.yaml +++ b/config/crd/bases/proxmox.alperen.cloud_virtualmachinetemplates.yaml @@ -11,10 +11,44 @@ spec: kind: VirtualMachineTemplate listKind: VirtualMachineTemplateList plural: virtualmachinetemplates + shortNames: + - vmt singular: virtualmachinetemplate scope: Namespaced versions: - - name: v1alpha1 + - additionalPrinterColumns: + - description: The node name + jsonPath: .spec.node + name: Node + type: string + - description: The number of cores + jsonPath: .spec.virtualMachineConfig.cores + name: Cores + type: string + - description: The amount of memory + jsonPath: .spec.virtualMachineConfig.memory + name: Memory + type: string + - description: The name of the image + jsonPath: .spec.imageConfig.filename + name: Image + type: string + - description: The username + jsonPath: .spec.cloudInitConfig.user + name: Username + type: string + - description: The password + jsonPath: .spec.cloudInitConfig.password + name: Password + type: string + - description: The status of the VM + jsonPath: .status.condition[0].type + name: Status + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 schema: openAPIV3Schema: description: VirtualMachineTemplate is the Schema for the virtualmachinetemplates @@ -81,6 +115,9 @@ spec: description: User is the user name for the template type: string type: object + deletionProtection: + default: false + type: boolean imageConfig: description: Image config properties: diff --git a/docs/crds/virtualmachinetemplate.md b/docs/crds/virtualmachinetemplate.md new file mode 100644 index 0000000..715829a --- /dev/null +++ b/docs/crds/virtualmachinetemplate.md @@ -0,0 +1,45 @@ +# VirtualMachineTemplate + +`VirtualMachineTemplate` is a Kubernetes Custom Resource Definition (CRD) that defines a template for a VirtualMachine. It is used to create a VirtualMachine object and then converts that into a template for future use. It utilizes the 'CloudInit' behind the scenes to configure the VirtualMachine. `VirtualMachineTemplate` has three main specs that you can define: + +- `cloudInitConfig`: This struct includes cloud-init configuration that will be used to configure the VirtualMachine. This field is mandatory to create a VirtualMachineTemplate. + +- `imageConfig`: This struct includes the image configuration that will be used to create the VirtualMachine from the template. You should use cloud-init compatible images within this field. + +- `virtualMachineConfig`: This struct includes the VirtualMachine configuration that will be used to create the VirtualMachine from the template. You can define the VirtualMachine configuration like cores, memory, disk, network, etc. within this field. This is non-mandatory struct and default values will be used if not defined. + + +## Creating VirtualMachineTemplate + +To create a new VirtualMachineTemplate in Proxmox, you need to create a `VirtualMachineTemplate` object. This object should be generated by other VirtualMachineTemplate objects. + +```yaml +apiVersion: proxmox.alperen.cloud/v1alpha1 +kind: VirtualMachineTemplate +metadata: + name: virtualmachinetemplate-sample +spec: + name: virtualmachinetemplate-sample + node: lowtower + cloudInitConfig: + ipConfig: + cidr: "24" + gateway: 10.153.0.1 + ip: 10.153.0.99 + password: ubuntu + upgradePackages: true + user: ubuntu + imageConfig: + content: iso + filename: bionic-server-cloudimg-amd64.img + node: lowtower + storage: local + url: https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img + virtualMachineConfig: + cores: 2 + memory: 2048 + network: + bridge: vmbr0 + model: virtio + sockets: 1 +``` \ No newline at end of file diff --git a/internal/controller/proxmox/virtualmachinetemplate_controller.go b/internal/controller/proxmox/virtualmachinetemplate_controller.go index 6bd3b26..fad9809 100644 --- a/internal/controller/proxmox/virtualmachinetemplate_controller.go +++ b/internal/controller/proxmox/virtualmachinetemplate_controller.go @@ -278,7 +278,12 @@ func (r *VirtualMachineTemplateReconciler) deleteVirtualMachineTemplate(ctx cont logger := log.FromContext(ctx) logger.Info(fmt.Sprintf("Deleting VirtualMachineTemplate %s", vmTemplate.Name)) // Delete the VM - proxmox.DeleteVM(vmTemplate.Spec.Name, vmTemplate.Spec.NodeName) + if vmTemplate.Spec.DeletionProtection { + logger.Info("Deletion protection is enabled, skipping the deletion of VM") + return + } else { + proxmox.DeleteVM(vmTemplate.Spec.Name, vmTemplate.Spec.NodeName) + } } func (r *VirtualMachineTemplateReconciler) createStorageDownloadURLCR(ctx context.Context, diff --git a/mkdocs.yml b/mkdocs.yml index fabdf5a..76300c1 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -9,6 +9,7 @@ nav: - Virtual Machines (QEMU): - VirtualMachine: crds/virtualmachine.md - ManagedVirtualMachine: crds/managedvirtualmachine.md + - VirtualMachineTemplate: crds/virtualmachinetemplate.md - VirtualMachineSet: crds/virtualmachineset.md - VirtualMachineSnapshot: crds/virtualmachinesnapshot.md - VirtualMachineSnapshotPolicy: crds/virtualmachinesnapshotpolicy.md