func ConfigToComponentPorts()

in internal/manifests/collector/adapters/config_to_ports.go [78:144]


func ConfigToComponentPorts(logger logr.Logger, cType ComponentType, config map[interface{}]interface{}) ([]corev1.ServicePort, error) {
	// now, we gather which ports we might need to open
	// for that, we get all the exporters and check their `endpoint` properties,
	// extracting the port from it. The port name has to be a "DNS_LABEL", so, we try to make it follow the pattern:
	// examples:
	// ```yaml
	// components:
	//   componentexample:
	//     endpoint: 0.0.0.0:12345
	//   componentexample/settings:
	//     endpoint: 0.0.0.0:12346
	// in this case, we have 2 ports, named: "componentexample" and "componentexample-settings"
	componentsProperty, ok := config[fmt.Sprintf("%ss", cType.String())]
	if !ok {
		return nil, fmt.Errorf("no %ss available as part of the configuration", cType)
	}

	components, ok := componentsProperty.(map[interface{}]interface{})
	if !ok {
		return nil, fmt.Errorf("%ss doesn't contain valid components", cType.String())
	}

	compEnabled := getEnabledComponents(config, cType)

	if compEnabled == nil {
		return nil, fmt.Errorf("no enabled %ss available as part of the configuration", cType)
	}

	ports := []corev1.ServicePort{}
	for key, val := range components {
		// This check will pass only the enabled components,
		// then only the related ports will be opened.
		if !compEnabled[key] {
			continue
		}
		extractedComponent, ok := val.(map[interface{}]interface{})
		if !ok {
			logger.V(2).Info("component doesn't seem to be a map of properties", cType.String(), key)
			extractedComponent = map[interface{}]interface{}{}
		}

		cmptName := key.(string)
		var cmptParser parser.ComponentPortParser
		var err error
		cmptParser, err = receiverParser.For(logger, cmptName, extractedComponent)
		if err != nil {
			logger.V(2).Info("no parser found for '%s'", cmptName)
			continue
		}

		exprtPorts, err := cmptParser.Ports()
		if err != nil {
			logger.Error(err, "parser for '%s' has returned an error: %w", cmptName, err)
			continue
		}

		if len(exprtPorts) > 0 {
			ports = append(ports, exprtPorts...)
		}
	}

	sort.Slice(ports, func(i, j int) bool {
		return ports[i].Name < ports[j].Name
	})

	return ports, nil
}