diff --git a/test/new-e2e/examples/kind_local_operator_test.go b/test/new-e2e/examples/kind_local_operator_test.go new file mode 100644 index 0000000000000..ccb59885b7a35 --- /dev/null +++ b/test/new-e2e/examples/kind_local_operator_test.go @@ -0,0 +1,137 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +package examples + +import ( + "context" + "github.com/DataDog/test-infra-definitions/components/datadog/agent" + "github.com/DataDog/test-infra-definitions/components/datadog/agentwithoperatorparams" + fakeintakeComp "github.com/DataDog/test-infra-definitions/components/datadog/fakeintake" + "github.com/DataDog/test-infra-definitions/components/datadog/operator" + "github.com/DataDog/test-infra-definitions/components/datadog/operatorparams" + compkube "github.com/DataDog/test-infra-definitions/components/kubernetes" + "github.com/DataDog/test-infra-definitions/resources/local" + "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes" + "github.com/pulumi/pulumi/sdk/v3/go/pulumi" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e" + "github.com/DataDog/datadog-agent/test/new-e2e/pkg/environments" +) + +const namespace = "datadog" + +type localKindOperatorSuite struct { + e2e.BaseSuite[environments.Kubernetes] +} + +func localKindOperatorProvisioner() e2e.PulumiEnvRunFunc[environments.Kubernetes] { + return func(ctx *pulumi.Context, env *environments.Kubernetes) error { + kindEnv, err := local.NewEnvironment(ctx) + if err != nil { + return err + } + + kindCluster, err := compkube.NewLocalKindCluster(&kindEnv, kindEnv.CommonNamer().ResourceName("kind-operator"), kindEnv.KubernetesVersion()) + if err != nil { + return err + } + + if err := kindCluster.Export(ctx, &env.KubernetesCluster.ClusterOutput); err != nil { + return err + } + + // Build Kubernetes provider + kindKubeProvider, err := kubernetes.NewProvider(ctx, kindEnv.CommonNamer().ResourceName("k8s-provider"), &kubernetes.ProviderArgs{ + Kubeconfig: kindCluster.KubeConfig, + EnableServerSideApply: pulumi.BoolPtr(true), + }) + if err != nil { + return err + } + + kindCluster.KubeProvider = kindKubeProvider + + fakeIntake, err := fakeintakeComp.NewLocalDockerFakeintake(&kindEnv, "fakeintake") + if err != nil { + return err + } + if err := fakeIntake.Export(ctx, &env.FakeIntake.FakeintakeOutput); err != nil { + return err + } + + // Setup default operator options + operatorOpts := make([]operatorparams.Option, 0) + operatorOpts = append( + operatorOpts, + operatorparams.WithNamespace(namespace), + ) + + // Create Operator component + _, err = operator.NewOperator(&kindEnv, kindEnv.CommonNamer().ResourceName("dd-operator"), kindCluster.KubeProvider, operatorOpts...) + + if err != nil { + return err + } + + customDDA := agentwithoperatorparams.DDAConfig{ + Name: "ccr-enabled", + YamlConfig: ` +apiVersion: datadoghq.com/v2alpha1 +kind: DatadogAgent +spec: + global: + kubelet: + tlsVerify: false + features: + clusterChecks: + useClusterChecksRunners: true +`, + } + + // Setup DDA options + ddaOptions := make([]agentwithoperatorparams.Option, 0) + ddaOptions = append( + ddaOptions, + agentwithoperatorparams.WithDDAConfig(customDDA), + agentwithoperatorparams.WithNamespace(namespace), + agentwithoperatorparams.WithFakeIntake(fakeIntake), + ) + + // Create Datadog Agent with Operator component + agentComponent, err := agent.NewDDAWithOperator(&kindEnv, ctx.Stack(), kindCluster.KubeProvider, ddaOptions...) + + if err != nil { + return err + } + err = agentComponent.Export(ctx, &env.Agent.KubernetesAgentOutput) + if err != nil { + return err + } + + return nil + } +} + +func TestOperatorKindSuite(t *testing.T) { + e2e.Run(t, &localKindOperatorSuite{}, e2e.WithPulumiProvisioner(localKindOperatorProvisioner(), nil)) +} + +func (k *localKindOperatorSuite) TestClusterChecksRunner() { + res, _ := k.Env().KubernetesCluster.Client().CoreV1().Pods(namespace).List(context.TODO(), v1.ListOptions{}) + containsCCR := false + for _, pod := range res.Items { + if strings.Contains(pod.Name, "cluster-checks-runner") { + containsCCR = true + break + } + } + assert.True(k.T(), containsCCR, "Cluster checks runner not found") +} diff --git a/test/new-e2e/examples/kind_operator_test.go b/test/new-e2e/examples/kind_operator_test.go new file mode 100644 index 0000000000000..0241d966e8a6e --- /dev/null +++ b/test/new-e2e/examples/kind_operator_test.go @@ -0,0 +1,60 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +package examples + +import ( + "context" + "github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e" + "github.com/DataDog/datadog-agent/test/new-e2e/pkg/environments" + awskubernetes "github.com/DataDog/datadog-agent/test/new-e2e/pkg/environments/aws/kubernetes" + "github.com/DataDog/test-infra-definitions/components/datadog/agentwithoperatorparams" + "github.com/stretchr/testify/assert" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "strings" + "testing" +) + +type kindOperatorSuite struct { + e2e.BaseSuite[environments.Kubernetes] +} + +func TestKindOperatorSuite(t *testing.T) { + customDDA := agentwithoperatorparams.DDAConfig{ + Name: "ccr-enabled", + YamlConfig: ` +apiVersion: datadoghq.com/v2alpha1 +kind: DatadogAgent +spec: + global: + kubelet: + tlsVerify: false + features: + clusterChecks: + useClusterChecksRunners: true +`, + } + + e2e.Run(t, &kindOperatorSuite{}, e2e.WithProvisioner(awskubernetes.KindProvisioner( + awskubernetes.WithOperator(), + awskubernetes.WithOperatorDDAOptions([]agentwithoperatorparams.Option{ + agentwithoperatorparams.WithDDAConfig(customDDA), + }...), + ))) +} + +func (k *kindOperatorSuite) TestClusterChecksRunner() { + { + res, _ := k.Env().KubernetesCluster.Client().CoreV1().Pods("datadog").List(context.TODO(), v1.ListOptions{}) + containsCCR := false + for _, pod := range res.Items { + if strings.Contains(pod.Name, "cluster-checks-runner") { + containsCCR = true + break + } + } + assert.True(k.T(), containsCCR, "Cluster checks runner not found") + } +} diff --git a/test/new-e2e/pkg/components/operator.go b/test/new-e2e/pkg/components/operator.go new file mode 100644 index 0000000000000..4fc9e4be0332f --- /dev/null +++ b/test/new-e2e/pkg/components/operator.go @@ -0,0 +1,15 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +package components + +import ( + "github.com/DataDog/test-infra-definitions/components/datadog/operator" +) + +// Operator is a Datadog Operator running in a Kubernetes cluster +type Operator struct { + operator.OperatorOutput +} diff --git a/test/new-e2e/pkg/environments/aws/kubernetes/kind.go b/test/new-e2e/pkg/environments/aws/kubernetes/kind.go index 60da0620167f9..581e4829ca366 100644 --- a/test/new-e2e/pkg/environments/aws/kubernetes/kind.go +++ b/test/new-e2e/pkg/environments/aws/kubernetes/kind.go @@ -9,8 +9,11 @@ package awskubernetes import ( "context" "fmt" - "github.com/DataDog/test-infra-definitions/common/utils" + "github.com/DataDog/test-infra-definitions/components/datadog/agent" + "github.com/DataDog/test-infra-definitions/components/datadog/agentwithoperatorparams" + "github.com/DataDog/test-infra-definitions/components/datadog/operator" + "github.com/DataDog/test-infra-definitions/components/datadog/operatorparams" "github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e" "github.com/DataDog/datadog-agent/test/new-e2e/pkg/environments" @@ -123,12 +126,16 @@ func KindRunFunc(ctx *pulumi.Context, env *environments.Kubernetes, params *Prov newOpts := []kubernetesagentparams.Option{kubernetesagentparams.WithFakeintake(fakeIntake)} params.agentOptions = append(newOpts, params.agentOptions...) } + if params.operatorDDAOptions != nil { + newDdaOpts := []agentwithoperatorparams.Option{agentwithoperatorparams.WithFakeIntake(fakeIntake)} + params.operatorDDAOptions = append(newDdaOpts, params.operatorDDAOptions...) + } } else { env.FakeIntake = nil } var dependsOnCrd []pulumi.Resource - if params.agentOptions != nil { + if params.agentOptions != nil && !params.deployOperator { kindClusterName := ctx.Stack() helmValues := fmt.Sprintf(` datadog: @@ -150,8 +157,23 @@ agents: return err } dependsOnCrd = append(dependsOnCrd, agent) - } else { - env.Agent = nil + } + + if params.deployOperator { + operatorOpts := make([]operatorparams.Option, 0) + operatorOpts = append( + operatorOpts, + params.operatorOptions..., + ) + + operatorComp, err := operator.NewOperator(&awsEnv, awsEnv.Namer.ResourceName("dd-operator"), kubeProvider, operatorOpts...) + if err != nil { + return err + } + err = operatorComp.Export(ctx, nil) + if err != nil { + return err + } } if params.deployDogstatsd { @@ -208,5 +230,25 @@ agents: } } + if params.deployOperator && params.operatorDDAOptions != nil { + ddaOptions := make([]agentwithoperatorparams.Option, 0) + ddaOptions = append( + ddaOptions, + params.operatorDDAOptions..., + ) + + ddaWithOperatorComp, err := agent.NewDDAWithOperator(&awsEnv, awsEnv.CommonNamer().ResourceName("kind-with-operator"), kubeProvider, ddaOptions...) + if err != nil { + return err + } + + if err := ddaWithOperatorComp.Export(ctx, &env.Agent.KubernetesAgentOutput); err != nil { + return err + } + + } else { + env.Agent = nil + } + return nil } diff --git a/test/new-e2e/pkg/environments/aws/kubernetes/params.go b/test/new-e2e/pkg/environments/aws/kubernetes/params.go index 9f7e9f1c394ff..833899ce7b1f3 100644 --- a/test/new-e2e/pkg/environments/aws/kubernetes/params.go +++ b/test/new-e2e/pkg/environments/aws/kubernetes/params.go @@ -8,6 +8,8 @@ package awskubernetes import ( "fmt" + "github.com/DataDog/test-infra-definitions/components/datadog/agentwithoperatorparams" + "github.com/DataDog/test-infra-definitions/components/datadog/operatorparams" "github.com/DataDog/datadog-agent/test/new-e2e/pkg/runner" "github.com/DataDog/datadog-agent/test/new-e2e/pkg/utils/optional" @@ -25,13 +27,15 @@ import ( // ProvisionerParams contains all the parameters needed to create the environment type ProvisionerParams struct { - name string - vmOptions []ec2.VMOption - agentOptions []kubernetesagentparams.Option - fakeintakeOptions []fakeintake.Option - eksOptions []eks.Option - extraConfigParams runner.ConfigMap - workloadAppFuncs []WorkloadAppFunc + name string + vmOptions []ec2.VMOption + agentOptions []kubernetesagentparams.Option + fakeintakeOptions []fakeintake.Option + eksOptions []eks.Option + extraConfigParams runner.ConfigMap + workloadAppFuncs []WorkloadAppFunc + operatorOptions []operatorparams.Option + operatorDDAOptions []agentwithoperatorparams.Option eksLinuxNodeGroup bool eksLinuxARMNodeGroup bool @@ -40,23 +44,27 @@ type ProvisionerParams struct { awsEnv *aws.Environment deployDogstatsd bool deployTestWorkload bool + deployOperator bool } func newProvisionerParams() *ProvisionerParams { return &ProvisionerParams{ - name: defaultVMName, - vmOptions: []ec2.VMOption{}, - agentOptions: []kubernetesagentparams.Option{}, - fakeintakeOptions: []fakeintake.Option{}, - eksOptions: []eks.Option{}, - extraConfigParams: runner.ConfigMap{}, - workloadAppFuncs: []WorkloadAppFunc{}, + name: defaultVMName, + vmOptions: []ec2.VMOption{}, + agentOptions: []kubernetesagentparams.Option{}, + fakeintakeOptions: []fakeintake.Option{}, + eksOptions: []eks.Option{}, + extraConfigParams: runner.ConfigMap{}, + workloadAppFuncs: []WorkloadAppFunc{}, + operatorOptions: []operatorparams.Option{}, + operatorDDAOptions: []agentwithoperatorparams.Option{}, eksLinuxNodeGroup: false, eksLinuxARMNodeGroup: false, eksBottlerocketNodeGroup: false, eksWindowsNodeGroup: false, deployDogstatsd: false, + deployOperator: false, } } @@ -171,3 +179,36 @@ func WithAwsEnv(env *aws.Environment) ProvisionerOption { return nil } } + +// WithOperator Deploys the Datadog Operator +func WithOperator() ProvisionerOption { + return func(params *ProvisionerParams) error { + params.deployOperator = true + return nil + } +} + +// WithOperatorOptions Configures the Datadog Operator +func WithOperatorOptions(opts ...operatorparams.Option) ProvisionerOption { + return func(params *ProvisionerParams) error { + params.operatorOptions = opts + return nil + } +} + +// WithOperatorDDAOptions Configures the DatadogAgent custom resource +func WithOperatorDDAOptions(opts ...agentwithoperatorparams.Option) ProvisionerOption { + return func(params *ProvisionerParams) error { + params.operatorDDAOptions = opts + return nil + } +} + +// WithoutDDA removes the DatadogAgent custom resource +func WithoutDDA() ProvisionerOption { + return func(params *ProvisionerParams) error { + params.operatorDDAOptions = nil + params.agentOptions = nil + return nil + } +}