-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_builder.go
196 lines (170 loc) · 6.55 KB
/
component_builder.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
package models
import (
"github.com/equinor/radix-api/api/secrets/suffix"
errorutils "github.com/equinor/radix-common/utils/errors"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/deployment"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
)
// ComponentBuilder Builds DTOs
type ComponentBuilder interface {
WithStatus(ComponentStatus) ComponentBuilder
WithPodNames([]string) ComponentBuilder
WithReplicaSummaryList([]ReplicaSummary) ComponentBuilder
WithSchedulerPort(schedulerPort *int32) ComponentBuilder
WithScheduledJobPayloadPath(scheduledJobPayloadPath string) ComponentBuilder
WithRadixEnvironmentVariables(map[string]string) ComponentBuilder
WithComponent(v1.RadixCommonDeployComponent) ComponentBuilder
WithAuxiliaryResource(AuxiliaryResource) ComponentBuilder
BuildComponentSummary() (*ComponentSummary, error)
BuildComponent() (*Component, error)
}
type componentBuilder struct {
componentName string
componentType string
status ComponentStatus
componentImage string
podNames []string
replicaSummaryList []ReplicaSummary
environmentVariables map[string]string
radixEnvironmentVariables map[string]string
secrets []string
ports []Port
schedulerPort *int32
scheduledJobPayloadPath string
auxResource AuxiliaryResource
errors []error
}
func (b *componentBuilder) WithStatus(status ComponentStatus) ComponentBuilder {
b.status = status
return b
}
func (b *componentBuilder) WithPodNames(podNames []string) ComponentBuilder {
b.podNames = podNames
return b
}
func (b *componentBuilder) WithReplicaSummaryList(replicaSummaryList []ReplicaSummary) ComponentBuilder {
b.replicaSummaryList = replicaSummaryList
return b
}
func (b *componentBuilder) WithRadixEnvironmentVariables(radixEnvironmentVariables map[string]string) ComponentBuilder {
b.radixEnvironmentVariables = radixEnvironmentVariables
return b
}
func (b *componentBuilder) WithSchedulerPort(schedulerPort *int32) ComponentBuilder {
b.schedulerPort = schedulerPort
return b
}
func (b *componentBuilder) WithScheduledJobPayloadPath(scheduledJobPayloadPath string) ComponentBuilder {
b.scheduledJobPayloadPath = scheduledJobPayloadPath
return b
}
func (b *componentBuilder) WithAuxiliaryResource(auxResource AuxiliaryResource) ComponentBuilder {
b.auxResource = auxResource
return b
}
func (b *componentBuilder) WithComponent(component v1.RadixCommonDeployComponent) ComponentBuilder {
b.componentName = component.GetName()
b.componentType = string(component.GetType())
b.componentImage = component.GetImage()
ports := []Port{}
if component.GetPorts() != nil {
for _, port := range component.GetPorts() {
ports = append(ports, Port{
Name: port.Name,
Port: port.Port,
})
}
}
b.ports = ports
b.secrets = component.GetSecrets()
if b.secrets == nil {
b.secrets = []string{}
}
for _, externalAlias := range component.GetDNSExternalAlias() {
b.secrets = append(b.secrets, externalAlias+suffix.ExternalDNSTLSCert)
b.secrets = append(b.secrets, externalAlias+suffix.ExternalDNSTLSKey)
}
for _, volumeMount := range component.GetVolumeMounts() {
switch volumeMount.Type {
case v1.MountTypeBlob:
secretName := defaults.GetBlobFuseCredsSecretName(component.GetName(), volumeMount.Name)
b.secrets = append(b.secrets, secretName+defaults.BlobFuseCredsAccountKeyPartSuffix)
b.secrets = append(b.secrets, secretName+defaults.BlobFuseCredsAccountNamePartSuffix)
case v1.MountTypeBlobCsiAzure, v1.MountTypeFileCsiAzure:
secretName := defaults.GetCsiAzureVolumeMountCredsSecretName(component.GetName(), volumeMount.Name)
b.secrets = append(b.secrets, secretName+defaults.CsiAzureCredsAccountKeyPartSuffix)
b.secrets = append(b.secrets, secretName+defaults.CsiAzureCredsAccountNamePartSuffix)
}
}
secretRef := component.GetSecretRefs()
if secretRef.AzureKeyVaults != nil {
for _, azureKeyVault := range secretRef.AzureKeyVaults {
secretName := defaults.GetCsiAzureKeyVaultCredsSecretName(component.GetName(), azureKeyVault.Name)
b.secrets = append(b.secrets, secretName+defaults.CsiAzureKeyVaultCredsClientIdSuffix)
b.secrets = append(b.secrets, secretName+defaults.CsiAzureKeyVaultCredsClientSecretSuffix)
for _, item := range azureKeyVault.Items {
b.secrets = append(b.secrets, item.EnvVar)
}
}
}
if auth := component.GetAuthentication(); auth != nil && component.IsPublic() {
if deployment.IsSecretRequiredForClientCertificate(auth.ClientCertificate) {
b.secrets = append(b.secrets, utils.GetComponentClientCertificateSecretName(component.GetName()))
}
if auth.OAuth2 != nil {
oauth2, err := defaults.NewOAuth2Config(defaults.WithOAuth2Defaults()).MergeWith(auth.OAuth2)
if err != nil {
b.errors = append(b.errors, err)
}
b.secrets = append(b.secrets, component.GetName()+suffix.OAuth2ClientSecret)
b.secrets = append(b.secrets, component.GetName()+suffix.OAuth2CookieSecret)
if oauth2.SessionStoreType == v1.SessionStoreRedis {
b.secrets = append(b.secrets, component.GetName()+suffix.OAuth2RedisPassword)
}
}
}
b.environmentVariables = component.GetEnvironmentVariables()
return b
}
func (b *componentBuilder) buildError() error {
if len(b.errors) == 0 {
return nil
}
return errorutils.Concat(b.errors)
}
func (b *componentBuilder) BuildComponentSummary() (*ComponentSummary, error) {
return &ComponentSummary{
Name: b.componentName,
Type: b.componentType,
Image: b.componentImage,
}, b.buildError()
}
func (b *componentBuilder) BuildComponent() (*Component, error) {
variables := v1.EnvVarsMap{}
for name, value := range b.environmentVariables {
variables[name] = value
}
for name, value := range b.radixEnvironmentVariables {
variables[name] = value
}
return &Component{
Name: b.componentName,
Type: b.componentType,
Status: b.status.String(),
Image: b.componentImage,
Ports: b.ports,
Secrets: b.secrets,
Variables: variables,
Replicas: b.podNames,
ReplicaList: b.replicaSummaryList,
SchedulerPort: b.schedulerPort,
ScheduledJobPayloadPath: b.scheduledJobPayloadPath,
AuxiliaryResource: b.auxResource,
}, b.buildError()
}
// NewComponentBuilder Constructor for application component
func NewComponentBuilder() ComponentBuilder {
return &componentBuilder{}
}