/*
 * Copyright 2013-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.kubernetes.profile;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.cloud.kubernetes.StandardPodUtils;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

public class KubernetesProfileEnvironmentPostProcessor
		implements EnvironmentPostProcessor, Ordered {

	private static final Log LOG = LogFactory
			.getLog(KubernetesProfileEnvironmentPostProcessor.class);

	// Before ConfigFileApplicationListener so values there can use these ones
	private static final int ORDER = ConfigFileApplicationListener.DEFAULT_ORDER - 1;

	private static final String KUBERNETES_PROFILE = "kubernetes";

	@Override
	public void postProcessEnvironment(ConfigurableEnvironment environment,
			SpringApplication application) {
		final StandardPodUtils podUtils = new StandardPodUtils(
				new DefaultKubernetesClient());
		if (podUtils.isInsideKubernetes()) {
			if (hasKubernetesProfile(environment)) {
				if (LOG.isDebugEnabled()) {
					LOG.debug("'kubernetes' already in list of active profiles");
				}
			}
			else {
				if (LOG.isDebugEnabled()) {
					LOG.debug("Adding 'kubernetes' to list of active profiles");
				}
				environment.addActiveProfile(KUBERNETES_PROFILE);
			}
		}
		else {
			if (LOG.isDebugEnabled()) {
				LOG.warn(
						"Not running inside kubernetes. Skipping 'kubernetes' profile activation.");
			}
		}
	}

	private boolean hasKubernetesProfile(Environment environment) {
		for (String activeProfile : environment.getActiveProfiles()) {
			if (KUBERNETES_PROFILE.equalsIgnoreCase(activeProfile)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int getOrder() {
		return ORDER;
	}

}

@@ -1778,16 +1778,179 @@
 tion) %7B%0A
+%0A%09%09final String enabledStr = environment.getProperty(%22spring.cloud.kubernetes.enabled%22, %22false%22);%0A%09%09if (%22true%22.equals(enabledStr.toLowerCase())) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A
 %09%09final 

Make Post Processor honor spring.cloud.kubernetes.enabled (#421)