github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/cmd/ndm_daemonset/probe/customtagprobe.go (about)

     1  /*
     2  Copyright 2020 The OpenEBS Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package probe
    18  
    19  import (
    20  	"github.com/openebs/node-disk-manager/blockdevice"
    21  	"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
    22  	"github.com/openebs/node-disk-manager/db/kubernetes"
    23  	"github.com/openebs/node-disk-manager/pkg/util"
    24  
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  const (
    29  	customTagProbePriority = 7
    30  
    31  	tagTypePath = "path"
    32  )
    33  
    34  var (
    35  	customTagProbeState = defaultEnabled
    36  
    37  	supportedTagTypes = []string{tagTypePath}
    38  )
    39  
    40  type customTagProbe struct {
    41  	tags []tag
    42  }
    43  
    44  type tag struct {
    45  	tagType string
    46  	regex   string
    47  	label   string
    48  }
    49  
    50  // The label validation regex should be the same as used in
    51  // https://github.com/kubernetes/apimachinery/blob/master/pkg/util/validation/validation.go
    52  const labelValidatorRegex = "(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?"
    53  
    54  var customTagProbeRegister = func() {
    55  	// Get a controller object
    56  	ctrl := <-controller.ControllerBroadcastChannel
    57  	if ctrl == nil {
    58  		klog.Error("unable to configure custom tag probe")
    59  		return
    60  	}
    61  	tagProbe := &customTagProbe{}
    62  
    63  	if ctrl.NDMConfig != nil {
    64  		for _, tagConfig := range ctrl.NDMConfig.TagConfigs {
    65  			if !util.Contains(supportedTagTypes, tagConfig.Type) {
    66  				klog.Errorf("unsupported tag type: %s", tagConfig.Type)
    67  			}
    68  
    69  			if !util.IsMatchRegex(labelValidatorRegex, tagConfig.TagName) {
    70  				klog.Errorf("not a valid label \"%s\"", tagConfig.TagName)
    71  			}
    72  
    73  			tagProbe.tags = append(tagProbe.tags, tag{
    74  				tagType: tagConfig.Type,
    75  				regex:   tagConfig.Pattern,
    76  				label:   tagConfig.TagName,
    77  			})
    78  		}
    79  	}
    80  	newRegisterProbe := &registerProbe{
    81  		priority:   customTagProbePriority,
    82  		name:       "Custom Tag Probe",
    83  		state:      customTagProbeState,
    84  		pi:         tagProbe,
    85  		controller: ctrl,
    86  	}
    87  	newRegisterProbe.register()
    88  }
    89  
    90  func (ctp *customTagProbe) Start() {}
    91  
    92  func (ctp *customTagProbe) FillBlockDeviceDetails(bd *blockdevice.BlockDevice) {
    93  	for _, tag := range ctp.tags {
    94  		var fieldToMatch string
    95  		switch tag.tagType {
    96  		case tagTypePath:
    97  			fieldToMatch = bd.DevPath
    98  		}
    99  		if util.IsMatchRegex(tag.regex, fieldToMatch) {
   100  			bd.Labels[kubernetes.BlockDeviceTagLabel] = tag.label
   101  			klog.Infof("Device: %s Label %s:%s added by custom tag probe", bd.DevPath, kubernetes.BlockDeviceTagLabel, tag.label)
   102  		}
   103  	}
   104  }