-
Notifications
You must be signed in to change notification settings - Fork 4
/
ecs_logconfig.go
152 lines (148 loc) · 5.61 KB
/
ecs_logconfig.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
package awsecs
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
)
func alterLogConfigurationLogDriverOptions(copy ecs.LogConfiguration, overrides map[string]map[string]string) ecs.LogConfiguration {
knockOutDriver := ""
for logDriver, overrides := range overrides {
if copy.LogDriver != nil && *copy.LogDriver == logDriver {
for optionName, optionValue := range overrides {
if optionName == EnvKnockOutValue {
knockOutDriver = logDriver
}
if copy.Options == nil {
copy.Options = map[string]*string{}
}
copy.Options[optionName] = aws.String(optionValue)
if optionValue == EnvKnockOutValue || optionName == EnvKnockOutValue {
delete(copy.Options, optionName)
}
}
}
}
if knockOutDriver != "" {
delete(overrides, knockOutDriver)
copy.LogDriver = nil
}
if copy.LogDriver == nil && len(overrides) == 1 {
for logDriver, options := range overrides {
copy.LogDriver = aws.String(logDriver)
for optionName, optionValue := range options {
if copy.Options == nil {
copy.Options = map[string]*string{}
}
copy.Options[optionName] = aws.String(optionValue)
if optionValue == EnvKnockOutValue || optionName == EnvKnockOutValue {
delete(copy.Options, optionName)
}
}
}
}
return copy
}
func alterLogConfigurationLogDriverSecrets(copy ecs.LogConfiguration, overrides map[string]map[string]string) ecs.LogConfiguration {
knockOutDriver := ""
for logDriver, overrides := range overrides {
if copy.LogDriver != nil && *copy.LogDriver == logDriver {
for optionName, optionValue := range overrides {
if optionName == EnvKnockOutValue {
knockOutDriver = logDriver
}
thisOptionChanged := false
for _, secretOption := range copy.SecretOptions {
if secretOption != nil && secretOption.Name != nil && *secretOption.Name == optionName {
thisOptionChanged = true
secretOption.ValueFrom = aws.String(optionValue)
}
}
if !thisOptionChanged {
copy.SecretOptions = append(copy.SecretOptions, &ecs.Secret{Name: aws.String(optionName), ValueFrom: aws.String(optionValue)})
}
var filteredSecretOptions []*ecs.Secret
for _, secretOption := range copy.SecretOptions {
if secretOption != nil && secretOption.Name != nil && *secretOption.Name == optionName {
if optionValue != EnvKnockOutValue && optionName != EnvKnockOutValue {
filteredSecretOptions = append(filteredSecretOptions, secretOption)
}
} else {
filteredSecretOptions = append(filteredSecretOptions, secretOption)
}
}
copy.SecretOptions = filteredSecretOptions
}
}
}
if knockOutDriver != "" {
delete(overrides, knockOutDriver)
copy.LogDriver = nil
}
if copy.LogDriver == nil && len(overrides) == 1 {
for logDriver, options := range overrides {
copy.LogDriver = aws.String(logDriver)
for optionName, optionValue := range options {
thisOptionChanged := false
for _, secretOption := range copy.SecretOptions {
if secretOption != nil && secretOption.Name != nil && *secretOption.Name == optionName {
thisOptionChanged = true
}
}
if !thisOptionChanged {
copy.SecretOptions = append(copy.SecretOptions, &ecs.Secret{Name: aws.String(optionName), ValueFrom: aws.String(optionValue)})
}
var filteredSecretOptions []*ecs.Secret
for _, secretOption := range copy.SecretOptions {
if secretOption != nil && secretOption.Name != nil && *secretOption.Name == optionName {
if optionValue != EnvKnockOutValue && optionName != EnvKnockOutValue {
filteredSecretOptions = append(filteredSecretOptions, secretOption)
}
} else {
filteredSecretOptions = append(filteredSecretOptions, secretOption)
}
}
copy.SecretOptions = filteredSecretOptions
}
}
}
return copy
}
func alterLogConfigurations(copy ecs.RegisterTaskDefinitionInput, containersOptions map[string]map[string]map[string]string, containersSecrets map[string]map[string]map[string]string) ecs.RegisterTaskDefinitionInput {
obj := panicMarshal(copy)
copyClone := ecs.RegisterTaskDefinitionInput{}
panicUnmarshal(obj, ©Clone)
for _, containerDefinition := range copyClone.ContainerDefinitions {
for containerName, containerOptions := range containersOptions {
if containerDefinition.Name != nil && *containerDefinition.Name == containerName {
var logConfiguration *ecs.LogConfiguration
if containerDefinition.LogConfiguration != nil {
logConfiguration = containerDefinition.LogConfiguration
} else {
logConfiguration = &ecs.LogConfiguration{}
}
*logConfiguration = alterLogConfigurationLogDriverOptions(*logConfiguration, containerOptions)
if logConfiguration.LogDriver == nil || (logConfiguration.LogDriver != nil && *logConfiguration.LogDriver == "") {
containerDefinition.LogConfiguration = nil
} else {
containerDefinition.LogConfiguration = logConfiguration
}
}
}
for containerName, containerOptions := range containersSecrets {
if containerDefinition.Name != nil && *containerDefinition.Name == containerName {
var logConfiguration *ecs.LogConfiguration
if containerDefinition.LogConfiguration != nil {
logConfiguration = containerDefinition.LogConfiguration
} else {
logConfiguration = &ecs.LogConfiguration{}
}
*logConfiguration = alterLogConfigurationLogDriverSecrets(*logConfiguration, containerOptions)
if logConfiguration.LogDriver == nil || (logConfiguration.LogDriver != nil && *logConfiguration.LogDriver == "") {
containerDefinition.LogConfiguration = nil
} else {
containerDefinition.LogConfiguration = logConfiguration
}
}
}
}
return copyClone
}