...

Source file src/edge-infra.dev/pkg/edge/ctlfish/option/config.go

Documentation: edge-infra.dev/pkg/edge/ctlfish/option

     1  package option
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/viper"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/apimachinery/pkg/runtime/schema"
    10  	"k8s.io/client-go/dynamic"
    11  
    12  	"edge-infra.dev/pkg/edge/api/graph/mapper"
    13  	"edge-infra.dev/pkg/edge/info"
    14  	"edge-infra.dev/pkg/lib/logging"
    15  )
    16  
    17  const (
    18  	MonitoredResources = "monitorResources"
    19  	WatchResources     = "watchResources"
    20  	Interval           = "interval"
    21  )
    22  
    23  // MetricConfig represents ctlfish configuration found in configmap
    24  type MetricConfig struct {
    25  	MonitorResources map[string]bool
    26  	WatchResources   map[string]bool
    27  	Interval         int
    28  	ClusterEdgeID    string
    29  	ForemanProjectID string
    30  	ProjectID        string
    31  	ClusterName      string
    32  	PubSubActive     bool
    33  }
    34  
    35  // IsMonitored always exclude these resources, i.e. do not add a watch for these resources
    36  func (m MetricConfig) IsMonitored(gvr schema.GroupVersionResource) bool {
    37  	_, ok := m.MonitorResources[GVRToString(gvr)]
    38  	return ok
    39  }
    40  
    41  // IsWatched checked to add a watch for `create`, `update` (for `delete` add a watch for all non excluded resources)
    42  func (m MetricConfig) IsWatched(gvr schema.GroupVersionResource) bool {
    43  	_, ok := m.WatchResources[GVRToString(gvr)]
    44  	return ok
    45  }
    46  
    47  // GVRToString creates a unique key for resources
    48  func GVRToString(gvr schema.GroupVersionResource) string {
    49  	if gvr.Group == "" {
    50  		return fmt.Sprintf("%s/%s", gvr.Version, gvr.Resource)
    51  	}
    52  	return fmt.Sprintf("%s/%s.%s", gvr.Version, gvr.Resource, gvr.Group)
    53  }
    54  
    55  // GetConfig uses viper to decode a yaml config file
    56  // It takes in a config file name and returns the list of resources wanted
    57  func GetConfig(configFileName string, logger *logging.EdgeLogger, cs dynamic.Interface) (*MetricConfig, error) {
    58  	// get config file using viper
    59  	viper.SetConfigName(configFileName)
    60  	viper.AddConfigPath(".")
    61  	viper.AddConfigPath("testdata/")
    62  	viper.AddConfigPath("/opt/")
    63  	viper.SetConfigType("yaml")
    64  	//Read config if found
    65  	if err := viper.ReadInConfig(); err != nil {
    66  		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
    67  			logger.Error(err, "Config file not found at "+viper.ConfigFileUsed()+" , using default values") //Was a Warn
    68  		} else {
    69  			logger.Error(err, "Failed to read in config")
    70  			return nil, err
    71  		}
    72  	}
    73  	// Set resource variable to all resources if not defined
    74  	viper.SetDefault(Interval, 600)
    75  
    76  	// get list of resources from the config
    77  	resources := viper.GetStringSlice(MonitoredResources)
    78  	filter := viper.GetStringSlice(WatchResources)
    79  	interval := viper.GetInt(Interval)
    80  
    81  	configMapResource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
    82  	configMapRes, err := cs.Resource(configMapResource).Namespace(info.EdgeConfigMapNS).Get(context.Background(), info.EdgeConfigMapName, metav1.GetOptions{})
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	configMap, err := mapper.ToConvertUnstructuredToConfigMap(configMapRes)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	return &MetricConfig{
    92  		MonitorResources: createMapping(resources),
    93  		WatchResources:   createMapping(filter),
    94  		Interval:         interval,
    95  		ClusterEdgeID:    configMap.Data[info.ClusterEdgeID],
    96  		ForemanProjectID: configMap.Data[info.ForemanProjectID],
    97  		ProjectID:        configMap.Data[info.ProjectID],
    98  		ClusterName:      configMap.Data[info.StoreName],
    99  		PubSubActive:     true,
   100  	}, nil
   101  }
   102  
   103  func createMapping(resources []string) map[string]bool {
   104  	mapping := map[string]bool{}
   105  	for _, resource := range resources {
   106  		mapping[resource] = true
   107  	}
   108  	return mapping
   109  }
   110  

View as plain text