-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathclass.go
More file actions
180 lines (165 loc) · 5.87 KB
/
class.go
File metadata and controls
180 lines (165 loc) · 5.87 KB
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
/*
* === This file is part of ALICE O² ===
*
* Copyright 2018-2023 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package taskclass
import (
"fmt"
"time"
"github.com/AliceO2Group/Control/common"
"github.com/AliceO2Group/Control/common/controlmode"
"github.com/AliceO2Group/Control/common/gera"
"github.com/AliceO2Group/Control/common/logger"
"github.com/AliceO2Group/Control/core/task/channel"
"github.com/AliceO2Group/Control/core/task/constraint"
"github.com/sirupsen/logrus"
)
var log = logger.New(logrus.StandardLogger(), "taskclass")
type Id struct {
RepoIdentifier string
Hash string
Name string
}
func (tcID Id) String() string {
return fmt.Sprintf("%s/tasks/%s@%s", tcID.RepoIdentifier, tcID.Name, tcID.Hash)
}
func (tcID *Id) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
err = unmarshal(&tcID.Name)
return
}
// ↓ We need the roles tree to know *where* to run it and how to *configure* it, but
//
// the following information is enough to run the task even with no environment or
// role Class.
type Class struct {
Identifier Id `yaml:"name"`
Defaults gera.Map[string, string] `yaml:"defaults"`
Vars gera.Map[string, string] `yaml:"vars"`
Control struct {
Mode controlmode.ControlMode `yaml:"mode"`
} `yaml:"control"`
Command *common.CommandInfo `yaml:"command"`
Wants ResourceWants `yaml:"wants"`
Limits *ResourceLimits `yaml:"limits"`
Bind []channel.Inbound `yaml:"bind"`
Properties gera.Map[string, string] `yaml:"properties"`
Constraints []constraint.Constraint `yaml:"constraints"`
Connect []channel.Outbound `yaml:"connect"`
UpdatedTimestamp time.Time `yaml:"-"`
}
func (c *Class) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
// We need to make a fake type to unmarshal into because
// gera.Map is an interface
type _class struct {
Identifier Id `yaml:"name"`
Defaults map[string]string `yaml:"defaults"`
Vars map[string]string `yaml:"vars"`
Control struct {
Mode controlmode.ControlMode `yaml:"mode"`
} `yaml:"control"`
Command *common.CommandInfo `yaml:"command"`
Wants ResourceWants `yaml:"wants"`
Limits *ResourceLimits `yaml:"limits"`
Bind []channel.Inbound `yaml:"bind"`
Properties map[string]string `yaml:"properties"`
Constraints []constraint.Constraint `yaml:"constraints"`
Connect []channel.Outbound `yaml:"connect"`
}
aux := _class{
Defaults: make(map[string]string),
Vars: make(map[string]string),
Properties: make(map[string]string),
}
err = unmarshal(&aux)
if err == nil {
for j, ch := range aux.Connect {
if ch.Target != "" {
ch.Target = ""
aux.Connect[j] = ch
log.Warn("task template outbound channel definition has a target (will be ignored)")
}
}
*c = Class{
Identifier: aux.Identifier,
Defaults: gera.MakeMapWithMap(aux.Defaults),
Vars: gera.MakeMapWithMap(aux.Vars),
Control: aux.Control,
Command: aux.Command,
Wants: aux.Wants,
Limits: aux.Limits,
Bind: aux.Bind,
Properties: gera.MakeMapWithMap(aux.Properties),
Constraints: aux.Constraints,
Connect: aux.Connect,
UpdatedTimestamp: time.Now(),
}
}
return
}
func (c *Class) MarshalYAML() (interface{}, error) {
type _class struct {
Name string `yaml:"name"`
Defaults map[string]string `yaml:"defaults,omitempty"`
Vars map[string]string `yaml:"vars,omitempty"`
Control struct {
Mode string `yaml:"mode"`
} `yaml:"control"`
Wants ResourceWants `yaml:"wants"`
Limits *ResourceLimits `yaml:"limits,omitempty"`
Bind []channel.Inbound `yaml:"bind,omitempty"`
Properties map[string]string `yaml:"properties,omitempty"`
Constraints []constraint.Constraint `yaml:"constraints,omitempty"`
Command *common.CommandInfo `yaml:"command"`
}
aux := _class{
Name: c.Identifier.Name,
Defaults: c.Defaults.RawCopy(),
Vars: c.Vars.RawCopy(),
Properties: c.Properties.RawCopy(),
Wants: c.Wants,
Limits: c.Limits,
Bind: c.Bind,
Constraints: c.Constraints,
Command: c.Command,
}
// if c.Control.Mode == controlmode.FAIRMQ {
// aux.Control.Mode = "fairmq"
// } else if c.Control.Mode == controlmode.BASIC {
// aux.Control.Mode = "basic"
// } else if c.Control.Mode == controlmode.KUBECTL {
// aux.Control.Mode = "kubectl"
// } else {
// aux.Control.Mode = "direct"
// }
aux.Control.Mode = c.Control.Mode.String()
return aux, nil
}
func (c *Class) Equals(other *Class) (response bool) {
if c == nil || other == nil {
return false
}
response = c.Command.Equals(other.Command) &&
*c.Wants.Cpu == *other.Wants.Cpu &&
*c.Wants.Memory == *other.Wants.Memory &&
c.Wants.Ports.Equals(other.Wants.Ports)
return
}