github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/plugin/utils.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 plugin
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/alibaba/sealer/logger"
    22  
    23  	"github.com/alibaba/sealer/common"
    24  	"github.com/alibaba/sealer/pkg/client/k8s"
    25  	"github.com/alibaba/sealer/utils"
    26  )
    27  
    28  const (
    29  	DelSymbol   = "-"
    30  	EqualSymbol = "="
    31  	ColonSymbol = ":"
    32  	SplitSymbol = "|"
    33  )
    34  
    35  func GetIpsByOnField(on string, context Context, phase Phase) (ipList []string, err error) {
    36  	on = strings.TrimSpace(on)
    37  	if strings.Contains(on, EqualSymbol) {
    38  		if (phase != PhasePostInstall && phase != PhasePostJoin) && phase != PhasePreClean {
    39  			logger.Warn("Current phase is %s. When nodes is specified with a label, the plugin action must be PostInstall or PostJoin, ", phase)
    40  			return nil, nil
    41  		}
    42  		client, err := k8s.Newk8sClient()
    43  		if err != nil {
    44  			return nil, fmt.Errorf("failed to get k8s client: %v", err)
    45  		}
    46  		ipList, err = client.ListNodeIPByLabel(strings.TrimSpace(on))
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  	} else if on == common.MASTER || on == common.NODE {
    51  		ipList = context.Cluster.GetIPSByRole(on)
    52  	} else if on == common.MASTER0 {
    53  		ipList = context.Cluster.GetIPSByRole(common.MASTER)
    54  		if len(ipList) < 1 {
    55  			return nil, fmt.Errorf("invalid on filed: [%s]", on)
    56  		}
    57  		ipList = ipList[:1]
    58  	} else {
    59  		ipList = utils.DisassembleIPList(on)
    60  	}
    61  	if len(ipList) == 0 {
    62  		logger.Debug("node not found by on field [%s]", on)
    63  	}
    64  	return ipList, nil
    65  }