/
app_deployment_types.go
221 lines (185 loc) · 6.96 KB
/
app_deployment_types.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
// Copyright 2023 XigXog
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0
package v1alpha1
import (
"errors"
"fmt"
"github.com/xigxog/kubefox/api"
common "github.com/xigxog/kubefox/api/kubernetes"
"github.com/xigxog/kubefox/core"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// AppDeploymentSpec defines the desired state of AppDeployment
type AppDeploymentSpec struct {
// +kubebuilder:validation:Required
AppName string `json:"appName"`
// Version of the defined App. Use of semantic versioning is recommended.
// Once set the AppDeployment spec becomes immutable.
Version string `json:"version,omitempty"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern="^[a-z0-9]{40}$"
Commit string `json:"commit"`
// +kubebuilder:validation:Required
CommitTime metav1.Time `json:"commitTime"`
Branch string `json:"branch,omitempty"`
Tag string `json:"tag,omitempty"`
RepoURL string `json:"repoURL,omitempty"`
ContainerRegistry string `json:"containerRegistry,omitempty"`
ImagePullSecretName string `json:"imagePullSecretName,omitempty"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinProperties=1
Components map[string]*api.ComponentDefinition `json:"components"`
}
// AppDeploymentStatus defines the observed state of AppDeployment
type AppDeploymentStatus struct {
// +patchStrategy=merge
// +patchMergeKey=type
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
Problems api.Problems `json:"problems,omitempty"`
}
// AppDeploymentDetails defines additional details of AppDeployment
type AppDeploymentDetails struct {
api.Details `json:",inline"`
Components map[string]api.Details `json:"components,omitempty"`
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=appdeployments,shortName=appdep;appdeps;app;apps
// +kubebuilder:printcolumn:name="App",type=string,JSONPath=`.spec.appName`
// +kubebuilder:printcolumn:name="Version",type=string,JSONPath=`.spec.version`
// +kubebuilder:printcolumn:name="Available",type=string,JSONPath=`.status.conditions[?(@.type=='Available')].status`
// +kubebuilder:printcolumn:name="Reason",type=string,JSONPath=`.status.conditions[?(@.type=='Available')].reason`
// +kubebuilder:printcolumn:name="Progressing",type=string,JSONPath=`.status.conditions[?(@.type=='Progressing')].status`
// AppDeployment is the Schema for the AppDeployments API
type AppDeployment struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec AppDeploymentSpec `json:"spec,omitempty"`
Status AppDeploymentStatus `json:"status,omitempty"`
Details AppDeploymentDetails `json:"details,omitempty"`
}
//+kubebuilder:object:root=true
// AppDeploymentList contains a list of AppDeployments
type AppDeploymentList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []AppDeployment `json:"items"`
}
func (d *AppDeployment) Validate(data *api.Data, getAdapter common.GetAdapterFunc) (api.Problems, error) {
problems := api.Problems{}
for compName, comp := range d.Spec.Components {
if data != nil {
problems = append(problems, comp.EnvVarSchema.Validate("Var", data.Vars, &api.ProblemSource{
Kind: api.ProblemSourceKindAppDeployment,
Name: d.Name,
ObservedGeneration: d.Generation,
Path: fmt.Sprintf("$.spec.components.%s.envVarSchema", compName),
}, true)...)
for i, route := range comp.Routes {
// All route vars are required.
for _, d := range route.EnvVarSchema {
d.Required = true
}
problems = append(problems, route.EnvVarSchema.Validate("Var", data.Vars, &api.ProblemSource{
Kind: api.ProblemSourceKindAppDeployment,
Name: d.Name,
ObservedGeneration: d.Generation,
Path: fmt.Sprintf("$.spec.components.%s.routes[%d]", compName, i),
}, true)...)
}
}
for depName, dep := range comp.Dependencies {
found := true
switch {
case dep.Type == api.ComponentTypeKubeFox:
_, found = d.Spec.Components[depName]
case dep.Type.IsAdapter():
adapter, err := getAdapter(depName, dep.Type)
switch {
case err == nil:
if data != nil {
problems = append(problems, adapter.Validate(data)...)
}
case apierrors.IsNotFound(err) || errors.Is(err, core.ErrNotFound()):
found = false
default:
return nil, err
}
default:
// Unsupported dependency type.
problems = append(problems, api.Problem{
Type: api.ProblemTypeDependencyInvalid,
Message: fmt.Sprintf(`Component "%s" dependency "%s" has unsupported type "%s".`,
compName, depName, dep.Type),
Causes: []api.ProblemSource{
{
Kind: api.ProblemSourceKindAppDeployment,
Name: d.Name,
ObservedGeneration: d.Generation,
Path: fmt.Sprintf("$.spec.components.%s.dependencies.%s.type",
compName, depName),
Value: (*string)(&dep.Type),
},
},
})
}
if !found {
problems = append(problems, api.Problem{
Type: api.ProblemTypeDependencyNotFound,
Message: fmt.Sprintf(`Component "%s" dependency "%s" of type "%s" not found.`,
compName, depName, dep.Type),
Causes: []api.ProblemSource{
{
Kind: api.ProblemSourceKindAppDeployment,
Name: d.Name,
ObservedGeneration: d.Generation,
Path: fmt.Sprintf("$.spec.components.%s.dependencies.%s",
compName, depName),
},
},
})
}
}
}
return problems, nil
}
func (a *AppDeployment) GetDefinition(comp *core.Component) (*api.ComponentDefinition, error) {
if comp == nil {
return nil, core.ErrComponentMismatch(fmt.Errorf("component not part of app"))
}
if comp.App != "" && comp.App != a.Spec.AppName {
return nil, core.ErrComponentMismatch(fmt.Errorf("component not part of app"))
}
c, found := a.Spec.Components[comp.Name]
if !found {
return nil, core.ErrComponentMismatch(fmt.Errorf("component not part of app"))
}
if comp.Type != string(c.Type) {
return nil, core.ErrComponentMismatch(fmt.Errorf("component type does not match app"))
}
if comp.Hash != "" && comp.Hash != c.Hash {
return nil, core.ErrComponentMismatch(fmt.Errorf("component hash does not match app"))
}
return c, nil
}
func (a *AppDeployment) HasDependency(name string, typ api.ComponentType) bool {
for _, c := range a.Spec.Components {
for n, d := range c.Dependencies {
if name == n && typ == d.Type {
return true
}
}
}
return false
}
func init() {
SchemeBuilder.Register(&AppDeployment{}, &AppDeploymentList{})
}