Skip to content

Commit 678106c

Browse files
committed
use ipconfig instead of custom cloud init
1 parent 2102f1f commit 678106c

15 files changed

+134
-173
lines changed

api/v1beta1/cloudinit_types.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package v1beta1
33
// CloudInit defines options related to the bootstrapping systems where
44
// CloudInit is used.
55
type CloudInit struct {
6-
User User `json:"user,omitempty"`
7-
Meta Meta `json:"meta,omitempty"`
8-
Network Network `json:"network,omitempty"`
6+
User *User `json:"user,omitempty"`
7+
8+
// Meta *Meta `json:"-"`
9+
10+
// DEPRECATED : use IPConfig instead
11+
Network *Network `json:"-"`
912
}
1013

1114
type User struct {
@@ -23,23 +26,24 @@ type User struct {
2326
}
2427

2528
type Network struct {
26-
Version int `json:"version,omitempty"`
27-
Config []NetworkConfig `json:"config,omitempty"`
29+
Version int `yaml:"version,omitempty" json:"version,omitempty"`
30+
Config []NetworkConfig `yaml:"config,omitempty" json:"config,omitempty"`
2831
}
2932

3033
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"`
34+
Type string `yaml:"type,omitempty" json:"type,omitempty"`
35+
Name string `yaml:"name,omitempty" json:"name,omitempty"`
36+
MacAddress string `yaml:"mac_address,omitempty" json:"mac_address,omitempty"`
37+
Subnets []Subnet `yaml:"subnets,omitempty" json:"subnets,omitempty"`
38+
Destination string `yaml:"destination,omitempty" json:"destination,omitempty"`
39+
Gateway string `yaml:"gateway,omitempty" json:"gateway,omitempty"`
3740
}
3841

3942
type Subnet struct {
40-
Type string `json:"type,omitempty"`
41-
Address string `json:"address,omitempty"`
42-
Gateway string `json:"gateway,omitempty"`
43+
Type string `yaml:"type,omitempty" json:"type,omitempty"`
44+
Address string `yaml:"address,omitempty" json:"address,omitempty"`
45+
NetMask string `yaml:"netmask,omitempty" json:"netmask,omitempty"`
46+
Gateway string `yaml:"gateway,omitempty" json:"gateway,omitempty"`
4347
}
4448

4549
type Meta struct {

api/v1beta1/proxmoxcluster_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type ProxmoxClusterStatus struct {
5555

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

6161
// ProxmoxCluster is the Schema for the proxmoxclusters API

api/v1beta1/proxmoxmachine_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type ProxmoxMachineSpec struct {
4646
// +optional
4747
CloudInit CloudInit `json:"cloudInit,omitempty"`
4848

49+
// IPConfig
50+
// to do : should accept multiple IPConfig
51+
IPConfig IPConfig `json:"ipConfig,omitempty"`
52+
4953
// FailureDomain is the failure domain unique identifier this Machine should be attached to, as defined in Cluster API.
5054
// For this infrastructure provider, the ID is equivalent to an AWS Availability Zone.
5155
// If multiple subnets are matched for the availability zone, the first one returned is picked.

api/v1beta1/type.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1beta1
22

33
import (
4+
"fmt"
5+
46
"github.com/sp-yduck/proxmox/pkg/service/node/vm"
57
)
68

@@ -28,6 +30,26 @@ type ObjectReference struct {
2830
// ChecksumType *string `json:"checksumType,omitempty"`
2931
// }
3032

33+
// IPConfig
34+
type IPConfig struct {
35+
IP string `json:"ip,omitempty"`
36+
Gateway4 string `json:"gateway,omitempty"`
37+
}
38+
39+
func (i *IPConfig) String() string {
40+
config := ""
41+
if i.IP != "" {
42+
config += fmt.Sprintf("ip=%s", i.IP)
43+
}
44+
if i.IP != "" && i.Gateway4 != "" {
45+
config += ","
46+
}
47+
if i.Gateway4 != "" {
48+
config += fmt.Sprintf("gw=%s", i.Gateway4)
49+
}
50+
return config
51+
}
52+
3153
// Storage for image and snippets
3254
type Storage struct {
3355
Name string `json:"name"`

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/cloudinit/types.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

cloud/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type MachineGetter interface {
6060
GetInstanceStatus() *infrav1.InstanceStatus
6161
GetStorage() infrav1.Storage
6262
GetCloudInit() infrav1.CloudInit
63+
GetIPConfig() infrav1.IPConfig
6364
}
6465

6566
// MachineSetter is an interface which can set machine information.

cloud/scope/machine.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/sp-yduck/proxmox/pkg/service/node/vm"
2424
corev1 "k8s.io/api/core/v1"
2525
"k8s.io/apimachinery/pkg/types"
26-
"k8s.io/klog/v2"
2726
"k8s.io/utils/pointer"
2827
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2928
"sigs.k8s.io/cluster-api/controllers/noderefutil"
@@ -153,10 +152,13 @@ func (m *MachineScope) GetProviderID() string {
153152
}
154153

155154
func (m *MachineScope) GetCloudInit() infrav1.CloudInit {
156-
klog.Info(m.ProxmoxMachine.Spec.CloudInit)
157155
return m.ProxmoxMachine.Spec.CloudInit
158156
}
159157

158+
func (m *MachineScope) GetIPConfig() infrav1.IPConfig {
159+
return m.ProxmoxMachine.Spec.IPConfig
160+
}
161+
160162
// SetProviderID sets the ProxmoxMachine providerID in spec.
161163
func (m *MachineScope) SetProviderID(instance *vm.VirtualMachine) error {
162164
providerid, err := providerid.New(instance.Node.Name(), instance.VMID)

cloud/services/compute/instance/cloudinit.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import (
44
"fmt"
55

66
"github.com/pkg/errors"
7-
"k8s.io/klog/v2"
87

8+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
99
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/cloudinit"
1010
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
11-
12-
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
1311
)
1412

1513
// reconcileCloudInit
@@ -18,18 +16,10 @@ func reconcileCloudInit(s *Service, vmid int, bootstrap string) error {
1816
storageName := s.scope.GetStorage().Name
1917
cloudInit := s.scope.GetCloudInit()
2018

21-
klog.Info(cloudInit)
22-
2319
// user
24-
if err := reconcileCloudInitUser(vmid, vmName, storageName, bootstrap, cloudInit.User, s.remote); err != nil {
25-
return err
26-
}
27-
28-
// meta & network
29-
if err := reconcileCloudInitConfig(vmid, vmName, storageName, cloudInit, s.remote); err != nil {
20+
if err := reconcileCloudInitUser(vmid, vmName, storageName, bootstrap, *cloudInit.User, s.remote); err != nil {
3021
return err
3122
}
32-
3323
return nil
3424
}
3525

@@ -59,25 +49,21 @@ func reconcileCloudInitUser(vmid int, vmName, storageName, bootstrap string, con
5949
return errors.Errorf("ssh command error : %s : %v", out, err)
6050
}
6151

62-
if err := ApplyCICustom(vmid, vmName, storageName, "user", ssh); err != nil {
63-
return err
64-
}
6552
return nil
6653
}
6754

55+
// DEPRECATED : network can be configured via API with ipconfig params
6856
func reconcileCloudInitConfig(vmid int, vmName, storageName string, cloudInit infrav1.CloudInit, ssh scope.SSHClient) error {
69-
klog.Info(cloudInit.Network)
7057

71-
networkYaml, err := cloudinit.GenerateNetworkYaml(cloudInit.Network)
72-
if err != nil {
73-
return err
74-
}
75-
out, err := ssh.RunWithStdin(fmt.Sprintf("tee /var/lib/vz/%s/snippets/%s-network.yml", storageName, vmName), networkYaml)
76-
if err != nil {
77-
return errors.Errorf("ssh command error : %s : %v", out, err)
78-
}
79-
if err := ApplyCICustom(vmid, vmName, storageName, "network", ssh); err != nil {
80-
return err
58+
if cloudInit.Network != nil {
59+
networkYaml, err := cloudinit.GenerateNetworkYaml(*cloudInit.Network)
60+
if err != nil {
61+
return err
62+
}
63+
out, err := ssh.RunWithStdin(fmt.Sprintf("tee /var/lib/vz/%s/snippets/%s-network.yml", storageName, vmName), networkYaml)
64+
if err != nil {
65+
return errors.Errorf("ssh command error : %s : %v", out, err)
66+
}
8167
}
8268

8369
// if meta != nil {
@@ -89,14 +75,12 @@ func reconcileCloudInitConfig(vmid int, vmName, storageName string, cloudInit in
8975
// if err != nil {
9076
// return errors.Errorf("ssh command error : %s : %v", out, err)
9177
// }
92-
// if err := ApplyCICustom(vmid, vmName, storageName, "meta", ssh); err != nil {
93-
// return err
94-
// }
9578
// }
9679

9780
return nil
9881
}
9982

83+
// DEPRECATED : cicustom should be set via API
10084
func ApplyCICustom(vmid int, vmName, storageName, ciType string, ssh scope.SSHClient) error {
10185
if !cloudinit.IsValidType(ciType) {
10286
return errors.Errorf("invalid cloud init type: %s", ciType)

cloud/services/compute/instance/reconcile.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (s *Service) CreateInstance(ctx context.Context, bootstrap string) (*vm.Vir
114114
}
115115

116116
// create vm
117-
vmoption := generateVMOptions(s.scope.Name(), s.scope.GetStorage().Name)
117+
vmoption := generateVMOptions(s.scope.Name(), s.scope.GetStorage().Name, s.scope.GetIPConfig())
118118
vm, err := node.CreateVirtualMachine(vmid, vmoption)
119119
if err != nil {
120120
log.Error(err, "failed to create virtual machine")
@@ -203,21 +203,23 @@ func SetCloudImage(ctx context.Context, vmid int, storageName string, ssh scope.
203203
return nil
204204
}
205205

206-
func generateVMOptions(vmName, storageName string) vm.VirtualMachineCreateOptions {
206+
func generateVMOptions(vmName, storageName string, ipconfig infrav1.IPConfig) vm.VirtualMachineCreateOptions {
207+
207208
vmoptions := vm.VirtualMachineCreateOptions{
208-
Agent: "enabled=1",
209-
Cores: 2,
210-
Memory: 1024 * 4,
211-
Name: vmName,
212-
Boot: "order=scsi0",
213-
Ide: vm.Ide{Ide2: fmt.Sprintf("file=%s:cloudinit,media=cdrom", storageName)},
214-
// IPConfig: vm.IPConfig{IPConfig0: "ip=192.168.1.222/32,gw=192.168.1.1"},
215-
OSType: vm.L26,
216-
Net: vm.Net{Net0: "model=virtio,bridge=vmbr0,firewall=1"},
217-
Scsi: vm.Scsi{Scsi0: fmt.Sprintf("file=%s:8", storageName)},
218-
ScsiHw: vm.VirtioScsiPci,
219-
Serial: vm.Serial{Serial0: "socket"},
220-
VGA: "serial0",
209+
Agent: "enabled=1",
210+
Cores: 2,
211+
Memory: 1024 * 4,
212+
Name: vmName,
213+
Boot: "order=scsi0",
214+
Ide: vm.Ide{Ide2: fmt.Sprintf("file=%s:cloudinit,media=cdrom", storageName)},
215+
CiCustom: fmt.Sprintf("user=%s:snippets/%s-user.yml", storageName, vmName),
216+
IPConfig: vm.IPConfig{IPConfig0: ipconfig.String()},
217+
OSType: vm.L26,
218+
Net: vm.Net{Net0: "model=virtio,bridge=vmbr0,firewall=1"},
219+
Scsi: vm.Scsi{Scsi0: fmt.Sprintf("file=%s:8", storageName)},
220+
ScsiHw: vm.VirtioScsiPci,
221+
Serial: vm.Serial{Serial0: "socket"},
222+
VGA: "serial0",
221223
}
222224
return vmoptions
223225
}

0 commit comments

Comments
 (0)