Skip to content

Commit

Permalink
Auto generate a CLI config
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman committed Jul 14, 2023
1 parent 3f84a37 commit 7beaabf
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 3 deletions.
9 changes: 8 additions & 1 deletion docs/management/010_overview.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////

Copyright (c) 2020, Oracle and/or its affiliates.
Copyright (c) 2020, 2023, Oracle and/or its affiliates.
Licensed under the Universal Permissive License v 1.0 as shown at
http://oss.oracle.com/licenses/upl.

Expand All @@ -22,6 +22,13 @@ in a cluster deployed in Kubernetes.
Coherence Management over REST feature
--
[CARD]
.The Coherence CLI
[link=docs/management/025_coherence_cli.adoc]
--
Using the Coherence CLI in Pods
--
[CARD]
.VisualVM
[link=docs/management/030_visualvm.adoc]
Expand Down
96 changes: 96 additions & 0 deletions docs/management/025_coherence_cli.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
///////////////////////////////////////////////////////////////////////////////

Copyright (c) 2023, Oracle and/or its affiliates.
Licensed under the Universal Permissive License v 1.0 as shown at
http://oss.oracle.com/licenses/upl.

///////////////////////////////////////////////////////////////////////////////
= The Coherence CLI
== The Coherence CLI
If Coherence Management over REST is enabled, it is possible to use the
https://github.com/oracle/coherence-cli[Coherence CLI]
of access management information. The Operator enables Coherence Management over REST by default, so unless it
has specifically been disabled, the CLI can be used.
See the https://oracle.github.io/coherence-cli/docs/latest/#/docs/about/01_overview[Coherence CLI Documentation]
for more information on how to use the CLI.
The Coherence CLI is automatically added to Coherence Pods by the Operator, so it is available as an executable
that can be run using `kubectl exec`.
At start-up of a Coherence container a default Coherence CLI configuration is created so that the CLI
knows about the local cluster member.
=== Using the CLI in Pods
The Operator installs the CLI at the location `/coherence-operator/utils/cohctl`.
Most official Coherence images are distroless images so they do not have a shell that can be used to create a session and execute commands. Each `cohctl` command will need to be executed as a separate `kubectl exec` command.
Once a Pod is running is it simple to use the CLI.
For example, the yaml below will create a simple three member cluster.
[source]
.minimal.yaml
----
apiVersion: coherence.oracle.com/v1
kind: Coherence
metadata:
name: storage
spec:
replicas: 3
----
The cluster name is `storage` and there will be three Pods created, `storage-0`, `storage-1` and `storage-2`.
To list the services running in the `storage-0` Pod the following command can be run:
[source,bash]
----
kubectl exec storage-0 -c coherence -- /coherence-operator/utils/cohctl get services
----
The `-c coherence` option tells `kubectl` to exec the command in the `coherence` container.
By default this is the only container that will be running in the Pod, so the option could be omitted.
If the option is omitted, `kubectl` will display a warning to say it assumes you mean the `coherence` container.
Everything after the `--` is the command to run in the Pod. In this case we execute:
[source,bash]
----
/coherence-operator/utils/cohctl get services
----
which runs the Coherence CLI binary at `/coherence-operator/utils/cohctl` with the command `get services`.
The output displayed by the command will look something like this:
[source,bash]
----
Using cluster connection 'default' from current context.
SERVICE NAME TYPE MEMBERS STATUS HA STORAGE PARTITIONS
"$GRPC:GrpcProxy" Proxy 3 n/a -1 -1
"$SYS:Concurrent" DistributedCache 3 NODE-SAFE 3 257
"$SYS:ConcurrentProxy" Proxy 3 n/a -1 -1
"$SYS:Config" DistributedCache 3 NODE-SAFE 3 257
"$SYS:HealthHttpProxy" Proxy 3 n/a -1 -1
"$SYS:SystemProxy" Proxy 3 n/a -1 -1
ManagementHttpProxy Proxy 3 n/a -1 -1
MetricsHttpProxy Proxy 3 n/a -1 -1
PartitionedCache DistributedCache 3 NODE-SAFE 3 257
PartitionedTopic PagedTopic 3 NODE-SAFE 3 257
Proxy Proxy 3 n/a -1 -1
----
The exact output will vary depending on the version of Coherence and the configurations being used.
More CLI commands can be run by changing the CLI commands specified after `/coherence-operator/utils/cohctl`.
For example, to list all the members of the cluster:
[source,bash]
----
kubectl exec storage-0 -c coherence -- /coherence-operator/utils/cohctl get members
----
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

package com.oracle.coherence.k8s;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Enumeration;
import java.util.concurrent.CompletableFuture;

import com.tangosol.coherence.config.Config;
import com.tangosol.discovery.NSLookup;
import com.tangosol.internal.net.management.HttpHelper;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Cluster;
import com.tangosol.net.Coherence;
import com.tangosol.net.DefaultCacheServer;
import com.tangosol.net.Member;
import com.tangosol.net.NameService;
import com.tangosol.net.OperationalContext;
import com.tangosol.net.Service;
import com.tangosol.util.Resources;

/**
* A main class that is used to run some initialisation code before
Expand Down Expand Up @@ -42,7 +65,8 @@ else if (DEFAULT_MAIN.equals(args[0])) {
}

init();

CompletableFuture.runAsync(Main::initCohCtl);

String sMainClass = args[0];
String[] asArgsReal = new String[args.length - 1];
System.arraycopy(args, 1, asArgsReal, 0, asArgsReal.length);
Expand Down Expand Up @@ -75,4 +99,57 @@ private static String getMainClass() {
return DefaultCacheServer.class.getCanonicalName();
}
}

private static void initCohCtl() {
try {
Cluster cluster = CacheFactory.getCluster();
Member member = cluster.getLocalMember();
String clusterName = member.getClusterName();
String port = Config.getProperty("coherence.management.http.port", "30000");
String provider = Config.getProperty("coherence.management.http.provider");
String defaultProtocol = provider == null || provider.isEmpty() ? "http" : "https";
String protocol = Config.getProperty("coherence.operator.cli.protocol", defaultProtocol);
String home = System.getProperty("user.home");
String connectionType = "http";

File cohctlHome = new File(home + File.separator + ".cohctl");
File configFile = new File(cohctlHome, "cohctl.yaml");

if (!configFile.exists()) {
System.out.println("CoherenceOperator: creating default cohctl config at " + configFile.getAbsolutePath());
if (!cohctlHome.exists()) {
cohctlHome.mkdirs();
}
try (PrintWriter out = new PrintWriter(configFile)) {
out.println("clusters:");
out.println(" - name: default");
out.println(" discoverytype: manual");
out.println(" connectiontype: " + connectionType);
out.println(" connectionurl: " + protocol + "://127.0.0.1:" + port + "/management/coherence/cluster");
out.println(" nameservicediscovery: \"\"");
out.println(" clusterversion: \"" + CacheFactory.VERSION + "\"");
out.println(" clustername: \"" + clusterName + "\"");
out.println(" clustertype: Standalone");
out.println(" manuallycreated: false");
out.println(" baseclasspath: \"\"");
out.println(" additionalclasspath: \"\"");
out.println(" arguments: \"\"");
out.println(" managementport: 0");
out.println(" persistencemode: \"\"");
out.println(" loggingdestination: \"\"");
out.println(" managementavailable: false");
out.println("color: \"on\"");
out.println("currentcontext: default");
out.println("debug: false");
out.println("defaultbytesformat: m");
out.println("ignoreinvalidcerts: false");
out.println("requesttimeout: 30");
}
}
}
catch (Exception e) {
System.err.println("Coherence Operator: Failed to create default cohctl config");
e.printStackTrace();
}
}
}

0 comments on commit 7beaabf

Please sign in to comment.