Skip to content

Commit

Permalink
Merge pull request #38 from alperencelik/vm-template-cr
Browse files Browse the repository at this point in the history
Add support for virtualMachineTemplate CRD with cloud-init
  • Loading branch information
alperencelik authored Sep 18, 2024
2 parents ea2b9bd + 3ab3f80 commit 982c0d8
Show file tree
Hide file tree
Showing 24 changed files with 1,707 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ linters:
- gofmt
- goimports
- goprintffuncname
- gosec
# - gosec // Disable gosec for uint64 to int conversion issue. TODO: Handle uint64 to int conversion via utils.
- gosimple
- govet
- ineffassign
Expand Down
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ resources:
kind: StorageDownloadURL
path: github.com/alperencelik/kubemox/api/proxmox/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: alperen.cloud
group: proxmox
kind: VirtualMachineTemplate
path: github.com/alperencelik/kubemox/api/proxmox/v1alpha1
version: v1alpha1
version: "3"
6 changes: 4 additions & 2 deletions api/proxmox/v1alpha1/storagedownloadurl_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ type StorageDownloadURLSpec struct {
Content string `json:"content"`
Filename string `json:"filename"`
Node string `json:"node"`
Storage string `json:"storage"`
URL string `json:"url"`
// +kubebuilder:default:=local
Storage string `json:"storage,omitempty"`
URL string `json:"url"`
// Optional fields
Checksum string `json:"checksum,omitempty"`
ChecksumAlgorithm string `json:"checksumAlgorithm,omitempty"`
Expand All @@ -46,6 +47,7 @@ type StorageDownloadURLStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"` //nolint:lll // This is required by kubebuilder
Status string `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
132 changes: 132 additions & 0 deletions api/proxmox/v1alpha1/virtualmachinetemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// VirtualMachineTemplateSpec defines the desired state of VirtualMachineTemplate
type VirtualMachineTemplateSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Name is the name of the VM
Name string `json:"name"`
// NodeName is the node name
NodeName string `json:"node"`

// VirtualMachineConfig is the configuration of the VM
VirtualMachineConfig VirtualMachineConfig `json:"virtualMachineConfig,omitempty"`

// Image config
ImageConfig StorageDownloadURLSpec `json:"imageConfig"`

// Cloud Init Config
CloudInitConfig CloudInitConfig `json:"cloudInitConfig,omitempty"`
}

type VirtualMachineConfig struct {
// Sockets
// +kubebuilder:default:=1
Sockets int `json:"sockets,omitempty"`
// Cores
// +kubebuilder:default:=2
Cores int `json:"cores,omitempty"`
// Memory as MB
// +kubebuilder:default:=2048
Memory int `json:"memory,omitempty"`
Network VMTemplateNetwork `json:"network,omitempty"`
}

type VMTemplateNetwork struct {
// +kubebuilder:default:="virtio"
Model string `json:"model,omitempty"`
// +kubebuilder:default:="vmbr0"
Bridge string `json:"bridge,omitempty"`
}

type CloudInitConfig struct {

// User is the user name for the template
User string `json:"user,omitempty"`
// Password is the password for the template
Password string `json:"password,omitempty"`
// DNS Domain
DNSDomain string `json:"dnsDomain,omitempty"`
// DNS Servers
DNSServers []string `json:"dnsServers,omitempty"`
// SSH Keys -- suppose to be on openSSH format
SSHKeys []string `json:"sshKeys,omitempty"`
// Upgrade Packages
// +kubebuilder:default:=true
UpgradePackages bool `json:"upgradePackages,omitempty"`
// IPConfig is the IP configuration for the VM
IPConfig IPConfig `json:"ipConfig,omitempty"`
// TODO: Implement the following
// cicustom
// ipconfig[n]
}

type IPConfig struct {
// Gateway
Gateway string `json:"gateway,omitempty"`
// GatewayIPv6
GatewayIPv6 string `json:"gatewayIPv6,omitempty"`
// IP Address
IP string `json:"ip,omitempty"`
// IPv6 Address
IPv6 string `json:"ipv6,omitempty"`
// Subnet Mask
CIDR string `json:"cidr,omitempty"`
}

// VirtualMachineTemplateStatus defines the observed state of VirtualMachineTemplate
type VirtualMachineTemplateStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Conditions []metav1.Condition `json:"condition,omitempty"`
Status string `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// VirtualMachineTemplate is the Schema for the virtualmachinetemplates API
type VirtualMachineTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec VirtualMachineTemplateSpec `json:"spec,omitempty"`
Status VirtualMachineTemplateStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// VirtualMachineTemplateList contains a list of VirtualMachineTemplate
type VirtualMachineTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []VirtualMachineTemplate `json:"items"`
}

func init() {
SchemeBuilder.Register(&VirtualMachineTemplate{}, &VirtualMachineTemplateList{})
}
171 changes: 171 additions & 0 deletions api/proxmox/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "StorageDownloadURL")
os.Exit(1)
}
if err = (&proxmoxcontroller.VirtualMachineTemplateReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Watchers: proxmox.NewExternalWatchers(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "VirtualMachineTemplate")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Loading

0 comments on commit 982c0d8

Please sign in to comment.