use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.
the class KubernetesHelper method createJenkinshiftOpenShiftClient.
public static OpenShiftClient createJenkinshiftOpenShiftClient(String jenkinshiftUrl) {
Config config = createJenkinshiftConfig(jenkinshiftUrl);
// TODO until jenkinshift supports HTTPS lets disable HTTPS by default
// openShiftClient = new DefaultOpenShiftClient(jenkinshiftUrl);
JenkinShiftClient jenkinShiftClient = new JenkinShiftClient(config);
jenkinShiftClient.updateHttpClient(config);
return jenkinShiftClient;
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.
the class KubernetesHelper method getServiceURLInCurrentNamespace.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURLInCurrentNamespace(KubernetesClient client, String serviceName, String serviceProtocol, String servicePortName, boolean serviceExternal) {
Service srv = null;
String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else {
srv = client.services().withName(serviceName).get();
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName);
}
if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
RouteList routeList = openShiftClient.routes().list();
for (Route route : routeList.getItems()) {
if (route.getSpec().getTo().getName().equals(serviceName)) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
}
ServicePort port = findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in current namespace is head-less. Search for endpoints instead.");
}
return (serviceProto + "://" + clusterIP + ":" + port.getPort()).toLowerCase();
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.
the class KubernetesHelper method findKubernetesResourcesOnClasspath.
/**
* Returns the kubernetes resources on the classpath of the current project
*/
public static List<HasMetadata> findKubernetesResourcesOnClasspath(Controller controller) throws IOException {
String resourceName = "kubernetes.yml";
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE) && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
resourceName = "openshift.yml";
}
URL configUrl = findConfigResource("/META-INF/fabric8/" + resourceName);
if (configUrl == null) {
configUrl = findConfigResource("kubernetes.json");
}
if (configUrl != null) {
String configText = IOHelpers.loadFully(configUrl);
Object dto = null;
String configPath = configUrl.getPath();
if (configPath.endsWith(".yml") || configPath.endsWith(".yaml")) {
dto = loadYaml(configText, KubernetesResource.class);
} else {
dto = loadJson(configText);
}
KubernetesList kubeList = KubernetesHelper.asKubernetesList(dto);
List<HasMetadata> items = kubeList.getItems();
return items;
} else {
return new ArrayList<>();
}
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.
the class KubernetesHelper method getServiceURL.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURL(KubernetesClient client, String serviceName, String serviceNamespace, String serviceProtocol, String servicePortName, boolean serviceExternal) {
Service srv = null;
String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
// Use specified or fallback namespace.
String actualNamespace = Strings.isNotBlank(serviceNamespace) ? serviceNamespace : client.getNamespace();
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else if (Strings.isNotBlank(actualNamespace)) {
try {
srv = client.services().inNamespace(actualNamespace).withName(serviceName).get();
} catch (Exception e) {
LOGGER.warn("Could not lookup service:" + serviceName + " in namespace:" + actualNamespace + ", due to: " + e.getMessage());
}
}
if (srv == null) {
// lets try use environment variables
String hostAndPort = Systems.getServiceHostAndPort(serviceName, "", "");
if (!hostAndPort.startsWith(":")) {
return serviceProto + "://" + hostAndPort;
}
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName + " in namespace: " + actualNamespace);
}
String answer = getOrCreateAnnotations(srv).get(Annotations.Service.EXPOSE_URL);
if (Strings.isNotBlank(answer)) {
return answer;
}
try {
if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
Route route = openShiftClient.routes().inNamespace(actualNamespace).withName(serviceName).get();
if (route != null) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
} catch (KubernetesClientException e) {
if (e.getCode() == 403) {
LOGGER.warn("Could not lookup route:" + serviceName + " in namespace:" + actualNamespace + ", due to: " + e.getMessage());
} else {
throw e;
}
}
ServicePort port = findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in namespace:" + serviceNamespace + "is head-less. Search for endpoints instead.");
}
Integer portNumber = port.getPort();
if (Strings.isNullOrBlank(clusterIP)) {
IngressList ingresses = client.extensions().ingresses().inNamespace(serviceNamespace).list();
if (ingresses != null) {
List<Ingress> items = ingresses.getItems();
if (items != null) {
for (Ingress item : items) {
String ns = getNamespace(item);
if (Objects.equal(serviceNamespace, ns)) {
IngressSpec spec = item.getSpec();
if (spec != null) {
List<IngressRule> rules = spec.getRules();
List<IngressTLS> tls = spec.getTls();
if (rules != null) {
for (IngressRule rule : rules) {
HTTPIngressRuleValue http = rule.getHttp();
if (http != null) {
List<HTTPIngressPath> paths = http.getPaths();
if (paths != null) {
for (HTTPIngressPath path : paths) {
IngressBackend backend = path.getBackend();
if (backend != null) {
String backendServiceName = backend.getServiceName();
if (serviceName.equals(backendServiceName) && portsMatch(port, backend.getServicePort())) {
String pathPostfix = path.getPath();
if (tls != null) {
for (IngressTLS tlsHost : tls) {
List<String> hosts = tlsHost.getHosts();
if (hosts != null) {
for (String host : hosts) {
if (Strings.isNotBlank(host)) {
if (Strings.isNullOrBlank(pathPostfix)) {
pathPostfix = "/";
}
return "https://" + URLUtils.pathJoin(host, pathPostfix);
}
}
}
}
}
answer = rule.getHost();
if (Strings.isNotBlank(answer)) {
if (Strings.isNullOrBlank(pathPostfix)) {
pathPostfix = "/";
}
return "http://" + URLUtils.pathJoin(answer, pathPostfix);
}
}
}
}
}
}
}
}
}
}
}
}
}
// lets try use the status on GKE
ServiceStatus status = srv.getStatus();
if (status != null) {
LoadBalancerStatus loadBalancerStatus = status.getLoadBalancer();
if (loadBalancerStatus != null) {
List<LoadBalancerIngress> loadBalancerIngresses = loadBalancerStatus.getIngress();
if (loadBalancerIngresses != null) {
for (LoadBalancerIngress loadBalancerIngress : loadBalancerIngresses) {
String ip = loadBalancerIngress.getIp();
if (Strings.isNotBlank(ip)) {
clusterIP = ip;
break;
}
}
}
}
}
}
if (Strings.isNullOrBlank(clusterIP)) {
// on vanilla kubernetes we can use nodePort to access things externally
boolean found = false;
Integer nodePort = port.getNodePort();
if (nodePort != null) {
try {
NodeList nodeList = client.nodes().list();
if (nodeList != null) {
List<Node> items = nodeList.getItems();
if (items != null) {
for (Node item : items) {
NodeStatus status = item.getStatus();
if (!found && status != null) {
List<NodeAddress> addresses = status.getAddresses();
if (addresses != null) {
for (NodeAddress address : addresses) {
String ip = address.getAddress();
if (Strings.isNotBlank(ip)) {
clusterIP = ip;
portNumber = nodePort;
found = true;
break;
}
}
}
}
if (!found) {
NodeSpec spec = item.getSpec();
if (spec != null) {
clusterIP = spec.getExternalID();
if (Strings.isNotBlank(clusterIP)) {
portNumber = nodePort;
break;
}
}
}
}
}
}
} catch (Exception e) {
// ignore could not find a node!
LOG.warn("Could not find a node!: " + e, e);
}
}
}
return (serviceProto + "://" + clusterIP + ":" + portNumber).toLowerCase();
}
use of io.fabric8.openshift.client.OpenShiftClient 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);
}
}
}
}
Aggregations