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

Commit

Permalink
merge develop to master for v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rlcundiff committed Nov 21, 2019
2 parents b99ecff + d09a4be commit d56749f
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 62 deletions.
9 changes: 7 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/bin/bash
set -Eeo pipefail

# Travis builds won't have a peer build dir
VERSION=x.x.x
if [ -e ../build/version.sh ]; then
. ../build/version.sh
fi

IMAGE=kappnav-apis
VERSION=0.1.1

echo "Building ${IMAGE} ${VERSION}"
docker build --build-arg VERSION=$VERSION --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') -t ${IMAGE} .
docker build --pull --build-arg VERSION=$VERSION --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') -t ${IMAGE} .
6 changes: 3 additions & 3 deletions src/main/java/application/rest/v1/ActionsEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import javax.inject.Inject;
import javax.validation.constraints.Pattern;
import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
Expand Down Expand Up @@ -160,7 +160,7 @@ public Response resolve(@Pattern(regexp = NAME_PATTERN_ONE_OR_MORE) @PathParam("
public Response executeApplicationCommand(String jsonstr, @Pattern(regexp = NAME_PATTERN_ONE_OR_MORE) @PathParam("application-name") @Parameter(description = "The name of the application") String name,
@Pattern(regexp = NAME_PATTERN_ZERO_OR_MORE) @DefaultValue("default") @QueryParam("namespace") @Parameter(description = "The namespace of the application") String namespace,
@PathParam("command-action-name") @Parameter(description = "The name of the command action") String commandName,
@HeaderParam("x-user") @DefaultValue("") @Parameter(description = "The user that submitted the command action") String user) {
@CookieParam("kappnav-user") @DefaultValue("") @Parameter(description = "The user that submitted the command action") String user) {
return executeCommand(jsonstr, name, APPLICATION_KIND, namespace, commandName, null, null, user);
}

Expand All @@ -185,7 +185,7 @@ public Response executeComponentCommand(String jsonstr, @Pattern(regexp = NAME_P
@PathParam("component-kind") @Parameter(description = "The Kubernetes resource kind for the component") String kind,
@Pattern(regexp = NAME_PATTERN_ZERO_OR_MORE) @DefaultValue("default") @QueryParam("namespace") @Parameter(description = "The namespace of the component") String namespace,
@PathParam("command-action-name") @Parameter(description = "The name of the command action") String commandName,
@HeaderParam("x-user") @DefaultValue("") @Parameter(description = "The user that submitted the command action") String user) {
@CookieParam("kappnav-user") @DefaultValue("") @Parameter(description = "The user that submitted the command action") String user) {
return executeCommand(jsonstr, name, kind, namespace, commandName, appName, appNamespace, user);
}

Expand Down
24 changes: 16 additions & 8 deletions src/main/java/application/rest/v1/ApplicationsEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.gson.JsonObject;

import application.rest.v1.configmaps.ConfigMapProcessor;
import application.rest.v1.configmaps.SectionConfigMapProcessor;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;

Expand All @@ -49,12 +50,13 @@ public class ApplicationsEndpoint extends KAppNavEndpoint {
private static final String APPLICATIONS_PROPERTY_NAME = "applications";
private static final String APPLICATION_PROPERTY_NAME = "application";
private static final String ACTION_MAP_PROPERTY_NAME = "action-map";
private static final String SECTION_MAP_PROPERTY_NAME = "section-map";

@GET
@Produces(MediaType.APPLICATION_JSON)
@Operation(
summary = "Retrieve application objects and action maps.",
description = "Returns a JSON structure of all application objects and their action maps, optionally filtered by namespace."
summary = "Retrieve application objects and action and section maps.",
description = "Returns a JSON structure of all application objects and their action and section maps, optionally filtered by namespace."
)
@APIResponses({@APIResponse(responseCode = "200", description = "OK"),
@APIResponse(responseCode = "207", description = "Multi-Status (Error from Kubernetes API)"),
Expand All @@ -81,8 +83,8 @@ public Response getApplications(@Pattern(regexp = NAME_PATTERN_ZERO_OR_MORE) @De
@Produces(MediaType.APPLICATION_JSON)
@Path("/{application-name}")
@Operation(
summary = "Retrieve an application object and its action map.",
description = "Returns a JSON structure containing the application object and action map for the specified application."
summary = "Retrieve an application object and its action and section maps.",
description = "Returns a JSON structure containing the application object and action and section maps for the specified application."
)
@APIResponses({@APIResponse(responseCode = "200", description = "OK"),
@APIResponse(responseCode = "207", description = "Multi-Status (Error from Kubernetes API)"),
Expand All @@ -92,7 +94,7 @@ public Response getApplicationAndMap(@Pattern(regexp = NAME_PATTERN_ONE_OR_MORE)
@Pattern(regexp = NAME_PATTERN_ZERO_OR_MORE) @DefaultValue("default") @QueryParam("namespace") @Parameter(description = "The namespace of the application") String namespace) {
try {
final ApiClient client = getApiClient();
final Object o = getNamespacedApplicationObject(client, namespace, name);
final Object o = getNamespacedApplicationObject(client, namespace, name);
return processApplications(client, getItemAsList(client, o));
}
catch (IOException | ApiException e) {
Expand All @@ -103,8 +105,9 @@ public Response getApplicationAndMap(@Pattern(regexp = NAME_PATTERN_ONE_OR_MORE)
private Response processApplications(ApiClient client, List<JsonObject> appObjects) throws ApiException {
final AppResponse response = new AppResponse();
final ConfigMapProcessor processor = new ConfigMapProcessor(APPLICATION_PROPERTY_NAME);
final SectionConfigMapProcessor sectionProcessor = new SectionConfigMapProcessor(APPLICATION_PROPERTY_NAME);
appObjects.forEach(v -> {
response.add(v, processor.getConfigMap(client, v, ConfigMapProcessor.ConfigMapType.ACTION));
response.add(v, processor.getConfigMap(client, v, ConfigMapProcessor.ConfigMapType.ACTION), sectionProcessor.processSectionMap(client, v));
});
return Response.ok(response.getJSON()).build();
}
Expand All @@ -114,25 +117,30 @@ static final class AppResponse {
private final JsonArray applications;
// Constructs:
// {
// applications: [ { application: {...}, action-map: {...} }, ... ]
// applications: [ { application: {...}, action-map: {...}, section-map: {...}, ... ]
// }
//
// 'applications' is an array of objects.
// Each object in the applications array is comprised of an application and action-map.
// An application is an instance of the application CRD.
// An action-map is a config map containing the action definitions belonging to the associated application.
// An section-map is a config map containing the section and section datasource definitions belonging to the associated application.
public AppResponse() {
o = new JsonObject();
o.add(APPLICATIONS_PROPERTY_NAME, applications = new JsonArray());
}
public void add(final JsonObject application, final JsonObject actionMap) {
public void add(final JsonObject application, final JsonObject actionMap, final JsonObject sectionMap) {
final JsonObject tuple = new JsonObject();
tuple.add(APPLICATION_PROPERTY_NAME, application != null ? application : new JsonObject());
tuple.add(ACTION_MAP_PROPERTY_NAME, actionMap != null ? actionMap : new JsonObject());
tuple.add(SECTION_MAP_PROPERTY_NAME, sectionMap != null ? sectionMap : new JsonObject());
applications.add(tuple);
}
public String getJSON() {
return o.toString();
}
}



}
32 changes: 21 additions & 11 deletions src/main/java/application/rest/v1/ComponentsEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;

import javax.inject.Inject;
import javax.validation.constraints.Pattern;
Expand All @@ -40,8 +42,10 @@

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

import application.rest.v1.configmaps.ConfigMapProcessor;
import application.rest.v1.configmaps.SectionConfigMapProcessor;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;

Expand All @@ -52,7 +56,7 @@ public class ComponentsEndpoint extends KAppNavEndpoint {
private static final String COMPONENTS_PROPERTY_NAME = "components";
private static final String COMPONENT_PROPERTY_NAME = "component";
private static final String ACTION_MAP_PROPERTY_NAME = "action-map";

private static final String SECTION_MAP_PROPERTY_NAME = "section-map";
private static final String KIND_PROPERTY_NAME = "kind";

@Inject
Expand All @@ -62,8 +66,8 @@ public class ComponentsEndpoint extends KAppNavEndpoint {
@Produces(MediaType.APPLICATION_JSON)
@Path("/{application-name}")
@Operation(
summary = "Retrieve component objects and action maps for an application.",
description = "Returns a JSON structure of all component objects and their action maps for the specified application."
summary = "Retrieve component objects and action and section maps for an application.",
description = "Returns a JSON structure of all component objects and their action and section maps for the specified application."
)
@APIResponses({@APIResponse(responseCode = "200", description = "OK"),
@APIResponse(responseCode = "207", description = "Multi-Status (Error from Kubernetes API)"),
Expand Down Expand Up @@ -120,17 +124,19 @@ private Response processComponentKinds(ApiClient client, List<ComponentKind> com

private void processComponents(ApiClient client, ComponentResponse response, ComponentKind componentKind, List<JsonObject> components) {
final ConfigMapProcessor processor = new ConfigMapProcessor(componentKind.kind);
final SectionConfigMapProcessor sectionProcessor = new SectionConfigMapProcessor(componentKind.kind);
components.forEach(v -> {
// Add 'kind' property to components that are missing it.
if (v.get(KIND_PROPERTY_NAME) == null) {
v.addProperty(KIND_PROPERTY_NAME, componentKind.kind);
}
response.add(v, processor.getConfigMap(client, v, ConfigMapProcessor.ConfigMapType.ACTION));
}
response.add(v, processor.getConfigMap(client, v, ConfigMapProcessor.ConfigMapType.ACTION), sectionProcessor.processSectionMap(client, v));
});
}

private void processComponents(ApiClient client, ComponentResponse response, ComponentKind componentKind, List<JsonObject> components, String appNamespace, String appName) {
final ConfigMapProcessor processor = new ConfigMapProcessor(componentKind.kind);
final SectionConfigMapProcessor sectionProcessor = new SectionConfigMapProcessor(componentKind.kind);
components.forEach(v -> {
// Add 'kind' property to components that are missing it.
if (v.get(KIND_PROPERTY_NAME) == null) {
Expand All @@ -140,8 +146,8 @@ private void processComponents(ApiClient client, ComponentResponse response, Com
// filter out recursive app from component list
if (!(componentKind.kind.equals("Application") &&
getComponentName(v).equals(appName) &&
getComponentNamespace(v).equals(appNamespace))) {
response.add(v, processor.getConfigMap(client, v, ConfigMapProcessor.ConfigMapType.ACTION));
getComponentNamespace(v).equals(appNamespace))) {
response.add(v, processor.getConfigMap(client, v, ConfigMapProcessor.ConfigMapType.ACTION), sectionProcessor.processSectionMap(client, v));
}
});
}
Expand All @@ -165,26 +171,30 @@ private List<String> getNamespaceList(ApiClient client, Object o, String namespa
}
return newNamespaces;
}



static final class ComponentResponse {
private final JsonObject o;
private final JsonArray components;
// Constructs:
// {
// components: [ { component: {...}, action-map: {...} }, ... ]
// }
// components: [ { component: {...}, action-map: {...}, section-map: {...} } ]
// }
public ComponentResponse() {
o = new JsonObject();
o.add(COMPONENTS_PROPERTY_NAME, components = new JsonArray());
}
public void add(final JsonObject component, final JsonObject actionMap) {
public void add(final JsonObject component, final JsonObject actionMap, final JsonObject sectionMap) {
final JsonObject tuple = new JsonObject();
tuple.add(COMPONENT_PROPERTY_NAME, component != null ? component : new JsonObject());
tuple.add(ACTION_MAP_PROPERTY_NAME, actionMap != null ? actionMap : new JsonObject());
tuple.add(SECTION_MAP_PROPERTY_NAME, sectionMap != null ? sectionMap : new JsonObject());
components.add(tuple);
}
public String getJSON() {
return o.toString();
}
}


}
50 changes: 12 additions & 38 deletions src/main/java/application/rest/v1/actions/ReplicaSetFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

// replicaset() or replicaset(<deployment-namespace>,<deployment-name>)
// Returns the replica set name for the specified deployment.
// Return value structure is: { "ReplicaSet" : "name"}
// Return value structure is: {"name"}
public class ReplicaSetFunction implements Function {

private static final String DEPLOYMENT_KIND = "Deployment";
Expand Down Expand Up @@ -84,45 +84,19 @@ public String invoke(ResolutionContext context, List<String> parameters) {
try {
Object o = registry.listClusterObject(client, REPLICA_SET_KIND, null, labelSelector, null, null);
List<JsonObject> items = KAppNavEndpoint.getItemsAsList(client, o);
ReplicaSetResult result = new ReplicaSetResult();
//loop over each replica set
items.forEach(v -> {
JsonElement element = v.get(METADATA_PROPERTY_NAME);
if (element != null && element.isJsonObject()) {
JsonObject metadata = element.getAsJsonObject();
//get replicaset name
JsonElement rsName = metadata.get("name");
if (rsName!= null && rsName.isJsonPrimitive()) {
result.add(rsName.getAsString());
}
}
});
return result.getJSON();
// only one replicaSet per deployment
JsonElement element = items.get(0).get(METADATA_PROPERTY_NAME);
if (element != null && element.isJsonObject()) {
JsonObject metadata = element.getAsJsonObject();
//get replicaset name
JsonElement rsName = metadata.get("name");
if (rsName!= null && rsName.isJsonPrimitive()) {
return rsName.getAsString();
}
}
}
catch (ApiException e) {}
}
return null;
}




static final class ReplicaSetResult {
private final JsonObject o;
// Constructs:
// {
// ReplicaSet: name
// }
public ReplicaSetResult() {
o = new JsonObject();
}

public void add(final String name) {
o.addProperty(REPLICA_SET_PROPERTY_NAME, name);
}

public String getJSON() {
return o.toString();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public String resolve(ResolutionContext context, String suffix) {
// Cache the resolved value.
context.setResolvedVariable(suffix, value);
return value;
} else {
value="undefined";
context.setResolvedVariable(suffix, value);
return value;
}
}
return null;
Expand Down
Loading

0 comments on commit d56749f

Please sign in to comment.