github.com/verrazzano/verrazzano@v1.7.1/application-operator/controllers/metricsbinding/metricsbinding_utils.go (about) 1 // Copyright (c) 2021, 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 metricsbinding 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/Jeffail/gabs/v2" 11 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/app/v1alpha1" 12 "github.com/verrazzano/verrazzano/application-operator/constants" 13 vzconst "github.com/verrazzano/verrazzano/pkg/constants" 14 v1 "k8s.io/api/core/v1" 15 "k8s.io/apimachinery/pkg/api/errors" 16 k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 17 "k8s.io/apimachinery/pkg/runtime/schema" 18 "sigs.k8s.io/yaml" 19 ) 20 21 const ( 22 prometheusConfigKey = "prometheus.yml" 23 prometheusScrapeConfigsLabel = "scrape_configs" 24 25 configMapKind = "ConfigMap" 26 k8sV1APIVersion = "v1" 27 28 finalizerName = "metricsbinding.finalizers.verrazzano.io/finalizer" 29 30 workloadSourceLabel = "__meta_kubernetes_pod_label_app_verrazzano_io_workload" 31 ) 32 33 // Creates a job name in the format <namespace>_<name>_<kind> 34 func createJobName(metricsbinding *vzapi.MetricsBinding) string { 35 reference := metricsbinding.Spec.Workload 36 return fmt.Sprintf("%s_%s_%s_%s", metricsbinding.Namespace, reference.Name, strings.Replace(reference.TypeMeta.APIVersion, "/", "_", 1), reference.TypeMeta.Kind) 37 } 38 39 // returns a container of the Prometheus config data from the configmap 40 func getConfigData(configMap *v1.ConfigMap) (*gabs.Container, error) { 41 if configMap.Data == nil { 42 return nil, errors.NewNotFound(schema.GroupResource{ 43 Group: "default", 44 Resource: configMap.Kind, 45 }, configMap.Name) 46 } 47 oldPromConfigData := configMap.Data[prometheusConfigKey] 48 promConfigJSON, err := yaml.YAMLToJSON([]byte(oldPromConfigData)) 49 if err != nil { 50 return nil, err 51 } 52 promConfig, err := gabs.ParseJSON(promConfigJSON) 53 if err != nil { 54 return nil, err 55 } 56 return promConfig, nil 57 } 58 59 // returns a container of the Prometheus config data from the given secret 60 func getConfigDataFromSecret(secret *v1.Secret, key string) (*gabs.Container, error) { 61 if secret.Data == nil { 62 return nil, errors.NewNotFound(schema.GroupResource{ 63 Group: "default", 64 Resource: secret.Kind, 65 }, secret.Name) 66 } 67 oldPromConfigData := secret.Data[key] 68 promConfigJSON, err := yaml.YAMLToJSON([]byte(oldPromConfigData)) 69 if err != nil { 70 return nil, err 71 } 72 promConfig, err := gabs.ParseJSON(promConfigJSON) 73 if err != nil { 74 return nil, err 75 } 76 return promConfig, nil 77 } 78 79 // Formats job name as specified by the Prometheus config 80 func formatJobName(jobName string) string { 81 return fmt.Sprintf("%s: %s\n", vzconst.PrometheusJobNameKey, jobName) 82 } 83 84 // getPromConfigMap returns the Prometheus ConfigMap given in the MetricsBinding 85 func getPromConfigMap(metricsBinding *vzapi.MetricsBinding) *v1.ConfigMap { 86 targetConfigMap := metricsBinding.Spec.PrometheusConfigMap 87 if targetConfigMap.Name == "" { 88 return nil 89 } 90 return &v1.ConfigMap{ 91 TypeMeta: k8smetav1.TypeMeta{ 92 Kind: configMapKind, 93 APIVersion: k8sV1APIVersion, 94 }, 95 ObjectMeta: k8smetav1.ObjectMeta{ 96 Name: targetConfigMap.Name, 97 Namespace: targetConfigMap.Namespace, 98 }, 99 } 100 } 101 102 // getPromConfigSecret returns the Prometheus Config Secret given in the MetricsBinding, along with the key 103 func getPromConfigSecret(metricsBinding *vzapi.MetricsBinding) (*v1.Secret, string) { 104 targetSecret := metricsBinding.Spec.PrometheusConfigSecret 105 if targetSecret.Name == "" { 106 return nil, "" 107 } 108 return &v1.Secret{ 109 TypeMeta: k8smetav1.TypeMeta{ 110 Kind: vzconst.SecretKind, 111 APIVersion: k8sV1APIVersion, 112 }, 113 ObjectMeta: k8smetav1.ObjectMeta{ 114 Name: targetSecret.Name, 115 Namespace: targetSecret.Namespace, 116 }, 117 }, targetSecret.Key 118 } 119 120 // isLegacyDefaultMetricsBinding determines whether the given binding uses the 121 // "default" metrics template used pre-Verrazzano 1.4 AND the legacy VMI system prometheus config map 122 func isLegacyDefaultMetricsBinding(metricsBinding *vzapi.MetricsBinding) bool { 123 templateName := metricsBinding.Spec.MetricsTemplate 124 configMapName := metricsBinding.Spec.PrometheusConfigMap 125 return templateName.Namespace == constants.LegacyDefaultMetricsTemplateNamespace && 126 templateName.Name == constants.LegacyDefaultMetricsTemplateName && 127 isLegacyVmiPrometheusConfigMapName(configMapName) 128 } 129 130 // isLegacyVmiPrometheusConfigMapName returns true if the given NamespaceName is that of the legacy 131 // vmi system prometheus config map 132 func isLegacyVmiPrometheusConfigMapName(configMapName vzapi.NamespaceName) bool { 133 return configMapName.Namespace == constants.VerrazzanoSystemNamespace && 134 configMapName.Name == vzconst.VmiPromConfigName 135 }