use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.
the class KubernetesConfigAdminBridge method updateConfig.
// **********************
// ConfigAdmin
// **********************
private void updateConfig(ConfigMap map) {
Long ver = Long.parseLong(map.getMetadata().getResourceVersion());
String pid = map.getMetadata().getLabels().get(pidLabel);
String[] p = parsePid(pid);
try {
final Configuration config = getConfiguration(configAdmin.get(), pid, p[0], p[1]);
final Map<String, String> configMapData = map.getData();
if (configMapData == null) {
LOGGER.debug("Ignoring configuration pid={}, (empty)", config.getPid());
return;
}
final Dictionary<String, Object> props = config.getProperties();
final Hashtable<String, Object> configAdmCfg = props != null ? new Hashtable<String, Object>() : null;
Hashtable<String, Object> configMapCfg = new Hashtable<>();
/*
* If there is a key named as pid + ".cfg" (as the pid file on karaf)
* it will be used as source of configuration instead of the content
* of the data field. The name of the key can be changed by setting
* the key fabric8.config.pid.cfg
*
* i.e.
* apiVersion: v1
* data:
* org.ops4j.pax.logging.cfg: |+
* log4j.rootLogger=DEBUG, out
*/
String pidCfg = configMapData.get(FABRIC8_CONFIG_PID_CFG);
if (pidCfg == null) {
pidCfg = pid + ".cfg";
}
String cfgString = configMapData.get(pidCfg);
if (Utils.isNotNullOrEmpty(cfgString)) {
java.util.Properties cfg = new java.util.Properties();
cfg.load(new StringReader(cfgString));
for (Map.Entry<Object, Object> entry : cfg.entrySet()) {
configMapCfg.put((String) entry.getKey(), entry.getValue());
}
} else {
for (Map.Entry<String, String> entry : map.getData().entrySet()) {
configMapCfg.put(entry.getKey(), entry.getValue());
}
}
/*
* Configure if mete-data should be added to the Config Admin or not
*/
boolean meta = configMapData.containsKey(FABRIC8_CONFIG_META) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_META)) : configMeta;
/*
* Configure if ConfigMap data should be merge with ConfigAdmin or it
* should override it.
*/
boolean merge = configMapData.containsKey(FABRIC8_CONFIG_MERGE) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_MERGE)) : configMerge;
if (configAdmCfg != null) {
Long oldVer = (Long) props.get(FABRIC8_K8S_META_RESOURCE_VERSION);
if (oldVer != null && (oldVer >= ver)) {
LOGGER.debug("Ignoring configuration pid={}, oldVersion={} newVersion={} (no changes)", config.getPid(), oldVer, ver);
return;
}
for (Enumeration<String> e = props.keys(); e.hasMoreElements(); ) {
String key = e.nextElement();
Object val = props.get(key);
configAdmCfg.put(key, val);
}
}
if (shouldUpdate(configAdmCfg, configMapCfg)) {
LOGGER.debug("Updating configuration pid={}", config.getPid());
if (meta) {
configMapCfg.put(FABRIC8_PID, pid);
configMapCfg.put(FABRIC8_K8S_META_RESOURCE_VERSION, ver);
configMapCfg.put(FABRIC8_K8S_META_NAME, map.getMetadata().getName());
configMapCfg.put(FABRIC8_K8S_META_NAMESPACE, map.getMetadata().getNamespace());
}
if (merge && configAdmCfg != null) {
for (Map.Entry<String, Object> entry : configMapCfg.entrySet()) {
// Do not override ConfigAdmin meta data
if (!CM_META_KEYS.contains(entry.getKey())) {
configAdmCfg.put(entry.getKey(), entry.getValue());
}
}
configMapCfg = configAdmCfg;
}
config.update(configMapCfg);
} else {
LOGGER.debug("Ignoring configuration pid={} (no changes)", config.getPid());
}
} catch (Exception e) {
LOGGER.warn("", e);
}
}
use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.
the class SessionListener method applyConfiguration.
private boolean applyConfiguration(KubernetesClient client, Controller controller, Configuration configuration, Session session, List<KubernetesList> kubeConfigs) throws Exception {
Logger log = session.getLogger();
Map<Integer, Callable<Boolean>> conditions = new TreeMap<>();
Callable<Boolean> sessionPodsReady = new SessionPodsAreReady(client, session);
Callable<Boolean> servicesReady = new SessionServicesAreReady(client, session, configuration);
Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
for (KubernetesList c : kubeConfigs) {
entities.addAll(enhance(session, configuration, c).getItems());
}
if (containsImageStreamResources(entities)) {
// no need to use a local image registry
// as we are using OpenShift and
} else {
String registry = getLocalDockerRegistry();
if (Strings.isNotBlank(registry)) {
log.status("Adapting resources to pull images from registry: " + registry);
addRegistryToImageNameIfNotPresent(entities, registry);
} else {
log.status("No local fabric8 docker registry found");
}
}
List<Object> items = new ArrayList<>();
items.addAll(entities);
// Ensure services are processed first.
Collections.sort(items, new Comparator<Object>() {
@Override
public int compare(Object left, Object right) {
if (left instanceof Service) {
return -1;
} else if (right instanceof Service) {
return 1;
} else {
return 0;
}
}
});
boolean isOpenshift = client.isAdaptable(OpenShiftClient.class);
String namespace = session.getNamespace();
String routeDomain = null;
if (Strings.isNotBlank(configuration.getKubernetesDomain())) {
routeDomain = configuration.getKubernetesDomain();
}
preprocessEnvironment(client, controller, configuration, session);
Set<HasMetadata> extraEntities = new TreeSet<>(new HasMetadataComparator());
for (Object entity : items) {
if (entity instanceof Pod) {
Pod pod = (Pod) entity;
log.status("Applying pod:" + getName(pod));
Set<Secret> secrets = generateSecrets(client, session, pod.getMetadata());
String serviceAccountName = pod.getSpec().getServiceAccountName();
if (Strings.isNotBlank(serviceAccountName)) {
generateServiceAccount(client, session, secrets, serviceAccountName);
}
controller.applyPod(pod, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof Service) {
Service service = (Service) entity;
String serviceName = getName(service);
log.status("Applying service:" + serviceName);
controller.applyService(service, session.getId());
conditions.put(2, servicesReady);
if (isOpenshift) {
Route route = Routes.createRouteForService(routeDomain, namespace, service, log);
if (route != null) {
log.status("Applying route for:" + serviceName);
controller.applyRoute(route, "route for " + serviceName);
extraEntities.add(route);
}
}
} else if (entity instanceof ReplicationController) {
ReplicationController replicationController = (ReplicationController) entity;
log.status("Applying replication controller:" + getName(replicationController));
Set<Secret> secrets = generateSecrets(client, session, replicationController.getSpec().getTemplate().getMetadata());
String serviceAccountName = replicationController.getSpec().getTemplate().getSpec().getServiceAccountName();
if (Strings.isNotBlank(serviceAccountName)) {
generateServiceAccount(client, session, secrets, serviceAccountName);
}
controller.applyReplicationController(replicationController, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof ReplicaSet || entity instanceof Deployment || entity instanceof DeploymentConfig) {
log.status("Applying " + entity.getClass().getSimpleName() + ".");
controller.apply(entity, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof OAuthClient) {
OAuthClient oc = (OAuthClient) entity;
// these are global so lets create a custom one for the new namespace
ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(oc);
String name = metadata.getName();
if (isOpenshift) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
OAuthClient current = openShiftClient.oAuthClients().withName(name).get();
boolean create = false;
if (current == null) {
current = oc;
create = true;
}
boolean updated = false;
// lets add a new redirect entry
List<String> redirectURIs = current.getRedirectURIs();
String namespaceSuffix = "-" + namespace;
String redirectUri = "http://" + name + namespaceSuffix;
if (Strings.isNotBlank(routeDomain)) {
redirectUri += "." + Strings.stripPrefix(routeDomain, ".");
}
if (!redirectURIs.contains(redirectUri)) {
redirectURIs.add(redirectUri);
updated = true;
}
current.setRedirectURIs(redirectURIs);
log.status("Applying OAuthClient:" + name);
controller.setSupportOAuthClients(true);
if (create) {
openShiftClient.oAuthClients().create(current);
} else {
if (updated) {
// TODO this should work!
// openShiftClient.oAuthClients().withName(name).replace(current);
openShiftClient.oAuthClients().withName(name).delete();
current.getMetadata().setResourceVersion(null);
openShiftClient.oAuthClients().create(current);
}
}
}
} else if (entity instanceof HasMetadata) {
log.status("Applying " + entity.getClass().getSimpleName() + ":" + KubernetesHelper.getName((HasMetadata) entity));
controller.apply(entity, session.getId());
} else if (entity != null) {
log.status("Applying " + entity.getClass().getSimpleName() + ".");
controller.apply(entity, session.getId());
}
}
entities.addAll(extraEntities);
// Wait until conditions are meet.
if (!conditions.isEmpty()) {
Callable<Boolean> compositeCondition = new CompositeCondition(conditions.values());
WaitStrategy waitStrategy = new WaitStrategy(compositeCondition, configuration.getWaitTimeout(), configuration.getWaitPollInterval());
if (!waitStrategy.await()) {
log.error("Timed out waiting for pods/services!");
return false;
} else {
log.status("All pods/services are currently 'running'!");
}
} else {
log.warn("No pods/services/replication controllers defined in the configuration!");
}
return true;
}
use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.
the class Example method listPods.
protected static void listPods(KubernetesClient kube) {
System.out.println("\n\nLooking up pods");
System.out.println("=========================================================================");
PodList pods = kube.pods().list();
// System.out.println("Got pods: " + pods);
List<Pod> items = pods.getItems();
for (Pod item : items) {
System.out.println("Pod " + KubernetesHelper.getName(item) + " with ip: " + item.getStatus().getPodIP() + " created: " + item.getMetadata().getCreationTimestamp());
PodSpec spec = item.getSpec();
if (spec != null) {
List<Container> containers = spec.getContainers();
if (containers != null) {
for (Container container : containers) {
System.out.println("Container " + container.getImage() + " " + container.getCommand() + " ports: " + container.getPorts());
}
}
}
Map<String, ContainerStatus> currentContainers = KubernetesHelper.getCurrentContainers(item);
System.out.println("Has " + currentContainers.size() + " container(s)");
Set<Map.Entry<String, ContainerStatus>> entries = currentContainers.entrySet();
for (Map.Entry<String, ContainerStatus> entry : entries) {
String id = entry.getKey();
ContainerStatus info = entry.getValue();
System.out.println("Current container: " + id + " info: " + info);
}
}
System.out.println();
}
use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.
the class ConfigMaps method createConfigMapAnnotations.
private static Map<String, String> createConfigMapAnnotations(Session session, String status) {
Map<String, String> annotations = new HashMap<>();
File dir = Util.getProjectBaseDir(session);
String gitUrl = Util.findGitUrl(session, dir);
annotations.put(Annotations.Tests.SESSION_ID, session.getId());
annotations.put(Annotations.Tests.TEST_SESSION_STATUS, status);
if (Strings.isNotBlank(gitUrl)) {
annotations.put(Annotations.Builds.GIT_URL, gitUrl);
}
// lets see if there's a maven generated set of pom properties
File pomProperties = new File(dir, "target/maven-archiver/pom.properties");
if (pomProperties.isFile()) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream(pomProperties));
Map<String, String> map = PropertiesHelper.toMap(properties);
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (Strings.isNotBlank(key) && Strings.isNotBlank(value)) {
annotations.put(Annotations.Project.PREFIX + key, value);
}
}
} catch (IOException e) {
session.getLogger().warn("Failed to load " + pomProperties + " file to annotate the namespace: " + e);
}
}
return annotations;
}
use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project docker-maven-plugin by fabric8io.
the class AbstractDockerMojo method execute.
/**
* Entry point for this plugin. It will set up the helper class and then calls
* {@link #executeInternal(ServiceHub)}
* which must be implemented by subclass.
*
* @throws MojoExecutionException
* @throws MojoFailureException
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!skip) {
boolean ansiRestore = Ansi.isEnabled();
File output = null;
if (outputFile != null) {
output = new File(outputFile);
if (output.exists()) {
output.delete();
}
}
log = new AnsiLogger(getLog(), useColorForLogging(), verbose, !settings.getInteractiveMode(), getLogPrefix(), output);
try {
authConfigFactory.setLog(log);
imageConfigResolver.setLog(log);
LogOutputSpecFactory logSpecFactory = new LogOutputSpecFactory(useColor, logStdout, logDate);
ConfigHelper.validateExternalPropertyActivation(project, getAllImages());
DockerAccess access = null;
try {
// The 'real' images configuration to use (configured images + externally resolved images)
this.minimalApiVersion = initImageConfiguration(getBuildTimestamp());
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
access = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
ServiceHub serviceHub = serviceHubFactory.createServiceHub(project, session, access, log, logSpecFactory);
executeInternal(serviceHub);
} catch (IOException | ExecException exp) {
logException(exp);
throw new MojoExecutionException(log.errorMessage(exp.getMessage()), exp);
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
} finally {
if (access != null) {
access.shutdown();
}
}
} finally {
Ansi.setEnabled(ansiRestore);
try {
log.close();
} catch (IOException exp) {
logException(exp);
}
}
}
}
Aggregations