Skip to content

Commit

Permalink
add new kind provisioners (aws and local) and update generic k8s e2e …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
fanny-jiang committed Dec 27, 2024
1 parent 45bff5e commit 0c3c3a0
Show file tree
Hide file tree
Showing 15 changed files with 1,194 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: registry.ddbuild.io/images/mirror/golang:1.22.7
image: registry.ddbuild.io/images/mirror/golang:1.23.0
variables:
PROJECTNAME: "datadog-operator"
PROJECTNAME_CHECK: "datadog-operator-check"
Expand All @@ -13,7 +13,7 @@ variables:
RH_PARTNER_REGISTRY_USER: "redhat-isv-containers+5e7c8ebc1c86a3163d1a69be-robot"
RH_PARTNER_REGISTRY_KEY_SSM_KEY: redhat_registry_key
RH_PARTNER_API_KEY_SSM_KEY: redhat_api_key
TEST_INFRA_DEFINITIONS_BUILDIMAGES: 3c7d2dc2d3dd
TEST_INFRA_DEFINITIONS_BUILDIMAGES: 36860d3c29b4
PUSH_IMAGES_TO_STAGING:
description: "Set PUSH_IMAGE_TO_STAGING to 'true' if you want to push the operator to internal staging registry."

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ integration-tests-v2: $(ENVTEST) ## Run tests with reconciler V2

.PHONY: e2e-tests
e2e-tests: manifests $(KUSTOMIZE) ## Run E2E tests and destroy environment stacks after tests complete. To run locally, complete pre-reqs (see docs/how-to-contribute.md) and prepend command with `aws-vault exec sso-agent-sandbox-account-admin --`. E.g. `aws-vault exec sso-agent-sandbox-account-admin -- make e2e-tests`.
KUBEBUILDER_ASSETS="$(ROOT)/bin/$(PLATFORM)/" go test -C test/e2e --tags=e2e github.com/DataDog/datadog-operator/e2e -v -timeout 1h -coverprofile cover_e2e.out
KUBEBUILDER_ASSETS="$(ROOT)/bin/$(PLATFORM)/" go test -C test/e2e github.com/DataDog/datadog-operator/test/e2e -run TestAWSKindSuite -count=1 -v -timeout 1h -coverprofile cover_e2e.out

.PHONY: e2e-tests-keep-stacks
e2e-tests-keep-stacks: manifests $(KUSTOMIZE) ## Run E2E tests and keep environment stacks running. To run locally, complete pre-reqs (see docs/how-to-contribute.md) and prepend command with `aws-vault exec sso-agent-sandbox-account-admin --`. E.g. `aws-vault exec sso-agent-sandbox-account-admin -- make e2e-tests-keep-stacks`.
Expand Down
7 changes: 7 additions & 0 deletions config/new-e2e/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: datadog-operator-e2e-
namespace: e2e-operator
resources:
- ../crd
- ../rbac
5 changes: 5 additions & 0 deletions test/e2e/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: e2elocal
runtime: go
description: Generic scenario (check scenario variable)
config:
pulumi:disable-default-providers: ["*"]
85 changes: 85 additions & 0 deletions test/e2e/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 common

import (
"encoding/json"
"os"
"path/filepath"
"regexp"
"runtime"
)

var (
NamespaceName = "e2e-operator"
K8sVersion = GetEnv("K8S_VERSION", "1.26")
ImgPullPassword = GetEnv("IMAGE_PULL_PASSWORD", "")
OperatorImageName = GetEnv("IMG", "")

DdaMinimalPath = filepath.Join(ManifestsPath, "datadog-agent-minimum.yaml")
ManifestsPath = filepath.Join(ProjectRootPath, "test/e2e/manifests/new_manifests")

timeout int64 = 60
ProjectRootPath = projectRoot()
)

const (
NodeAgentSelector = "agent.datadoghq.com/component=agent"
ClusterAgentSelector = "agent.datadoghq.com/component=cluster-agent"
ClusterCheckRunnerSelector = "agent.datadoghq.com/component=cluster-checks-runner"
)

// GetAbsPath Return absolute path for given path
func GetAbsPath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
_, err = os.Stat(absPath)
if err != nil {
return "", err
}
if os.IsNotExist(err) {
return "", err
}

return absPath, nil
}

func GetEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

func ParseCollectorJson(collectorOutput string) map[string]interface{} {
var jsonString string
var jsonObject map[string]interface{}

re := regexp.MustCompile(`(\{.*\})`)
match := re.FindStringSubmatch(collectorOutput)
if len(match) > 0 {
jsonString = match[0]
} else {
return map[string]interface{}{}
}

// Parse collector JSON
err := json.Unmarshal([]byte(jsonString), &jsonObject)
if err != nil {
return map[string]interface{}{}
}
return jsonObject
}

func projectRoot() string {
_, b, _, ok := runtime.Caller(0)
if ok {
return filepath.Join(filepath.Dir(b), "../../..")
}
return ""
}
35 changes: 35 additions & 0 deletions test/e2e/manifests/new_manifests/autodiscovery-annotation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
namespace: e2e-operator
spec:
replicas: 1
selector:
matchLabels:
app: nginx
e2e: autodiscovery
template:
metadata:
annotations:
ad.datadoghq.com/nginx.check_names: '["http_check"]'
ad.datadoghq.com/nginx.init_configs: '[{}]'
ad.datadoghq.com/nginx.instances: |
[
{
"name": "http_custom_identifier",
"url": "http://www.google.com"
}
]
ad.datadoghq.com/tolerate-unready: "true"
labels:
app: nginx
e2e: autodiscovery
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
19 changes: 19 additions & 0 deletions test/e2e/manifests/new_manifests/datadog-agent-ccr-enabled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: datadoghq.com/v2alpha1
kind: DatadogAgent
metadata:
name: datadog-ccr-enabled
namespace: e2e-operator
labels:
agent.datadoghq.com/e2e-test: datadog-agent-ccr-enabled
spec:
global:
kubelet:
tlsVerify: false
features:
clusterChecks:
enabled: true
useClusterChecksRunners: true
liveContainerCollection:
enabled: true
logCollection:
enabled: true
20 changes: 20 additions & 0 deletions test/e2e/manifests/new_manifests/datadog-agent-logs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: datadoghq.com/v2alpha1
kind: DatadogAgent
metadata:
name: datadog-agent-logs
namespace: e2e-operator
labels:
agent.datadoghq.com/e2e-test: datadog-agent-logs
spec:
global:
kubelet:
tlsVerify: false
features:
clusterChecks:
enabled: true
useClusterChecksRunners: false
logCollection:
enabled: true
containerCollectAll: true
liveContainerCollection:
enabled: true
10 changes: 10 additions & 0 deletions test/e2e/manifests/new_manifests/datadog-agent-minimum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: datadoghq.com/v2alpha1
kind: DatadogAgent
metadata:
namespace: e2e-operator
labels:
agent.datadoghq.com/e2e-test: datadog-agent-minimum
spec:
global:
kubelet:
tlsVerify: false
126 changes: 126 additions & 0 deletions test/e2e/provisioners/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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 provisioners

import (
"fmt"
"github.com/DataDog/datadog-operator/pkg/plugin/common"
e2ecommon "github.com/DataDog/datadog-operator/test/e2e/common"
"os"
"path/filepath"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/resid"
"sigs.k8s.io/yaml"
)

var (
NewMgrKustomizeDirPath = filepath.Join(e2ecommon.ProjectRootPath, "config", "new-e2e")
)

const (
DefaultMgrImageName = "gcr.io/datadoghq/operator"
DefaultMgrImgTag = "latest"
DefaultMgrFileName = "e2e-manager.yaml"
UserData = `#!/bin/bash
echo "User Data"
echo "Installing kubectl"
snap install kubectl --classic
echo "Verifying kubectl"
kubectl version --client
echo "Installing kubens"
curl -sLo kubens https://github.com/ahmetb/kubectx/releases/download/v0.9.5/kubens
chmod +x kubens
mv kubens /usr/local/bin/
echo '
alias k="kubectl"
alias kg="kubectl get"
alias kgp="kubectl get pod"
alias krm="kubectl delete"
alias krmp="kubectl delete pod"
alias kd="kubectl describe"
alias kdp="kubectl describe pod"
alias ke="kubectl edit"
alias kl="kubectl logs"
alias kx="kubectl exec"
' >> /home/ubuntu/.bashrc
`
)

func loadKustomization(path string) (*types.Kustomization, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}

var kustomization types.Kustomization
if err := yaml.Unmarshal(data, &kustomization); err != nil {
return nil, err
}

return &kustomization, nil
}

func saveKustomization(path string, kustomization *types.Kustomization) error {
data, err := yaml.Marshal(kustomization)
if err != nil {
return err
}

if err := os.WriteFile(path, data, 0644); err != nil {
return err
}

return nil
}

// updateKustomization Updates kustomization.yaml file in given kustomize directory with extra resources and image name and tag if `IMG` environment variable is set.
func UpdateKustomization(kustomizeDirPath string, kustomizeResourcePaths []string) error {
var imgName, imgTag string

kustomizationFilePath := fmt.Sprintf("%s/kustomization.yaml", kustomizeDirPath)
k, err := loadKustomization(kustomizationFilePath)
if err != nil {
return err
}

// Update resources with target e2e-manager resource yaml
if kustomizeResourcePaths != nil {
// We empty slice to avoid accumulating patches from previous tests
k.Patches = k.Patches[:0]
for _, res := range kustomizeResourcePaths {
k.Patches = append(k.Patches, types.Patch{
Path: res,
Target: &types.Selector{
ResId: resid.NewResIdKindOnly("Deployment", "manager"),
},
})
}
}

// Update image
if os.Getenv("IMG") != "" {
imgName, imgTag = common.SplitImageString(os.Getenv("IMG"))
} else {
imgName = DefaultMgrImageName
imgTag = DefaultMgrImgTag
}
for i, img := range k.Images {
if img.Name == "controller" {
k.Images[i].NewName = imgName
k.Images[i].NewTag = imgTag
}
}

if err := saveKustomization(kustomizationFilePath, k); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 0c3c3a0

Please sign in to comment.