...

Source file src/edge-infra.dev/pkg/edge/logging/logcfg/log_level_config.go

Documentation: edge-infra.dev/pkg/edge/logging/logcfg

     1  package logcfg
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	v1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	"edge-infra.dev/pkg/edge/api/graph/model"
    12  )
    13  
    14  // Final Struct to hold the ConfigMaP Data
    15  type LogLevelData struct {
    16  	LogLevels string
    17  }
    18  
    19  // This is what the final Config-Map should like the multi-line
    20  // value is a single string it is done in this format since lua
    21  // would consider each key value pair to be its own file under
    22  // the /log-levels dir and the lua-severity filter prefers
    23  // a single file with multiple lines for easier parsing due
    24  // the use of REGEX.
    25  // Data:
    26  // 	log-levels: |
    27  // 		cluster:warn
    28  //      namespace: namespace1: Warn
    29  //      namespace: namespace2: ALERT
    30  //      ...
    31  
    32  // BuildLogLevelConfigMap creates an log-level configmap
    33  func BuildLogLevelConfigMap(clusterLevel string, namespaceLogLevels []*model.NamespaceLogLevel) (*v1.ConfigMap, error) {
    34  	levels, err := CreateMultilineString(clusterLevel, namespaceLogLevels)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	i := &LogLevelData{
    39  		LogLevels: levels,
    40  	}
    41  	return i.ToConfigMap(), nil
    42  }
    43  
    44  // ToConfigMap create log-levels ConfigMap
    45  func (i LogLevelData) ToConfigMap() *v1.ConfigMap {
    46  	return &v1.ConfigMap{
    47  		TypeMeta: metav1.TypeMeta{
    48  			Kind:       "ConfigMap",
    49  			APIVersion: v1.SchemeGroupVersion.String(),
    50  		},
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name:      LogLevelConfigMapName,
    53  			Namespace: LogLevelConfigMapNS,
    54  		},
    55  		Data: map[string]string{
    56  			LogLevelDataFieldName: i.LogLevels,
    57  		},
    58  	}
    59  }
    60  
    61  // ValidateConfigMap validates log-level ConfigMap
    62  func ValidateConfigMap(cfg *v1.ConfigMap) error {
    63  	var missing []string
    64  	if cfg.Data[LogLevelDataFieldName] == "" {
    65  		missing = append(missing, LogLevelDataFieldName)
    66  	}
    67  
    68  	if len(missing) > 0 {
    69  		return fmt.Errorf("log-level configmap invalid, value(s) not provided: %s", strings.Join(missing, ","))
    70  	}
    71  	return nil
    72  }
    73  
    74  // New creates a valid log-level ref
    75  func New(cfg *v1.ConfigMap) (*LogLevelData, error) {
    76  	if err := ValidateConfigMap(cfg); err != nil {
    77  		return nil, err
    78  	}
    79  	return FromConfigMap(cfg), nil
    80  }
    81  
    82  // FromConfigMap util function to create log-level from config map
    83  func FromConfigMap(cfg *v1.ConfigMap) *LogLevelData {
    84  	return (&LogLevelData{}).FromConfigMap(cfg)
    85  }
    86  
    87  // FromConfigMap retrieves data from ConfigMap to log-levels
    88  func (i *LogLevelData) FromConfigMap(cfg *v1.ConfigMap) *LogLevelData {
    89  	i.LogLevels = cfg.Data[LogLevelDataFieldName]
    90  	return i
    91  }
    92  
    93  // IsLogLevelConfigMap util function to check if object is log level config map
    94  func IsLogLevelConfigMap(name, namespace string) bool {
    95  	hasRequiredName := name == LogLevelConfigMapName
    96  	inRequiredNamespace := namespace == LogLevelConfigMapNS
    97  	return hasRequiredName && inRequiredNamespace
    98  }
    99  
   100  // ConfigMapToString converts the provided configmap to a string.
   101  func ConfigMapToString(configMap *v1.ConfigMap) ([]byte, error) {
   102  	return json.Marshal(configMap)
   103  }
   104  
   105  // convert JSON to a single multiline string for the Configmap
   106  func CreateMultilineString(clusterLevel string, namespaceLogLevels []*model.NamespaceLogLevel) (string, error) {
   107  	returnVal := fmt.Sprintf("cluster: %s\n", clusterLevel)
   108  
   109  	if len(namespaceLogLevels) == 0 || namespaceLogLevels == nil {
   110  		return returnVal, nil
   111  	}
   112  
   113  	for _, entry := range namespaceLogLevels {
   114  		returnVal = fmt.Sprintf("%snamespace:%s: %s\n", returnVal, entry.Namespace, entry.Level)
   115  	}
   116  	return returnVal, nil
   117  }
   118  

View as plain text