-
Notifications
You must be signed in to change notification settings - Fork 153
/
config.go
255 lines (222 loc) · 7.13 KB
/
config.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2023 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"github.com/creasty/defaults"
"sigs.k8s.io/yaml"
"github.com/pipe-cd/pipecd/pkg/model"
)
const (
SharedConfigurationDirName = ".pipe"
versionV1Beta1 = "pipecd.dev/v1beta1"
)
// Kind represents the kind of configuration the data contains.
type Kind string
const (
// KindKubernetesApp represents application configuration for a Kubernetes application.
// This application can be a group of plain-YAML Kubernetes manifests,
// or kustomization manifests or helm manifests.
KindKubernetesApp Kind = "KubernetesApp"
// KindTerraformApp represents application configuration for a Terraform application.
// This application contains a single workspace of a terraform root module.
KindTerraformApp Kind = "TerraformApp"
// KindLambdaApp represents application configuration for an AWS Lambda application.
KindLambdaApp Kind = "LambdaApp"
// KindCloudRunApp represents application configuration for a CloudRun application.
KindCloudRunApp Kind = "CloudRunApp"
// KindECSApp represents application configuration for an AWS ECS.
KindECSApp Kind = "ECSApp"
)
const (
// KindPiped represents configuration for piped.
// This configuration will be loaded while the piped is starting up.
KindPiped Kind = "Piped"
// KindControlPlane represents configuration for control plane's services.
KindControlPlane Kind = "ControlPlane"
// KindAnalysisTemplate represents shared analysis template for a repository.
// This configuration file should be placed in .pipe directory
// at the root of the repository.
KindAnalysisTemplate Kind = "AnalysisTemplate"
// KindEventWatcher represents configuration for Event Watcher.
KindEventWatcher Kind = "EventWatcher"
)
var (
ErrNotFound = errors.New("not found")
)
// Config represents configuration data load from file.
// The spec is depend on the kind of configuration.
type Config struct {
Kind Kind
APIVersion string
spec interface{}
KubernetesApplicationSpec *KubernetesApplicationSpec
TerraformApplicationSpec *TerraformApplicationSpec
CloudRunApplicationSpec *CloudRunApplicationSpec
LambdaApplicationSpec *LambdaApplicationSpec
ECSApplicationSpec *ECSApplicationSpec
PipedSpec *PipedSpec
ControlPlaneSpec *ControlPlaneSpec
AnalysisTemplateSpec *AnalysisTemplateSpec
EventWatcherSpec *EventWatcherSpec
}
type genericConfig struct {
Kind Kind `json:"kind"`
APIVersion string `json:"apiVersion,omitempty"`
Spec json.RawMessage `json:"spec"`
}
func (c *Config) init(kind Kind, apiVersion string) error {
c.Kind = kind
c.APIVersion = apiVersion
switch kind {
case KindKubernetesApp:
c.KubernetesApplicationSpec = &KubernetesApplicationSpec{}
c.spec = c.KubernetesApplicationSpec
case KindTerraformApp:
c.TerraformApplicationSpec = &TerraformApplicationSpec{}
c.spec = c.TerraformApplicationSpec
case KindCloudRunApp:
c.CloudRunApplicationSpec = &CloudRunApplicationSpec{}
c.spec = c.CloudRunApplicationSpec
case KindLambdaApp:
c.LambdaApplicationSpec = &LambdaApplicationSpec{}
c.spec = c.LambdaApplicationSpec
case KindECSApp:
c.ECSApplicationSpec = &ECSApplicationSpec{}
c.spec = c.ECSApplicationSpec
case KindPiped:
c.PipedSpec = &PipedSpec{}
c.spec = c.PipedSpec
case KindControlPlane:
c.ControlPlaneSpec = &ControlPlaneSpec{}
c.spec = c.ControlPlaneSpec
case KindAnalysisTemplate:
c.AnalysisTemplateSpec = &AnalysisTemplateSpec{}
c.spec = c.AnalysisTemplateSpec
case KindEventWatcher:
c.EventWatcherSpec = &EventWatcherSpec{}
c.spec = c.EventWatcherSpec
default:
return fmt.Errorf("unsupported kind: %s", c.Kind)
}
return nil
}
// UnmarshalJSON customizes the way to unmarshal json data into Config struct.
// Firstly, this unmarshal to a generic config and then unmarshal the spec
// which depend on the kind of configuration.
func (c *Config) UnmarshalJSON(data []byte) error {
var (
err error
gc = genericConfig{}
)
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
if err := dec.Decode(&gc); err != nil {
return err
}
if err = c.init(gc.Kind, gc.APIVersion); err != nil {
return err
}
if len(gc.Spec) > 0 {
dec := json.NewDecoder(bytes.NewReader(gc.Spec))
dec.DisallowUnknownFields()
err = dec.Decode(c.spec)
}
return err
}
type validator interface {
Validate() error
}
// Validate validates the value of all fields.
func (c *Config) Validate() error {
if c.APIVersion != versionV1Beta1 {
return fmt.Errorf("unsupported version: %s", c.APIVersion)
}
if c.Kind == "" {
return fmt.Errorf("kind is required")
}
if c.spec == nil {
return fmt.Errorf("spec is required")
}
spec, ok := c.spec.(validator)
if !ok {
return fmt.Errorf("spec must have Validate function")
}
if err := spec.Validate(); err != nil {
return err
}
return nil
}
// LoadFromYAML reads and decodes a yaml file to construct the Config.
func LoadFromYAML(file string) (*Config, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return DecodeYAML(data)
}
// DecodeYAML unmarshals config YAML data to config struct.
// It also validates the configuration after decoding.
func DecodeYAML(data []byte) (*Config, error) {
js, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
c := &Config{}
if err := json.Unmarshal(js, c); err != nil {
return nil, err
}
if err := defaults.Set(c); err != nil {
return nil, err
}
if err := c.Validate(); err != nil {
return nil, err
}
return c, nil
}
// ToApplicationKind converts configuration kind to application kind.
func (k Kind) ToApplicationKind() (model.ApplicationKind, bool) {
switch k {
case KindKubernetesApp:
return model.ApplicationKind_KUBERNETES, true
case KindTerraformApp:
return model.ApplicationKind_TERRAFORM, true
case KindLambdaApp:
return model.ApplicationKind_LAMBDA, true
case KindCloudRunApp:
return model.ApplicationKind_CLOUDRUN, true
case KindECSApp:
return model.ApplicationKind_ECS, true
}
return model.ApplicationKind_KUBERNETES, false
}
func (c *Config) GetGenericApplication() (GenericApplicationSpec, bool) {
switch c.Kind {
case KindKubernetesApp:
return c.KubernetesApplicationSpec.GenericApplicationSpec, true
case KindTerraformApp:
return c.TerraformApplicationSpec.GenericApplicationSpec, true
case KindCloudRunApp:
return c.CloudRunApplicationSpec.GenericApplicationSpec, true
case KindLambdaApp:
return c.LambdaApplicationSpec.GenericApplicationSpec, true
case KindECSApp:
return c.ECSApplicationSpec.GenericApplicationSpec, true
}
return GenericApplicationSpec{}, false
}