/
environment.go
194 lines (172 loc) · 6.31 KB
/
environment.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
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
package service
import (
"context"
api "github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/config"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/repository"
)
type EnvironmentServiceServer struct {
Repository repository.Repository
}
func (o *EnvironmentServiceServer) GetEnvironmentConfig(
ctx context.Context,
in *api.GetEnvironmentConfigRequest) (*api.GetEnvironmentConfigResponse, error) {
state := o.Repository.State()
config, err := state.GetEnvironmentConfig(in.Environment)
if err != nil {
return nil, err
}
var out api.GetEnvironmentConfigResponse
out.Config = TransformEnvironmentConfigToApi(*config)
return &out, nil
}
func TransformEnvironmentConfigToApi(in config.EnvironmentConfig) *api.EnvironmentConfig {
return &api.EnvironmentConfig{
Upstream: transformUpstreamToApi(in.Upstream),
Argocd: transformArgoCdToApi(in.ArgoCd),
EnvironmentGroup: in.EnvironmentGroup,
}
}
func transformUpstreamToConfig(upstream *api.EnvironmentConfig_Upstream) *config.EnvironmentConfigUpstream {
if upstream == nil {
return nil
}
if upstream.GetLatest() {
return &config.EnvironmentConfigUpstream{
Latest: true,
}
}
if upstream.GetEnvironment() != "" {
return &config.EnvironmentConfigUpstream{
Environment: upstream.GetEnvironment(),
}
}
return nil
}
func transformUpstreamToApi(in *config.EnvironmentConfigUpstream) *api.EnvironmentConfig_Upstream {
if in == nil {
return nil
}
return &api.EnvironmentConfig_Upstream{
Environment: &in.Environment,
Latest: &in.Latest,
}
}
func transformArgoCdToApi(in *config.EnvironmentConfigArgoCd) *api.EnvironmentConfig_ArgoCD {
if in == nil {
return nil
}
return &api.EnvironmentConfig_ArgoCD{
SyncWindows: transformSyncWindowsToApi(in.SyncWindows),
Destination: transformDestinationToApi(&in.Destination),
AccessList: transformAccessEntryToApi(in.ClusterResourceWhitelist),
}
}
func transformSyncWindowsToConfig(syncWindows []*api.EnvironmentConfig_ArgoCD_SyncWindows) []config.ArgoCdSyncWindow {
var transformedSyncWindows []config.ArgoCdSyncWindow
for _, syncWindow := range syncWindows {
transformedSyncWindows = append(transformedSyncWindows, config.ArgoCdSyncWindow{
Schedule: syncWindow.Schedule,
Duration: syncWindow.Duration,
Kind: syncWindow.Kind,
Apps: syncWindow.Applications,
})
}
return transformedSyncWindows
}
func transformSyncWindowsToApi(in []config.ArgoCdSyncWindow) []*api.EnvironmentConfig_ArgoCD_SyncWindows {
var out []*api.EnvironmentConfig_ArgoCD_SyncWindows
for _, syncWindow := range in {
out = append(out, &api.EnvironmentConfig_ArgoCD_SyncWindows{
Kind: syncWindow.Kind,
Schedule: syncWindow.Schedule,
Duration: syncWindow.Duration,
})
}
return out
}
func transformAccessListToConfig(accessList []*api.EnvironmentConfig_ArgoCD_AccessEntry) []config.AccessEntry {
var transformedAccessList []config.AccessEntry
for _, accessEntry := range accessList {
transformedAccessList = append(transformedAccessList, config.AccessEntry{
Group: accessEntry.Group,
Kind: accessEntry.Kind,
})
}
return transformedAccessList
}
func transformAccessEntryToApi(in []config.AccessEntry) []*api.EnvironmentConfig_ArgoCD_AccessEntry {
var out []*api.EnvironmentConfig_ArgoCD_AccessEntry
for _, accessEntry := range in {
out = append(out, &api.EnvironmentConfig_ArgoCD_AccessEntry{
Group: accessEntry.Group,
Kind: accessEntry.Kind,
})
}
return out
}
func transformIgnoreDifferencesToConfig(ignoreDifferences []*api.EnvironmentConfig_ArgoCD_IgnoreDifferences) []config.ArgoCdIgnoreDifference {
var transformedIgnoreDifferences []config.ArgoCdIgnoreDifference
for _, ignoreDifference := range ignoreDifferences {
transformedIgnoreDifferences = append(transformedIgnoreDifferences, config.ArgoCdIgnoreDifference{
Group: ignoreDifference.Group,
Kind: ignoreDifference.Kind,
Name: ignoreDifference.Name,
Namespace: ignoreDifference.Namespace,
JSONPointers: ignoreDifference.JsonPointers,
JqPathExpressions: ignoreDifference.JqPathExpressions,
ManagedFieldsManagers: ignoreDifference.ManagedFieldsManagers,
})
}
return transformedIgnoreDifferences
}
func transformIgnoreDifferencesToApi(in []*config.ArgoCdIgnoreDifference) []api.EnvironmentConfig_ArgoCD_IgnoreDifferences {
var out []api.EnvironmentConfig_ArgoCD_IgnoreDifferences
for _, ignoreDifference := range in {
out = append(out, api.EnvironmentConfig_ArgoCD_IgnoreDifferences{
Group: ignoreDifference.Group,
Kind: ignoreDifference.Kind,
Name: ignoreDifference.Name,
Namespace: ignoreDifference.Namespace,
JsonPointers: ignoreDifference.JSONPointers,
JqPathExpressions: ignoreDifference.JqPathExpressions,
ManagedFieldsManagers: ignoreDifference.ManagedFieldsManagers,
})
}
return out
}
func transformDestinationToConfig(in *api.EnvironmentConfig_ArgoCD_Destination) config.ArgoCdDestination {
if in == nil {
return config.ArgoCdDestination{}
}
return config.ArgoCdDestination{
Name: in.Name,
Server: in.Server,
Namespace: in.Namespace,
AppProjectNamespace: in.AppProjectNamespace,
ApplicationNamespace: in.ApplicationNamespace,
}
}
func transformDestinationToApi(in *config.ArgoCdDestination) *api.EnvironmentConfig_ArgoCD_Destination {
if in == nil {
return &api.EnvironmentConfig_ArgoCD_Destination{}
}
return &api.EnvironmentConfig_ArgoCD_Destination{
Name: in.Name,
Server: in.Server,
Namespace: in.Namespace,
AppProjectNamespace: in.AppProjectNamespace,
ApplicationNamespace: in.ApplicationNamespace,
}
}