-
Notifications
You must be signed in to change notification settings - Fork 124
/
service.go
209 lines (172 loc) · 5.14 KB
/
service.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
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package types
import (
"fmt"
"github.com/lastbackend/lastbackend/pkg/util/resource"
)
const (
DEFAULT_SERVICE_MEMORY int64 = 128
DEFAULT_SERVICE_REPLICAS int = 1
)
type Service struct {
Runtime
Meta ServiceMeta `json:"meta"`
Status ServiceStatus `json:"status"`
Spec ServiceSpec `json:"spec"`
}
type ServiceMap struct {
Runtime
Items map[string]*Service
}
type ServiceList struct {
Runtime
Items []*Service
}
type ServiceMeta struct {
Meta
Namespace string `json:"namespace"`
SelfLink string `json:"self_link"`
Endpoint string `json:"endpoint"`
IP string `json:"ip"`
}
type ServiceEndpoint struct {
Name string `json:"name"`
Main bool `json:"main"`
}
type ServiceStatus struct {
State string `json:"state"`
Message string `json:"message"`
Network ServiceStatusNetwork `json:"network"`
}
type ServiceSpec struct {
Replicas int `json:"replicas" yaml:"replicas"`
State SpecState `json:"state" yaml:"state"`
Network SpecNetwork `json:"network" yaml:"network" `
Strategy SpecStrategy `json:"strategy" yaml:"strategy"`
Selector SpecSelector `json:"selector" yaml:"selector"`
Template SpecTemplate `json:"template" yaml:"template"`
}
type ServiceStatusNetwork struct {
IP string `json:"ip"`
}
type ServiceSpecStrategy struct {
Type string `json:"type"` // Rolling
RollingOptions ServiceSpecStrategyRollingOptions `json:"rollingOptions"`
Resources ServiceSpecStrategyResources `json:"resources"`
Deadline int `json:"deadline"`
}
type ServiceSpecStrategyResources struct{}
type ServiceSpecStrategyRollingOptions struct {
PeriodUpdate int `json:"period_update"`
Interval int `json:"interval"`
Timeout int `json:"timeout"`
MaxUnavailable int `json:"max_unavailable"`
MaxSurge int `json:"max_surge"`
}
type ServiceReplicas struct {
Total int `json:"total"`
Provision int `json:"provision"`
Created int `json:"created"`
Started int `json:"started"`
Stopped int `json:"stopped"`
Errored int `json:"errored"`
}
func (s *ServiceSpec) SetDefault() {
s.Replicas = DEFAULT_SERVICE_REPLICAS
s.Template.Volumes = make(SpecTemplateVolumeList, 0)
s.Template.Containers = make(SpecTemplateContainers, 0)
}
func (s *Service) SelfLink() string {
if s.Meta.SelfLink == "" {
s.Meta.SelfLink = s.CreateSelfLink(s.Meta.Namespace, s.Meta.Name)
}
return s.Meta.SelfLink
}
func (s *Service) CreateSelfLink(namespace, name string) string {
return fmt.Sprintf("%s:%s", namespace, name)
}
type ServiceManifest struct {
Meta ServiceMeta `json:"meta"`
}
type ServiceRemoveOptions struct {
Force bool `json:"force"`
}
type ServiceImageSpec struct {
Name *string `json:"image"`
Secret *string `json:"secret"`
}
type ServiceOptionsSpec struct {
Replicas *int `json:"replicas"`
Memory *int64 `json:"memory,omitempty"`
Entrypoint *string `json:"entrypoint,omitempty"`
Command *string `json:"command,omitempty"`
EnvVars *[]ServiceEnvOption `json:"env,omitempty"`
Ports map[uint16]string `json:"ports,omitempty"`
}
type ServiceEnvOption struct {
Name string `json:"name"`
Value string `json:"value"`
From ServiceEnvFromOption `json:"from"`
}
type ServiceEnvFromOption struct {
Name string `json:"name"`
Key string `json:"key"`
}
func (s *ServiceSpec) GetResourceRequest() ResourceRequest {
rr := ResourceRequest{}
var (
limitsRAM int64
limitsCPU int64
requestRAM int64
requestCPU int64
)
for _, c := range s.Template.Containers {
limitsCPU += c.Resources.Limits.CPU
limitsRAM += c.Resources.Limits.RAM
requestCPU += c.Resources.Request.CPU
requestRAM += c.Resources.Request.RAM
}
if requestRAM > 0 {
requestRAM = int64(s.Replicas) * requestRAM
rr.Request.RAM = resource.EncodeMemoryResource(requestRAM)
}
if requestCPU > 0 {
requestCPU = int64(s.Replicas) * requestCPU
rr.Request.CPU = resource.EncodeCpuResource(requestCPU)
}
if limitsRAM > 0 {
limitsRAM = int64(s.Replicas) * limitsRAM
rr.Limits.RAM = resource.EncodeMemoryResource(limitsRAM)
}
if limitsCPU > 0 {
limitsCPU = int64(s.Replicas) * limitsCPU
rr.Limits.CPU = resource.EncodeCpuResource(limitsCPU)
}
return rr
}
func NewServiceList() *ServiceList {
dm := new(ServiceList)
dm.Items = make([]*Service, 0)
return dm
}
func NewServiceMap() *ServiceMap {
dm := new(ServiceMap)
dm.Items = make(map[string]*Service)
return dm
}