github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/vmo/vmospec.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 vmo
     5  
     6  import (
     7  	vmcontrollerv1 "github.com/verrazzano/verrazzano-monitoring-operator/pkg/apis/vmcontroller/v1"
     8  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/config"
     9  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/constants"
    10  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/resources"
    11  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/resources/nodes"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  )
    15  
    16  // InitializeVMOSpec initializes any uninitialized elements of the VMO spec.
    17  func InitializeVMOSpec(controller *Controller, vmo *vmcontrollerv1.VerrazzanoMonitoringInstance) {
    18  	// The secretName we use for basic authentication in the Nginx ingress controller
    19  	vmo.Spec.SecretName = vmo.Name + "-basicauth"
    20  
    21  	/*********************
    22  	 * Create Secrets
    23  	 **********************/
    24  	controller.log.Oncef("Loading auth secret data")
    25  	credsMap, err := controller.loadAllAuthSecretData(vmo.Namespace, vmo.Spec.SecretsName)
    26  	if err != nil {
    27  		controller.log.Errorf("Failed to extract VMO Secrets for VMI %s: %v", vmo.Name, err)
    28  	}
    29  
    30  	controller.log.Oncef("Reconciling auth secrets")
    31  	err = CreateOrUpdateAuthSecrets(controller, vmo, credsMap)
    32  	if err != nil {
    33  		controller.log.Errorf("Failed to create VMO Secrets for VMI %s: %v", vmo.Name, err)
    34  	}
    35  
    36  	// Create TLS secrets or get certs
    37  	controller.log.Oncef("Reconciling TLS secrets")
    38  	err = CreateOrUpdateTLSSecrets(controller, vmo)
    39  	if err != nil {
    40  		controller.log.Errorf("Failed to create TLS Secrets for VMI %s: %v", vmo.Name, err)
    41  	}
    42  
    43  	// Set creation time
    44  	if vmo.Status.CreationTime == nil {
    45  		now := metav1.Now()
    46  		vmo.Status.CreationTime = &now
    47  	}
    48  
    49  	// Set environment
    50  	if vmo.Status.EnvName == "" {
    51  		vmo.Status.EnvName = controller.operatorConfig.EnvName
    52  	}
    53  
    54  	// Service type
    55  	if vmo.Spec.ServiceType == "" {
    56  		vmo.Spec.ServiceType = corev1.ServiceTypeClusterIP
    57  	}
    58  
    59  	// Referenced ConfigMaps
    60  	if vmo.Spec.Grafana.DashboardsConfigMap == "" {
    61  		vmo.Spec.Grafana.DashboardsConfigMap = resources.GetMetaName(vmo.Name, constants.DashboardConfig)
    62  	}
    63  	if vmo.Spec.Grafana.DatasourcesConfigMap == "" {
    64  		vmo.Spec.Grafana.DatasourcesConfigMap = resources.GetMetaName(vmo.Name, constants.DatasourceConfig)
    65  	}
    66  
    67  	// Number of replicas for each component
    68  	if vmo.Spec.Kibana.Replicas == 0 {
    69  		vmo.Spec.Kibana.Replicas = int32(*controller.operatorConfig.DefaultSimpleComponentReplicas)
    70  	}
    71  
    72  	// Default roles for VMO components
    73  	initNode(&vmo.Spec.Elasticsearch.MasterNode, vmcontrollerv1.MasterRole)
    74  	initNode(&vmo.Spec.Elasticsearch.IngestNode, vmcontrollerv1.IngestRole)
    75  	initNode(&vmo.Spec.Elasticsearch.DataNode, vmcontrollerv1.DataRole)
    76  
    77  	// Setup default storage elements
    78  	for _, component := range config.StorageEnableComponents {
    79  		storageElement := resources.GetStorageElementForComponent(vmo, component)
    80  		replicas := int(resources.GetReplicasForComponent(vmo, component))
    81  		pvcName := resources.GetMetaName(vmo.Name, component.Name)
    82  		initStorageElement(storageElement, replicas, pvcName)
    83  	}
    84  
    85  	// Setup data node storage elements
    86  	for _, node := range nodes.DataNodes(vmo) {
    87  		initStorageElement(node.Storage, int(node.Replicas), resources.GetMetaName(vmo.Name, node.Name))
    88  	}
    89  
    90  	// Overall status
    91  	if vmo.Status.State == "" {
    92  		vmo.Status.State = string(constants.Running)
    93  	}
    94  
    95  	// set label for managed-cluster-name
    96  	vmo.Labels[constants.ClusterNameData] = controller.clusterInfo.clusterName
    97  }
    98  
    99  func initNode(node *vmcontrollerv1.ElasticsearchNode, role vmcontrollerv1.NodeRole) {
   100  	if len(node.Name) < 1 {
   101  		node.Name = "es-" + string(role)
   102  	}
   103  	if len(node.Roles) < 1 {
   104  		node.Roles = []vmcontrollerv1.NodeRole{
   105  			role,
   106  		}
   107  	}
   108  }
   109  
   110  func initStorageElement(storageElement *vmcontrollerv1.Storage, replicas int, pvcName string) {
   111  	if storageElement == nil || storageElement.Size == "" {
   112  		return // No storage specified, so nothing to do
   113  	}
   114  	// Initialize the current state of the storage element, if not already set
   115  	if storageElement.PvcNames == nil || len(storageElement.PvcNames) == 0 {
   116  		// Initialize slice of storageElement.PvcNames
   117  		storageElement.PvcNames = []string{}
   118  		storageElement.PvcNames = append(storageElement.PvcNames, pvcName)
   119  		// Base the rest of the PVC names on the format of the first
   120  		for i := 1; i < replicas; i++ {
   121  			pvcName = resources.GetNextStringInSequence(pvcName)
   122  			storageElement.PvcNames = append(storageElement.PvcNames, pvcName)
   123  		}
   124  	}
   125  	if len(storageElement.PvcNames) < replicas {
   126  		newPvcs := replicas - len(storageElement.PvcNames)
   127  		pvcName := storageElement.PvcNames[len(storageElement.PvcNames)-1]
   128  		for i := 0; i < newPvcs; i++ {
   129  			pvcName = resources.GetNextStringInSequence(pvcName)
   130  			storageElement.PvcNames = append(storageElement.PvcNames, pvcName)
   131  		}
   132  	}
   133  	// If we're over the expected number of PVCs, remove the extras from the VMO spec
   134  	for len(storageElement.PvcNames) > replicas {
   135  		storageElement.PvcNames = storageElement.PvcNames[:len(storageElement.PvcNames)-1]
   136  	}
   137  }