Skip to content
Open
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
25 changes: 25 additions & 0 deletions controllers/maasmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/spectrocloud/maas-client-go/maasclient"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -227,6 +228,30 @@ func (r *MaasMachineReconciler) reconcileDelete(_ context.Context, machineScope
api := clusterScope.GetMaasClientIdentity()
// choose ExternalIP first, then InternalIP
nodeIP := getNodeIP(m.Addresses)
// For control-plane BM that backs an LXD VM host, force-delete guest VMs to unblock release
if clusterScope.IsLXDHostEnabled() && machineScope.IsControlPlane() {
ctx := context.Background()
client := maasclient.NewAuthenticatedClientSet(api.URL, api.Token)
if hosts, herr := client.VMHosts().List(ctx, nil); herr == nil {
for _, h := range hosts {
if h.HostSystemID() == m.ID {
if guests, gerr := h.Machines().List(ctx); gerr == nil {
for _, g := range guests {
gid := g.SystemID()
if gid == "" {
continue
}
// Fetch details to confirm and delete
if gm, ge := client.Machines().Machine(gid).Get(ctx); ge == nil {
_ = client.Machines().Machine(gm.SystemID()).Delete(ctx)
}
}
}
break
}
}
}
}
if nodeIP != "" {
if uerr := lxd.UnregisterLXDHostWithMaasClient(api.Token, api.URL, nodeIP); uerr != nil {
machineScope.Error(uerr, "failed to unregister LXD VM host prior to release")
Expand Down
2 changes: 1 addition & 1 deletion controllers/templates/lxd_initializer_ds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ spec:
mountPropagation: HostToContainer
containers:
- name: lxd-initializer
image: "us-east1-docker.pkg.dev/spectro-images/dev/cluster-api/capmaas-lxd-initializer:v0.0.1"
image: "us-east1-docker.pkg.dev/spectro-images/dev/cluster-api/capmaas-lxd-initializer:kundiffpass2"
securityContext:
privileged: true
env:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ require (

replace (
github.com/prometheus/common v0.32.1 => github.com/prometheus/common v0.26.0
github.com/spectrocloud/maas-client-go => github.com/spectrocloud/maas-client-go v0.0.8-beta1
sigs.k8s.io/structured-merge-diff/v6 => sigs.k8s.io/structured-merge-diff/v4 v4.4.3
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/spectrocloud/maas-client-go v0.0.7-beta1 h1:2GryA5JSrjlsvzLaCIGyPfxcaSCPrw7fm8ixMf7aRbY=
github.com/spectrocloud/maas-client-go v0.0.7-beta1/go.mod h1:CaqAAlh6/xfzc/cDpU8eMG0wqnwx1ODSyXcH86uV7Ww=
github.com/spectrocloud/maas-client-go v0.0.8-beta1 h1:PCY6M3M9uXZG8dzoe0jNcMnh4nOhJuZBF2C3vsUXp9A=
github.com/spectrocloud/maas-client-go v0.0.8-beta1/go.mod h1:CaqAAlh6/xfzc/cDpU8eMG0wqnwx1ODSyXcH86uV7Ww=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
Expand Down
4 changes: 3 additions & 1 deletion lxd-initializer/lxd-initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ func main() {

trustPassword := *trustPasswordFlag
if trustPassword == "" {
trustPassword = os.Getenv("TRUST_PASSWORD")
basePassword := os.Getenv("TRUST_PASSWORD")
trustPassword = basePassword + nodeName
log.Printf("Using trust password: %s", trustPassword)
}

// Determine action based on flag or default to both
Expand Down
86 changes: 46 additions & 40 deletions pkg/maas/lxd/host_maas_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ func SetupLXDHostWithMaasClient(config HostConfig) error {
return nil
}

// Register the host with MAAS as a KVM host
if err := registerWithMaasClient(client, config); err != nil {
return fmt.Errorf("failed to register with MAAS: %w", err)
}

log.Info("Successfully set up LXD host", "node", config.NodeIP)
// Skip automatic registration - manual registration required
log.Info("Skipping automatic LXD host registration (manual registration required)", "node", config.NodeIP)
return nil

// Register the host with MAAS as a KVM host (DISABLED)
// if err := registerWithMaasClient(client, config); err != nil {
// return fmt.Errorf("failed to register with MAAS: %w", err)
// }

// log.Info("Successfully set up LXD host", "node", config.NodeIP)
// return nil
}

// normalizeHost extracts the host part from a MAAS power_address or raw string
Expand Down Expand Up @@ -138,40 +142,42 @@ func isHostRegisteredWithMaasClient(client maasclient.ClientSetInterface, nodeIP
}

// registerWithMaasClient registers a host with MAAS as a VM host
func registerWithMaasClient(client maasclient.ClientSetInterface, config HostConfig) error {
ctx := context.Background()

// Create registration parameters
params := maasclient.ParamsBuilder().
Set("type", "lxd").
Set("power_address", fmt.Sprintf("https://%s:8443", config.NodeIP)).
Set("name", fmt.Sprintf("lxd-host-%s", config.NodeIP))

if config.Zone != "" {
// Pass the zone name directly. MAAS API expects the zone name, not ID.
params.Set("zone", config.Zone)
}

if config.ResourcePool != "" {
// Pass pool name directly.
params.Set("pool", config.ResourcePool)
}

if config.TrustPassword != "" {
params.Set("password", config.TrustPassword)
}

log := textlogger.NewLogger(textlogger.NewConfig())
log.Info("register params", "zone", params.Values().Get("zone"), "pool", params.Values().Get("pool"))

// Register the host with MAAS
_, err := client.VMHosts().Create(ctx, params)
if err != nil {
return fmt.Errorf("failed to register host with MAAS: %w", err)
}

return nil
}
// func registerWithMaasClient(client maasclient.ClientSetInterface, config HostConfig) error {
// ctx := context.Background()
// log := textlogger.NewLogger(textlogger.NewConfig())
// log.Info("registering with MAAS", "node", config.NodeIP, "trust_password", config.TrustPassword)

// // Create registration parameters
// params := maasclient.ParamsBuilder().
// Set("type", "lxd").
// Set("power_address", config.NodeIP).
// Set("name", fmt.Sprintf("lxd-host-%s", config.NodeIP)).
// Set("project", "maas")

// if config.Zone != "" {
// // Pass the zone name directly. MAAS API expects the zone name, not ID.
// params.Set("zone", config.Zone)
// }

// if config.ResourcePool != "" {
// // Pass pool name directly.
// params.Set("pool", config.ResourcePool)
// }

// if config.TrustPassword != "" {
// params.Set("password", config.TrustPassword)
// }

// log.Info("register params", "zone", params.Values().Get("zone"), "pool", params.Values().Get("pool"))

// // Register the host with MAAS
// _, err := client.VMHosts().Create(ctx, params)
// if err != nil {
// return fmt.Errorf("failed to register host with MAAS: %w", err)
// }

// return nil
// }

// GetAvailableLXDHostsWithMaasClient returns a list of available LXD hosts from MAAS
func GetAvailableLXDHostsWithMaasClient(apiKey, apiEndpoint string) ([]maasclient.VMHost, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/maas/lxd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (s *Service) setupLXDOnMachine(machine *v1beta1.MaasMachine) error {
NetworkBridge: lxdConfig.NetworkBridge,
ResourcePool: *machine.Spec.ResourcePool,
Zone: *machine.Spec.FailureDomain,
TrustPassword: "capmaas",
TrustPassword: "capmaas" + *machine.Status.Hostname,
}

// Check if LXD initialization is complete on the node before attempting MAAS registration
Expand Down