Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions controllers/packetmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
}
}
if dev == nil {
createDeviceReq := packet.CreateDeviceRequest{
MachineScope: machineScope,
}
mUID := uuid.New().String()
tags := []string{
packet.GenerateMachineTag(mUID),
Expand All @@ -228,13 +231,13 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
Address: controlPlaneEndpoint.Address,
}
addrs = append(addrs, a)
// This tag is currently not used. I placed it there to easely localize the device that currently has the IP attached.
// Probably it is not neeed but I wil get back to it when working at #141.
tags = append(tags, fmt.Sprintf("capp.control-plane-endpoint=%s", controlPlaneEndpoint.Address))
}
createDeviceReq.ControlPlaneEndpoint = controlPlaneEndpoint.Address
}

dev, err = r.PacketClient.NewDevice(machineScope, tags)
createDeviceReq.ExtraTags = tags

dev, err = r.PacketClient.NewDevice(createDeviceReq)
if err != nil {
errs := fmt.Errorf("failed to create machine %s: %v", machineScope.Name(), err)
machineScope.SetErrorReason(capierrors.CreateMachineError)
Expand Down
37 changes: 23 additions & 14 deletions pkg/cloud/packet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,21 @@ func (p *PacketClient) GetDevice(deviceID string) (*packngo.Device, error) {
return dev, err
}

func (p *PacketClient) NewDevice(machineScope *scope.MachineScope, extraTags []string) (*packngo.Device, error) {
userDataRaw, err := machineScope.GetRawBootstrapData()
type CreateDeviceRequest struct {
ExtraTags []string
MachineScope *scope.MachineScope
ControlPlaneEndpoint string
}

func (p *PacketClient) NewDevice(req CreateDeviceRequest) (*packngo.Device, error) {
userDataRaw, err := req.MachineScope.GetRawBootstrapData()
if err != nil {
return nil, errors.Wrap(err, "impossible to retrieve bootstrap data from secret")
}

userData := string(userDataRaw)
tags := append(machineScope.PacketMachine.Spec.Tags, extraTags...)
if machineScope.IsControlPlane() {
tags := append(req.MachineScope.PacketMachine.Spec.Tags, req.ExtraTags...)
if req.MachineScope.IsControlPlane() {
// control plane machines should get the API key injected
tmpl, err := template.New("control-plane-user-data").Parse(userData)
if err != nil {
Expand All @@ -85,6 +91,9 @@ func (p *PacketClient) NewDevice(machineScope *scope.MachineScope, extraTags []s
apiKeyStruct := map[string]interface{}{
"apiKey": p.Client.APIKey,
}
if req.ControlPlaneEndpoint != "" {
apiKeyStruct["controlPlaneEndpoint"] = req.ControlPlaneEndpoint
}
if err := tmpl.Execute(stringWriter, apiKeyStruct); err != nil {
return nil, fmt.Errorf("error executing control-plane userdata template: %v", err)
}
Expand All @@ -94,24 +103,24 @@ func (p *PacketClient) NewDevice(machineScope *scope.MachineScope, extraTags []s
tags = append(tags, infrastructurev1alpha3.WorkerTag)
}
serverCreateOpts := &packngo.DeviceCreateRequest{
Hostname: machineScope.Name(),
ProjectID: machineScope.PacketCluster.Spec.ProjectID,
Facility: []string{machineScope.PacketCluster.Spec.Facility},
BillingCycle: machineScope.PacketMachine.Spec.BillingCycle,
HardwareReservationID: machineScope.PacketMachine.Spec.HardwareReservationID,
Plan: machineScope.PacketMachine.Spec.MachineType,
OS: machineScope.PacketMachine.Spec.OS,
Hostname: req.MachineScope.Name(),
ProjectID: req.MachineScope.PacketCluster.Spec.ProjectID,
Facility: []string{req.MachineScope.PacketCluster.Spec.Facility},
BillingCycle: req.MachineScope.PacketMachine.Spec.BillingCycle,
HardwareReservationID: req.MachineScope.PacketMachine.Spec.HardwareReservationID,
Plan: req.MachineScope.PacketMachine.Spec.MachineType,
OS: req.MachineScope.PacketMachine.Spec.OS,
Tags: tags,
UserData: userData,
}

// Update server options to pass pxe url if specified
if machineScope.PacketMachine.Spec.IPXEUrl != "" {
if req.MachineScope.PacketMachine.Spec.IPXEUrl != "" {
// Error if pxe url and OS conflict
if machineScope.PacketMachine.Spec.OS != ipxeOS {
if req.MachineScope.PacketMachine.Spec.OS != ipxeOS {
return nil, fmt.Errorf("os should be set to custom_pxe when using pxe urls")
}
serverCreateOpts.IPXEScriptURL = machineScope.PacketMachine.Spec.IPXEUrl
serverCreateOpts.IPXEScriptURL = req.MachineScope.PacketMachine.Spec.IPXEUrl
}

dev, _, err := p.Client.Devices.Create(serverCreateOpts)
Expand Down
17 changes: 9 additions & 8 deletions templates/cluster-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ spec:
kubeletExtraArgs:
cloud-provider: external
postKubeadmCommands:
- |
cat <<EOF >> /etc/network/interfaces
auto lo:0
iface lo:0 inet static
address {{ .controlPlaneEndpoint }}
netmask 255.255.255.255
EOF
- systemctl restart networking
- 'kubectl --kubeconfig /etc/kubernetes/admin.conf create secret generic -n kube-system packet-cloud-config --from-literal=cloud-sa.json=''{"apiKey": "{{ .apiKey }}","projectID": "${PROJECT_ID}"}'''
- kubectl apply --kubeconfig /etc/kubernetes/admin.conf -f https://raw.githubusercontent.com/packethost/packet-ccm/9779a5b1dcaa039c40159a6a2c2e57b8ab4874d8/deploy/releases/v1.0.1/deployment.yaml
preKubeadmCommands:
Expand All @@ -44,14 +52,7 @@ spec:
- systemctl daemon-reload
- systemctl enable docker
- systemctl start docker
- |
cat <<EOF >> /etc/network/interfaces
auto lo:0
iface lo:0 inet static
address $(curl -s https://metadata.packet.net/metadata | jq -r '.network.addresses[] | select(.address_family == 4 and .public == true and .management == false) | .address')
netmask 255.255.255.255
EOF
- systemctl restart networking
- ping -c 3 -q {{ .controlPlaneEndpoint }} && echo OK || ip addr add {{ .controlPlaneEndpoint }} dev lo
---
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha3
kind: PacketMachineTemplate
Expand Down