github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/config/config.go (about)

     1  // Copyright (C) 2020, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package config
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"go.uber.org/zap"
    10  	"gopkg.in/yaml.v2"
    11  	corev1 "k8s.io/api/core/v1"
    12  )
    13  
    14  // NewConfigFromConfigMap creates a new OperatorConfig from the given ConfigMap,
    15  func NewConfigFromConfigMap(configMap *corev1.ConfigMap) (*OperatorConfig, error) {
    16  	// Parse configMap content and unmarshall into OperatorConfig struct
    17  	zap.S().Infow("Constructing config from config map")
    18  	var configString string
    19  	if value, ok := configMap.Data[configKeyValue]; ok {
    20  		configString = value
    21  	} else {
    22  		return nil, fmt.Errorf("Failed, expected key '%s' not found in ConfigMap %s", configKeyValue, configMap.Name)
    23  	}
    24  	var config OperatorConfig
    25  	err := yaml.Unmarshal([]byte(configString), &config)
    26  	if err != nil {
    27  		return nil, fmt.Errorf("Failed to unmarshall ConfigMap %s: %v", configMap.String(), err)
    28  	}
    29  	// Comment out the debug line below to avoid halting the debugger in Goland/IntelliJ
    30  	// see https://youtrack.jetbrains.com/issue/GO-8953
    31  	zap.S().Debugf("Unmarshalled configmap is:\n %s", configMap.String())
    32  
    33  	// Set defaults for any uninitialized values
    34  	zap.S().Infow("Setting config defaults")
    35  	setConfigDefaults(&config)
    36  	return &config, nil
    37  }
    38  
    39  // Sets defaults for the given OperatorConfig object.
    40  func setConfigDefaults(config *OperatorConfig) {
    41  
    42  	if config.DefaultSimpleComponentReplicas == nil {
    43  		config.DefaultSimpleComponentReplicas = newIntVal(defaultSimpleComponentReplicas)
    44  	}
    45  	if config.MetricsPort == nil {
    46  		config.MetricsPort = newIntVal(defaultMetricsPort)
    47  	}
    48  
    49  }
    50  
    51  func newIntVal(value int) *int {
    52  	var val = value
    53  	return &val
    54  }