-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathamd_gpu_operator.go
254 lines (225 loc) · 7.07 KB
/
amd_gpu_operator.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package amdgpu
import (
"context"
"strings"
"text/template"
"github.com/kelseyhightower/envconfig"
"github.com/lib/pq"
"github.com/openshift/assisted-service/internal/common"
"github.com/openshift/assisted-service/internal/operators/api"
operatorscommon "github.com/openshift/assisted-service/internal/operators/common"
"github.com/openshift/assisted-service/internal/templating"
"github.com/openshift/assisted-service/models"
"github.com/sirupsen/logrus"
)
// amdVendorID is the PCI vendor identifier of AMD devices.
const amdVendorID = "1002"
var Operator = models.MonitoredOperator{
Namespace: "kube-amd-gpu",
Name: "amd-gpu",
OperatorType: models.OperatorTypeOlm,
SubscriptionName: "amd-gpu-operator",
TimeoutSeconds: 30 * 60,
Bundles: pq.StringArray{
operatorscommon.BundleOpenShiftAIAMD.ID,
},
}
// operator is an AMD GPU OLM operator plugin.
type operator struct {
log logrus.FieldLogger
config *Config
templates *template.Template
}
// NewAMDGPUOperator creates new AMD GPU operator.
func NewAMDGPUOperator(log logrus.FieldLogger) *operator {
config := &Config{}
err := envconfig.Process(common.EnvConfigPrefix, config)
if err != nil {
log.Fatal(err.Error())
}
templates, err := templating.LoadTemplates(templatesRoot)
if err != nil {
log.Fatal(err.Error())
}
return &operator{
log: log,
config: config,
templates: templates,
}
}
// GetName reports the name of an operator.
func (o *operator) GetName() string {
return Operator.Name
}
func (o *operator) GetFullName() string {
return "AMD GPU"
}
// GetDependencies provides a list of dependencies of the Operator
func (o *operator) GetDependencies(c *common.Cluster) ([]string, error) {
result := []string{}
return result, nil
}
// GetClusterValidationID returns cluster validation ID for the operator.
func (o *operator) GetClusterValidationID() string {
return string(models.ClusterValidationIDAmdGpuRequirementsSatisfied)
}
// GetHostValidationID returns host validation ID for the operator.
func (o *operator) GetHostValidationID() string {
return string(models.HostValidationIDAmdGpuRequirementsSatisfied)
}
// ValidateCluster checks if the cluster satisfies the requirements to install the operator.
func (o *operator) ValidateCluster(ctx context.Context, cluster *common.Cluster) (result api.ValidationResult,
err error) {
result.ValidationId = o.GetClusterValidationID()
result = api.ValidationResult{
Status: api.Success,
ValidationId: o.GetClusterValidationID(),
}
// Check that there is at least one supported GPU:
if o.config.RequireGPU {
var gpuList []*models.Gpu
gpuList, err = o.gpusInCluster(cluster)
if err != nil {
return
}
var supportedGpuCount int64
for _, gpu := range gpuList {
if o.isSupportedGpu(gpu) {
supportedGpuCount++
}
}
if supportedGpuCount == 0 {
result.Reasons = append(
result.Reasons,
"The AMD GPU operator requires at least one supported AMD GPU, but there is none in "+
"the discovered hosts.",
)
}
}
if len(result.Reasons) > 0 {
result.Status = api.Failure
}
return
}
func (o *operator) gpusInCluster(cluster *common.Cluster) (result []*models.Gpu, err error) {
for _, host := range cluster.Hosts {
var gpus []*models.Gpu
gpus, err = o.gpusInHost(host)
if err != nil {
return
}
result = append(result, gpus...)
}
return
}
func (o *operator) gpusInHost(host *models.Host) (result []*models.Gpu, err error) {
if host.Inventory == "" {
return
}
inventory, err := common.UnmarshalInventory(host.Inventory)
if err != nil {
return
}
result = inventory.Gpus
return
}
func (o *operator) isSupportedGpu(gpu *models.Gpu) bool {
for _, supportedGpu := range o.config.SupportedGPUs {
if strings.EqualFold(gpu.VendorID, supportedGpu) {
return true
}
}
return false
}
// ValidateHost returns validationResult based on node type requirements such as memory and CPU.
func (o *operator) ValidateHost(ctx context.Context, cluster *common.Cluster, host *models.Host,
hostRequirements *models.ClusterHostRequirementsDetails) (result api.ValidationResult, err error) {
result = api.ValidationResult{
Status: api.Success,
ValidationId: o.GetHostValidationID(),
}
// Get the inventory:
if host.Inventory == "" {
result.Status = api.Pending
result.Reasons = []string{
"Missing inventory in the host",
}
return
}
inventory, err := common.UnmarshalInventory(host.Inventory)
if err != nil {
result.Status = api.Pending
result.Reasons = []string{
"Failed to get inventory from host",
}
return
}
// Secure boot must be disabled if there are AMD GPUs in the node, as otherwise it won't be possible to load
// AMD drivers.
if o.hasAMDGPU(inventory) && o.isSecureBootEnabled(inventory) {
result.Status = api.Failure
result.Reasons = append(
result.Reasons,
"Secure boot is enabled, but it needs to be disabled in order to load AMD GPU drivers",
)
return
}
return
}
func (o *operator) hasAMDGPU(inventory *models.Inventory) bool {
for _, gpu := range inventory.Gpus {
if gpu.VendorID == amdVendorID {
return true
}
}
return false
}
func (o *operator) isSecureBootEnabled(inventory *models.Inventory) bool {
return inventory.Boot != nil && inventory.Boot.SecureBootState == models.SecureBootStateEnabled
}
// GetProperties provides description of operator properties.
func (o *operator) GetProperties() models.OperatorProperties {
return models.OperatorProperties{}
}
// GetMonitoredOperator returns the information that describes how to monitor the operator.
func (o *operator) GetMonitoredOperator() *models.MonitoredOperator {
return &Operator
}
// GetHostRequirements provides the requirements that the host needs to satisfy in order to be able to install the
// operator.
func (o *operator) GetHostRequirements(ctx context.Context, cluster *common.Cluster,
host *models.Host) (*models.ClusterHostRequirementsDetails, error) {
preflightRequirements, err := o.GetPreflightRequirements(ctx, cluster)
if err != nil {
o.log.WithError(err).Errorf("Cannot retrieve preflight requirements for cluster %s", cluster.ID)
return nil, err
}
return preflightRequirements.Requirements.Worker.Quantitative, nil
}
// GetPreflightRequirements returns operator hardware requirements that can be determined with cluster data only.
func (o *operator) GetPreflightRequirements(context context.Context,
cluster *common.Cluster) (result *models.OperatorHardwareRequirements, err error) {
dependencies, err := o.GetDependencies(cluster)
if err != nil {
return
}
result = &models.OperatorHardwareRequirements{
OperatorName: o.GetName(),
Dependencies: dependencies,
Requirements: &models.HostTypeHardwareRequirementsWrapper{
Master: &models.HostTypeHardwareRequirements{
Quantitative: &models.ClusterHostRequirementsDetails{},
},
Worker: &models.HostTypeHardwareRequirements{
Quantitative: &models.ClusterHostRequirementsDetails{},
},
},
}
return
}
func (o *operator) GetFeatureSupportID() models.FeatureSupportLevelID {
return models.FeatureSupportLevelIDAMDGPU
}
func (o *operator) GetBundleLabels() []string {
return []string(Operator.Bundles)
}