func applyResources()

in internal/stack/resources.go [147:224]


func applyResources(profile *profile.Profile, stackVersion string) error {
	stackDir := filepath.Join(profile.ProfilePath, ProfileStackPath)

	var agentPorts []string
	if err := profile.Decode("stack.agent.ports", &agentPorts); err != nil {
		return fmt.Errorf("failed to unmarshal stack.agent.ports: %w", err)
	}

	elasticSubscriptionProfile := profile.Config(configElasticSubscription, "trial")
	if !slices.Contains(elasticSubscriptionsSupported, elasticSubscriptionProfile) {
		return fmt.Errorf("unsupported Elastic subscription %q: supported subscriptions: %s", elasticSubscriptionProfile, strings.Join(elasticSubscriptionsSupported, ", "))
	}

	resourceManager := resource.NewManager()
	resourceManager.AddFacter(resource.StaticFacter{
		"registry_base_image":   PackageRegistryBaseImage,
		"elasticsearch_version": stackVersion,
		"kibana_version":        stackVersion,
		"agent_version":         stackVersion,

		"kibana_host":        "https://kibana:5601",
		"fleet_url":          "https://fleet-server:8220",
		"elasticsearch_host": "https://elasticsearch:9200",

		"api_key":          "",
		"username":         elasticsearchUsername,
		"password":         elasticsearchPassword,
		"enrollment_token": "",

		"agent_publish_ports":  strings.Join(agentPorts, ","),
		"apm_enabled":          profile.Config(configAPMEnabled, "false"),
		"geoip_dir":            profile.Config(configGeoIPDir, "./ingest-geoip"),
		"kibana_http2_enabled": profile.Config(configKibanaHTTP2Enabled, "true"),
		"logsdb_enabled":       profile.Config(configLogsDBEnabled, "false"),
		"logstash_enabled":     profile.Config(configLogstashEnabled, "false"),
		"self_monitor_enabled": profile.Config(configSelfMonitorEnabled, "false"),
		"elastic_subscription": elasticSubscriptionProfile,
	})

	if err := os.MkdirAll(stackDir, 0755); err != nil {
		return fmt.Errorf("failed to create stack directory: %w", err)
	}
	resourceManager.RegisterProvider("file", &resource.FileProvider{
		Prefix: stackDir,
	})
	resources := append([]resource.Resource{}, stackResources...)

	// Keeping certificates in the profile directory for backwards compatibility reasons.
	resourceManager.RegisterProvider(CertsFolder, &resource.FileProvider{
		Prefix: profile.ProfilePath,
	})
	certResources, err := initTLSCertificates(CertsFolder, profile.ProfilePath, tlsServices)
	if err != nil {
		return fmt.Errorf("failed to create TLS files: %w", err)
	}
	resources = append(resources, certResources...)

	// Add related resources and client certificates if logstash is enabled.
	if profile.Config("stack.logstash_enabled", "false") == "true" {
		resources = append(resources, logstashResources...)
		if err := addClientCertsToResources(resourceManager, certResources); err != nil {
			return fmt.Errorf("error adding client certificates: %w", err)
		}
	}

	results, err := resourceManager.Apply(resources)
	if err != nil {
		var errors []string
		for _, result := range results {
			if err := result.Err(); err != nil {
				errors = append(errors, err.Error())
			}
		}
		return fmt.Errorf("%w: %s", err, strings.Join(errors, ", "))
	}

	return nil
}