github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/k8s.go (about)

     1  // Copyright 2023 iLogtail Authors
     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 helper
    16  
    17  import (
    18  	"regexp"
    19  	"strconv"
    20  )
    21  
    22  var deploymentPodReg = regexp.MustCompile(`^([\w\-]+)\-([0-9a-z]{9,10})\-([0-9a-z]{5})$`)
    23  var setPodReg = regexp.MustCompile(`^([\w\-]+)\-([0-9a-z]{5})$`)
    24  var statefulSetPodReg = regexp.MustCompile(`^([\w\-]+)\-(\d+)$`)
    25  
    26  func ExtractPodWorkload(name string) string {
    27  	switch {
    28  	case deploymentPodReg.MatchString(name):
    29  		index := deploymentPodReg.FindStringSubmatchIndex(name)
    30  		return name[index[2]:index[3]]
    31  	case setPodReg.MatchString(name):
    32  		index := setPodReg.FindStringSubmatchIndex(name)
    33  		return name[index[2]:index[3]]
    34  	case statefulSetPodReg.MatchString(name):
    35  		index := statefulSetPodReg.FindStringSubmatchIndex(name)
    36  		return name[index[2]:index[3]]
    37  	}
    38  	return name
    39  }
    40  
    41  func ExtractStatefulSetNum(pod string) int {
    42  	if statefulSetPodReg.MatchString(pod) {
    43  		index := statefulSetPodReg.FindStringSubmatchIndex(pod)
    44  		num := pod[index[4]:index[5]]
    45  		n, _ := strconv.Atoi(num)
    46  		return n
    47  	}
    48  	return -1
    49  }