/
host.go
206 lines (177 loc) · 6.44 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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 awsdocker contains the definition of the AWS Docker environment.
package awsdocker
import (
"fmt"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/environments"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/runner"
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/utils/optional"
"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/dockeragentparams"
"github.com/DataDog/test-infra-definitions/components/docker"
"github.com/DataDog/test-infra-definitions/resources/aws"
"github.com/DataDog/test-infra-definitions/scenarios/aws/ec2"
"github.com/DataDog/test-infra-definitions/scenarios/aws/fakeintake"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
const (
provisionerBaseID = "aws-ec2docker-"
defaultVMName = "dockervm"
)
// ProvisionerParams contains all the parameters needed to create the environment
type ProvisionerParams struct {
name string
vmOptions []ec2.VMOption
agentOptions []dockeragentparams.Option
fakeintakeOptions []fakeintake.Option
extraConfigParams runner.ConfigMap
}
func newProvisionerParams() *ProvisionerParams {
// We use nil arrays to decide if we should create or not
return &ProvisionerParams{
name: defaultVMName,
vmOptions: []ec2.VMOption{},
agentOptions: []dockeragentparams.Option{},
fakeintakeOptions: []fakeintake.Option{},
extraConfigParams: runner.ConfigMap{},
}
}
// GetProvisionerParams return ProvisionerParams from options opts setup
func GetProvisionerParams(opts ...ProvisionerOption) *ProvisionerParams {
params := newProvisionerParams()
err := optional.ApplyOptions(params, opts)
if err != nil {
panic(fmt.Errorf("unable to apply ProvisionerOption, err: %w", err))
}
return params
}
// ProvisionerOption is a function that modifies the ProvisionerParams
type ProvisionerOption func(*ProvisionerParams) error
// WithName sets the name of the provisioner
func WithName(name string) ProvisionerOption {
return func(params *ProvisionerParams) error {
params.name = name
return nil
}
}
// WithEC2VMOptions sets the options for the EC2 VM
func WithEC2VMOptions(opts ...ec2.VMOption) ProvisionerOption {
return func(params *ProvisionerParams) error {
params.vmOptions = append(params.vmOptions, opts...)
return nil
}
}
// WithAgentOptions sets the options for the Docker Agent
func WithAgentOptions(opts ...dockeragentparams.Option) ProvisionerOption {
return func(params *ProvisionerParams) error {
params.agentOptions = append(params.agentOptions, opts...)
return nil
}
}
// WithFakeIntakeOptions sets the options for the FakeIntake
func WithFakeIntakeOptions(opts ...fakeintake.Option) ProvisionerOption {
return func(params *ProvisionerParams) error {
params.fakeintakeOptions = append(params.fakeintakeOptions, opts...)
return nil
}
}
// WithExtraConfigParams sets the extra config params for the environment
func WithExtraConfigParams(configMap runner.ConfigMap) ProvisionerOption {
return func(params *ProvisionerParams) error {
params.extraConfigParams = configMap
return nil
}
}
// WithoutFakeIntake deactivates the creation of the FakeIntake
func WithoutFakeIntake() ProvisionerOption {
return func(params *ProvisionerParams) error {
params.fakeintakeOptions = nil
return nil
}
}
// WithoutAgent deactivates the creation of the Docker Agent
func WithoutAgent() ProvisionerOption {
return func(params *ProvisionerParams) error {
params.agentOptions = nil
return nil
}
}
// Run deploys a docker environment given a pulumi.Context
func Run(ctx *pulumi.Context, env *environments.DockerHost, params *ProvisionerParams) error {
var awsEnv aws.Environment
var err error
if env.AwsEnvironment != nil {
awsEnv = *env.AwsEnvironment
} else {
awsEnv, err = aws.NewEnvironment(ctx)
if err != nil {
return err
}
}
host, err := ec2.NewVM(awsEnv, params.name, params.vmOptions...)
if err != nil {
return err
}
err = host.Export(ctx, &env.RemoteHost.HostOutput)
if err != nil {
return err
}
installEcrCredsHelperCmd, err := ec2.InstallECRCredentialsHelper(awsEnv, host)
if err != nil {
return err
}
manager, _, err := docker.NewManager(*awsEnv.CommonEnvironment, host, true, utils.PulumiDependsOn(installEcrCredsHelperCmd))
if err != nil {
return err
}
// Create FakeIntake if required
if params.fakeintakeOptions != nil {
fakeIntake, err := fakeintake.NewECSFargateInstance(awsEnv, params.name, params.fakeintakeOptions...)
if err != nil {
return err
}
err = fakeIntake.Export(ctx, &env.FakeIntake.FakeintakeOutput)
if err != nil {
return err
}
// Normally if FakeIntake is enabled, Agent is enabled, but just in case
if params.agentOptions != nil {
// Prepend in case it's overridden by the user
newOpts := []dockeragentparams.Option{dockeragentparams.WithFakeintake(fakeIntake)}
params.agentOptions = append(newOpts, params.agentOptions...)
}
} else {
// Suite inits all fields by default, so we need to explicitly set it to nil
env.FakeIntake = nil
}
// Create Agent if required
if params.agentOptions != nil {
agent, err := agent.NewDockerAgent(*awsEnv.CommonEnvironment, host, manager, params.agentOptions...)
if err != nil {
return err
}
err = agent.Export(ctx, &env.Agent.DockerAgentOutput)
if err != nil {
return err
}
} else {
// Suite inits all fields by default, so we need to explicitly set it to nil
env.Agent = nil
}
return nil
}
// Provisioner creates a VM environment with an EC2 VM with Docker, an ECS Fargate FakeIntake and a Docker Agent configured to talk to each other.
// FakeIntake and Agent creation can be deactivated by using [WithoutFakeIntake] and [WithoutAgent] options.
func Provisioner(opts ...ProvisionerOption) e2e.TypedProvisioner[environments.DockerHost] {
// We need to build params here to be able to use params.name in the provisioner name
params := GetProvisionerParams(opts...)
provisioner := e2e.NewTypedPulumiProvisioner(provisionerBaseID+params.name, func(ctx *pulumi.Context, env *environments.DockerHost) error {
return Run(ctx, env, params)
}, params.extraConfigParams)
return provisioner
}