diff --git a/common/config/providers.go b/common/config/providers.go index 645fd2149..1ed39f419 100644 --- a/common/config/providers.go +++ b/common/config/providers.go @@ -6,6 +6,7 @@ import ( "github.com/pulumi/pulumi-awsx/sdk/v2/go/awsx" "github.com/pulumi/pulumi-command/sdk/go/command" + "github.com/pulumi/pulumi-docker/sdk/v4/go/docker" "github.com/pulumi/pulumi-eks/sdk/v2/go/eks" "github.com/pulumi/pulumi-random/sdk/v4/go/random" "github.com/pulumi/pulumi-tls/sdk/v4/go/tls" @@ -22,6 +23,7 @@ const ( ProviderAWSX ProviderID = "awsx" ProviderEKS ProviderID = "eks" ProviderAzure ProviderID = "azure" + ProviderDocker ProviderID = "docker" ) func dummyProvidersFactory() map[ProviderID]func(ctx *pulumi.Context, name string) (pulumi.ProviderResource, error) { @@ -46,6 +48,10 @@ func dummyProvidersFactory() map[ProviderID]func(ctx *pulumi.Context, name strin provider, err := eks.NewProvider(ctx, name, nil) return provider, err }, + ProviderDocker: func(ctx *pulumi.Context, name string) (pulumi.ProviderResource, error) { + provider, err := docker.NewProvider(ctx, name, nil) + return provider, err + }, } } diff --git a/components/datadog/agent/kubernetes_helm.go b/components/datadog/agent/kubernetes_helm.go index 10d50c670..694c13d65 100644 --- a/components/datadog/agent/kubernetes_helm.go +++ b/components/datadog/agent/kubernetes_helm.go @@ -485,7 +485,7 @@ func (values HelmValues) configureFakeintake(e config.Env, fakeintake *fakeintak }, pulumi.StringMap{ "name": pulumi.String("DD_LOGS_CONFIG_ADDITIONAL_ENDPOINTS"), - "value": pulumi.Sprintf(`[{"host": "%s"}]`, fakeintake.Host), + "value": pulumi.Sprintf(`[{"host": "%s", "port": %v, "use_ssl": "%t"}]`, fakeintake.Host, fakeintake.Port, fakeintake.Scheme == "https"), }, pulumi.StringMap{ "name": pulumi.String("DD_LOGS_CONFIG_USE_HTTP"), diff --git a/components/datadog/fakeintake/docker.go b/components/datadog/fakeintake/docker.go new file mode 100644 index 000000000..a105edcd3 --- /dev/null +++ b/components/datadog/fakeintake/docker.go @@ -0,0 +1,57 @@ +package fakeintake + +import ( + "net" + + "github.com/DataDog/test-infra-definitions/common/config" + "github.com/DataDog/test-infra-definitions/components" + "github.com/pulumi/pulumi-docker/sdk/v4/go/docker" + "github.com/pulumi/pulumi/sdk/v3/go/pulumi" +) + +const hostPort = 30080 + +func NewLocalDockerFakeintake(e config.Env, resourceName string) (*Fakeintake, error) { + return components.NewComponent(e, resourceName, func(comp *Fakeintake) error { + + _, err := docker.NewContainer(e.Ctx(), e.CommonNamer().ResourceName("local-docker-container"), &docker.ContainerArgs{ + Image: pulumi.String("public.ecr.aws/datadog/fakeintake:latest"), + Ports: docker.ContainerPortArray{ + &docker.ContainerPortArgs{ + Internal: pulumi.Int(80), + External: pulumi.Int(hostPort), + }, + }, + }, e.WithProviders(config.ProviderDocker)) + if err != nil { + return err + } + + localIP, err := getLocalIP() + if err != nil { + return err + } + + comp.Host = pulumi.Sprintf(localIP.String()) + comp.Port = hostPort + comp.Scheme = "http" + comp.URL = pulumi.Sprintf("%s://%s:%d", comp.Scheme, comp.Host, comp.Port) + + return nil + }) +} + +func getLocalIP() (net.IP, error) { + // Open a connection to an external valid URL to + // get the local address from the connection instance + // The URL does not need to exist + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + return nil, err + } + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + + return localAddr.IP, nil +} diff --git a/components/kubernetes/kind-cluster.yaml b/components/kubernetes/kind-cluster.yaml index 0e91c8def..b89e1f07d 100644 --- a/components/kubernetes/kind-cluster.yaml +++ b/components/kubernetes/kind-cluster.yaml @@ -12,3 +12,4 @@ containerdConfigPatches: networking: apiServerAddress: "0.0.0.0" apiServerPort: 8443 + diff --git a/components/kubernetes/kind.go b/components/kubernetes/kind.go index b651af28e..40c213f13 100644 --- a/components/kubernetes/kind.go +++ b/components/kubernetes/kind.go @@ -3,6 +3,7 @@ package kubernetes import ( _ "embed" "fmt" + "regexp" "strings" @@ -108,3 +109,60 @@ func NewKindCluster(env config.Env, vm *remote.Host, resourceName, kindClusterNa return nil }, opts...) } + +func NewLocalKindCluster(env config.Env, resourceName, kindClusterName string, kubeVersion string, opts ...pulumi.ResourceOption) (*Cluster, error) { + return components.NewComponent(env, resourceName, func(clusterComp *Cluster) error { + opts = utils.MergeOptions[pulumi.ResourceOption](opts, pulumi.Parent(clusterComp)) + commonEnvironment := env + + kindVersionConfig, err := getKindVersionConfig(kubeVersion) + if err != nil { + return err + } + + runner := command.NewLocalRunner(env, command.LocalRunnerArgs{ + // User: user.Username, + OSCommand: command.NewUnixOSCommand(), + }) + + clusterConfigFilePath := fmt.Sprintf("/tmp/kind-cluster-%s.yaml", kindClusterName) + clusterConfig, err := runner.Command("kind-config", &command.Args{ + Create: pulumi.Sprintf("cat - | tee %s > /dev/null", clusterConfigFilePath), + Delete: pulumi.Sprintf("rm -f %s", clusterConfigFilePath), + Stdin: pulumi.String(kindClusterConfig), + }, opts...) + if err != nil { + return err + } + + nodeImage := fmt.Sprintf("%s/%s:%s", env.InfraOSDescriptor(), kindNodeImageName, kindVersionConfig.nodeImageVersion) + createCluster, err := runner.Command( + commonEnvironment.CommonNamer().ResourceName("kind-create-cluster", resourceName), + &command.Args{ + Create: pulumi.Sprintf("kind create cluster --name %s --config %s --image %s --wait %s", kindClusterName, clusterConfigFilePath, nodeImage, kindReadinessWait), + Delete: pulumi.Sprintf("kind delete cluster --name %s", kindClusterName), + Triggers: pulumi.Array{pulumi.String(kindClusterConfig)}, + }, + utils.MergeOptions(opts, utils.PulumiDependsOn(clusterConfig), pulumi.DeleteBeforeReplace(true))..., + ) + if err != nil { + return err + } + + kubeConfigCmd, err := runner.Command( + commonEnvironment.CommonNamer().ResourceName("kind-kubeconfig", resourceName), + &command.Args{ + Create: pulumi.Sprintf("kind get kubeconfig --name %s", kindClusterName), + }, + utils.MergeOptions(opts, utils.PulumiDependsOn(createCluster))..., + ) + if err != nil { + return err + } + + clusterComp.KubeConfig = kubeConfigCmd.Stdout + clusterComp.ClusterName = pulumi.String(kindClusterName).ToStringOutput() + + return nil + }, opts...) +} diff --git a/go.mod b/go.mod index cc0c9193f..bab69fc72 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/pulumi/pulumi-azure-native-sdk/network/v2 v2.30.0 github.com/pulumi/pulumi-azure-native-sdk/v2 v2.30.0 github.com/pulumi/pulumi-command/sdk v0.9.2 + github.com/pulumi/pulumi-docker/sdk/v4 v4.5.1 github.com/pulumi/pulumi-eks/sdk/v2 v2.2.1 github.com/pulumi/pulumi-kubernetes/sdk/v4 v4.9.0 github.com/pulumi/pulumi-libvirt/sdk v0.4.5 @@ -103,7 +104,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pulumi/appdash v0.0.0-20231130102222-75f619a67231 // indirect github.com/pulumi/esc v0.8.2 // indirect - github.com/pulumi/pulumi-docker/sdk/v4 v4.5.1 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect