This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
resource.go
189 lines (164 loc) · 4.29 KB
/
resource.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
// 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
//
// https://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.
//
// Original source: github.com/micro/go-micro/v3/runtime/resource.go
// Package runtime is a service runtime manager
package runtime
import "fmt"
const (
TypeNamespace = "namespace"
TypeNetworkPolicy = "networkpolicy"
TypeResourceQuota = "resourcequota"
TypeService = "service"
)
var (
defaultRequests = &Resources{
Mem: 2000, // 2GB
Disk: 5000, // 5GB
}
defaultLimits = &Resources{
Mem: 4000, // 4GB
Disk: 10000, // 10GB
}
)
// Resource represents any resource handled by runtime
type Resource interface {
String() string
Type() string
}
// Namespace represents a logical namespace for organising resources
type Namespace struct {
// Name of the namespace
Name string
}
// NewNamespace mints a new namespace
func NewNamespace(name string) (*Namespace, error) {
if name == "" {
return nil, ErrInvalidResource
}
return &Namespace{
Name: name,
}, nil
}
// String implements Resource
func (r *Namespace) String() string {
return r.Name
}
// Type implements Resource
func (*Namespace) Type() string {
return TypeNamespace
}
// NetworkPolicy represents an ACL of label pairs allowing ignress to a namespace
type NetworkPolicy struct {
// The labels allowed ingress by this policy
AllowedLabels map[string]string
// Name of the network policy
Name string
// Namespace the network policy belongs to
Namespace string
}
// NewNetworkPolicy mints a new networkpolicy
func NewNetworkPolicy(name, namespace string, allowedLabels map[string]string) (*NetworkPolicy, error) {
if name == "" || namespace == "" {
return nil, ErrInvalidResource
}
if allowedLabels == nil {
allowedLabels = map[string]string{
"origin": "micro",
}
}
return &NetworkPolicy{
AllowedLabels: allowedLabels,
Name: name,
Namespace: namespace,
}, nil
}
// String implements Resource
func (r *NetworkPolicy) String() string {
return fmt.Sprintf("%s.%s", r.Namespace, r.Name)
}
// Type implements Resource
func (*NetworkPolicy) Type() string {
return TypeNetworkPolicy
}
// ResourceQuota represents an ACL of label pairs allowing ignress to a namespace
type ResourceQuota struct {
// Name of the resource quota
Name string
// Namespace the resource quota belongs to
Namespace string
// Quota for resource REQUESTS
Requests *Resources
// Quota for resource LIMITS
Limits *Resources
}
// NewResourceQuota mints a new resourcequota
func NewResourceQuota(name, namespace string, requests, limits *Resources) (*ResourceQuota, error) {
if name == "" || namespace == "" {
return nil, ErrInvalidResource
}
rq := &ResourceQuota{
Name: name,
Namespace: namespace,
Requests: defaultRequests,
Limits: defaultLimits,
}
if requests != nil {
rq.Requests = requests
}
if limits != nil {
rq.Limits = limits
}
return rq, nil
}
// String implements Resource
func (r *ResourceQuota) String() string {
return fmt.Sprintf("%s.%s", r.Namespace, r.Name)
}
// Type implements Resource
func (*ResourceQuota) Type() string {
return TypeResourceQuota
}
// Service represents a Micro service running within a namespace
type Service struct {
// Name of the service
Name string
// Version of the service
Version string
// url location of source
Source string
// Metadata stores metadata
Metadata map[string]string
// Status of the service
Status ServiceStatus
}
// NewService mints a new service
func NewService(name, version string) (*Service, error) {
if name == "" {
return nil, ErrInvalidResource
}
if version == "" {
version = "latest"
}
return &Service{
Name: name,
Version: version,
}, nil
}
// String implements Resource
func (r *Service) String() string {
return fmt.Sprintf("service://%s@%s:%s", r.Metadata["namespace"], r.Name, r.Version)
}
// Type implements Resource
func (*Service) Type() string {
return TypeService
}