github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/resource.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/runtime/resource.go 14 15 // Package runtime is a service runtime manager 16 package runtime 17 18 import "fmt" 19 20 const ( 21 TypeNamespace = "namespace" 22 TypeNetworkPolicy = "networkpolicy" 23 TypeResourceQuota = "resourcequota" 24 TypeService = "service" 25 ) 26 27 var ( 28 defaultRequests = &Resources{ 29 Mem: 2000, // 2GB 30 Disk: 5000, // 5GB 31 } 32 defaultLimits = &Resources{ 33 Mem: 4000, // 4GB 34 Disk: 10000, // 10GB 35 } 36 ) 37 38 // Resource represents any resource handled by runtime 39 type Resource interface { 40 String() string 41 Type() string 42 } 43 44 // Namespace represents a logical namespace for organising resources 45 type Namespace struct { 46 // Name of the namespace 47 Name string 48 } 49 50 // NewNamespace mints a new namespace 51 func NewNamespace(name string) (*Namespace, error) { 52 if name == "" { 53 return nil, ErrInvalidResource 54 } 55 return &Namespace{ 56 Name: name, 57 }, nil 58 } 59 60 // String implements Resource 61 func (r *Namespace) String() string { 62 return r.Name 63 } 64 65 // Type implements Resource 66 func (*Namespace) Type() string { 67 return TypeNamespace 68 } 69 70 // NetworkPolicy represents an ACL of label pairs allowing ignress to a namespace 71 type NetworkPolicy struct { 72 // The labels allowed ingress by this policy 73 AllowedLabels map[string]string 74 // Name of the network policy 75 Name string 76 // Namespace the network policy belongs to 77 Namespace string 78 } 79 80 // NewNetworkPolicy mints a new networkpolicy 81 func NewNetworkPolicy(name, namespace string, allowedLabels map[string]string) (*NetworkPolicy, error) { 82 if name == "" || namespace == "" { 83 return nil, ErrInvalidResource 84 } 85 if allowedLabels == nil { 86 allowedLabels = map[string]string{ 87 "origin": "micro", 88 } 89 } 90 return &NetworkPolicy{ 91 AllowedLabels: allowedLabels, 92 Name: name, 93 Namespace: namespace, 94 }, nil 95 } 96 97 // String implements Resource 98 func (r *NetworkPolicy) String() string { 99 return fmt.Sprintf("%s.%s", r.Namespace, r.Name) 100 } 101 102 // Type implements Resource 103 func (*NetworkPolicy) Type() string { 104 return TypeNetworkPolicy 105 } 106 107 // ResourceQuota represents an ACL of label pairs allowing ignress to a namespace 108 type ResourceQuota struct { 109 // Name of the resource quota 110 Name string 111 // Namespace the resource quota belongs to 112 Namespace string 113 // Quota for resource REQUESTS 114 Requests *Resources 115 // Quota for resource LIMITS 116 Limits *Resources 117 } 118 119 // NewResourceQuota mints a new resourcequota 120 func NewResourceQuota(name, namespace string, requests, limits *Resources) (*ResourceQuota, error) { 121 if name == "" || namespace == "" { 122 return nil, ErrInvalidResource 123 } 124 125 rq := &ResourceQuota{ 126 Name: name, 127 Namespace: namespace, 128 Requests: defaultRequests, 129 Limits: defaultLimits, 130 } 131 132 if requests != nil { 133 rq.Requests = requests 134 } 135 136 if limits != nil { 137 rq.Limits = limits 138 } 139 140 return rq, nil 141 } 142 143 // String implements Resource 144 func (r *ResourceQuota) String() string { 145 return fmt.Sprintf("%s.%s", r.Namespace, r.Name) 146 } 147 148 // Type implements Resource 149 func (*ResourceQuota) Type() string { 150 return TypeResourceQuota 151 } 152 153 // Service represents a Micro service running within a namespace 154 type Service struct { 155 // Name of the service 156 Name string 157 // Version of the service 158 Version string 159 // url location of source 160 Source string 161 // Metadata stores metadata 162 Metadata map[string]string 163 // Status of the service 164 Status ServiceStatus 165 } 166 167 // NewService mints a new service 168 func NewService(name, version string) (*Service, error) { 169 if name == "" { 170 return nil, ErrInvalidResource 171 } 172 if version == "" { 173 version = "latest" 174 } 175 return &Service{ 176 Name: name, 177 Version: version, 178 }, nil 179 } 180 181 // String implements Resource 182 func (r *Service) String() string { 183 return fmt.Sprintf("service://%s@%s:%s", r.Metadata["namespace"], r.Name, r.Version) 184 } 185 186 // Type implements Resource 187 func (*Service) Type() string { 188 return TypeService 189 }