Newer
Older
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
/*
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.html
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 components
import (
settings "k8s.libre.sh/application/settings"
meta "k8s.libre.sh/meta"
"k8s.libre.sh/objects"
deployment "k8s.libre.sh/objects/deployment"
corev1 "k8s.io/api/core/v1"
)
const DefaultComponent string = "app"
// +kubebuilder:object:generate=true
type Workload struct {
*meta.ObjectMeta `json:"commonMeta,omitempty"`
*deployment.Deployment `json:"deployment,omitempty"`
*Network `json:"network,omitempty"`
Dependencies bool `json:"dependencies,omitempty"`
}
func (w *Workload) HasDependencies() bool {
return w.Dependencies
}
func (w *Workload) GetMeta() meta.Instance {
return w.ObjectMeta
}
func (w *Workload) GetObjects() map[int]objects.Object {
objects := make(map[int]objects.Object)
ing := &Ingress{
// TODO TO FIX
ObjectMeta: w.IngressMeta,
Backends: []Backend{
*w.Backend,
},
}
srv := &Service{
// TODO TO FIX
ObjectMeta: w.ServiceMeta,
Backend: w.Backend,
}
meta.SetObjectMeta(w.ObjectMeta, ing.ObjectMeta)
meta.SetObjectMeta(w.ObjectMeta, srv.ObjectMeta)
objects[0] = srv
objects[1] = ing
objects[2] = w.Deployment
return objects
}
func (w *Workload) SetDependencies(s *settings.Component) { w.ConfigSpec = s.ConfigSpec }
func (w *Workload) GetDependencies() settings.Config { return w.ConfigSpec }
func (w *Workload) SetDefaults() {
// TODO FIXME
w.Dependencies = true
w.Backend.ServiceName = w.ServiceMeta.GetName()
containerPort := corev1.ContainerPort{
Name: w.Backend.Port.Name,
ContainerPort: w.Backend.Port.Port,
Protocol: w.Backend.Port.Protocol,
}
w.ContainerSpec.Ports = append(w.ContainerSpec.Ports, containerPort)
}
func (w *Workload) Init() {
// TODO improve initialisation of objectMeta, labels...
if w.ObjectMeta == nil {
w.ObjectMeta = &meta.ObjectMeta{}
}
if len(w.ObjectMeta.Labels) == 0 {
w.ObjectMeta.Labels = make(map[string]string)
}
// TODO FIXME
if len(w.ObjectMeta.GetComponent()) == 0 {
w.ObjectMeta.SetComponent(DefaultComponent)
}
if w.Deployment.ObjectMeta == nil {
w.Deployment.ObjectMeta = &meta.ObjectMeta{}
}
if w.ConfigSpec == nil {
w.ConfigSpec = &settings.ConfigSpec{}
}
if w.Backend == nil {
w.Backend = &Backend{}
}
if w.ServiceMeta == nil {
w.ServiceMeta = &meta.ObjectMeta{}
}
if w.IngressMeta == nil {
w.IngressMeta = &meta.ObjectMeta{}
}
}