use of io.fabric8.maven.core.model.Configuration in project fabric8 by jboss-fuse.
the class SessionListener method expandTemplate.
protected Object expandTemplate(Controller controller, Configuration configuration, Logger log, String namespace, String sourceName, Object dto) {
if (dto instanceof Template) {
Template template = (Template) dto;
KubernetesHelper.setNamespace(template, namespace);
String parameterNamePrefix = "";
overrideTemplateParameters(template, configuration.getProperties(), parameterNamePrefix);
log.status("Applying template in namespace " + namespace);
controller.installTemplate(template, sourceName);
dto = controller.processTemplate(template, sourceName);
if (dto == null) {
throw new IllegalArgumentException("Failed to process Template!");
}
}
return dto;
}
use of io.fabric8.maven.core.model.Configuration 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.core.model.Configuration in project fabric8 by jboss-fuse.
the class PipelineConfigurationParseTest method testLoadMutateAndLoadPipelineConfiguration.
@Test
public void testLoadMutateAndLoadPipelineConfiguration() throws Exception {
ConfigMap configMap = loadTestConfigMap();
PipelineConfiguration pipelineConfiguration = PipelineConfiguration.getPipelineConfiguration(configMap);
pipelineConfiguration.setCdBranchPatterns(Arrays.asList("cd-1", "cd-2"));
pipelineConfiguration.setCiBranchPatterns(Arrays.asList("ci-1", "ci-2"));
pipelineConfiguration.setJobNameToKindMap(new HashMap<>());
pipelineConfiguration.setJobNamesCD("job-cd");
pipelineConfiguration.setJobNamesCI("job-ci");
HashMap<String, List<String>> cdGitHostAndOrganisationToBranchPatterns = new HashMap<>();
cdGitHostAndOrganisationToBranchPatterns.put("github.com/acme", Arrays.asList("release1"));
cdGitHostAndOrganisationToBranchPatterns.put("github.com/another", Arrays.asList("release2"));
pipelineConfiguration.setCdGitHostAndOrganisationToBranchPatterns(cdGitHostAndOrganisationToBranchPatterns);
// reload the modified configuration
configMap = pipelineConfiguration.createConfigMap();
pipelineConfiguration = PipelineConfiguration.getPipelineConfiguration(configMap);
assertThat(pipelineConfiguration.getCdBranchPatterns()).describedAs("getCdBranchPatterns()").contains("cd-1").contains("cd-2");
assertThat(pipelineConfiguration.getCiBranchPatterns()).describedAs("getCiBranchPatterns()").contains("ci-1").contains("ci-2");
assertThat(pipelineConfiguration.getJobNameToKindMap()).describedAs("getJobNameToKindMap()").containsEntry("job-cd", PipelineKind.CD).containsEntry("job-ci", PipelineKind.CI);
assertThat(pipelineConfiguration.getCdGitHostAndOrganisationToBranchPatterns()).describedAs("getCdGitHostAndOrganisationToBranchPatterns()").containsEntry("github.com/acme", Arrays.asList("release1")).containsEntry("github.com/another", Arrays.asList("release2"));
}
use of io.fabric8.maven.core.model.Configuration in project fabric8 by jboss-fuse.
the class DevOpsConnector method execute.
/**
* For a given project this operation will try to update the associated DevOps resources
*
* @throws Exception
*/
public void execute() throws Exception {
loadConfigFile();
KubernetesClient kubernetes = getKubernetes();
String name = projectName;
if (Strings.isNullOrBlank(name)) {
if (projectConfig != null) {
name = projectConfig.getBuildName();
}
if (Strings.isNullOrBlank(name)) {
name = jenkinsJob;
}
if (Strings.isNullOrBlank(name)) {
name = ProjectRepositories.createBuildName(username, repoName);
if (projectConfig != null) {
projectConfig.setBuildName(name);
}
}
}
if (Strings.isNullOrBlank(projectName)) {
projectName = name;
}
Map<String, String> labels = new HashMap<>();
labels.put("user", username);
labels.put("repo", repoName);
getLog().info("build name " + name);
taiga = null;
taigaProject = null;
try {
taiga = createTaiga();
taigaProject = createTaigaProject(taiga);
} catch (Exception e) {
getLog().error("Failed to load or lazily create the Taiga project: " + e, e);
}
getLog().info("taiga " + taiga);
LetsChatClient letschat = null;
try {
letschat = createLetsChat();
} catch (Exception e) {
getLog().error("Failed to load or lazily create the LetsChat client: " + e, e);
}
getLog().info("letschat " + letschat);
/*
* Create Gerrit Git to if isGerritReview is enabled
*/
if (projectConfig != null && projectConfig.hasCodeReview()) {
try {
createGerritRepo(repoName, gerritUser, gerritPwd, gerritGitInitialCommit, gerritGitRepoDesription);
} catch (Exception e) {
getLog().error("Failed to create GerritGit repo : " + e, e);
}
}
Map<String, String> annotations = new HashMap<>();
jenkinsJobUrl = null;
String jenkinsUrl = null;
try {
jenkinsUrl = getJenkinsServiceUrl(true);
if (Strings.isNotBlank(jenkinsUrl)) {
if (Strings.isNotBlank(jenkinsMonitorView)) {
String url = URLUtils.pathJoin(jenkinsUrl, "/view", jenkinsMonitorView);
annotationLink(annotations, "fabric8.link.jenkins.monitor/", url, "Monitor");
}
if (Strings.isNotBlank(jenkinsPipelineView)) {
String url = URLUtils.pathJoin(jenkinsUrl, "/view", jenkinsPipelineView);
annotationLink(annotations, "fabric8.link.jenkins.pipeline/", url, "Pipeline");
}
if (Strings.isNotBlank(name)) {
jenkinsJobUrl = URLUtils.pathJoin(jenkinsUrl, "/job", name);
annotationLink(annotations, "fabric8.link.jenkins.job/", jenkinsJobUrl, "Job");
}
}
} catch (Exception e) {
getLog().warn("Could not find the Jenkins URL!: " + e, e);
}
getLog().info("jenkins " + jenkinsUrl);
if (!annotationLink(annotations, "fabric8.link.issues/", issueTrackerUrl, issueTrackerLabel)) {
String taigaLink = getProjectPageLink(taiga, taigaProject, this.taigaProjectLinkPage);
annotationLink(annotations, "fabric8.link.taiga/", taigaLink, taigaProjectLinkLabel);
}
if (!annotationLink(annotations, "fabric8.link.team/", teamUrl, teamLabel)) {
String taigaTeamLink = getProjectPageLink(taiga, taigaProject, this.taigaTeamLinkPage);
annotationLink(annotations, "fabric8.link.taiga.team/", taigaTeamLink, taigaTeamLinkLabel);
}
annotationLink(annotations, "fabric8.link.releases/", releasesUrl, releasesLabel);
String chatRoomLink = getChatRoomLink(letschat);
annotationLink(annotations, "fabric8.link.letschat.room/", chatRoomLink, letschatRoomLinkLabel);
annotationLink(annotations, "fabric8.link.repository.browse/", repositoryBrowseLink, repositoryBrowseLabel);
ProjectConfigs.defaultEnvironments(projectConfig, namespace);
String consoleUrl = getServiceUrl(ServiceNames.FABRIC8_CONSOLE, namespace, fabric8ConsoleNamespace);
if (projectConfig != null) {
Map<String, String> environments = projectConfig.getEnvironments();
updateEnvironmentConfigMap(environments, kubernetes, annotations, consoleUrl);
}
addLink("Git", getGitUrl());
Controller controller = createController();
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrJenkinshift();
BuildConfig buildConfig = null;
if (openShiftClient != null) {
try {
buildConfig = openShiftClient.buildConfigs().withName(projectName).get();
} catch (Exception e) {
log.error("Failed to load build config for " + namespace + "/" + projectName + ". " + e, e);
}
log.info("Loaded build config for " + namespace + "/" + projectName + " " + buildConfig);
}
// if we have loaded a build config then lets assume its correct!
boolean foundExistingGitUrl = false;
if (buildConfig != null) {
BuildConfigSpec spec = buildConfig.getSpec();
if (spec != null) {
BuildSource source = spec.getSource();
if (source != null) {
GitBuildSource git = source.getGit();
if (git != null) {
gitUrl = git.getUri();
log.info("Loaded existing BuildConfig git url: " + gitUrl);
foundExistingGitUrl = true;
}
LocalObjectReference sourceSecret = source.getSourceSecret();
if (sourceSecret != null) {
gitSourceSecretName = sourceSecret.getName();
}
}
}
if (!foundExistingGitUrl) {
log.warn("Could not find a git url in the loaded BuildConfig: " + buildConfig);
}
log.info("Loaded gitSourceSecretName: " + gitSourceSecretName);
}
log.info("gitUrl is: " + gitUrl);
if (buildConfig == null) {
buildConfig = new BuildConfig();
}
ObjectMeta metadata = getOrCreateMetadata(buildConfig);
metadata.setName(projectName);
metadata.setLabels(labels);
putAnnotations(metadata, annotations);
Map<String, String> currentAnnotations = metadata.getAnnotations();
if (!currentAnnotations.containsKey(Annotations.Builds.GIT_CLONE_URL)) {
currentAnnotations.put(Annotations.Builds.GIT_CLONE_URL, gitUrl);
}
String localGitUrl = getLocalGitUrl();
if (!currentAnnotations.containsKey(Annotations.Builds.LOCAL_GIT_CLONE_URL) && Strings.isNotBlank(localGitUrl)) {
currentAnnotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localGitUrl);
}
// lets switch to the local git URL to avoid DNS issues in forge or jenkins
if (Strings.isNotBlank(localGitUrl)) {
gitUrl = localGitUrl;
}
Builds.configureDefaultBuildConfig(buildConfig, name, gitUrl, foundExistingGitUrl, buildImageStream, buildImageTag, s2iCustomBuilderImage, secret, jenkinsUrl);
try {
getLog().info("About to apply build config: " + new JSONObject(KubernetesHelper.toJson(buildConfig)).toString(4));
controller.applyBuildConfig(buildConfig, "maven");
getLog().info("Created build configuration for " + name + " in namespace: " + controller.getNamespace() + " at " + kubernetes.getMasterUrl());
} catch (Exception e) {
getLog().error("Failed to create BuildConfig for " + KubernetesHelper.toJson(buildConfig) + ". " + e, e);
}
this.jenkinsJobName = name;
if (isRegisterWebHooks()) {
registerWebHooks();
getLog().info("webhooks done");
}
if (modifiedConfig) {
if (basedir == null) {
getLog().error("Could not save updated " + ProjectConfigs.FILE_NAME + " due to missing basedir");
} else {
try {
ProjectConfigs.saveToFolder(basedir, projectConfig, true);
getLog().info("Updated " + ProjectConfigs.FILE_NAME);
} catch (IOException e) {
getLog().error("Could not save updated " + ProjectConfigs.FILE_NAME + ": " + e, e);
}
}
}
}
use of io.fabric8.maven.core.model.Configuration 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);
}
}
Aggregations