func ReplaceConfig()

in internal/manifests/collector/config_replace.go [36:93]


func ReplaceConfig(instance v1alpha1.AmazonCloudWatchAgent) (string, error) {
	// Parse the original configuration from instance.Spec.Config
	config, err := adapters.ConfigFromJSONString(instance.Spec.Config)
	if err != nil {
		return "", err
	}

	conf := confmap.NewFromStringMap(config)

	prometheusFilePath := conf.Get("logs::metrics_collected::prometheus::prometheus_config_path")
	if prometheusFilePath == nil {
		prometheusFilePath = "/etc/prometheusconfig/prometheus.yaml"
	}
	if conf.IsSet("logs::metrics_collected::prometheus") && !instance.Spec.Prometheus.IsEmpty() {
		prometheusConfig := confmap.NewFromStringMap(map[string]interface{}{
			"logs": map[string]interface{}{
				"metrics_collected": map[string]interface{}{
					"prometheus": map[string]interface{}{
						"prometheus_config_path": prometheusFilePath,
					},
				},
			},
		})

		err = conf.Merge(prometheusConfig)
		if err != nil {
			return "", err
		}
	}
	prometheusFilePath = conf.Get("metrics::metrics_collected::prometheus::prometheus_config_path")
	if prometheusFilePath == nil {
		prometheusFilePath = "/etc/prometheusconfig/prometheus.yaml"
	}
	if conf.IsSet("metrics::metrics_collected::prometheus") && !instance.Spec.Prometheus.IsEmpty() {
		prometheusConfig := confmap.NewFromStringMap(map[string]interface{}{
			"metrics": map[string]interface{}{
				"metrics_collected": map[string]interface{}{
					"prometheus": map[string]interface{}{
						"prometheus_config_path": prometheusFilePath,
					},
				},
			},
		})

		err = conf.Merge(prometheusConfig)
		if err != nil {
			return "", err
		}
	}

	finalConfig := conf.ToStringMap()
	out, err := json.Marshal(finalConfig)
	if err != nil {
		return "", err
	}

	return string(out), nil
}