Skip to content

Commit

Permalink
Merge pull request #151 from Josh-Diamond/jkeslar/qatask-1039-v29
Browse files Browse the repository at this point in the history
adds support for deleting init node
  • Loading branch information
slickwarren authored May 2, 2024
2 parents 351f9be + 9053c7b commit 2bcb20d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
4 changes: 2 additions & 2 deletions extensions/clusters/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ func logClusterInfoWithChanges(clusterID, clusterInfo string, summary summary.Su
// WatchAndWaitForCluster is function that waits for a cluster to go unactive before checking its active state.
func WatchAndWaitForCluster(client *rancher.Client, steveID string) error {
var clusterResp *v1.SteveAPIObject
err := kwait.Poll(500*time.Millisecond, 2*time.Minute, func() (done bool, err error) {
err := kwait.PollUntilContextTimeout(context.TODO(), 1*time.Second, defaults.TwoMinuteTimeout, true, func(ctx context.Context) (done bool, err error) {
clusterResp, err = client.Steve.SteveType(ProvisioningSteveResourceType).ByID(steveID)
if err != nil {
return false, err
Expand All @@ -1263,7 +1263,7 @@ func WatchAndWaitForCluster(client *rancher.Client, steveID string) error {
if err != nil {
return err
}
logrus.Infof("waiting for cluster to be up.............")
logrus.Infof("waiting for cluster to be up...")

adminClient, err := rancher.NewClient(client.RancherConfig.AdminToken, client.Session)
if err != nil {
Expand Down
38 changes: 36 additions & 2 deletions extensions/machinepools/machinepools.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rancher/shepherd/clients/rancher"
v1 "github.com/rancher/shepherd/clients/rancher/v1"
"github.com/rancher/shepherd/extensions/defaults"
"github.com/rancher/shepherd/extensions/kubeapi/secrets"
nodestat "github.com/rancher/shepherd/extensions/nodes"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
Expand All @@ -20,8 +21,15 @@ import (
)

const (
active = "active"
pool = "pool"
active = "active"
fleetNamespace = "fleet-default"
initNodeLabelKey = "rke.cattle.io/init-node"
local = "local"
machineNameSteveLabel = "rke.cattle.io/machine-name"
machinePlanSecretType = "rke.cattle.io/machine-plan"
machineSteveResourceType = "cluster.x-k8s.io.machine"
pool = "pool"
True = "true"

nodeRoleListLength = 4
)
Expand Down Expand Up @@ -290,3 +298,29 @@ func MatchRoleToPool(poolRole string, allRoles []Roles) int {
logrus.Warn("unable to match pool to role, likely missing [roles] in machineConfig")
return -1
}

// GetInitMachine accepts a client and clusterID and returns the "init node" machine
// object for rke2/k3s clusters
func GetInitMachine(client *rancher.Client, clusterID string) (*v1.SteveAPIObject, error) {
logrus.Info("Retrieving secret and identifying machine...")
secret, err := secrets.ListSecrets(client, local, fleetNamespace, metav1.ListOptions{
LabelSelector: initNodeLabelKey + "=" + True,
})
if err != nil {
return nil, err
}

// secret.Items[0] will never change when targeting the init node secret,
// as the list has been filtered above to grab the single init node secret
initNodeMachineName := secret.Items[0].ObjectMeta.Labels[machineNameSteveLabel]

logrus.Info("Retrieving machine...")
initMachine, err := client.Steve.SteveType(machineSteveResourceType).ByID(fleetNamespace + "/" + initNodeMachineName)
if err != nil {
return nil, err
}

logrus.Infof("Successfully retrieved machine: %s", initNodeMachineName)

return initMachine, nil
}
38 changes: 38 additions & 0 deletions extensions/steve/steve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package steve

import (
"context"
"strings"
"time"

"github.com/rancher/shepherd/clients/rancher"
"github.com/sirupsen/logrus"
kwait "k8s.io/apimachinery/pkg/util/wait"
)

const (
notFound = "not found"
)

// WaitForSteveResourceDeletion accepts a client, steve resource type, and resource ID, then waits for a steve resource to be deleted
func WaitForSteveResourceDeletion(client *rancher.Client, interval, timeout time.Duration, steveResourceType, steveID string) error {
err := kwait.PollUntilContextTimeout(context.TODO(), interval, timeout, true, func(ctx context.Context) (done bool, err error) {
_, err = client.Steve.SteveType(steveResourceType).ByID(steveID)
if err != nil {
if strings.Contains(err.Error(), notFound) {
logrus.Info("Resource was successfully removed!")
return true, nil
} else {
return false, err
}
}

return false, nil
})

if err != nil {
return err
}

return nil
}

0 comments on commit 2bcb20d

Please sign in to comment.