Java Code Examples for io.fabric8.kubernetes.client.KubernetesClient#getNamespace()

The following examples show how to use io.fabric8.kubernetes.client.KubernetesClient#getNamespace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServicePresentCondition.java    From dekorate with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
 Optional<OnServicePresentCondition> annotation = context.getElement().map(e -> e.getAnnotation(OnServicePresentCondition.class));
 if (!annotation.isPresent()) {
  return ConditionEvaluationResult.enabled("Condition not found!");
 }
 OnServicePresentCondition condition = annotation.get();
  try {
    KubernetesClient client = getKubernetesClient(context);
    String namespace = Strings.isNotNullOrEmpty(condition.namespace()) ? condition.namespace() :  client.getNamespace();
    Service service = getKubernetesClient(context).services().inNamespace(namespace).withName(condition.value()).get();
    if (service != null) {
      return ConditionEvaluationResult.enabled("Found service:" + condition.value() + " in namespace:" + namespace + " .");
    } else {
      return ConditionEvaluationResult.disabled("Could not find service:" + condition.value() + " in namespace:" + namespace + " .");
    }
  } catch (Throwable t) {
    return ConditionEvaluationResult.disabled("Could not lookup for service.");
  }
}
 
Example 2
Source File: ConfigUtils.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
public static <C extends AbstractConfigProperties> String getApplicationNamespace(
		KubernetesClient client, String configNamespace, String configurationTarget) {
	String namespace = configNamespace;
	if (StringUtils.isEmpty(namespace)) {
		if (LOG.isDebugEnabled()) {
			LOG.debug(configurationTarget
					+ " namespace has not been set, taking it from client (ns="
					+ client.getNamespace() + ")");
		}

		namespace = client.getNamespace();
	}

	return namespace;
}
 
Example 3
Source File: ConfigUtils.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
public static <C extends AbstractConfigProperties> String getApplicationNamespace(KubernetesClient client, Environment env, C config) {
    String namespace = config.getNamespace();
    if (StringUtils.isEmpty(namespace)) {
        LOGGER.debug(config.getConfigurationTarget() + " namespace has not been set, taking it from client (ns={})",
                client.getNamespace());

        namespace = client.getNamespace();
    }

    return namespace;
}
 
Example 4
Source File: ZipkinKubernetesAutoConfiguration.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Bean
public ZipkinSpanReporter reporter(KubernetesClient client, KubernetesZipkinDiscoveryProperties discoveryProperties, SpanMetricReporter spanMetricReporter, ZipkinProperties zipkin) {
    String serviceName = discoveryProperties.getServiceName();
    String serviceNamespace = Utils.isNotNullOrEmpty(discoveryProperties.getServiceNamespace()) ? discoveryProperties.getServiceNamespace() : client.getNamespace();

    List<ServiceInstance> services = getInstances(client, serviceName, serviceNamespace);
    String serviceUrl = services.stream()
            .findFirst()
            .map(s -> s.getUri().toString())
            .orElse(null);

    return serviceUrl == null || serviceUrl.isEmpty()
            ? new NullZipkinSpanReporter()
            : new HttpZipkinSpanReporter(serviceUrl, zipkin.getFlushInterval(), zipkin.getCompression().isEnabled(), spanMetricReporter);
}
 
Example 5
Source File: KubernetesCloud.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Check not too many already running.
 *
 */
private boolean addProvisionedSlave(@Nonnull PodTemplate template, @CheckForNull Label label, int scheduledCount) throws Exception {
    if (containerCap == 0) {
        return true;
    }

    KubernetesClient client = connect();
    String templateNamespace = template.getNamespace();
    // If template's namespace is not defined, take the
    // Kubernetes Namespace.
    if (Strings.isNullOrEmpty(templateNamespace)) {
        templateNamespace = client.getNamespace();
    }

    Map<String, String> podLabels = getPodLabelsMap();
    List<Pod> allActiveSlavePods = getActiveSlavePods(client, templateNamespace, podLabels);
    if (allActiveSlavePods != null && containerCap <= allActiveSlavePods.size() + scheduledCount) {
        LOGGER.log(Level.INFO,
                "Maximum number of concurrently running agent pods ({0}) reached for Kubernetes Cloud {4}, not provisioning: {1} running or pending in namespace {2} with Kubernetes labels {3}",
                new Object[] { containerCap, allActiveSlavePods.size() + scheduledCount, templateNamespace, getLabels(), name });
        return false;
    }

    Map<String, String> labelsMap = new HashMap<>(podLabels);
    labelsMap.putAll(template.getLabelsMap());
    List<Pod> activeTemplateSlavePods = getActiveSlavePods(client, templateNamespace, labelsMap);
    if (activeTemplateSlavePods != null && allActiveSlavePods != null && template.getInstanceCap() <= activeTemplateSlavePods.size() + scheduledCount) {
        LOGGER.log(Level.INFO,
                "Maximum number of concurrently running agent pods ({0}) reached for template {1} in Kubernetes Cloud {6}, not provisioning: {2} running or pending in namespace {3} with label \"{4}\" and Kubernetes labels {5}",
                new Object[] { template.getInstanceCap(), template.getName(), activeTemplateSlavePods.size() + scheduledCount,
                        templateNamespace, label == null ? "" : label.toString(), labelsMap, name });
        return false;
    }
    return true;
}
 
Example 6
Source File: ConfigMapExample.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
  Config config = new ConfigBuilder().build();
  KubernetesClient client = new DefaultKubernetesClient(config);

  String namespace = null;
  if (args.length > 0) {
    namespace = args[0];
  }
  if (namespace == null) {
    namespace = client.getNamespace();
  }
  if (namespace == null) {
    namespace = "default";
  }

  String name = "cheese";
  try {
    Resource<ConfigMap, DoneableConfigMap> configMapResource = client.configMaps().inNamespace(namespace).withName(name);


    ConfigMap configMap = configMapResource.createOrReplace(new ConfigMapBuilder().
        withNewMetadata().withName(name).endMetadata().
        addToData("foo", "" + new Date()).
        addToData("bar", "beer").
        build());

    log("Upserted ConfigMap at " + configMap.getMetadata().getSelfLink() + " data " + configMap.getData());

  } finally {
    client.close();
  }
}
 
Example 7
Source File: AppConfig.java    From rabbitmq-operator with Apache License 2.0 4 votes vote down vote up
@Bean
public String namespace(final KubernetesClient client) {
    return client.getNamespace();
}