Skip to content

Commit

Permalink
Add fixes for flaky-cni
Browse files Browse the repository at this point in the history
  • Loading branch information
NiniOak committed Mar 15, 2024
1 parent 411c821 commit d419825
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
56 changes: 45 additions & 11 deletions acceptance/framework/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/gruntwork-io/terratest/modules/helm"
"github.com/gruntwork-io/terratest/modules/k8s"
terratestLogger "github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/hashicorp/consul-k8s/acceptance/framework/logger"
Expand Down Expand Up @@ -196,7 +197,7 @@ type Command struct {
Logger *terratestLogger.Logger
}

func RunCommand(t testutil.TestingTB, command Command) (string, error) {
func RunCommand(t testutil.TestingTB, options *k8s.KubectlOptions, command Command) (string, error) {
t.Helper()
cmd, err := exec.Command(command.Command, command.Args...).CombinedOutput()

Expand All @@ -205,7 +206,7 @@ func RunCommand(t testutil.TestingTB, command Command) (string, error) {
if strings.Contains(arg, "delete") {
errCh := make(chan error)
go func() {
errCh <- getCRDRemoveFinalizers(t)
errCh <- getCRDRemoveFinalizers(t, options)
}()
if err := <-errCh; err != nil {
return "", err
Expand All @@ -217,17 +218,17 @@ func RunCommand(t testutil.TestingTB, command Command) (string, error) {
}

// getCRDRemoveFinalizers gets CRDs with finalizers and removes them.
func getCRDRemoveFinalizers(t testutil.TestingTB) error {
func getCRDRemoveFinalizers(t testutil.TestingTB, options *k8s.KubectlOptions) error {
t.Helper()
// Get CRD names with finalizers
crdNames, err := getCRDsWithFinalizers()
crdNames, err := getCRDsWithFinalizers(options)
if err != nil {
return err
}

// Remove finalizers for each CRD with finalizers
if len(crdNames) > 0 {
if err := removeFinalizers(crdNames); err != nil {
if err := removeFinalizers(options, crdNames); err != nil {
return err
}
}
Expand All @@ -245,10 +246,20 @@ type CRD struct {
}

// getCRDsWithFinalizers gets CRDs with finalizers.
func getCRDsWithFinalizers() ([]string, error) {
cmd := exec.Command("kubectl", "get", "crd", "-o=json")
func getCRDsWithFinalizers(options *k8s.KubectlOptions) ([]string, error) {

cmdArgs := createCmdArgs(options)
args := []string{"get", "crd", "-o=json"}

cmdArgs = append(cmdArgs, args...)
command := Command{
Command: "kubectl",
Args: cmdArgs,
Env: options.Env,
}

output, err := exec.Command(command.Command, command.Args...).CombinedOutput()

output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error executing command: %v", err)
}
Expand All @@ -269,15 +280,38 @@ func getCRDsWithFinalizers() ([]string, error) {
}

// removeFinalizers removes finalizers from CRDs.
func removeFinalizers(crdNames []string) error {
func removeFinalizers(options *k8s.KubectlOptions, crdNames []string) error {
cmdArgs := createCmdArgs(options)
args := []string{"patch", "crd", "--type=json", "-p=[{\"op\": \"remove\", \"path\": \"/metadata/finalizers\"}]"}

cmdArgs = append(cmdArgs, args...)
command := Command{
Command: "kubectl",
Args: cmdArgs,
Env: options.Env,
}
for _, crd := range crdNames {
cmd := exec.Command("kubectl", "patch", "crd", crd, "--type=json", "-p=[{\"op\": \"remove\", \"path\": \"/metadata/finalizers\"}]")
fmt.Printf("executing command: %s %s\n", command.Command, command.Args)
_, err := exec.Command(command.Command, command.Args...).CombinedOutput()

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
}

func createCmdArgs(options *k8s.KubectlOptions) []string {
var cmdArgs []string
if options.ContextName != "" {
cmdArgs = append(cmdArgs, "--context", options.ContextName)
}
if options.ConfigPath != "" {
cmdArgs = append(cmdArgs, "--kubeconfig", options.ConfigPath)
}
if options.Namespace != "" {
cmdArgs = append(cmdArgs, "--namespace", options.Namespace)
}
return cmdArgs
}
5 changes: 1 addition & 4 deletions acceptance/framework/k8s/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ func CheckStaticServerConnectionMultipleFailureMessages(t *testing.T, options *k
expectedOutput = expectedSuccessOutput
}

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

Check failure on line 132 in acceptance/framework/k8s/deploy.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)

args := []string{"exec", resourceType + sourceApp, "-c", sourceApp, "--", "curl", "-vvvsSf"}
args = append(args, curlArgs...)
Expand Down
2 changes: 1 addition & 1 deletion acceptance/framework/k8s/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func RunKubectlAndGetOutputWithLoggerE(t testutil.TestingTB, options *k8s.Kubect
var output string
var err error
retry.RunWith(counter, t, func(r *retry.R) {
output, err = helpers.RunCommand(r, command)
output, err = helpers.RunCommand(r, options, command)
if err != nil {
// Want to retry on errors connecting to actual Kube API because
// these are intermittent.
Expand Down

0 comments on commit d419825

Please sign in to comment.