func ConfigMap()

in internal/manifests/targetallocator/configmap.go [26:92]


func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
	name := naming.TAConfigMap(params.OtelCol.Name)
	version := strings.Split(params.OtelCol.Spec.TargetAllocator.Image, ":")
	labels := Labels(params.OtelCol, name)
	if len(version) > 1 {
		labels["app.kubernetes.io/version"] = version[len(version)-1]
	} else {
		labels["app.kubernetes.io/version"] = "latest"
	}

	promConfigYaml, err := params.OtelCol.Spec.Prometheus.Yaml()
	if err != nil {
		return &corev1.ConfigMap{}, fmt.Errorf("%s could not convert json to yaml", err)
	}

	prometheusConfig, err := adapters.GetPromConfig(promConfigYaml)
	if err != nil {
		return &corev1.ConfigMap{}, err
	}

	taConfig := make(map[interface{}]interface{})
	prometheusCRConfig := make(map[interface{}]interface{})
	taConfig["label_selector"] = manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, collector.ComponentAmazonCloudWatchAgent)
	// We only take the "config" from the returned object, if it's present
	if prometheusConfig, ok := prometheusConfig["config"]; ok {
		taConfig["config"] = prometheusConfig
	}

	taConfig["allocation_strategy"] = v1alpha1.AmazonCloudWatchAgentTargetAllocatorAllocationStrategyConsistentHashing

	if len(params.OtelCol.Spec.TargetAllocator.FilterStrategy) > 0 {
		taConfig["filter_strategy"] = params.OtelCol.Spec.TargetAllocator.FilterStrategy
	}

	if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Size() > 0 {
		prometheusCRConfig["scrape_interval"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Duration
	}

	if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector != nil {
		taConfig["service_monitor_selector"] = &params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector
	}

	if params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector != nil {
		taConfig["pod_monitor_selector"] = &params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector
	}

	if len(prometheusCRConfig) > 0 {
		taConfig["prometheus_cr"] = prometheusCRConfig
	}

	taConfigYAML, err := yaml.Marshal(taConfig)
	if err != nil {
		return &corev1.ConfigMap{}, err
	}

	return &corev1.ConfigMap{
		ObjectMeta: metav1.ObjectMeta{
			Name:        name,
			Namespace:   params.OtelCol.Namespace,
			Labels:      labels,
			Annotations: params.OtelCol.Annotations,
		},
		Data: map[string]string{
			targetAllocatorFilename: string(taConfigYAML),
		},
	}, nil
}