From 736bcae1be6b9aec086df106c3d5fc9ab2bfcb50 Mon Sep 17 00:00:00 2001 From: Celian Raimbault Date: Fri, 17 Jan 2025 11:10:43 +0100 Subject: [PATCH] [create-vm-agent-config-adxt-84] docker: Added PoC for custom config --- .../datadog/dockeragentparams/params.go | 9 +++ components/docker/component.go | 64 ++++++++++++++++++- scenarios/aws/ec2/vm_run.go | 15 ++--- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/components/datadog/dockeragentparams/params.go b/components/datadog/dockeragentparams/params.go index 67fd25d54..4fef3e202 100644 --- a/components/datadog/dockeragentparams/params.go +++ b/components/datadog/dockeragentparams/params.go @@ -51,6 +51,8 @@ type Params struct { PulumiDependsOn []pulumi.ResourceOption // FIPS is true if FIPS image is needed. FIPS bool + // Will be merged with the default agent configuration + AgentConfig string } type Option = func(*Params) error @@ -244,3 +246,10 @@ func WithExtraComposeInlineManifest(cpms ...docker.ComposeInlineManifest) func(* return nil } } + +func WithAgentConfig(config string) func(*Params) error { + return func(p *Params) error { + p.AgentConfig = config + return nil + } +} diff --git a/components/docker/component.go b/components/docker/component.go index e6f0007e7..a48a95dfb 100644 --- a/components/docker/component.go +++ b/components/docker/component.go @@ -92,7 +92,7 @@ func (d *Manager) ComposeFileUp(composeFilePath string, opts ...pulumi.ResourceO ) } -func (d *Manager) ComposeStrUp(name string, composeManifests []ComposeInlineManifest, envVars pulumi.StringMap, opts ...pulumi.ResourceOption) (command.Command, error) { +func (d *Manager) ComposeStrUp(name string, composeManifests []ComposeInlineManifest, envVars pulumi.StringMap, opts ...pulumi.ResourceOption) (pulumi.Resource, error) { opts = utils.MergeOptions(d.opts, opts...) homeCmd, composePath, err := d.Host.OS.FileManager().HomeDirectory(name+"-compose-tmp", opts...) @@ -128,7 +128,7 @@ func (d *Manager) ComposeStrUp(name string, composeManifests []ComposeInlineMani envVars["CONTENT_HASH"] = contentHash composeFileArgs := "-f " + strings.Join(remoteComposePaths, " -f ") - return d.Host.OS.Runner().Command( + cmd, err := d.Host.OS.Runner().Command( d.namer.ResourceName("compose-run", name), &command.Args{ Create: pulumi.Sprintf("docker-compose %s up --detach --wait --timeout %d", composeFileArgs, defaultTimeout), @@ -137,6 +137,66 @@ func (d *Manager) ComposeStrUp(name string, composeManifests []ComposeInlineMani }, utils.MergeOptions(d.opts, utils.PulumiDependsOn(runCommandDeps...), pulumi.DeleteBeforeReplace(true))..., ) + if err != nil { + return nil, err + } + + // Merge configurations if needed + // TODO: If ... return cmd, nil + + // TODO: Check errors + // 1. Read default agent configuration + // TODO: Restore config on delete ? + cmd2, err := d.Host.OS.Runner().Command( + d.namer.ResourceName("read-container-agent-config", name), + &command.Args{ + Create: pulumi.Sprintf("docker-compose %s exec -T %s /bin/cat /etc/datadog-agent/datadog.yaml", composeFileArgs, name), + }, + utils.MergeOptions(d.opts, utils.PulumiDependsOn(cmd))..., + ) + + if err != nil { + return nil, err + } + + // 2. Merge default and custom configurations + // TODO: True merge not concat + with custom configuration + mergedConfig := cmd2.StdoutOutput().ApplyT(func(output string) string { + return output + "\n" + "hello: world" + }).(pulumi.StringOutput) + + // 3. Write merged configuration + // 3.a. Copy to host config + // TODO: True tmp + cmd3, err := d.Host.OS.FileManager().CopyInlineFile(mergedConfig, "/tmp/datadog.yaml", opts...) + + if err != nil { + return nil, err + } + + // 3.b. Copy to container config + // docker-compose -f agent-compose-tmp/docker-compose-agent.yml cp /tmp/datadog.yaml agent:/tmp/datadog.yaml + cmd4, err := d.Host.OS.Runner().Command( + d.namer.ResourceName("write-container-agent-config", name), + &command.Args{ + Create: pulumi.Sprintf("docker-compose %s cp /tmp/datadog.yaml %s:/etc/datadog-agent/datadog.yaml", composeFileArgs, name), + }, + utils.MergeOptions(d.opts, utils.PulumiDependsOn(cmd3))..., + ) + + if err != nil { + return nil, err + } + + return cmd4, err + + // return d.Host.OS.Runner().Command( + // d.namer.ResourceName("cc-test", name), + // &command.Args{ + // Create: pulumi.Sprintf("docker-compose %s exec -T %s /bin/bash -c 'echo \"hello: world\" >> /etc/datadog-agent/datadog.yaml'", composeFileArgs, name), + // }, + // utils.MergeOptions(d.opts, utils.PulumiDependsOn(a))..., + // ) } func (d *Manager) install() (command.Command, error) { diff --git a/scenarios/aws/ec2/vm_run.go b/scenarios/aws/ec2/vm_run.go index a09c1370a..d316e4e57 100644 --- a/scenarios/aws/ec2/vm_run.go +++ b/scenarios/aws/ec2/vm_run.go @@ -144,14 +144,13 @@ func VMRunWithDocker(ctx *pulumi.Context) error { agentOptions = append(agentOptions, dockeragentparams.WithFakeintake(fakeintake)) } - // TODO A: Update docker params - // if env.AgentConfigPath() != "" { - // configContent, err := env.CustomAgentConfig() - // if err != nil { - // return err - // } - // agentOptions = append(agentOptions, dockeragentparams.WithAgentConfig(configContent)) - // } + if env.AgentConfigPath() != "" { + configContent, err := env.CustomAgentConfig() + if err != nil { + return err + } + agentOptions = append(agentOptions, dockeragentparams.WithAgentConfig(configContent)) + } if env.TestingWorkloadDeploy() { agentOptions = append(agentOptions, dockeragentparams.WithExtraComposeManifest(dogstatsd.DockerComposeManifest.Name, dogstatsd.DockerComposeManifest.Content))