Skip to content

Commit

Permalink
Merge pull request #2678 from HubSpot/namespace-key-func
Browse files Browse the repository at this point in the history
  • Loading branch information
fusesource-ci authored Dec 18, 2020
2 parents da8e084 + 84f9210 commit edc20a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Fix #2656: Binding operations can be instantiated

#### Improvements
* Fix: Adds a convenience method for referring to Cache keys by namespace and name rather than item
* Fix: CustomResourceDefinitionContext.fromCrd support for v1 CustomResourceDefinition
* Fix #2642: Update kubernetes-examples to use apps/v1 Deployment rather than extensions/v1beta1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.utils.ReflectUtils;
import io.fabric8.kubernetes.client.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -400,15 +401,25 @@ public static String metaNamespaceKeyFunc(Object obj) {
throw new RuntimeException("Object is bad :" + obj);
}
}
if ((metadata.getNamespace() != null) && !metadata.getNamespace().isEmpty()) {
return metadata.getNamespace() + "/" + metadata.getName();
}
return metadata.getName();

return namespaceKeyFunc(metadata.getNamespace(), metadata.getName());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

/**
* Default index function that indexes based on an object's namespace and name.
*
* @see #metaNamespaceKeyFunc
*/
public static String namespaceKeyFunc(String objectNamespace, String objectName) {
if (Utils.isNullOrEmpty(objectNamespace)) {
return objectName;
}
return objectNamespace + "/" + objectName;
}

/**
* It is a default index function that indexes based on an object's namespace
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ void testDefaultNamespaceKey() {

cache.add(testPodObj);
assertEquals("default/test-pod4", Cache.metaNamespaceKeyFunc(testPodObj));
assertEquals("default/test-pod4", Cache.namespaceKeyFunc("default", "test-pod4"));
}

@Test
void testEmptyNamespaceKey() {
assertEquals("test-pod4", Cache.namespaceKeyFunc("", "test-pod4"));
assertEquals("test-pod4", Cache.namespaceKeyFunc(null, "test-pod4"));
}

@Test
Expand Down

0 comments on commit edc20a5

Please sign in to comment.