use of io.fabric8.mockwebserver.crud.AttributeSet in project kubernetes-client by fabric8io.
the class KubernetesAttributesExtractor method fromPath.
@Override
public AttributeSet fromPath(String s) {
if (s == null || s.isEmpty()) {
return new AttributeSet();
}
// Get paths
HttpUrl url = parseUrlFromPathAndQuery(s);
Matcher m = PATTERN.matcher(url.encodedPath());
if (m.matches()) {
AttributeSet set = new AttributeSet(extract(m).entrySet().stream().map(e -> new Attribute(e.getKey(), e.getValue())).collect(Collectors.toList()));
set = AttributeSet.merge(set, extractQueryParameters(url));
LOGGER.debug("fromPath {} : {}", s, set);
return set;
}
return new AttributeSet();
}
use of io.fabric8.mockwebserver.crud.AttributeSet in project kubernetes-client by fabric8io.
the class KubernetesAttributesExtractor method extractFieldSelector.
private static AttributeSet extractFieldSelector(HttpUrl url) {
AttributeSet attributes = new AttributeSet();
String fieldSelector = url.queryParameter("fieldSelector");
if (fieldSelector != null) {
for (String requirement : fieldSelector.split(",")) {
Attribute field = parseField(requirement);
if (field != null) {
attributes = attributes.add(field);
} else {
LOGGER.warn("Ignoring unsupported field requirement: {}", requirement);
}
}
}
return attributes;
}
use of io.fabric8.mockwebserver.crud.AttributeSet in project kubernetes-client by fabric8io.
the class KubernetesCrudDispatcher method handleGet.
/**
* Performs a get for the corresponding object from the in-memory db.
*
* @param path The path.
* @return The {@link MockResponse}
*/
@Override
public MockResponse handleGet(String path) {
if (detectWatchMode(path)) {
return handleWatch(path);
}
MockResponse response = new MockResponse();
List<String> items = new ArrayList<>();
AttributeSet query = attributeExtractor.fromPath(path);
synchronized (map) {
map.entrySet().stream().filter(entry -> entry.getKey().matches(query)).forEach(entry -> {
LOGGER.debug("Entry found for query {} : {}", query, entry);
items.add(entry.getValue());
});
}
if (query.containsKey(KubernetesAttributesExtractor.NAME)) {
if (!items.isEmpty()) {
response.setBody(items.get(0));
response.setResponseCode(HttpURLConnection.HTTP_OK);
} else {
response.setResponseCode(HttpURLConnection.HTTP_NOT_FOUND);
}
} else {
response.setBody(kubernetesResponseComposer.compose(items, String.valueOf(resourceVersion.get())));
response.setResponseCode(HttpURLConnection.HTTP_OK);
}
return response;
}
use of io.fabric8.mockwebserver.crud.AttributeSet in project kubernetes-client by fabric8io.
the class KubernetesCrudDispatcher method processEvent.
private void processEvent(String path, AttributeSet pathAttributes, AttributeSet oldAttributes, String newState) {
String existing = map.remove(oldAttributes);
AttributeSet newAttributes = null;
if (newState != null) {
newAttributes = kubernetesAttributesExtractor.fromResource(newState);
// corner case - we need to get the plural from the path
if (!newAttributes.containsKey(KubernetesAttributesExtractor.PLURAL)) {
newAttributes = AttributeSet.merge(pathAttributes, newAttributes);
}
map.put(newAttributes, newState);
}
if (!Objects.equals(existing, newState)) {
AttributeSet finalAttributeSet = newAttributes;
watchEventListeners.forEach(listener -> {
boolean matchesOld = oldAttributes != null && listener.attributeMatches(oldAttributes);
boolean matchesNew = finalAttributeSet != null && listener.attributeMatches(finalAttributeSet);
if (matchesOld && matchesNew) {
listener.sendWebSocketResponse(newState, Action.MODIFIED);
} else if (matchesOld) {
listener.sendWebSocketResponse(existing, Action.DELETED);
} else if (matchesNew) {
listener.sendWebSocketResponse(newState, Action.ADDED);
}
});
crdProcessor.process(path, Utils.getNonNullOrElse(newState, existing), newState == null);
}
}
use of io.fabric8.mockwebserver.crud.AttributeSet in project kubernetes-client by fabric8io.
the class KubernetesAttributesExtractor method extract.
public AttributeSet extract(HasMetadata hasMetadata) {
AttributeSet metadataAttributes = new AttributeSet();
String apiVersion = hasMetadata.getApiVersion();
String api = null;
String version = null;
if (!Utils.isNullOrEmpty(apiVersion)) {
api = ApiVersionUtil.trimGroup(apiVersion);
version = ApiVersionUtil.trimVersion(apiVersion);
if (!api.equals(apiVersion)) {
metadataAttributes = metadataAttributes.add(new Attribute(API, api));
} else {
api = null;
}
metadataAttributes = metadataAttributes.add(new Attribute(VERSION, version));
}
if (!Utils.isNullOrEmpty(hasMetadata.getMetadata().getName())) {
metadataAttributes = metadataAttributes.add(new Attribute(NAME, hasMetadata.getMetadata().getName()));
metadataAttributes = metadataAttributes.add(new Attribute(METADATA_NAME, hasMetadata.getMetadata().getName()));
}
if (!Utils.isNullOrEmpty(hasMetadata.getMetadata().getNamespace())) {
metadataAttributes = metadataAttributes.add(new Attribute(NAMESPACE, hasMetadata.getMetadata().getNamespace()));
metadataAttributes = metadataAttributes.add(new Attribute(METADATA_NAMESPACE, hasMetadata.getMetadata().getNamespace()));
}
if (hasMetadata.getMetadata().getLabels() != null) {
for (Map.Entry<String, String> label : hasMetadata.getMetadata().getLabels().entrySet()) {
metadataAttributes = metadataAttributes.add(new Attribute(LABEL_KEY_PREFIX + label.getKey(), label.getValue()));
}
}
if (!(hasMetadata instanceof GenericKubernetesResource)) {
metadataAttributes = metadataAttributes.add(new Attribute(PLURAL, hasMetadata.getPlural()));
} else {
Optional<CustomResourceDefinitionContext> context = findCrd(api, version);
if (context.isPresent()) {
metadataAttributes = metadataAttributes.add(new Attribute(PLURAL, context.get().getPlural()));
}
// else we shouldn't infer the plural without a crd registered - it will come from the path instead
}
return metadataAttributes;
}
Aggregations