github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/plugin/test/label_nodes.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	v1 "k8s.io/api/core/v1"
    22  
    23  	"github.com/alibaba/sealer/logger"
    24  	"github.com/alibaba/sealer/pkg/client/k8s"
    25  	"github.com/alibaba/sealer/pkg/plugin"
    26  )
    27  
    28  type LabelsNodes struct {
    29  	data   map[string][]label
    30  	client *k8s.Client
    31  }
    32  
    33  type label struct {
    34  	key   string
    35  	value string
    36  }
    37  
    38  func (l LabelsNodes) Run(context plugin.Context, phase plugin.Phase) error {
    39  	if phase != plugin.PhasePostInstall || context.Plugin.Spec.Type != "LABEL_TEST_SO" {
    40  		logger.Debug("label nodes is PostInstall!")
    41  		return nil
    42  	}
    43  	c, err := k8s.Newk8sClient()
    44  	if err != nil {
    45  		return err
    46  	}
    47  	l.client = c
    48  	l.data = l.formatData(context.Plugin.Spec.Data)
    49  	nodeList, err := l.client.ListNodes()
    50  	if err != nil {
    51  		return fmt.Errorf("current cluster nodes not found, %v", err)
    52  	}
    53  	for _, v := range nodeList.Items {
    54  		internalIP := l.getAddress(v.Status.Addresses)
    55  		labels, ok := l.data[internalIP]
    56  		if ok {
    57  			m := v.GetLabels()
    58  			for _, val := range labels {
    59  				m[val.key] = val.value
    60  			}
    61  			v.SetLabels(m)
    62  			v.SetResourceVersion("")
    63  
    64  			if _, err := l.client.UpdateNode(v); err != nil {
    65  				return fmt.Errorf("current cluster nodes label failed, %v", err)
    66  			}
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  func (l LabelsNodes) formatData(data string) map[string][]label {
    73  	m := make(map[string][]label)
    74  	items := strings.Split(data, "\n")
    75  	if len(items) == 0 {
    76  		logger.Debug("label data is empty!")
    77  		return m
    78  	}
    79  	for _, v := range items {
    80  		tmps := strings.Split(v, " ")
    81  		if len(tmps) != 2 {
    82  			//logger.Warn("label data is no-compliance with the rules! label data: %v", v)
    83  			continue
    84  		}
    85  		ip := tmps[0]
    86  		labelStr := strings.Split(tmps[1], ",")
    87  		var labels []label
    88  		for _, l := range labelStr {
    89  			tmp := strings.Split(l, "=")
    90  			if len(tmp) != 2 {
    91  				logger.Warn("label data is no-compliance with the rules! label data: %v", l)
    92  				continue
    93  			}
    94  			labels = append(labels, label{
    95  				key:   tmp[0],
    96  				value: tmp[1],
    97  			})
    98  		}
    99  		m[ip] = labels
   100  	}
   101  	return m
   102  }
   103  
   104  func (l LabelsNodes) getAddress(addresses []v1.NodeAddress) string {
   105  	for _, v := range addresses {
   106  		if strings.EqualFold(string(v.Type), "InternalIP") {
   107  			return v.Address
   108  		}
   109  	}
   110  	return ""
   111  }
   112  
   113  // Plugin is the exposed variable sealer will look up it.
   114  //nolint
   115  var Plugin LabelsNodes
   116  
   117  // PluginType is the exposed variable defined the type of this plugin.
   118  //nolint
   119  var PluginType = "LABEL_TEST_SO"