Skip to content

Commit 90cbf7a

Browse files
committed
Make control plane instance IDs label-safe
1 parent 5d472f6 commit 90cbf7a

5 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎controlplane/multitenant.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ func SetupMultiTenant(
146146
}
147147

148148
cpID := cfg.K8s.ControlPlaneID
149+
if cpID == "" {
150+
cpID = os.Getenv("POD_NAME")
151+
}
149152
if cpID == "" {
150153
cpID, _ = os.Hostname()
151154
}
@@ -158,7 +161,7 @@ func SetupMultiTenant(
158161
return nil, nil, nil, nil, nil, fmt.Errorf("generate control plane boot id: %w", err)
159162
}
160163
bootIDHex := hex.EncodeToString(bootID)
161-
cpInstanceID := podUID + ":" + bootIDHex
164+
cpInstanceID := makeControlPlaneInstanceID(podUID, bootIDHex)
162165

163166
baseCfg := K8sWorkerPoolConfig{
164167
Namespace: namespace,
@@ -326,3 +329,13 @@ func parseNodeSelector(s string) map[string]string {
326329
}
327330
return m
328331
}
332+
333+
func makeControlPlaneInstanceID(podUID, bootIDHex string) string {
334+
if podUID == "" {
335+
podUID = "cp"
336+
}
337+
if len(bootIDHex) > 16 {
338+
bootIDHex = bootIDHex[:16]
339+
}
340+
return controlPlaneIDLabelValue(podUID + "-" + bootIDHex)
341+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build kubernetes
2+
3+
package controlplane
4+
5+
import "testing"
6+
7+
func TestMakeControlPlaneInstanceID_StaysKubernetesSafe(t *testing.T) {
8+
t.Parallel()
9+
10+
id := makeControlPlaneInstanceID(
11+
"duckgres-control-plane-7fb9dd69c6-dcgzw",
12+
"14cd8dd9eb353e609c7a4387a594a418",
13+
)
14+
15+
if len(id) > 63 {
16+
t.Fatalf("expected cp_instance_id length <= 63, got %d (%q)", len(id), id)
17+
}
18+
if id == "duckgres-control-plane-7fb9dd69c6-dcgzw:14cd8dd9eb353e609c7a4387a594a418" {
19+
t.Fatalf("expected shortened cp_instance_id, got original %q", id)
20+
}
21+
if id != "duckgres-control-plane-7fb9dd69c6-dcgzw-14cd8dd9eb353e60" {
22+
t.Fatalf("unexpected cp_instance_id %q", id)
23+
}
24+
}

‎k8s/control-plane-deployment.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ spec:
3030
# Replace with your duckgres image built with -tags kubernetes
3131
image: duckgres:latest
3232
imagePullPolicy: IfNotPresent
33+
env:
34+
- name: POD_NAME
35+
valueFrom:
36+
fieldRef:
37+
fieldPath: metadata.name
38+
- name: POD_UID
39+
valueFrom:
40+
fieldRef:
41+
fieldPath: metadata.uid
3342
args:
3443
- "--mode"
3544
- "control-plane"

‎k8s/control-plane-multitenant-local.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ spec:
2929
- name: control-plane
3030
image: duckgres:test
3131
imagePullPolicy: IfNotPresent
32+
env:
33+
- name: POD_NAME
34+
valueFrom:
35+
fieldRef:
36+
fieldPath: metadata.name
37+
- name: POD_UID
38+
valueFrom:
39+
fieldRef:
40+
fieldPath: metadata.uid
3241
args:
3342
- "--mode"
3443
- "control-plane"

‎k8s/kind/control-plane.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ spec:
2929
- name: control-plane
3030
image: duckgres:test
3131
imagePullPolicy: IfNotPresent
32+
env:
33+
- name: POD_NAME
34+
valueFrom:
35+
fieldRef:
36+
fieldPath: metadata.name
37+
- name: POD_UID
38+
valueFrom:
39+
fieldRef:
40+
fieldPath: metadata.uid
3241
args:
3342
- "--mode"
3443
- "control-plane"

0 commit comments

Comments
 (0)