-
Notifications
You must be signed in to change notification settings - Fork 3
/
resources.go
120 lines (103 loc) · 2.99 KB
/
resources.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
/**
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you 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 http://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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package types
import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/adobe/aquarium-fish/lib/util"
)
func (r Resources) GormDataType() string {
return "blob"
}
func (r *Resources) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("Failed to unmarshal JSONB value: %s", value)
}
err := json.Unmarshal(bytes, r)
return err
}
func (r Resources) Value() (driver.Value, error) {
return json.Marshal(r)
}
func (r *Resources) Validate(disk_types []string, check_net bool) error {
// Check resources
if r.Cpu < 1 {
return fmt.Errorf("Driver: Number of CPU cores is less then 1")
}
if r.Ram < 1 {
return fmt.Errorf("Driver: Amount of RAM is less then 1GB")
}
for name, disk := range r.Disks {
if name == "" {
return fmt.Errorf("Driver: Disk name can't be empty")
}
if len(disk_types) > 0 && !util.Contains(disk_types, disk.Type) {
return fmt.Errorf(fmt.Sprintf("Driver: Type of disk must be one of: %+q", disk_types))
}
if disk.Size < 1 {
return fmt.Errorf("Driver: Size of the disk can't be less than 1GB")
}
}
if check_net && r.Network != "" && r.Network != "nat" {
return fmt.Errorf("Driver: The network configuration must be either '' (empty for hostonly) or 'nat'")
}
return nil
}
// Adds the Resources data to the existing data
func (r *Resources) Add(res Resources) error {
if r.Cpu == 0 && r.Ram == 0 {
// Set tenancy modificators for the first resource
r.Multitenancy = res.Multitenancy
r.CpuOverbook = res.CpuOverbook
r.RamOverbook = res.RamOverbook
}
// Set the used CPU & RAM
r.Cpu += res.Cpu
r.Ram += res.Ram
// TODO: Process disk too
return nil
}
// Subtracts the Resources data to the existing data
func (r *Resources) Subtract(res Resources) (err error) {
if r.Cpu < res.Cpu {
err = fmt.Errorf("Driver: Unable to subtract more CPU than we have: %d < %d", r.Cpu, res.Cpu)
r.Cpu = 0
} else {
r.Cpu -= res.Cpu
}
if r.Ram < res.Ram {
mem_err := fmt.Errorf("Driver: Unable to subtract more RAM than we have: %d < %d", r.Ram, res.Ram)
if err != nil {
err = fmt.Errorf("%v, %v", err, mem_err)
}
r.Ram = 0
} else {
r.Ram -= res.Ram
}
// TODO: Process disk too
return
}
// Checks if the Resources are filled with some values
func (r *Resources) IsEmpty() bool {
if r.Cpu != 0 {
return false
}
if r.Ram != 0 {
return false
}
if len(r.Disks) > 0 {
return false
}
return true
}