Skip to content

Commit

Permalink
remove finalizers from crds
Browse files Browse the repository at this point in the history
  • Loading branch information
NiniOak committed Mar 14, 2024
1 parent 0718833 commit cc2fe60
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
85 changes: 81 additions & 4 deletions acceptance/framework/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,85 @@ type Command struct {
Logger *terratestLogger.Logger
}

func RunCommand(r *retry.R, command Command) (string, error) {
r.Helper()
cmd, error := exec.Command(command.Command, command.Args...).CombinedOutput()
return string(cmd), error
func RunCommand(t testutil.TestingTB, command Command) (string, error) {
t.Helper()
cmd, err := exec.Command(command.Command, command.Args...).CombinedOutput()

// Check and remove finalizers in crds in the namespace
for _, arg := range command.Args {
if strings.Contains(arg, "delete") {
errCh := make(chan error)
go func() {
errCh <- getCRDRemoveFinalizers(t)
}()
if err := <-errCh; err != nil {
return "", err
}
}
}

return string(cmd), err
}

func getCRDRemoveFinalizers(t testutil.TestingTB) error {
t.Helper()
// Get CRD names with finalizers
crdNames, err := getCRDsWithFinalizers()
if err != nil {
return err
}

// Remove finalizers for each CRD with finalizers
if len(crdNames) > 0 {
if err := removeFinalizers(crdNames); err != nil {
return err
}
}
return nil
}

// CRD struct to parse CRD JSON output

Check failure on line 236 in acceptance/framework/helpers/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Comment should end in a period (godot)
type CRD struct {
Items []struct {
Metadata struct {
Name string `json:"name"`
Finalizers []string `json:"finalizers"`
} `json:"metadata"`
} `json:"items"`
}

func getCRDsWithFinalizers() ([]string, error) {
cmd := exec.Command("kubectl", "get", "crd", "-o=json")

output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error executing command: %v", err)
}

var crds CRD
if err := json.Unmarshal(output, &crds); err != nil {
return nil, fmt.Errorf("error parsing JSON: %v", err)
}

var crdNames []string
for _, item := range crds.Items {
if len(item.Metadata.Finalizers) > 0 {
crdNames = append(crdNames, item.Metadata.Name)
}
}

return crdNames, nil
}

func removeFinalizers(crdNames []string) error {
for _, crd := range crdNames {
cmd := exec.Command("kubectl", "patch", "crd", crd, "--type=json", "-p=[{\"op\": \"remove\", \"path\": \"/metadata/finalizers\"}]")

err := cmd.Run()
if err != nil {
return fmt.Errorf("error removing finalizers from CRD %s: %v", crd, err)
}
fmt.Printf("Finalizers removed from CRD %s\n", crd)
}
return nil
}
5 changes: 4 additions & 1 deletion acceptance/framework/k8s/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func CheckStaticServerConnectionMultipleFailureMessages(t *testing.T, options *k
expectedOutput = expectedSuccessOutput
}

retrier := &retry.Timer{Timeout: 320 * time.Second, Wait: 2 * time.Second}
retrier := &retry.Counter{
Count: 10,
Wait: 2 * time.Second,
}

args := []string{"exec", resourceType + sourceApp, "-c", sourceApp, "--", "curl", "-vvvsSf"}
args = append(args, curlArgs...)
Expand Down
2 changes: 1 addition & 1 deletion acceptance/tests/peering/peering_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestPeering_Gateway(t *testing.T) {
// leader election so we may need to wait a long time for
// the reconcile loop to run (hence the 1m timeout here).
var gatewayAddress string
counter := &retry.Counter{Count: 600, Wait: 2 * time.Second}
counter := &retry.Counter{Count: 10, Wait: 2 * time.Second}
retry.RunWith(counter, t, func(r *retry.R) {
var gateway gwv1beta1.Gateway
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: "gateway", Namespace: staticClientNamespace}, &gateway)
Expand Down

0 comments on commit cc2fe60

Please sign in to comment.