Skip to content

Commit

Permalink
[ADXT-275] [k8s] add dump of k8s cluster state on k8s suites errors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinFairise2 authored Jun 12, 2024
1 parent 7526168 commit bdaa549
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 3 deletions.
5 changes: 5 additions & 0 deletions test/new-e2e/pkg/e2e/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"io"
)

// Diagnosable defines the interface for a diagnosable provider.
type Diagnosable interface {
Diagnose(ctx context.Context, stackName string) (string, error)
}

// Provisioner defines the interface for a provisioner.
type Provisioner interface {
ID() string
Expand Down
20 changes: 17 additions & 3 deletions test/new-e2e/pkg/e2e/pulumi_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type PulumiEnvRunFunc[Env any] func(ctx *pulumi.Context, env *Env) error

// PulumiProvisioner is a provisioner based on Pulumi with binding to an environment.
type PulumiProvisioner[Env any] struct {
id string
runFunc PulumiEnvRunFunc[Env]
configMap runner.ConfigMap
id string
runFunc PulumiEnvRunFunc[Env]
configMap runner.ConfigMap
diagnoseFunc func(ctx context.Context, stackName string) (string, error)
}

var (
Expand Down Expand Up @@ -101,6 +102,19 @@ func (pp *PulumiProvisioner[Env]) ProvisionEnv(ctx context.Context, stackName st
return resources, nil
}

// Diagnose runs the diagnose function if it is set diagnoseFunc
func (pp *PulumiProvisioner[Env]) Diagnose(ctx context.Context, stackName string) (string, error) {
if pp.diagnoseFunc != nil {
return pp.diagnoseFunc(ctx, stackName)
}
return "", nil
}

// SetDiagnoseFunc sets the diagnose function.
func (pp *PulumiProvisioner[Env]) SetDiagnoseFunc(diagnoseFunc func(ctx context.Context, stackName string) (string, error)) {
pp.diagnoseFunc = diagnoseFunc
}

// Destroy deletes the Pulumi stack.
func (pp *PulumiProvisioner[Env]) Destroy(ctx context.Context, stackName string, logger io.Writer) error {
return infra.GetStackManager().DeleteStack(ctx, stackName, logger)
Expand Down
15 changes: 15 additions & 0 deletions test/new-e2e/pkg/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ import (

"github.com/DataDog/datadog-agent/test/new-e2e/pkg/runner"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/runner/parameters"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/utils/infra"
"github.com/DataDog/test-infra-definitions/common/utils"
"github.com/DataDog/test-infra-definitions/components"

Expand Down Expand Up @@ -284,6 +285,20 @@ func (bs *BaseSuite[Env]) reconcileEnv(targetProvisioners ProvisionerMap) error
}

if err != nil {
if diagnosableProvisioner, ok := provisioner.(Diagnosable); ok {
stackName, err := infra.GetStackManager().GetPulumiStackName(bs.params.stackName)
if err != nil {
bs.T().Logf("unable to get stack name for diagnose, err: %v", err)
} else {
diagnoseResult, diagnoseErr := diagnosableProvisioner.Diagnose(ctx, stackName)
if diagnoseErr != nil {
bs.T().Logf("WARNING: Diagnose failed: %v", diagnoseErr)
} else if diagnoseResult != "" {
bs.T().Logf("Diagnose result: %s", diagnoseResult)
}
}

}
return fmt.Errorf("your stack '%s' provisioning failed, check logs above. Provisioner was %s, failed with err: %v", bs.params.stackName, id, err)
}

Expand Down
13 changes: 13 additions & 0 deletions test/new-e2e/pkg/environments/aws/kubernetes/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package awskubernetes

import (
"context"
"fmt"

"github.com/DataDog/test-infra-definitions/common/config"
"github.com/DataDog/test-infra-definitions/common/utils"
"github.com/DataDog/test-infra-definitions/components"
Expand Down Expand Up @@ -34,6 +37,14 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func eksDiagnoseFunc(ctx context.Context, stackName string) (string, error) {
dumpResult, err := dumpEKSClusterState(ctx, stackName)
if err != nil {
return "", err
}
return fmt.Sprintf("Dumping EKS cluster state:\n%s", dumpResult), nil
}

// EKSProvisioner creates a new provisioner
func EKSProvisioner(opts ...ProvisionerOption) e2e.TypedProvisioner[environments.AwsKubernetes] {
// We ALWAYS need to make a deep copy of `params`, as the provisioner can be called multiple times.
Expand All @@ -50,6 +61,8 @@ func EKSProvisioner(opts ...ProvisionerOption) e2e.TypedProvisioner[environments
return EKSRunFunc(ctx, env, params)
}, params.extraConfigParams)

provisioner.SetDiagnoseFunc(eksDiagnoseFunc)

return provisioner
}

Expand Down
11 changes: 11 additions & 0 deletions test/new-e2e/pkg/environments/aws/kubernetes/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package awskubernetes

import (
"context"
"fmt"

"github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e"
Expand All @@ -31,6 +32,14 @@ const (
defaultVMName = "kind"
)

func kindDiagnoseFunc(ctx context.Context, stackName string) (string, error) {
dumpResult, err := dumpKindClusterState(ctx, stackName)
if err != nil {
return "", err
}
return fmt.Sprintf("Dumping Kind cluster state:\n%s", dumpResult), nil
}

// KindProvisioner creates a new provisioner
func KindProvisioner(opts ...ProvisionerOption) e2e.TypedProvisioner[environments.Kubernetes] {
// We ALWAYS need to make a deep copy of `params`, as the provisioner can be called multiple times.
Expand All @@ -47,6 +56,8 @@ func KindProvisioner(opts ...ProvisionerOption) e2e.TypedProvisioner[environment
return KindRunFunc(ctx, env, params)
}, params.extraConfigParams)

provisioner.SetDiagnoseFunc(kindDiagnoseFunc)

return provisioner
}

Expand Down
Loading

0 comments on commit bdaa549

Please sign in to comment.