Skip to content

Commit 2102f1f

Browse files
committed
wip: add cloud init to machine spec
1 parent fefbb7d commit 2102f1f

16 files changed

+524
-167
lines changed

api/v1beta1/cloudinit_types.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package v1beta1
2+
3+
// CloudInit defines options related to the bootstrapping systems where
4+
// CloudInit is used.
5+
type CloudInit struct {
6+
User User `json:"user,omitempty"`
7+
Meta Meta `json:"meta,omitempty"`
8+
Network Network `json:"network,omitempty"`
9+
}
10+
11+
type User struct {
12+
GrowPart GrowPart `yaml:"growpart,omitempty" json:"-"`
13+
HostName string `yaml:"hostname,omitempty" json:"-"`
14+
ManageEtcHosts bool `yaml:"manage_etc_hosts,omitempty" json:"-"`
15+
User string `yaml:"user,omitempty" json:"user,omitempty"`
16+
ChPasswd ChPasswd `yaml:"chpasswd,omitempty" json:"-"`
17+
Users []string `yaml:"users,omitempty" json:"-"`
18+
Password string `yaml:"password,omitempty" json:"password,omitempty"`
19+
Packages []string `yaml:"packages,omitempty" json:"-"`
20+
PackageUpgrade bool `yaml:"package_upgrade,omitempty" json:"-"`
21+
WriteFiles []WriteFiles `yaml:"write_files,omitempty" json:"-"`
22+
RunCmd []string `yaml:"runcmd,omitempty" json:"-"`
23+
}
24+
25+
type Network struct {
26+
Version int `json:"version,omitempty"`
27+
Config []NetworkConfig `json:"config,omitempty"`
28+
}
29+
30+
type NetworkConfig struct {
31+
Type string `json:"type,omitempty"`
32+
Name string `json:"name,omitempty"`
33+
MacAddress string `json:"mac_address,omitempty"`
34+
Subnets []Subnet `json:"subnets,omitempty"`
35+
Destination string `json:"destination,omitempty"`
36+
Gateway string `json:"gateway,omitempty"`
37+
}
38+
39+
type Subnet struct {
40+
Type string `json:"type,omitempty"`
41+
Address string `json:"address,omitempty"`
42+
Gateway string `json:"gateway,omitempty"`
43+
}
44+
45+
type Meta struct {
46+
}
47+
48+
type GrowPart struct {
49+
Mode string `yaml:"mode,omitempty" json:"-"`
50+
Devices []string `yaml:"devices,omitempty" json:"-"`
51+
IgnoreGrowrootDisabled bool `yaml:"ignore_growroot_disabled,omitempty" json:"-"`
52+
}
53+
54+
type ChPasswd struct {
55+
Expire string `yaml:"expire,omitempty" json:"-"`
56+
}
57+
58+
type WriteFiles struct {
59+
Path string `yaml:"path,omitempty" json:"-"`
60+
Owner string `yaml:"owner,omitempty" json:"-"`
61+
Permissions string `yaml:"permissions,omitempty" json:"-"`
62+
Content string `yaml:"content,omitempty" json:"-"`
63+
}

api/v1beta1/proxmoxcluster_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type ProxmoxClusterStatus struct {
5555

5656
//+kubebuilder:object:root=true
5757
//+kubebuilder:subresource:status
58+
// +kubebuilder:printcolumn:name="ControlPlaneEndpoint",type=string,JSONPath=`.spec.controlPlaneEndpoint`
59+
// +kubebuilder:printcolumn:name="Ready",type=string,JSONPath=`.status.ready`
5860

5961
// ProxmoxCluster is the Schema for the proxmoxclusters API
6062
type ProxmoxCluster struct {

api/v1beta1/proxmoxmachine_types.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type ProxmoxMachineSpec struct {
3838
// ProviderID
3939
ProviderID *string `json:"providerID,omitempty"`
4040

41+
// Image is the image to be provisioned
42+
// Image Image `json:"image"`
43+
4144
// CloudInit defines options related to the bootstrapping systems where
4245
// CloudInit is used.
4346
// +optional
@@ -47,9 +50,6 @@ type ProxmoxMachineSpec struct {
4750
// For this infrastructure provider, the ID is equivalent to an AWS Availability Zone.
4851
// If multiple subnets are matched for the availability zone, the first one returned is picked.
4952
FailureDomain *string `json:"failureDomain,omitempty"`
50-
51-
// Foo is an example field of ProxmoxMachine. Edit proxmoxmachine_types.go to remove/update
52-
Foo string `json:"foo,omitempty"`
5353
}
5454

5555
// ProxmoxMachineStatus defines the observed state of ProxmoxMachine
@@ -72,11 +72,14 @@ type ProxmoxMachineStatus struct {
7272

7373
// InstanceStatus is the status of the GCP instance for this machine.
7474
// +optional
75-
InstanceStatus *InstanceStatus `json:"instanceState,omitempty"` // InstanceStatus
75+
InstanceStatus *InstanceStatus `json:"instanceStatus,omitempty"` // InstanceStatus
7676
}
7777

7878
//+kubebuilder:object:root=true
7979
//+kubebuilder:subresource:status
80+
// +kubebuilder:printcolumn:name="Addresses",type=string,JSONPath=`.status.addresses`
81+
// +kubebuilder:printcolumn:name="ProviderID",type=string,JSONPath=`.spec.providerID`
82+
// +kubebuilder:printcolumn:name="InstanceStatus",type=string,JSONPath=`.status.instanceStatus`
8083

8184
// ProxmoxMachine is the Schema for the proxmoxmachines API
8285
type ProxmoxMachine struct {

api/v1beta1/type.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ type ObjectReference struct {
1616
Name string `json:"name"`
1717
}
1818

19-
// CloudInit defines options related to the bootstrapping systems where
20-
// CloudInit is used.
21-
type CloudInit struct {
22-
}
19+
// Image is the image to be provisioned
20+
// type Image struct {
21+
// // URL is a location of an image to deploy.
22+
// URL string `json:"url"`
23+
24+
// // Checksum
25+
// Checksum string `json:"checksum,omitempty"`
26+
27+
// // ChecksumType
28+
// ChecksumType *string `json:"checksumType,omitempty"`
29+
// }
2330

31+
// Storage for image and snippets
2432
type Storage struct {
2533
Name string `json:"name"`
2634
Path string `json:"path,omitempty"`

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 163 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/cloudinit/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cloudinit
2+
3+
func IsValidType(cloudInitType string) bool {
4+
switch cloudInitType {
5+
case "user", "meta", "network":
6+
return true
7+
default:
8+
return false
9+
}
10+
}

cloud/cloudinit/network.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cloudinit
2+
3+
import (
4+
"gopkg.in/yaml.v3"
5+
6+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
7+
)
8+
9+
func ParseNetwork(content string) (*infrav1.Network, error) {
10+
var config *infrav1.Network
11+
if err := yaml.Unmarshal([]byte(content), &config); err != nil {
12+
return nil, err
13+
}
14+
return config, nil
15+
}
16+
17+
func GenerateNetworkYaml(config infrav1.Network) (string, error) {
18+
b, err := yaml.Marshal(&config)
19+
if err != nil {
20+
return "", err
21+
}
22+
return string(b), nil
23+
}

cloud/cloudinit/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cloudinit

cloud/cloudinit/user.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cloudinit
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/imdario/mergo"
7+
"gopkg.in/yaml.v3"
8+
9+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
10+
)
11+
12+
func ParseUser(content string) (*infrav1.User, error) {
13+
var config *infrav1.User
14+
if err := yaml.Unmarshal([]byte(content), &config); err != nil {
15+
return nil, err
16+
}
17+
return config, nil
18+
}
19+
20+
func GenerateUserYaml(config infrav1.User) (string, error) {
21+
b, err := yaml.Marshal(&config)
22+
if err != nil {
23+
return "", err
24+
}
25+
return fmt.Sprintf("#cloud-config\n%s", string(b)), nil
26+
}
27+
28+
func MergeUsers(a, b infrav1.User) (*infrav1.User, error) {
29+
if err := mergo.Merge(&a, b, mergo.WithAppendSlice); err != nil {
30+
return nil, err
31+
}
32+
return &a, nil
33+
}

0 commit comments

Comments
 (0)