Skip to content

Commit 6060aff

Browse files
committed
work in progress
1 parent 3749493 commit 6060aff

File tree

5 files changed

+444
-130
lines changed

5 files changed

+444
-130
lines changed

src/main/java/oracle/kubernetes/operator/Main.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import oracle.kubernetes.operator.helpers.CallBuilder;
3939
import oracle.kubernetes.operator.helpers.ClientHelper;
4040
import oracle.kubernetes.operator.helpers.ClientHolder;
41-
import oracle.kubernetes.operator.helpers.ConfigMapHelper;
41+
import oracle.kubernetes.operator.helpers.ConfigMapConsumer;
4242
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
4343
import oracle.kubernetes.operator.helpers.DomainPresenceInfo.ServerStartupInfo;
4444
import oracle.kubernetes.operator.helpers.HealthCheckHelper.KubernetesVersion;
@@ -117,9 +117,8 @@ public static void main(String[] args) {
117117

118118
Collection<String> targetNamespaces = getTargetNamespaces(namespace);
119119

120-
ConfigMapHelper cmh = new ConfigMapHelper("/operator/config");
121-
122-
String serviceAccountName = cmh.get("serviceaccount");
120+
ConfigMapConsumer cmc = new ConfigMapConsumer("/operator/config");
121+
String serviceAccountName = cmc.get("serviceaccount");
123122
if (serviceAccountName == null) {
124123
serviceAccountName = "default";
125124
}
@@ -1414,8 +1413,8 @@ public NextAction onSuccess(Packet packet, V1Status result, int statusCode, Map<
14141413
private static Collection<String> getTargetNamespaces(String namespace) {
14151414
Collection<String> targetNamespaces = new ArrayList<String>();
14161415

1417-
ConfigMapHelper cmh = new ConfigMapHelper("/operator/config");
1418-
String tnValue = cmh.get("targetNamespaces");
1416+
ConfigMapConsumer cmc = new ConfigMapConsumer("/operator/config");
1417+
String tnValue = cmc.get("targetNamespaces");
14191418
if (tnValue != null) {
14201419
StringTokenizer st = new StringTokenizer(tnValue, ",");
14211420
while (st.hasMoreTokens()) {

src/main/java/oracle/kubernetes/operator/helpers/CallBuilder.java

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import io.kubernetes.client.ApiCallback;
1818
import io.kubernetes.client.ApiException;
19+
import io.kubernetes.client.models.V1ConfigMap;
20+
import io.kubernetes.client.models.V1ConfigMapList;
1921
import io.kubernetes.client.models.V1DeleteOptions;
2022
import io.kubernetes.client.models.V1ListMeta;
2123
import io.kubernetes.client.models.V1PersistentVolumeClaimList;
@@ -350,6 +352,188 @@ public Step createCustomResourceDefinitionAsync(String name, V1beta1CustomResour
350352
return createRequestAsync(responseStep, new RequestParams("createCustomResourceDefinition", null, name, body), CREATE_CUSTOMRESOURCEDEFINITION);
351353
}
352354

355+
/* Config Maps */
356+
357+
/**
358+
* List config maps
359+
* @param namespace Namespace
360+
* @return List of config maps
361+
* @throws ApiException API Exception
362+
*/
363+
public V1ConfigMapList listConfigMap(String namespace) throws ApiException {
364+
String _continue = "";
365+
ClientUsage cu = useClient();
366+
try {
367+
return cu.client().getCoreApiClient().listNamespacedConfigMap(namespace, pretty, _continue, fieldSelector,
368+
includeUninitialized, labelSelector, limit, resourceVersion, timeoutSeconds, watch);
369+
} finally {
370+
cu.recycle();
371+
}
372+
}
373+
374+
private com.squareup.okhttp.Call listConfigMapAsync(ClientUsage usage, String namespace, String _continue, ApiCallback<V1ConfigMapList> callback) throws ApiException {
375+
return usage.client().getCoreApiClient().listNamespacedConfigMapAsync(namespace, pretty, _continue,
376+
fieldSelector, includeUninitialized, labelSelector, limit, resourceVersion, timeoutSeconds, watch, callback);
377+
}
378+
379+
private final CallFactory<V1ConfigMapList> LIST_CONFIGMAP = (requestParams, usage, cont, callback) -> {
380+
return listConfigMapAsync(usage, requestParams.namespace, cont, callback);
381+
};
382+
383+
/**
384+
* Asynchronous step for listing config maps
385+
* @param namespace Namespace
386+
* @param responseStep Response step for when call completes
387+
* @return Asynchronous step
388+
*/
389+
public Step listConfigMapAsync(String namespace, ResponseStep<V1ConfigMapList> responseStep) {
390+
return createRequestAsync(responseStep, new RequestParams("listConfigMap", namespace, null, null), LIST_CONFIGMAP);
391+
}
392+
393+
/**
394+
* Read config map
395+
* @param name Name
396+
* @param namespace Namespace
397+
* @return Read config map
398+
* @throws ApiException API Exception
399+
*/
400+
public V1ConfigMap readConfigMap(String name, String namespace) throws ApiException {
401+
ClientUsage cu = useClient();
402+
try {
403+
return cu.client().getCoreApiClient().readNamespacedConfigMap(name, namespace, pretty, exact, export);
404+
} finally {
405+
cu.recycle();
406+
}
407+
}
408+
409+
private com.squareup.okhttp.Call readConfigMapAsync(ClientUsage usage, String name, String namespace, ApiCallback<V1ConfigMap> callback) throws ApiException {
410+
return usage.client().getCoreApiClient().readNamespacedConfigMapAsync(name, namespace, pretty, exact, export, callback);
411+
}
412+
413+
private final CallFactory<V1ConfigMap> READ_CONFIGMAP = (requestParams, usage, cont, callback) -> {
414+
return readConfigMapAsync(usage, requestParams.name, requestParams.namespace, callback);
415+
};
416+
417+
/**
418+
* Asynchronous step for reading config map
419+
* @param name Name
420+
* @param namespace Namespace
421+
* @param responseStep Response step for when call completes
422+
* @return Asynchronous step
423+
*/
424+
public Step readConfigMapAsync(String name, String namespace, ResponseStep<V1ConfigMap> responseStep) {
425+
return createRequestAsync(responseStep, new RequestParams("readConfigMap", namespace, name, null), READ_CONFIGMAP);
426+
}
427+
428+
/**
429+
* Create config map
430+
* @param namespace Namespace
431+
* @param body Body
432+
* @return Created config map
433+
* @throws ApiException API Exception
434+
*/
435+
public V1ConfigMap createConfigMap(String namespace, V1ConfigMap body) throws ApiException {
436+
ClientUsage cu = useClient();
437+
try {
438+
return cu.client().getCoreApiClient().createNamespacedConfigMap(namespace, body, pretty);
439+
} finally {
440+
cu.recycle();
441+
}
442+
}
443+
444+
private com.squareup.okhttp.Call createConfigMapAsync(ClientUsage usage, String namespace, V1ConfigMap body, ApiCallback<V1ConfigMap> callback) throws ApiException {
445+
return usage.client().getCoreApiClient().createNamespacedConfigMapAsync(namespace, body, pretty, callback);
446+
}
447+
448+
private final CallFactory<V1ConfigMap> CREATE_CONFIGMAP = (requestParams, usage, cont, callback) -> {
449+
return createConfigMapAsync(usage, requestParams.namespace, (V1ConfigMap) requestParams.body, callback);
450+
};
451+
452+
/**
453+
* Asynchronous step for creating config map
454+
* @param namespace Namespace
455+
* @param body Body
456+
* @param responseStep Response step for when call completes
457+
* @return Asynchronous step
458+
*/
459+
public Step createConfigMapAsync(String namespace, V1ConfigMap body, ResponseStep<V1ConfigMap> responseStep) {
460+
return createRequestAsync(responseStep, new RequestParams("createConfigMap", namespace, null, body), CREATE_CONFIGMAP);
461+
}
462+
463+
/**
464+
* Replace config map
465+
* @param name Name
466+
* @param namespace Namespace
467+
* @param body Body
468+
* @return Replaced config map
469+
* @throws ApiException API Exception
470+
*/
471+
public V1ConfigMap replaceConfigMap(String name, String namespace, V1ConfigMap body) throws ApiException {
472+
ClientUsage cu = useClient();
473+
try {
474+
return cu.client().getCoreApiClient().replaceNamespacedConfigMap(name, namespace, body, pretty);
475+
} finally {
476+
cu.recycle();
477+
}
478+
}
479+
480+
private com.squareup.okhttp.Call replaceConfigMapAsync(ClientUsage usage, String name, String namespace, V1ConfigMap body, ApiCallback<V1ConfigMap> callback) throws ApiException {
481+
return usage.client().getCoreApiClient().replaceNamespacedConfigMapAsync(name, namespace, body, pretty, callback);
482+
}
483+
484+
private final CallFactory<V1ConfigMap> REPLACE_CONFIGMAP = (requestParams, usage, cont, callback) -> {
485+
return replaceConfigMapAsync(usage, requestParams.name, requestParams.namespace, (V1ConfigMap) requestParams.body, callback);
486+
};
487+
488+
/**
489+
* Asynchronous step for replacing config map
490+
* @param name Name
491+
* @param namespace Namespace
492+
* @param body Body
493+
* @param responseStep Response step for when call completes
494+
* @return Asynchronous step
495+
*/
496+
public Step replaceConfigMapAsync(String name, String namespace, V1ConfigMap body, ResponseStep<V1ConfigMap> responseStep) {
497+
return createRequestAsync(responseStep, new RequestParams("replaceConfigMap", namespace, name, body), REPLACE_CONFIGMAP);
498+
}
499+
500+
/**
501+
* Delete config map
502+
* @param name Name
503+
* @param namespace Namespace
504+
* @param deleteOptions Delete options
505+
* @return Status of deletion
506+
* @throws ApiException API Exception
507+
*/
508+
public V1Status deleteConfigMap(String name, String namespace, V1DeleteOptions deleteOptions) throws ApiException {
509+
ClientUsage cu = useClient();
510+
try {
511+
return cu.client().getCoreApiClient().deleteNamespacedConfigMap(name, namespace, deleteOptions, pretty, gracePeriodSeconds,
512+
orphanDependents, propagationPolicy);
513+
} finally {
514+
cu.recycle();
515+
}
516+
}
517+
518+
private com.squareup.okhttp.Call deleteConfigMapAsync(ClientUsage usage, String name, String namespace, V1DeleteOptions deleteOptions, ApiCallback<V1Status> callback) throws ApiException {
519+
return usage.client().getCoreApiClient().deleteNamespacedConfigMapAsync(name, namespace, deleteOptions, pretty, gracePeriodSeconds, orphanDependents, propagationPolicy, callback);
520+
}
521+
522+
private final CallFactory<V1Status> DELETE_CONFIGMAP = (requestParams, usage, cont, callback) -> {
523+
return deleteConfigMapAsync(usage, requestParams.name, requestParams.namespace, (V1DeleteOptions) requestParams.body, callback);
524+
};
525+
526+
/**
527+
* Asynchronous step for deleting config map
528+
* @param name Name
529+
* @param namespace Namespace
530+
* @param responseStep Response step for when call completes
531+
* @return Asynchronous step
532+
*/
533+
public Step deleteConfigMapAsync(String name, String namespace, ResponseStep<V1Status> responseStep) {
534+
return createRequestAsync(responseStep, new RequestParams("deleteConfigMap", namespace, name, null), DELETE_CONFIGMAP);
535+
}
536+
353537
/* Pods */
354538

355539
/**
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright 2017, 2018, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
4+
package oracle.kubernetes.operator.helpers;
5+
6+
import oracle.kubernetes.operator.logging.LoggingFacade;
7+
import oracle.kubernetes.operator.logging.LoggingFactory;
8+
import oracle.kubernetes.operator.logging.MessageKeys;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.util.Collection;
14+
import java.util.HashSet;
15+
import java.util.Map;
16+
import java.util.Set;
17+
18+
/**
19+
* Kubernetes mounts ConfigMaps in the Pod's file-system as directories where the contained
20+
* files are named with the keys and the contents of the file are the values. This class
21+
* assists with parsing this data and representing it as a Map.
22+
*/
23+
public class ConfigMapConsumer implements Map<String, String> {
24+
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
25+
26+
private final File mountPointDir;
27+
28+
public ConfigMapConsumer(String mountPoint) {
29+
this.mountPointDir = new File(mountPoint);
30+
}
31+
32+
@Override
33+
public int size() {
34+
String[] list = mountPointDir.list();
35+
return list == null ? null : list.length;
36+
}
37+
38+
@Override
39+
public boolean isEmpty() {
40+
return size() == 0;
41+
}
42+
43+
@Override
44+
public boolean containsKey(Object key) {
45+
if (key instanceof String) {
46+
File child = new File(mountPointDir, (String) key);
47+
return child.exists();
48+
}
49+
return false;
50+
}
51+
52+
@Override
53+
public boolean containsValue(Object value) {
54+
throw new UnsupportedOperationException();
55+
}
56+
57+
@Override
58+
public String get(Object key) {
59+
if (key instanceof String) {
60+
return readValue((String) key);
61+
}
62+
return null;
63+
}
64+
65+
@Override
66+
public String put(String key, String value) {
67+
throw new UnsupportedOperationException();
68+
}
69+
70+
@Override
71+
public String remove(Object key) {
72+
throw new UnsupportedOperationException();
73+
}
74+
75+
@Override
76+
public void putAll(Map<? extends String, ? extends String> m) {
77+
throw new UnsupportedOperationException();
78+
}
79+
80+
@Override
81+
public void clear() {
82+
throw new UnsupportedOperationException();
83+
}
84+
85+
@Override
86+
public Set<String> keySet() {
87+
Set<String> keys = new HashSet<>();
88+
String[] list = mountPointDir.list();
89+
if (list != null) {
90+
for (String s : list) {
91+
keys.add(s);
92+
}
93+
}
94+
return keys;
95+
}
96+
97+
@Override
98+
public Collection<String> values() {
99+
throw new UnsupportedOperationException();
100+
}
101+
102+
@Override
103+
public Set<Entry<String, String>> entrySet() {
104+
Set<Entry<String, String>> entries = new HashSet<>();
105+
String[] list = mountPointDir.list();
106+
if (list != null) {
107+
for (String s : list) {
108+
entries.add(new Entry<String, String>() {
109+
110+
@Override
111+
public String getKey() {
112+
return s;
113+
}
114+
115+
@Override
116+
public String getValue() {
117+
return readValue(s);
118+
}
119+
120+
@Override
121+
public String setValue(String value) {
122+
throw new UnsupportedOperationException();
123+
}
124+
});
125+
}
126+
}
127+
return entries;
128+
}
129+
130+
private String readValue(String key) {
131+
File child = new File(mountPointDir, key);
132+
if (child.exists()) {
133+
try {
134+
return new String(Files.readAllBytes(child.toPath()));
135+
} catch (IOException e) {
136+
LOGGER.warning(MessageKeys.EXCEPTION, e);
137+
}
138+
}
139+
140+
return null;
141+
}
142+
143+
}

0 commit comments

Comments
 (0)