Skip to content

Commit

Permalink
Merge pull request #186 from oracle/fix-domain-cm-labels
Browse files Browse the repository at this point in the history
Replace the domainUID="" label on the per-domain operator config map with an operatorName="<operatornamespace>" label
  • Loading branch information
rjeberhard authored Apr 6, 2018
2 parents 7abfb77 + e4bcb56 commit 5efcac0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
17 changes: 11 additions & 6 deletions operator/src/main/java/oracle/kubernetes/operator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ public static void main(String[] args) {

private static void begin() {
// read the operator configuration
String namespace = System.getenv("OPERATOR_NAMESPACE");
if (namespace == null) {
namespace = "default";
}
String namespace = getOperatorNamespace();

Collection<String> targetNamespaces = getTargetNamespaces(namespace);

Expand Down Expand Up @@ -245,7 +242,7 @@ public NextAction onSuccess(Packet packet, DomainList result, int statusCode,
}
});

Step initialize = ConfigMapHelper.createScriptConfigMapStep(ns,
Step initialize = ConfigMapHelper.createScriptConfigMapStep(namespace, ns,
new ConfigMapAfterStep(ns, callBuilderFactory.create().with($ -> {
$.labelSelector = LabelConstants.DOMAINUID_LABEL + "," + LabelConstants.CREATEDBYOPERATOR_LABEL;
}).listPodAsync(ns, new ResponseStep<V1PodList>(callBuilderFactory.create().with($ -> {
Expand Down Expand Up @@ -1791,7 +1788,7 @@ private static void dispatchConfigMapWatch(Watch.Response<V1ConfigMap> item) {
switch (item.type) {
case "MODIFIED":
case "DELETED":
engine.createFiber().start(ConfigMapHelper.createScriptConfigMapStep(c.getMetadata().getNamespace(), null),
engine.createFiber().start(ConfigMapHelper.createScriptConfigMapStep(getOperatorNamespace(), c.getMetadata().getNamespace(), null),
new Packet(), new CompletionCallback() {
@Override
public void onCompletion(Packet packet) {
Expand Down Expand Up @@ -1919,4 +1916,12 @@ public static Collection<NetworkAccessPoint> adminChannelsToCreate(WlsDomainConf
LOGGER.exiting();
return validatedChannels;
}

private static String getOperatorNamespace() {
String namespace = System.getenv("OPERATOR_NAMESPACE");
if (namespace == null) {
namespace = "default";
}
return namespace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,31 @@ private ConfigMapHelper() {}

/**
* Factory for {@link Step} that creates config map containing scripts
* @param namespace Namespace
* @param operatorNamespace the operator's namespace
* @param domainNamespace the domain's namespace
* @param next Next processing step
* @return Step for creating config map containing scripts
*/
public static Step createScriptConfigMapStep(String namespace, Step next) {
return new ScriptConfigMapStep(namespace, next);
public static Step createScriptConfigMapStep(String operatorNamespace, String domainNamespace, Step next) {
return new ScriptConfigMapStep(operatorNamespace, domainNamespace, next);
}

// Make this public so that it can be unit tested
public static class ScriptConfigMapStep extends Step {
private final String namespace;
private final String operatorNamespace;
private final String domainNamespace;

public ScriptConfigMapStep(String namespace, Step next) {
public ScriptConfigMapStep(String operatorNamespace, String domainNamespace, Step next) {
super(next);
this.namespace = namespace;
this.operatorNamespace = operatorNamespace;
this.domainNamespace = domainNamespace;
}

@Override
public NextAction apply(Packet packet) {
V1ConfigMap cm = computeDomainConfigMap();
CallBuilderFactory factory = ContainerResolver.getInstance().getContainer().getSPI(CallBuilderFactory.class);
Step read = factory.create().readConfigMapAsync(cm.getMetadata().getName(), namespace, new ResponseStep<V1ConfigMap>(next) {
Step read = factory.create().readConfigMapAsync(cm.getMetadata().getName(), domainNamespace, new ResponseStep<V1ConfigMap>(next) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode,
Map<String, List<String>> responseHeaders) {
Expand All @@ -64,7 +67,7 @@ public NextAction onFailure(Packet packet, ApiException e, int statusCode,
public NextAction onSuccess(Packet packet, V1ConfigMap result, int statusCode,
Map<String, List<String>> responseHeaders) {
if (result == null) {
Step create = factory.create().createConfigMapAsync(namespace, cm, new ResponseStep<V1ConfigMap>(next) {
Step create = factory.create().createConfigMapAsync(domainNamespace, cm, new ResponseStep<V1ConfigMap>(next) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode,
Map<String, List<String>> responseHeaders) {
Expand All @@ -75,23 +78,23 @@ public NextAction onFailure(Packet packet, ApiException e, int statusCode,
public NextAction onSuccess(Packet packet, V1ConfigMap result, int statusCode,
Map<String, List<String>> responseHeaders) {

LOGGER.info(MessageKeys.CM_CREATED, namespace);
LOGGER.info(MessageKeys.CM_CREATED, domainNamespace);
packet.put(ProcessingConstants.SCRIPT_CONFIG_MAP, result);
return doNext(packet);
}
});
return doNext(create, packet);
} else if (AnnotationHelper.checkFormatAnnotation(result.getMetadata()) && result.getData().entrySet().containsAll(cm.getData().entrySet())) {
// existing config map has correct data
LOGGER.fine(MessageKeys.CM_EXISTS, namespace);
LOGGER.fine(MessageKeys.CM_EXISTS, domainNamespace);
packet.put(ProcessingConstants.SCRIPT_CONFIG_MAP, result);
return doNext(packet);
} else {
// we need to update the config map
Map<String, String> updated = result.getData();
updated.putAll(cm.getData());
cm.setData(updated);
Step replace = factory.create().replaceConfigMapAsync(cm.getMetadata().getName(), namespace, cm, new ResponseStep<V1ConfigMap>(next) {
Step replace = factory.create().replaceConfigMapAsync(cm.getMetadata().getName(), domainNamespace, cm, new ResponseStep<V1ConfigMap>(next) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode,
Map<String, List<String>> responseHeaders) {
Expand All @@ -101,7 +104,7 @@ public NextAction onFailure(Packet packet, ApiException e, int statusCode,
@Override
public NextAction onSuccess(Packet packet, V1ConfigMap result, int statusCode,
Map<String, List<String>> responseHeaders) {
LOGGER.info(MessageKeys.CM_REPLACED, namespace);
LOGGER.info(MessageKeys.CM_REPLACED, domainNamespace);
packet.put(ProcessingConstants.SCRIPT_CONFIG_MAP, result);
return doNext(packet);
}
Expand All @@ -123,17 +126,12 @@ protected V1ConfigMap computeDomainConfigMap() {

V1ObjectMeta metadata = new V1ObjectMeta();
metadata.setName(name);
metadata.setNamespace(namespace);
metadata.setNamespace(domainNamespace);

AnnotationHelper.annotateWithFormat(metadata);

Map<String, String> labels = new HashMap<>();
// This config map is a singleton that is shared by all the domains
// We need to add a domain uid label so that it can be located as
// related to the operator. However, we don't have a specific domain uid
// to set as the value. So, just set it to an empty string. That way,
// someone seleting on just the weblogic.domainUID label will find it.
labels.put(LabelConstants.DOMAINUID_LABEL, "");
labels.put(LabelConstants.OPERATORNAME_LABEL, operatorNamespace);
labels.put(LabelConstants.CREATEDBYOPERATOR_LABEL, "true");
metadata.setLabels(labels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
public class ConfigMapHelperConfigTest {

private static final String NAMESPACE = "test-namespace";
private static final String OPERATOR_NAMESPACE = "test-operator-namespace";
private static final String DOMAIN_NAMESPACE = "test-domain-namespace";
private static final String PROPERTY_LIVENESS_PROBE_SH = "livenessProbe.sh";
private static final String PROPERTY_READINESS_PROBE_SH = "readinessProbe.sh";
private static final String PROPERTY_READ_STATE_SH = "readState.sh";
Expand All @@ -45,8 +46,8 @@ private V1ConfigMap getDesiredDomainConfigMap() {
newConfigMap()
.metadata(newObjectMeta()
.name(DOMAIN_CONFIG_MAP_NAME)
.namespace(NAMESPACE)
.putLabelsItem(DOMAINUID_LABEL, "")
.namespace(DOMAIN_NAMESPACE)
.putLabelsItem(OPERATORNAME_LABEL, OPERATOR_NAMESPACE)
.putLabelsItem(CREATEDBYOPERATOR_LABEL, "true")
.putAnnotationsItem(FORMAT_ANNOTATION, FORMAT_VERSION))
.putDataItem(PROPERTY_LIVENESS_PROBE_SH, "")
Expand All @@ -60,7 +61,7 @@ private V1ConfigMap getActualDomainConfigMap() throws Exception {

private static class TestScriptConfigMapStep extends ConfigMapHelper.ScriptConfigMapStep {
public TestScriptConfigMapStep() {
super(NAMESPACE, null);
super(OPERATOR_NAMESPACE, DOMAIN_NAMESPACE, null);
}
@Override public V1ConfigMap computeDomainConfigMap() {
return super.computeDomainConfigMap();
Expand Down

0 comments on commit 5efcac0

Please sign in to comment.