Skip to content

Commit

Permalink
destroy updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpowermac committed Oct 3, 2024
1 parent 27a6d34 commit e8e1d87
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 20 deletions.
76 changes: 76 additions & 0 deletions pkg/destroy/vsphere/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/pbm"
pbmtypes "github.com/vmware/govmomi/pbm/types"
Expand Down Expand Up @@ -34,6 +35,7 @@ type API interface {
DeleteStoragePolicy(ctx context.Context, policyName string) error
DeleteTag(ctx context.Context, id string) error
DeleteTagCategory(ctx context.Context, id string) error
DeleteVMHostGroup(ctx context.Context, infraID string) error
}

// Client makes calls to the Azure API.
Expand Down Expand Up @@ -281,3 +283,77 @@ func (c *Client) DeleteTagCategory(ctx context.Context, categoryName string) err

return utilerrors.NewAggregate(errs)
}

/*
func (c *Client) ListComputeClusters(ctx context.Context) ([]interface{}, error) {
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
finder := find.NewFinder(c.client, false)
clusterObjs, err := finder.ClusterComputeResourceList(ctx, "*")
if err != nil {
return nil, err
}
for _, co := range clusterObjs {
co.Properties()
}
return nil, nil
}
*/

func (c *Client) DeleteVMHostGroup(ctx context.Context, infraID string) error {
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()

finder := find.NewFinder(c.client, false)

clusterObjs, err := finder.ClusterComputeResourceList(ctx, "*")
if err != nil {
return err
}

for _, ccr := range clusterObjs {

clusterConfig, err := ccr.Configuration(ctx)

if err != nil {
return err
}

for _, g := range clusterConfig.Group {
if vmg, ok := g.(*types.ClusterVmGroup); ok {
if strings.Contains(vmg.Name, infraID) {
clusterConfigSpec := &types.ClusterConfigSpecEx{
GroupSpec: []types.ClusterGroupSpec{
{
ArrayUpdateSpec: types.ArrayUpdateSpec{
Operation: types.ArrayUpdateOperation("remove"),
},
Info: &types.ClusterVmGroup{
ClusterGroupInfo: types.ClusterGroupInfo{
Name: vmg.Name,
},
},
},
},
}
task, err := ccr.Reconfigure(ctx, clusterConfigSpec, true)
if err != nil {
return err
}

if err := task.Wait(ctx); err != nil {
return err
}
}
}
}
}
return nil
}
18 changes: 7 additions & 11 deletions pkg/destroy/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,15 @@ func (o *ClusterUninstaller) deleteVirtualMachines(ctx context.Context) error {
return utilerrors.NewAggregate(errs)
}

// todo: jcallen: write destroy because its a pain to keep deleting

/*
func (o *ClusterUninstaller) deleteVMGroups(ctx context.Context) error {
o.InfraID
o.
for _, client := range o.clients {
if err := client.DeleteVMHostGroup(ctx, o.InfraID); err != nil {
return err
}
}
return nil
}

*/

func (o *ClusterUninstaller) destroyCluster(ctx context.Context) (bool, error) {
stagedFuncs := [][]struct {
name string
Expand All @@ -258,6 +253,7 @@ func (o *ClusterUninstaller) destroyCluster(ctx context.Context) (bool, error) {
{name: "Storage Policy", execute: o.deleteStoragePolicy},
{name: "Tag", execute: o.deleteTag},
{name: "Tag Category", execute: o.deleteTagCategory},
{name: "vm-host group", execute: o.deleteVMGroups},
}}

stageFailed := false
Expand Down
18 changes: 9 additions & 9 deletions pkg/types/vsphere/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// +kubebuilder:validation:Enum="";thin;thick;eagerZeroedThick
type DiskType string

// FailureDomainType is the name of the failure domain type.
// FailureDomainType is the string representation name of the failure domain type.
// There are two defined failure domains currently, Datacenter and ComputeCluster.
// Each represents a vCenter object type within a vSphere environment.
// +kubebuilder:validation:Enum=HostGroup;Datacenter;ComputeCluster
Expand Down Expand Up @@ -40,13 +40,12 @@ const (
BootstrapRole = "bootstrap"
)

// TODO: jcallen: API needs to be defined better
const (
// HostGroupFailureDomain is a failure domain for a host group.
// HostGroupFailureDomain is a failure domain for a vCenter vm-host group.
HostGroupFailureDomain FailureDomainType = "HostGroup"
// ComputeClusterFailureDomain is a failure domain for a compute cluster.
// ComputeClusterFailureDomain is a failure domain for a vCenter compute cluster.
ComputeClusterFailureDomain FailureDomainType = "ComputeCluster"
// DatacenterFailureDomain is a failure domain for a datacenter.
// DatacenterFailureDomain is a failure domain for a vCenter datacenter.
DatacenterFailureDomain FailureDomainType = "Datacenter"
)

Expand Down Expand Up @@ -186,11 +185,11 @@ type FailureDomain struct {
Topology Topology `json:"topology"`

// Type is the type of failure domain, the current values are "Datacenter", "ComputeCluster" and "HostGroup"
// +kubebuilder:validation:Enum="";Datacenter;ComputeCluster;HostGroup
// +kubebuilder:validation:Enum=Datacenter;ComputeCluster
// +optional
RegionType FailureDomainType `json:"regionType,omitempty"`
// Type is the type of failure domain, the current values are "Datacenter", "ComputeCluster" and "HostGroup"
// +kubebuilder:validation:Enum="";Datacenter;ComputeCluster;HostGroup
// +kubebuilder:validation:Enum=ComputeCluster;HostGroup
// +optional
ZoneType FailureDomainType `json:"zoneType,omitempty"`
}
Expand Down Expand Up @@ -245,8 +244,9 @@ type Topology struct {
// +optional
TagIDs []string `json:"tagIDs,omitempty"`

// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=2048
// hostGroup is the name of the vm-host group of type host within vCenter for this failure domain.
// This field is required when the FailureDomain zoneType is HostGroup
// +kubebuilder:validation:MaxLength=80
// +optional
HostGroup string `json:"hostGroup,omitempty"`
}
Expand Down

0 comments on commit e8e1d87

Please sign in to comment.