com.kapeta.spring.config.providers.KubernetesConfigProvider Maven / Gradle / Ivy
/*
* Copyright 2023 Kapeta Inc.
* SPDX-License-Identifier: MIT
*/
package com.kapeta.spring.config.providers;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kapeta.spring.config.providers.types.BlockInstanceDetails;
import com.kapeta.spring.config.providers.types.InstanceOperator;
import com.kapeta.spring.config.providers.types.ResourceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static com.kapeta.spring.config.KapetaDefaultConfig.createDefaultObjectMapper;
/**
* Configuration for remotely running blocks (In Kubernetes)
*
* Will read configuration from the ENV vars
*/
public class KubernetesConfigProvider implements KapetaConfigurationProvider {
private static final Logger log = LoggerFactory.getLogger(KubernetesConfigProvider.class);
private final ObjectMapper objectMapper = createDefaultObjectMapper();
private final String systemId;
private final Environment environment;
private Map instanceHosts;
public KubernetesConfigProvider(String systemId, Environment environment) {
this.systemId = systemId;
this.environment = environment;
}
@Override
public int getServerPort(String portType) {
if (!StringUtils.hasText(portType)) {
portType = DEFAULT_SERVER_PORT_TYPE;
}
String envVar = "KAPETA_PROVIDER_PORT_%s".formatted(toEnvName(portType));
String envVarValue = environment.getProperty(envVar);
if (StringUtils.hasText(envVarValue)) {
return Integer.parseInt(envVarValue);
}
return 80; //We default to port 80
}
@Override
public String getServerHost() {
if (environment.containsProperty("KAPETA_PROVIDER_HOST")) {
return environment.getProperty("KAPETA_PROVIDER_HOST");
}
//Any host within docker container
return "0.0.0.0";
}
@Override
public String getSystemId() {
return systemId;
}
@Override
public Environment getEnvironment() {
return environment;
}
@Override
public String getServiceAddress(String serviceName, String portType) {
var envVarName = "KAPETA_CONSUMER_SERVICE_%s_%s".formatted(toEnvName(serviceName), toEnvName(portType));
return requireEnvVar(envVarName);
}
@Override
public ResourceInfo getResourceInfo(String resourceType, String portType, String resourceName) {
var envVarName = "KAPETA_CONSUMER_RESOURCE_%s_%s".formatted(toEnvName(resourceName), toEnvName(portType));
var value = requireEnvVar(envVarName);
try {
return objectMapper.readValue(value, ResourceInfo.class);
} catch (IOException e) {
throw new IllegalStateException("Failed to parse resource info from env var: %s".formatted(envVarName), e);
}
}
@Override
public Map getInstanceConfig() {
var envVarName = "KAPETA_INSTANCE_CONFIG";
if (environment.containsProperty(envVarName)) {
var value = environment.getProperty(envVarName);
var typeRef = new TypeReference