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

     1  // Copyright 2021 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 containercenter
    16  
    17  import (
    18  	"encoding/json"
    19  	"strings"
    20  	"sync"
    21  	"time"
    22  
    23  	"github.com/alibaba/ilogtail/pkg/protocol"
    24  	"github.com/alibaba/ilogtail/pkg/util"
    25  )
    26  
    27  const ContainerIDPrefixSize = 12
    28  
    29  var addedContainerConfigResultMutex sync.Mutex
    30  
    31  // 新增的采集配置结果
    32  var AddedContainerConfigResult []*ContainerConfigResult
    33  
    34  // 采集配置结果内存存储
    35  var AddedContainerConfigResultMap map[string]*ContainerConfigResult
    36  
    37  type ContainerDetail struct {
    38  	DataType         string
    39  	Project          string
    40  	ContainerID      string
    41  	ContainerIP      string
    42  	ContainerName    string
    43  	RawContainerName string
    44  	LogPath          string
    45  	Driver           string
    46  	Namespace        string
    47  	ImageName        string
    48  	PodName          string
    49  	RootPath         string
    50  	Hostname         string
    51  	HostsPath        string
    52  	Env              map[string]string
    53  	ContainerLabels  map[string]string
    54  	K8sLabels        map[string]string
    55  }
    56  
    57  type ContainerConfigResult struct {
    58  	DataType                      string
    59  	Project                       string
    60  	Logstore                      string
    61  	ConfigName                    string
    62  	PathNotExistInputContainerIDs string
    63  	PathExistInputContainerIDs    string
    64  	SourceAddress                 string
    65  	InputType                     string
    66  	InputIsContainerFile          string
    67  	FlusherType                   string
    68  	FlusherTargetAddress          string
    69  }
    70  
    71  func InitContainer() {
    72  	addedContainerConfigResultMutex.Lock()
    73  	AddedContainerConfigResult = make([]*ContainerConfigResult, 0)
    74  	AddedContainerConfigResultMap = make(map[string]*ContainerConfigResult)
    75  	addedContainerConfigResultMutex.Unlock()
    76  }
    77  
    78  // 将内存Map中的数据转化到list中,用于输出
    79  func RecordContainerConfigResult() {
    80  	addedContainerConfigResultMutex.Lock()
    81  	defer addedContainerConfigResultMutex.Unlock()
    82  	for _, value := range AddedContainerConfigResultMap {
    83  		AddedContainerConfigResult = append(AddedContainerConfigResult, value)
    84  	}
    85  	AddedContainerConfigResultMap = make(map[string]*ContainerConfigResult)
    86  }
    87  
    88  // 内存中记录每个采集配置的结果,用于RecordContainerConfigResult的时候全量输出一遍
    89  func RecordContainerConfigResultMap(message *ContainerConfigResult) {
    90  	addedContainerConfigResultMutex.Lock()
    91  	defer addedContainerConfigResultMutex.Unlock()
    92  	AddedContainerConfigResultMap[message.ConfigName] = message
    93  }
    94  
    95  // 增量记录采集配置结果
    96  func RecordContainerConfigResultIncrement(message *ContainerConfigResult) {
    97  	addedContainerConfigResultMutex.Lock()
    98  	defer addedContainerConfigResultMutex.Unlock()
    99  	AddedContainerConfigResult = append(AddedContainerConfigResult, message)
   100  }
   101  
   102  func SerializeDeleteContainerToPb(logGroup *protocol.LogGroup, project string, containerIDsStr string) {
   103  	nowTime := time.Now()
   104  	log := &protocol.Log{}
   105  	log.Contents = append(log.Contents, &protocol.Log_Content{Key: "type", Value: "delete_containers"})
   106  	log.Contents = append(log.Contents, &protocol.Log_Content{Key: "project", Value: project})
   107  	log.Contents = append(log.Contents, &protocol.Log_Content{Key: "container_ids", Value: containerIDsStr})
   108  	log.Contents = append(log.Contents, &protocol.Log_Content{Key: "ip", Value: util.GetIPAddress()})
   109  	protocol.SetLogTime(log, uint32(nowTime.Unix()))
   110  	logGroup.Logs = append(logGroup.Logs, log)
   111  }
   112  
   113  func SerializeContainerToPb(logGroup *protocol.LogGroup, addedContainers []*ContainerDetail) {
   114  	nowTime := time.Now()
   115  	for _, item := range addedContainers {
   116  		log := &protocol.Log{}
   117  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "type", Value: item.DataType})
   118  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "project", Value: item.Project})
   119  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "container_id", Value: GetShortID(item.ContainerID)})
   120  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "container_ip", Value: item.ContainerIP})
   121  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "container_name", Value: item.ContainerName})
   122  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "raw_container_name", Value: item.RawContainerName})
   123  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "log_path", Value: item.LogPath})
   124  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "driver", Value: item.Driver})
   125  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "namespace", Value: item.Namespace})
   126  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "image_name", Value: item.ImageName})
   127  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "pod_name", Value: item.PodName})
   128  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "root_path", Value: item.RootPath})
   129  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "hostname", Value: item.Hostname})
   130  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "hosts_path", Value: item.HostsPath})
   131  
   132  		envStr, err := json.Marshal(item.Env)
   133  		if err == nil {
   134  			log.Contents = append(log.Contents, &protocol.Log_Content{Key: "env", Value: string(envStr)})
   135  		}
   136  		labelsStr, err := json.Marshal(item.ContainerLabels)
   137  		if err == nil {
   138  			log.Contents = append(log.Contents, &protocol.Log_Content{Key: "labels", Value: string(labelsStr)})
   139  		}
   140  		k8sLabelsStr, err := json.Marshal(item.K8sLabels)
   141  		if err == nil {
   142  			log.Contents = append(log.Contents, &protocol.Log_Content{Key: "k8s_labels", Value: string(k8sLabelsStr)})
   143  		}
   144  
   145  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "ip", Value: util.GetIPAddress()})
   146  		protocol.SetLogTime(log, uint32(nowTime.Unix()))
   147  		logGroup.Logs = append(logGroup.Logs, log)
   148  	}
   149  }
   150  
   151  func SerializeContainerConfigResultToPb(logGroup *protocol.LogGroup) {
   152  	nowTime := time.Now()
   153  	addedContainerConfigResultMutex.Lock()
   154  	defer addedContainerConfigResultMutex.Unlock()
   155  	for _, item := range AddedContainerConfigResult {
   156  		log := &protocol.Log{}
   157  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "type", Value: item.DataType})
   158  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "project", Value: item.Project})
   159  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "logstore", Value: item.Logstore})
   160  		configName := item.ConfigName
   161  		splitArrs := strings.Split(configName, "$")
   162  		if len(splitArrs) == 2 {
   163  			configName = splitArrs[1]
   164  		}
   165  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "config_name", Value: configName})
   166  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "input.source_addresses", Value: item.SourceAddress})
   167  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "input.path_exist_container_ids", Value: item.PathExistInputContainerIDs})
   168  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "input.path_not_exist_container_ids", Value: item.PathNotExistInputContainerIDs})
   169  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "input.type", Value: item.InputType})
   170  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "input.container_file", Value: item.InputIsContainerFile})
   171  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "flusher.type", Value: item.FlusherType})
   172  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "flusher.target_addresses", Value: item.FlusherTargetAddress})
   173  		protocol.SetLogTime(log, uint32(nowTime.Unix()))
   174  		log.Contents = append(log.Contents, &protocol.Log_Content{Key: "ip", Value: util.GetIPAddress()})
   175  		logGroup.Logs = append(logGroup.Logs, log)
   176  	}
   177  	AddedContainerConfigResult = AddedContainerConfigResult[:0]
   178  }
   179  
   180  func GetShortID(fullID string) string {
   181  	if len(fullID) < ContainerIDPrefixSize {
   182  		return fullID
   183  	}
   184  	return fullID[0:12]
   185  }
   186  
   187  func GetStringFromList(list []string) string {
   188  	return strings.Join(list, ";")
   189  }