From 00e26e550f28d22fcb4357c22a2e6ef284b825fb Mon Sep 17 00:00:00 2001 From: Fanny Jiang Date: Tue, 31 Dec 2024 14:09:36 -0500 Subject: [PATCH] add generic k8sapply comp --- components/kubernetes/k8sapply/component.go | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 components/kubernetes/k8sapply/component.go diff --git a/components/kubernetes/k8sapply/component.go b/components/kubernetes/k8sapply/component.go new file mode 100644 index 000000000..6be8e0dab --- /dev/null +++ b/components/kubernetes/k8sapply/component.go @@ -0,0 +1,33 @@ +package k8sapply + +import ( + "fmt" + "github.com/DataDog/test-infra-definitions/common/config" + kubeComp "github.com/DataDog/test-infra-definitions/components/kubernetes" + "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes" + "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/yaml" + "github.com/pulumi/pulumi/sdk/v3/go/pulumi" +) + +type YAMLWorkload struct { + Name string + Path string +} + +// K8sAppDefinition applies a generic Kubernetes resource from a YAML source file defined as a YAMLWorkload +func K8sAppDefinition(yamlWorkload YAMLWorkload) func(e config.Env, kubeProvider *kubernetes.Provider) (*kubeComp.Workload, error) { + return func(e config.Env, kubeProvider *kubernetes.Provider) (*kubeComp.Workload, error) { + k8sComponent := &kubeComp.Workload{} + + if err := e.Ctx().RegisterComponentResource("dd:apps", fmt.Sprintf("k8s-apply-%s", yamlWorkload.Name), k8sComponent); err != nil { + return nil, err + } + _, err := yaml.NewConfigFile(e.Ctx(), yamlWorkload.Name, &yaml.ConfigFileArgs{ + File: yamlWorkload.Path, + }, pulumi.Provider(kubeProvider), pulumi.Parent(k8sComponent)) + if err != nil { + return nil, err + } + return k8sComponent, nil + } +}