github.com/Rookout/GoSDK@v0.1.48/pkg/information/information.go (about)

     1  package information
     2  
     3  import (
     4  	pb "github.com/Rookout/GoSDK/pkg/protobuf"
     5  )
     6  
     7  type collector func(information *AgentInformation) error
     8  
     9  func collectors() map[string]collector {
    10  	return map[string]collector{
    11  		"version":           collectVersion,
    12  		"network":           collectNetwork,
    13  		"system":            collectSystem,
    14  		"platform":          collectPlatform,
    15  		"scm":               collectScm,
    16  		"executable":        collectExecutable,
    17  		"command_arguments": collectCommandArgs,
    18  		"process_id":        collectProcessId,
    19  		"k8s_namespace":     collectK8sNamespace,
    20  		"serverless_info":   collectServerless,
    21  	}
    22  }
    23  
    24  type AgentInformation struct {
    25  	pb.AgentInformation
    26  	DefaultK8sNamespaceFile string
    27  	K8sNamespaceFileName    string
    28  	K8sNamespace            string
    29  }
    30  
    31  func Collect(labels map[string]string, k8sNamespaceFile string) (*pb.AgentInformation, error) {
    32  	k8sNamespaceLabel := "k8s_namespace"
    33  	info := &AgentInformation{DefaultK8sNamespaceFile: "/var/run/secrets/kubernetes.io/serviceaccount/namespace"}
    34  
    35  	info.Labels = labels
    36  	if k8sNamespaceFile != "" {
    37  		info.K8sNamespaceFileName = k8sNamespaceFile
    38  	}
    39  	for _, collector := range collectors() {
    40  		err := collector(info)
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  	}
    45  
    46  	if info.K8sNamespace != "" {
    47  		if info.Labels == nil {
    48  			info.Labels = make(map[string]string, 1)
    49  		}
    50  		if _, exists := info.Labels[k8sNamespaceLabel]; !exists {
    51  			info.Labels[k8sNamespaceLabel] = info.K8sNamespace
    52  		}
    53  	}
    54  
    55  	return &info.AgentInformation, nil
    56  }