Skip to content

Commit b96dd8c

Browse files
authored
Additional changes (#225)
* Additional changes * trust password and code cleanup (#226)
1 parent 9473f87 commit b96dd8c

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

controllers/templates/lxd_initializer_ds.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ spec:
7373
mountPropagation: HostToContainer
7474
containers:
7575
- name: lxd-initializer
76-
image: "us-east1-docker.pkg.dev/spectro-images/dev/cluster-api/capmaas-lxd-initializer:v0.0.1"
76+
image: us-east1-docker.pkg.dev/spectro-images/dev/amit/cluster-api/lxd-initializer:v0.6.1-spectro-4.0.0-dev-11102025-03
7777
securityContext:
7878
privileged: true
7979
env:

lxd-initializer/lxd-initializer-daemonset.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ spec:
2020
hostPID: true
2121
containers:
2222
- name: lxd-initializer
23-
image: us-east1-docker.pkg.dev/spectro-images/dev/cluster-api/capmaas-lxd-initializer:v0.0.1
23+
image: us-east1-docker.pkg.dev/spectro-images/dev/amit/cluster-api/lxd-initializer:v0.6.1-spectro-4.0.0-dev-11102025-03
2424
imagePullPolicy: Always
2525
securityContext:
2626
privileged: true

lxd-initializer/lxd-initializer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func registerWithMAAS(maasEndpoint, maasAPIKey, systemID, nodeIP, trustPassword,
225225
profile := "ds"
226226
// Non-interactive login (idempotent)
227227
_ = runCmd("maas", []string{"login", profile, maasEndpoint, maasAPIKey})
228-
args := []string{profile, "vm-hosts", "create", "type=lxd", fmt.Sprintf("power_address=%s", wantHost), fmt.Sprintf("password=%s", trustPassword), fmt.Sprintf("name=%s", hostName)}
228+
args := []string{profile, "vm-hosts", "create", "type=lxd", fmt.Sprintf("power_address=%s", wantHost), fmt.Sprintf("password=%s", trustPassword), fmt.Sprintf("name=%s", hostName), "project=maas"}
229229
// Do not pass zone/pool on create
230230
if err := runCmd("maas", args); err != nil {
231231
return fmt.Errorf("maas cli create failed: %w", err)
@@ -242,6 +242,8 @@ func registerWithMAAS(maasEndpoint, maasAPIKey, systemID, nodeIP, trustPassword,
242242
if trustPassword != "" {
243243
params.Set("password", trustPassword)
244244
}
245+
// Set only project to 'maas' per request
246+
params.Set("project", "maas")
245247
if _, err := client.VMHosts().Create(ctx, params); err != nil {
246248
return fmt.Errorf("create vm host: %w", err)
247249
}
@@ -414,12 +416,18 @@ func main() {
414416
}
415417

416418
if actionStr == "register" || actionStr == "both" {
417-
// Build a stable host name using MAAS system-id
419+
// Build a stable host name using MAAS system-id and node hostname
418420
systemID, sErr := extractSystemIDFromNodeName(nodeName)
419421
if sErr != nil {
420422
log.Fatalf("Failed to extract system ID from node name: %v", sErr)
421423
}
422-
hostName := fmt.Sprintf("lxd-host-%s", systemID)
424+
hn := nodeName
425+
if hn == "" {
426+
if osHN, _ := os.Hostname(); osHN != "" {
427+
hn = osHN
428+
}
429+
}
430+
hostName := fmt.Sprintf("lxd-host-%s-%s", hn, systemID)
423431
if err := registerWithMAAS(maasEndpoint, maasAPIKey, systemID, nodeIP, trustPassword, zone, resourcePool, hostName); err != nil {
424432
log.Fatalf("Failed to register LXD host in MAAS: %v", err)
425433
}

pkg/util/trust/password.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package trust
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
)
7+
8+
// DeriveTrustPassword generates a deterministic trust password from a given seed.
9+
// The output is a hex string truncated to 32 characters for readability.
10+
func DeriveTrustPassword(seed string) string {
11+
sum := sha256.Sum256([]byte("lxd-trust:" + seed))
12+
s := hex.EncodeToString(sum[:])
13+
if len(s) > 32 {
14+
return s[:32]
15+
}
16+
return s
17+
}

0 commit comments

Comments
 (0)