-
Notifications
You must be signed in to change notification settings - Fork 56
/
blockio.go
188 lines (153 loc) · 5.22 KB
/
blockio.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
// Copyright 2019 Intel Corporation. All Rights Reserved.
//
// 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
//
// 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 CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blockio
import (
"errors"
"fmt"
"github.com/intel/cri-resource-manager/pkg/blockio"
"github.com/intel/cri-resource-manager/pkg/config"
"github.com/intel/cri-resource-manager/pkg/cri/client"
"github.com/intel/cri-resource-manager/pkg/cri/resource-manager/cache"
"github.com/intel/cri-resource-manager/pkg/cri/resource-manager/control"
logger "github.com/intel/cri-resource-manager/pkg/log"
)
const (
// BlockIOController is the name of the block I/O controller.
BlockIOController = cache.BlockIO
)
// blockio encapsulates the runtime state of our block I/O enforcement/controller.
type blockioctl struct {
cache cache.Cache // resource manager cache
idle *bool // true if we run without any classes configured
}
// Our logger instance.
var log logger.Logger = logger.NewLogger(BlockIOController)
// Our singleton block I/O controller instance.
var singleton *blockioctl
// getBlockIOController returns our singleton block I/O controller instance.
func getBlockIOController() *blockioctl {
if singleton == nil {
singleton = &blockioctl{}
}
return singleton
}
// Start initializes the controller for enforcing decisions.
func (ctl *blockioctl) Start(cache cache.Cache, client client.Client) error {
ctl.cache = cache
ctl.reconfigureRunningContainers()
return nil
}
// Stop shuts down the controller.
func (ctl *blockioctl) Stop() {
}
// PreCreateHook is the block I/O controller pre-create hook.
func (ctl *blockioctl) PreCreateHook(c cache.Container) error {
return nil
}
// PreStartHook is the block I/O controller pre-start hook.
func (ctl *blockioctl) PreStartHook(c cache.Container) error {
return nil
}
// PostStartHook is the block I/O controller post-start hook.
func (ctl *blockioctl) PostStartHook(c cache.Container) error {
if !c.HasPending(BlockIOController) {
return nil
}
if err := ctl.assign(c); err != nil {
return err
}
c.ClearPending(BlockIOController)
return nil
}
// PostUpdateHook is the block I/O controller post-update hook.
func (ctl *blockioctl) PostUpdateHook(c cache.Container) error {
if !c.HasPending(BlockIOController) {
return nil
}
if err := ctl.assign(c); err != nil {
return err
}
c.ClearPending(BlockIOController)
return nil
}
// PostStop is the block I/O controller post-stop hook.
func (ctl *blockioctl) PostStopHook(c cache.Container) error {
return nil
}
// isImplicitlyDisabled checks if we run without any classes confiured
func (ctl *blockioctl) isImplicitlyDisabled() bool {
if ctl.idle != nil {
return *ctl.idle
}
idle := len(blockio.GetClasses()) == 0
if idle {
log.Warn("controller implictly disabled (no configured classes)")
}
ctl.idle = &idle
return *ctl.idle
}
// assign assigns the container to the given block I/O class.
func (ctl *blockioctl) assign(c cache.Container) error {
class := c.GetBlockIOClass()
if class == "" {
return nil
}
if ctl.isImplicitlyDisabled() && cache.IsPodQOSClassName(class) {
return nil
}
if err := blockio.SetContainerClass(c, class); err != nil {
return blockioError("%q: failed to assign to class %q: %w", c.PrettyName(), class, err)
}
log.Info("%q: assigned to class %q", c.PrettyName(), class)
return nil
}
// configNotify is blockio class mapping and class definition configuration callback
func (ctl *blockioctl) configNotify(event config.Event, source config.Source) error {
ignoreErrors := (event == config.RevertEvent)
err := blockio.UpdateOciConfig(ignoreErrors)
if err != nil {
return err
}
// Possible errors in reconfiguring running containers are not errors in
// the updated configuration, therefore silently ignored.
ctl.reconfigureRunningContainers()
// We'll re-check idleness at next operation/request.
ctl.idle = nil
return nil
}
// reconfigureRunningContainers force setting current blockio configuration to all containers running on the node
func (ctl *blockioctl) reconfigureRunningContainers() error {
errs := []error{}
if ctl.cache == nil {
return nil
}
for _, c := range ctl.cache.GetContainers() {
class := c.GetBlockIOClass()
log.Debug("%q: configure blockio class %q", c.PrettyName(), class)
err := blockio.SetContainerClass(c, class)
if err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// blockioError creates a block I/O-controller-specific formatted error message.
func blockioError(format string, args ...interface{}) error {
return fmt.Errorf("blockio: "+format, args...)
}
// init registers this controller and sets configuration change handling.
func init() {
control.Register(BlockIOController, "Block I/O controller", getBlockIOController())
config.GetModule(blockio.ConfigModuleName).AddNotify(getBlockIOController().configNotify)
}