2018-02-15 12:13:18 +00:00
|
|
|
/*
|
2021-03-01 07:02:22 +00:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-02-15 12:13:18 +00:00
|
|
|
|
|
|
|
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 storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-08 10:23:49 +00:00
|
|
|
"os"
|
2021-06-24 17:35:34 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-06-21 16:02:30 +00:00
|
|
|
|
2021-06-24 17:35:34 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2024-02-27 13:42:23 +00:00
|
|
|
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
|
2021-06-24 17:35:34 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2022-10-06 11:05:11 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
|
2018-11-28 12:30:03 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2018-02-15 12:13:18 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 07:12:06 +00:00
|
|
|
// Name of this feature source
|
2021-05-12 13:27:29 +00:00
|
|
|
const Name = "storage"
|
|
|
|
|
2021-06-24 17:35:34 +00:00
|
|
|
const BlockFeature = "block"
|
|
|
|
|
|
|
|
// storageSource implements the FeatureSource and LabelSource interfaces.
|
|
|
|
type storageSource struct {
|
2022-07-04 11:05:58 +00:00
|
|
|
features *nfdv1alpha1.Features
|
2021-06-24 17:35:34 +00:00
|
|
|
}
|
2018-02-15 12:13:18 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
|
|
|
src storageSource
|
2021-06-24 17:35:34 +00:00
|
|
|
_ source.FeatureSource = &src
|
|
|
|
_ source.LabelSource = &src
|
2021-03-01 07:02:22 +00:00
|
|
|
)
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-06-24 17:35:34 +00:00
|
|
|
// queueAttrs is the list of files under /sys/block/<dev>/queue that we're trying to read
|
|
|
|
var queueAttrs = []string{"dax", "rotational", "nr_zones", "zoned"}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Name returns an identifier string for this feature source.
|
|
|
|
func (s *storageSource) Name() string { return Name }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *storageSource) Priority() int { return 0 }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *storageSource) GetLabels() (source.FeatureLabels, error) {
|
2021-06-24 17:35:34 +00:00
|
|
|
labels := source.FeatureLabels{}
|
|
|
|
features := s.GetFeatures()
|
|
|
|
|
|
|
|
for _, dev := range features.Instances[BlockFeature].Elements {
|
|
|
|
if dev.Attributes["rotational"] == "0" {
|
|
|
|
labels["nonrotationaldisk"] = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover method of the FeatureSource interface
|
|
|
|
func (s *storageSource) Discover() error {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-06-24 17:35:34 +00:00
|
|
|
|
|
|
|
devs, err := detectBlock()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to detect block devices: %w", err)
|
|
|
|
}
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Instances[BlockFeature] = nfdv1alpha1.InstanceFeatureSet{Elements: devs}
|
2021-06-24 17:35:34 +00:00
|
|
|
|
2023-05-17 13:42:32 +00:00
|
|
|
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))
|
2021-06-24 17:35:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeatures method of the FeatureSource Interface.
|
2022-07-04 11:05:58 +00:00
|
|
|
func (s *storageSource) GetFeatures() *nfdv1alpha1.Features {
|
2021-06-24 17:35:34 +00:00
|
|
|
if s.features == nil {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-06-24 17:35:34 +00:00
|
|
|
}
|
|
|
|
return s.features
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:56:35 +00:00
|
|
|
func detectBlock() ([]nfdv1alpha1.InstanceFeature, error) {
|
2022-10-06 11:05:11 +00:00
|
|
|
sysfsBasePath := hostpath.SysfsDir.Path("block")
|
2021-06-24 17:35:34 +00:00
|
|
|
|
2022-09-08 10:23:49 +00:00
|
|
|
blockdevices, err := os.ReadDir(sysfsBasePath)
|
2021-06-24 17:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to list block devices: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over devices
|
2022-07-04 07:56:35 +00:00
|
|
|
info := make([]nfdv1alpha1.InstanceFeature, 0, len(blockdevices))
|
2021-06-24 17:35:34 +00:00
|
|
|
for _, device := range blockdevices {
|
|
|
|
info = append(info, *readBlockDevQueueInfo(filepath.Join(sysfsBasePath, device.Name())))
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:56:35 +00:00
|
|
|
func readBlockDevQueueInfo(path string) *nfdv1alpha1.InstanceFeature {
|
2021-06-24 17:35:34 +00:00
|
|
|
attrs := map[string]string{"name": filepath.Base(path)}
|
|
|
|
for _, attrName := range queueAttrs {
|
2022-09-08 10:23:49 +00:00
|
|
|
data, err := os.ReadFile(filepath.Join(path, "queue", attrName))
|
2021-06-24 17:35:34 +00:00
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.V(3).ErrorS(err, "failed to read block device queue attribute", "attributeName", attrName)
|
2021-06-24 17:35:34 +00:00
|
|
|
continue
|
2018-02-15 12:13:18 +00:00
|
|
|
}
|
2021-06-24 17:35:34 +00:00
|
|
|
attrs[attrName] = strings.TrimSpace(string(data))
|
2018-02-15 12:13:18 +00:00
|
|
|
}
|
2022-07-04 07:56:35 +00:00
|
|
|
return nfdv1alpha1.NewInstanceFeature(attrs)
|
2018-02-15 12:13:18 +00:00
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|