Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 478d761

Browse files
author
Gianluca Arbezzano
committed
Teach ccm what it needs to look after the control plane endpoint
One of the Packet CCM responsability is to check the status of the Control Plane Endpoint (an elastic ip) keeping it assigned to an healthy control plane. In order to do that it needs to know the key=pair used to tag the elastic ip at creation phase. This PR pass this information to CCM via Kubernetes Secret
1 parent a0ac956 commit 478d761

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

controllers/packetmachine_controller.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
208208
}
209209
}
210210
if dev == nil {
211+
createDeviceReq := packet.CreateDeviceRequest{
212+
MachineScope: machineScope,
213+
}
211214
mUID := uuid.New().String()
212215
tags := []string{
213216
packet.GenerateMachineTag(mUID),
@@ -228,13 +231,13 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
228231
Address: controlPlaneEndpoint.Address,
229232
}
230233
addrs = append(addrs, a)
231-
// This tag is currently not used. I placed it there to easely localize the device that currently has the IP attached.
232-
// Probably it is not neeed but I wil get back to it when working at #141.
233-
tags = append(tags, fmt.Sprintf("capp.control-plane-endpoint=%s", controlPlaneEndpoint.Address))
234234
}
235+
createDeviceReq.ControlPlaneEndpoint = controlPlaneEndpoint.Address
235236
}
236237

237-
dev, err = r.PacketClient.NewDevice(machineScope, tags)
238+
createDeviceReq.ExtraTags = tags
239+
240+
dev, err = r.PacketClient.NewDevice(createDeviceReq)
238241
if err != nil {
239242
errs := fmt.Errorf("failed to create machine %s: %v", machineScope.Name(), err)
240243
machineScope.SetErrorReason(capierrors.CreateMachineError)

pkg/cloud/packet/client.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,21 @@ func (p *PacketClient) GetDevice(deviceID string) (*packngo.Device, error) {
6767
return dev, err
6868
}
6969

70-
func (p *PacketClient) NewDevice(machineScope *scope.MachineScope, extraTags []string) (*packngo.Device, error) {
71-
userDataRaw, err := machineScope.GetRawBootstrapData()
70+
type CreateDeviceRequest struct {
71+
ExtraTags []string
72+
MachineScope *scope.MachineScope
73+
ControlPlaneEndpoint string
74+
}
75+
76+
func (p *PacketClient) NewDevice(req CreateDeviceRequest) (*packngo.Device, error) {
77+
userDataRaw, err := req.MachineScope.GetRawBootstrapData()
7278
if err != nil {
7379
return nil, errors.Wrap(err, "impossible to retrieve bootstrap data from secret")
7480
}
7581

7682
userData := string(userDataRaw)
77-
tags := append(machineScope.PacketMachine.Spec.Tags, extraTags...)
78-
if machineScope.IsControlPlane() {
83+
tags := append(req.MachineScope.PacketMachine.Spec.Tags, req.ExtraTags...)
84+
if req.MachineScope.IsControlPlane() {
7985
// control plane machines should get the API key injected
8086
tmpl, err := template.New("control-plane-user-data").Parse(userData)
8187
if err != nil {
@@ -85,6 +91,9 @@ func (p *PacketClient) NewDevice(machineScope *scope.MachineScope, extraTags []s
8591
apiKeyStruct := map[string]interface{}{
8692
"apiKey": p.Client.APIKey,
8793
}
94+
if req.ControlPlaneEndpoint != "" {
95+
apiKeyStruct["controlPlaneEndpoint"] = req.ControlPlaneEndpoint
96+
}
8897
if err := tmpl.Execute(stringWriter, apiKeyStruct); err != nil {
8998
return nil, fmt.Errorf("error executing control-plane userdata template: %v", err)
9099
}
@@ -94,24 +103,24 @@ func (p *PacketClient) NewDevice(machineScope *scope.MachineScope, extraTags []s
94103
tags = append(tags, infrastructurev1alpha3.WorkerTag)
95104
}
96105
serverCreateOpts := &packngo.DeviceCreateRequest{
97-
Hostname: machineScope.Name(),
98-
ProjectID: machineScope.PacketCluster.Spec.ProjectID,
99-
Facility: []string{machineScope.PacketCluster.Spec.Facility},
100-
BillingCycle: machineScope.PacketMachine.Spec.BillingCycle,
101-
HardwareReservationID: machineScope.PacketMachine.Spec.HardwareReservationID,
102-
Plan: machineScope.PacketMachine.Spec.MachineType,
103-
OS: machineScope.PacketMachine.Spec.OS,
106+
Hostname: req.MachineScope.Name(),
107+
ProjectID: req.MachineScope.PacketCluster.Spec.ProjectID,
108+
Facility: []string{req.MachineScope.PacketCluster.Spec.Facility},
109+
BillingCycle: req.MachineScope.PacketMachine.Spec.BillingCycle,
110+
HardwareReservationID: req.MachineScope.PacketMachine.Spec.HardwareReservationID,
111+
Plan: req.MachineScope.PacketMachine.Spec.MachineType,
112+
OS: req.MachineScope.PacketMachine.Spec.OS,
104113
Tags: tags,
105114
UserData: userData,
106115
}
107116

108117
// Update server options to pass pxe url if specified
109-
if machineScope.PacketMachine.Spec.IPXEUrl != "" {
118+
if req.MachineScope.PacketMachine.Spec.IPXEUrl != "" {
110119
// Error if pxe url and OS conflict
111-
if machineScope.PacketMachine.Spec.OS != ipxeOS {
120+
if req.MachineScope.PacketMachine.Spec.OS != ipxeOS {
112121
return nil, fmt.Errorf("os should be set to custom_pxe when using pxe urls")
113122
}
114-
serverCreateOpts.IPXEScriptURL = machineScope.PacketMachine.Spec.IPXEUrl
123+
serverCreateOpts.IPXEScriptURL = req.MachineScope.PacketMachine.Spec.IPXEUrl
115124
}
116125

117126
dev, _, err := p.Client.Devices.Create(serverCreateOpts)

templates/cluster-template.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ spec:
2626
kubeletExtraArgs:
2727
cloud-provider: external
2828
postKubeadmCommands:
29+
- |
30+
cat <<EOF >> /etc/network/interfaces
31+
auto lo:0
32+
iface lo:0 inet static
33+
address {{ .controlPlaneEndpoint }}
34+
netmask 255.255.255.255
35+
EOF
36+
- systemctl restart networking
2937
- '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}"}'''
3038
- kubectl apply --kubeconfig /etc/kubernetes/admin.conf -f https://raw.githubusercontent.com/packethost/packet-ccm/9779a5b1dcaa039c40159a6a2c2e57b8ab4874d8/deploy/releases/v1.0.1/deployment.yaml
3139
preKubeadmCommands:
@@ -44,14 +52,7 @@ spec:
4452
- systemctl daemon-reload
4553
- systemctl enable docker
4654
- systemctl start docker
47-
- |
48-
cat <<EOF >> /etc/network/interfaces
49-
auto lo:0
50-
iface lo:0 inet static
51-
address $(curl -s https://metadata.packet.net/metadata | jq -r '.network.addresses[] | select(.address_family == 4 and .public == true and .management == false) | .address')
52-
netmask 255.255.255.255
53-
EOF
54-
- systemctl restart networking
55+
- ping -c 3 -q {{ .controlPlaneEndpoint }} && echo OK || ip addr add {{ .controlPlaneEndpoint }} dev lo
5556
---
5657
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha3
5758
kind: PacketMachineTemplate

0 commit comments

Comments
 (0)