Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
merge v0.1.1 to master
Browse files Browse the repository at this point in the history
  • Loading branch information
rlcundiff committed Oct 29, 2019
2 parents cef9201 + 7d23fa5 commit b99ecff
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 24 deletions.
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM kappnav/base-runtime:8-jre-ubi-min as liberty
FROM kappnav/base-runtime:8-jre-ubi-min-alt2 as liberty
ARG LIBERTY_VERSION=19.0.0.4
ARG LIBERTY_SHA=d4a9be40d4e1e6e859b836f68379636eb2c19c6d
ARG LIBERTY_DOWNLOAD_URL=https://repo1.maven.org/maven2/io/openliberty/openliberty-runtime/$LIBERTY_VERSION/openliberty-runtime-$LIBERTY_VERSION.zip
Expand Down Expand Up @@ -79,10 +79,14 @@ RUN mvn install

FROM liberty

ARG VERSION
ARG BUILD_DATE

LABEL name="Application Navigator" \
vendor="kAppNav" \
version="0.1.0" \
release="0.1.0" \
version=$VERSION \
release=$VERSION \
created=$BUILD_DATE \
summary="APIs image for Application Navigator" \
description="This image contains the APIs for Application Navigator"

Expand Down
15 changes: 4 additions & 11 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
#!/bin/bash
set -Eeo pipefail
if [ $(arch) = "ppc64le" ]; then
ARCH=ppc64le
elif [ $(arch) = "s390x" ]; then
ARCH=s390x
else
ARCH=amd64
fi
#IMAGE=kappnav-init-$ARCH
IMAGE=kappnav-apis

echo "Building ${IMAGE}"
IMAGE=kappnav-apis
VERSION=0.1.1

docker build -t ${IMAGE} .
echo "Building ${IMAGE} ${VERSION}"
docker build --build-arg VERSION=$VERSION --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') -t ${IMAGE} .
171 changes: 171 additions & 0 deletions src/main/java/application/rest/v1/actions/AppPodlistFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright 2019 IBM Corporation
*
* 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
*
* http://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 application.rest.v1.actions;

import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import application.rest.v1.ComponentInfoRegistry;
import application.rest.v1.KAppNavEndpoint;
import application.rest.v1.Selector;
import application.rest.v1.ComponentKind;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;

// apppodlist() or apppodlist(<application-namespace>,<application-name>)
// Returns a list of pod names from all deployments belonging to specified application.
// Return value structure is: { "pods" : "[<name1>,<name2>,etc]" }
public class AppPodlistFunction implements Function {

private static final String PODS_PROPERTY_NAME = "pods";
private static final String DEPLOYMENT_KIND = "Deployment";
private static final String APPLICATION_KIND = "Application";
private static final String POD_KIND = "Pod";
private static final String METADATA_PROPERTY_NAME = "metadata";

@Override
public String getName() {
return "apppodlist";
}

@Override
public boolean allowedParameterCount(int parameterCount) {
return parameterCount == 0 || parameterCount == 2;
}

@Override
public String invoke(ResolutionContext context, List<String> parameters) {

final ApiClient client = context.getApiClient();
final ComponentInfoRegistry registry = context.getComponentInfoRegistry();
final JsonObject resource;

if (parameters.size() == 0) {
if (!APPLICATION_KIND.equals(context.getResourceKind())) {
// The context resource isn't an application.
return null;
}
resource = context.getResource();
}
// Two parameter function
else {
// parameters.get(0) :: appNamespace
// parameters.get(1) :: appName
final String appNamespace = parameters.get(0);
final String appName = parameters.get(1);
try {
//get application namespaced object
Object o = registry.getNamespacedObject(client, APPLICATION_KIND, appNamespace, appName);
resource = KAppNavEndpoint.getItemAsObject(client, o);
}
catch (ApiException e) {
return null;
}
}
List<ComponentKind> componentKinds = ComponentKind.getComponentKinds(client, resource);
return getPodListFromApp(client, resource, registry, componentKinds);
}


private String getPodListFromApp(ApiClient client, JsonObject resource, ComponentInfoRegistry registry, List<ComponentKind> componentKinds) {
PodlistResult result = new PodlistResult();
//retrieve app namespace from the resource
String appNamespace = getNameSpaceFromResource(resource);

//check if app contains "Deployment" component kind
componentKinds.forEach(v -> {
if (v.kind.equals("Deployment")) {
final Selector aSelector = Selector.getSelector(resource);
if (!aSelector.isEmpty()) {
final String alabelSelector = aSelector.toString();
try {
//retrieve deployment object from app with the labels matching
Object deplO = registry.listClusterObject(client, DEPLOYMENT_KIND, null, alabelSelector, null, null);
List<JsonObject> deployments = KAppNavEndpoint.getItemsAsList(client, deplO);
deployments.forEach(d -> {
Selector dSelector = Selector.getSelector(d);
if (!dSelector.isEmpty()) {
final String dlabelSelector = dSelector.toString();
try {
//retrieve pod object from deployment with the label matching
Object podO = registry.listClusterObject(client, POD_KIND, null, dlabelSelector, null, null);
List<JsonObject> items = KAppNavEndpoint.getItemsAsList(client, podO);
items.forEach(p -> {
JsonElement element = p.get(METADATA_PROPERTY_NAME);
if (element != null && element.isJsonObject()) {
JsonObject pmetadata = element.getAsJsonObject();
//find pod's namespace matching with app's namespace
JsonElement pNamespace = pmetadata.get("namespace");
if (pNamespace!= null && pNamespace.isJsonPrimitive()) {
if (pNamespace.getAsString().equals(appNamespace)) {
//get pod name
JsonElement pName = pmetadata.get("name");
if (pName!= null && pName.isJsonPrimitive()) {
result.add(pName.getAsString());
}
}
}
}
});
} catch (ApiException e) {}
}
});
} catch (ApiException e) {}
}
}
});
return result.getJSON();
}

private String getNameSpaceFromResource(JsonObject resource) {
String appnamespace = null;
JsonElement element = resource.get(METADATA_PROPERTY_NAME);
if (element != null && element.isJsonObject()) {
JsonObject metadata = element.getAsJsonObject();
JsonElement namespace = metadata.get("namespace");
if (namespace!= null && namespace.isJsonPrimitive()) {
appnamespace = namespace.getAsString();
}
}
return appnamespace;
}


static final class PodlistResult {
private final JsonObject o;
private final JsonArray pods;
// Constructs:
// {
// pods: [ <name1>, <name2>, ... ]
// }
public PodlistResult() {
o = new JsonObject();
o.add(PODS_PROPERTY_NAME, pods = new JsonArray());
}
public void add(final String name) {
if (! pods.toString().contains(name))
pods.add(name);
}
public String getJSON() {
return o.toString();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public final class FunctionResolver implements Resolver {
functions = new HashMap<>();
addFunction(new KubectlGetFunction());
addFunction(new PodlistFunction());
addFunction(new AppPodlistFunction());
addFunction(new ReplicaSetFunction());
}

private static void addFunction(Function function) {
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/application/rest/v1/actions/PodlistFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class PodlistFunction implements Function {

private static final String DEPLOYMENT_KIND = "Deployment";
private static final String POD_KIND = "Pod";
private static final String METADATA_PROPERTY_NAME = "metadata";

@Override
public String getName() {
Expand Down Expand Up @@ -77,30 +78,56 @@ public String invoke(ResolutionContext context, List<String> parameters) {
return null;
}
}

return getPodListFromDeployment(client, resource, registry);
}

private String getPodListFromDeployment(ApiClient client, JsonObject resource, ComponentInfoRegistry registry) {
PodlistResult result = new PodlistResult();
//retrieve deployment namespace from the resource
String deplNamespace = getNameSpaceFromResource(resource);

// Retrieve the pods for the deployment.
final Selector selector = Selector.getSelector(resource);
if (!selector.isEmpty()) {
final String labelSelector = selector.toString();
try {
Object o = registry.listClusterObject(client, POD_KIND, null, labelSelector, null, null);
List<JsonObject> items = KAppNavEndpoint.getItemsAsList(client, o);
PodlistResult result = new PodlistResult();

items.forEach(v -> {
JsonElement element = v.get("metadata");
JsonElement element = v.get(METADATA_PROPERTY_NAME);
if (element != null && element.isJsonObject()) {
JsonObject metadata = element.getAsJsonObject();
element = metadata.get("name");
if (element != null && element.isJsonPrimitive()) {
result.add(element.getAsString());
}
//find pod's namespace matching with deployment's namespace
JsonElement pNamespace = metadata.get("namespace");
if (pNamespace!= null && pNamespace.isJsonPrimitive()) {
if (pNamespace.getAsString().equals(deplNamespace)) {
//get pod name
JsonElement pName = metadata.get("name");
if (pName!= null && pName.isJsonPrimitive()) {
result.add(pName.getAsString());
}
}
}
}
});
return result.getJSON();
});
}
catch (ApiException e) {}
}
return null;
return result.getJSON();
}

private String getNameSpaceFromResource(JsonObject resource) {
String deplnamespace = null;
JsonElement element = resource.get(METADATA_PROPERTY_NAME);
if (element != null && element.isJsonObject()) {
JsonObject metadata = element.getAsJsonObject();
JsonElement namespace = metadata.get("namespace");
if (namespace!= null && namespace.isJsonPrimitive()) {
deplnamespace = namespace.getAsString();
}
}
return deplnamespace;
}

static final class PodlistResult {
Expand Down
Loading

0 comments on commit b99ecff

Please sign in to comment.