use of io.fabric8.openshift.api.model.BuildConfigSpec in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method updateOrCreateBuildConfig.
private String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, ImageConfiguration imageConfig) {
ImageName imageName = new ImageName(imageConfig.getName());
String buildName = getS2IBuildName(config, imageName);
String imageStreamName = getImageStreamName(imageName);
String outputImageStreamTag = imageStreamName + ":" + (imageName.getTag() != null ? imageName.getTag() : "latest");
BuildStrategy buildStrategyResource = createBuildStrategy(imageConfig, config.getOpenshiftBuildStrategy());
BuildOutput buildOutput = new BuildOutputBuilder().withNewTo().withKind("ImageStreamTag").withName(outputImageStreamTag).endTo().build();
// Fetch exsting build config
BuildConfig buildConfig = client.buildConfigs().withName(buildName).get();
if (buildConfig != null) {
// lets verify the BC
BuildConfigSpec spec = getBuildConfigSpec(buildConfig);
validateSourceType(buildName, spec);
if (config.getBuildRecreateMode().isBuildConfig()) {
// Delete and recreate afresh
client.buildConfigs().withName(buildName).delete();
return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
} else {
// Update & return
return updateBuildConfig(client, buildName, buildStrategyResource, buildOutput, spec);
}
} else {
// Create afresh
return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
}
}
use of io.fabric8.openshift.api.model.BuildConfigSpec in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method findBuildConfigForGitRepo.
protected BuildConfig findBuildConfigForGitRepo(OpenShiftClient openShiftClient, String namespace, String gitRepoUrl, String gitRef) throws MojoExecutionException {
BuildConfigList buildConfigList = openShiftClient.buildConfigs().inNamespace(namespace).list();
if (buildConfigList != null) {
List<BuildConfig> items = buildConfigList.getItems();
if (items != null) {
for (BuildConfig item : items) {
BuildConfigSpec spec = item.getSpec();
if (spec != null) {
BuildSource source = spec.getSource();
if (source != null) {
GitBuildSource git = source.getGit();
if (git != null) {
String uri = git.getUri();
String ref = git.getRef();
if (Objects.equal(gitRepoUrl, uri)) {
if (Strings.isNullOrBlank(gitRef) && Strings.isNullOrBlank(ref)) {
return item;
}
if (Objects.equal(gitRef, ref)) {
return item;
}
}
}
}
}
}
}
}
return null;
}
use of io.fabric8.openshift.api.model.BuildConfigSpec in project fabric8 by fabric8io.
the class Builds method configureDefaultBuildConfig.
public static BuildConfig configureDefaultBuildConfig(BuildConfig buildConfig, String name, String gitUrl, boolean foundExistingGitUrl, String buildImageStream, String buildImageTag, String s2iCustomBuilderImage, String secret, String jenkinsUrl) {
BuildConfigSpec spec = buildConfig.getSpec();
if (spec == null) {
spec = new BuildConfigSpec();
buildConfig.setSpec(spec);
}
if (!foundExistingGitUrl && Strings.isNotBlank(gitUrl)) {
BuildSource source = spec.getSource();
if (source == null) {
source = new BuildSource();
spec.setSource(source);
}
source.setType("Git");
GitBuildSource git = source.getGit();
if (git == null) {
git = new GitBuildSource();
source.setGit(git);
}
git.setUri(gitUrl);
}
if (Strings.isNotBlank(buildImageStream) && Strings.isNotBlank(buildImageTag)) {
BuildStrategy strategy = spec.getStrategy();
if (strategy == null) {
strategy = new BuildStrategy();
spec.setStrategy(strategy);
}
// TODO only do this if we are using Jenkins?
strategy.setType("JenkinsPipeline");
JenkinsPipelineBuildStrategy buildStrategy = strategy.getJenkinsPipelineStrategy();
if (buildStrategy == null) {
buildStrategy = new JenkinsPipelineBuildStrategy();
strategy.setJenkinsPipelineStrategy(buildStrategy);
}
ListEnvVarBuilder envBuilder = new ListEnvVarBuilder();
if (Strings.isNotBlank(jenkinsUrl)) {
envBuilder.withEnvVar("BASE_URI", jenkinsUrl);
}
buildStrategy.setJenkinsfilePath("Jenkinsfile");
}
List<BuildTriggerPolicy> triggers = spec.getTriggers();
if (triggers == null) {
triggers = new ArrayList<>();
}
if (triggers.isEmpty()) {
triggers.add(new BuildTriggerPolicyBuilder().withType("GitHub").withNewGithub().withSecret(secret).endGithub().build());
triggers.add(new BuildTriggerPolicyBuilder().withType("Generic").withNewGeneric().withSecret(secret).endGeneric().build());
spec.setTriggers(triggers);
}
return buildConfig;
}
use of io.fabric8.openshift.api.model.BuildConfigSpec in project fabric8 by fabric8io.
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.openshift.api.model.BuildConfigSpec in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method getBuildConfigSpec.
private BuildConfigSpec getBuildConfigSpec(BuildConfig buildConfig) {
BuildConfigSpec spec = buildConfig.getSpec();
if (spec == null) {
spec = new BuildConfigSpec();
buildConfig.setSpec(spec);
}
return spec;
}
Aggregations