Skip to content

Commit 7ba091d

Browse files
Merge pull request #23 from bdpiparva/mandate-cluster-cacert
WIP: Different api authentication strategies for plugin
2 parents 0f79663 + ac5396e commit 7ba091d

File tree

46 files changed

+976
-615
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+976
-615
lines changed

src/main/java/cd/go/contrib/elasticagent/KubernetesAgentInstances.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3535
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.SPECIFIED_USING_POD_CONFIGURATION;
3636
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
37+
import static java.text.MessageFormat.format;
3738

3839
public class KubernetesAgentInstances implements AgentInstances<KubernetesInstance> {
3940
private final ConcurrentHashMap<String, KubernetesInstance> instances = new ConcurrentHashMap<>();
@@ -58,14 +59,14 @@ public KubernetesAgentInstances(KubernetesClientFactory factory, KubernetesInsta
5859

5960
@Override
6061
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) throws Exception {
61-
final Integer maxAllowedContainers = settings.getMaximumPendingAgentsCount();
62+
final Integer maxAllowedContainers = settings.getMaxPendingPods();
6263
synchronized (instances) {
6364
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));
6465

6566
if (semaphore.tryAcquire()) {
6667
return createKubernetesInstance(request, settings, pluginRequest);
6768
} else {
68-
LOG.warn(String.format("The number of pending kubernetes pods is currently at the maximum permissible limit (%d). Total kubernetes pods (%d). Not creating any more containers.", maxAllowedContainers, instances.size()));
69+
LOG.warn(format("The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size()));
6970
return null;
7071
}
7172
}
@@ -77,14 +78,14 @@ private void doWithLockOnSemaphore(Runnable runnable) {
7778
}
7879
}
7980

80-
private KubernetesInstance createKubernetesInstance(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) throws Exception {
81+
private KubernetesInstance createKubernetesInstance(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) {
8182
JobIdentifier jobIdentifier = request.jobIdentifier();
8283
if (isAgentCreatedForJob(jobIdentifier.getJobId())) {
83-
LOG.warn("[Create Agent Request] Request for creating an agent for Job Identifier [" + jobIdentifier + "] has already been scheduled. Skipping current request.");
84+
LOG.warn(format("[Create Agent Request] Request for creating an agent for Job Identifier [{0}] has already been scheduled. Skipping current request.", jobIdentifier));
8485
return null;
8586
}
8687

87-
KubernetesClient client = factory.kubernetes(settings);
88+
KubernetesClient client = factory.client(settings);
8889
KubernetesInstance instance = kubernetesInstanceFactory.create(request, settings, client, pluginRequest, isUsingPodYaml(request));
8990
register(instance);
9091

@@ -109,10 +110,10 @@ private boolean isUsingPodYaml(CreateAgentRequest request) {
109110
public void terminate(String agentId, PluginSettings settings) throws Exception {
110111
KubernetesInstance instance = instances.get(agentId);
111112
if (instance != null) {
112-
KubernetesClient client = factory.kubernetes(settings);
113+
KubernetesClient client = factory.client(settings);
113114
instance.terminate(client);
114115
} else {
115-
LOG.warn("Requested to terminate an instance that does not exist " + agentId);
116+
LOG.warn(format("Requested to terminate an instance that does not exist {0}.", agentId));
116117
}
117118
instances.remove(agentId);
118119
}
@@ -124,7 +125,7 @@ public void terminateUnregisteredInstances(PluginSettings settings, Agents agent
124125
return;
125126
}
126127

127-
LOG.warn("Terminating instances that did not register " + toTerminate.instances.keySet());
128+
LOG.warn(format("Terminating instances that did not register {0}.", toTerminate.instances.keySet()));
128129
for (KubernetesInstance container : toTerminate.instances.values()) {
129130
terminate(container.name(), settings);
130131
}
@@ -147,9 +148,9 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
147148
}
148149

149150
@Override
150-
public void refreshAll(PluginRequest pluginRequest) throws Exception {
151-
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information");
152-
KubernetesClient client = factory.kubernetes(pluginRequest.getPluginSettings());
151+
public void refreshAll(PluginRequest pluginRequest) {
152+
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information.");
153+
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
153154
PodList list = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list();
154155

155156
for (Pod pod : list.getItems()) {
@@ -174,7 +175,7 @@ private void register(KubernetesInstance instance) {
174175
private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
175176
Period period = settings.getAutoRegisterPeriod();
176177
KubernetesAgentInstances unregisteredInstances = new KubernetesAgentInstances();
177-
KubernetesClient client = factory.kubernetes(settings);
178+
KubernetesClient client = factory.client(settings);
178179

179180
for (String instanceName : instances.keySet()) {
180181
if (knownAgents.containsAgentWithId(instanceName)) {

src/main/java/cd/go/contrib/elasticagent/KubernetesClientFactory.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package cd.go.contrib.elasticagent;
1818

19-
import io.fabric8.kubernetes.client.Config;
2019
import io.fabric8.kubernetes.client.ConfigBuilder;
2120
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
2221
import io.fabric8.kubernetes.client.KubernetesClient;
23-
import org.apache.commons.lang3.StringUtils;
22+
23+
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
24+
import static java.text.MessageFormat.format;
2425

2526
public class KubernetesClientFactory {
2627
private static final KubernetesClientFactory KUBERNETES_CLIENT_FACTORY = new KubernetesClientFactory();
@@ -31,31 +32,26 @@ public static KubernetesClientFactory instance() {
3132
return KUBERNETES_CLIENT_FACTORY;
3233
}
3334

34-
private static KubernetesClient createClient(PluginSettings pluginSettings) throws Exception {
35-
ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(pluginSettings.getKubernetesClusterUrl());
36-
if (StringUtils.isNotBlank(pluginSettings.getKubernetesClusterUsername())) {
37-
configBuilder.withUsername(pluginSettings.getKubernetesClusterUsername());
38-
}
39-
40-
if (StringUtils.isNotBlank(pluginSettings.getKubernetesClusterPassword())) {
41-
configBuilder.withPassword(pluginSettings.getKubernetesClusterPassword());
42-
}
43-
44-
if (StringUtils.isNotBlank(pluginSettings.getKubernetesClusterCACert())) {
45-
configBuilder.withCaCertData(pluginSettings.getKubernetesClusterCACert());
46-
}
47-
48-
Config build = configBuilder.build();
49-
return new DefaultKubernetesClient(build);
50-
}
51-
52-
public synchronized KubernetesClient kubernetes(PluginSettings pluginSettings) throws Exception {
35+
public synchronized KubernetesClient client(PluginSettings pluginSettings) {
5336
if (pluginSettings.equals(this.pluginSettings) && this.client != null) {
37+
LOG.debug("Using previously created client.");
5438
return this.client;
5539
}
5640

41+
LOG.debug(format("Creating a new client because {0}.", (client == null) ? "client is null" : "plugin setting is changed"));
5742
this.pluginSettings = pluginSettings;
58-
this.client = createClient(pluginSettings);
43+
this.client = createClientFor(pluginSettings);
44+
LOG.debug("New client is created.");
5945
return this.client;
6046
}
47+
48+
private KubernetesClient createClientFor(PluginSettings pluginSettings) {
49+
final ConfigBuilder configBuilder = new ConfigBuilder()
50+
.withOauthToken(pluginSettings.getOauthToken())
51+
.withMasterUrl(pluginSettings.getClusterUrl())
52+
.withCaCertData(pluginSettings.getCaCertData());
53+
54+
return new DefaultKubernetesClient(configBuilder.build())
55+
.inNamespace(pluginSettings.getNamespace());
56+
}
6157
}

src/main/java/cd/go/contrib/elasticagent/KubernetesInstanceFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.PRIVILEGED;
4242
import static cd.go.contrib.elasticagent.utils.Util.GSON;
4343
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
44-
import static java.lang.String.format;
4544
import static java.lang.String.valueOf;
45+
import static java.text.MessageFormat.format;
4646
import static org.apache.commons.lang3.StringUtils.isBlank;
4747

4848
public class KubernetesInstanceFactory {
@@ -55,7 +55,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
5555
}
5656

5757
private KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, KubernetesClient client, PluginRequest pluginRequest) {
58-
String containerName = format("%s-%s", KUBERNETES_POD_NAME_PREFIX, UUID.randomUUID().toString());
58+
String containerName = format("{0}-{1}", KUBERNETES_POD_NAME_PREFIX, UUID.randomUUID().toString());
5959

6060
Container container = new Container();
6161
container.setName(containerName);
@@ -101,13 +101,13 @@ private ResourceRequirements getPodResources(CreateAgentRequest request) {
101101
String maxMemory = request.properties().get("MaxMemory");
102102
if (StringUtils.isNotBlank(maxMemory)) {
103103
Size mem = Size.parse(maxMemory);
104-
LOG.debug(format("[Create Agent] Setting memory resource limit on k8s pod:%s", new Quantity(valueOf(mem.toMegabytes()), "M")));
104+
LOG.debug(format("[Create Agent] Setting memory resource limit on k8s pod: {0}.", new Quantity(valueOf(mem.toMegabytes()), "M")));
105105
limits.put("memory", new Quantity(valueOf(mem.toBytes())));
106106
}
107107

108108
String maxCPU = request.properties().get("MaxCPU");
109109
if (StringUtils.isNotBlank(maxCPU)) {
110-
LOG.debug(format("[Create Agent] Setting cpu resource limit on k8s pod:%s", new Quantity(maxCPU)));
110+
LOG.debug(format("[Create Agent] Setting cpu resource limit on k8s pod: {0}.", new Quantity(maxCPU)));
111111
limits.put("cpu", new Quantity(maxCPU));
112112
}
113113

@@ -130,7 +130,7 @@ private static void setAnnotations(Pod pod, CreateAgentRequest request) {
130130
}
131131

132132
private KubernetesInstance createKubernetesPod(KubernetesClient client, Pod elasticAgentPod) {
133-
LOG.info(format("[Create Agent] Creating K8s pod with spec:%s", elasticAgentPod.toString()));
133+
LOG.info(format("[Create Agent] Creating K8s pod with spec: {0}.", elasticAgentPod.toString()));
134134
Pod pod = client.pods().inNamespace(KUBERNETES_NAMESPACE).create(elasticAgentPod);
135135
return fromKubernetesPod(pod);
136136
}

src/main/java/cd/go/contrib/elasticagent/KubernetesPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class KubernetesPlugin implements GoPlugin {
3535
public static final Logger LOG = Logger.getLoggerFor(KubernetesPlugin.class);
3636

3737
private PluginRequest pluginRequest;
38-
private AgentInstances agentInstances;
38+
private AgentInstances<KubernetesInstance> agentInstances;
3939

4040
@Override
4141
public void initializeGoApplicationAccessor(GoApplicationAccessor accessor) {

src/main/java/cd/go/contrib/elasticagent/PluginRequest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static cd.go.contrib.elasticagent.Constants.*;
2727
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
28+
import static java.text.MessageFormat.format;
2829

2930
public class PluginRequest {
3031
private final GoApplicationAccessor accessor;
@@ -52,10 +53,11 @@ public PluginSettings getPluginSettings() throws ServerRequestFailedException {
5253
throw ServerRequestFailedException.getPluginSettings(response);
5354
}
5455

55-
final PluginSettings pluginSettings = PluginSettings.fromJSON(response.responseBody());
56+
PluginSettings pluginSettings = PluginSettings.fromJSON(response.responseBody());
5657
if (pluginSettings == null) {
5758
throw new PluginSettingsNotConfiguredException();
5859
}
60+
5961
return pluginSettings;
6062
}
6163

@@ -71,7 +73,7 @@ public Agents listAgents() throws ServerRequestFailedException {
7173
}
7274

7375
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
74-
LOG.debug("[Server Ping] Disabling Agents:"+toBeDisabled.toString());
76+
LOG.debug(format("[Server Ping] Disabling Agents: {0}", toBeDisabled.toString()));
7577
if (toBeDisabled.isEmpty()) {
7678
return;
7779
}
@@ -87,7 +89,7 @@ public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFa
8789
}
8890

8991
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
90-
LOG.debug("[Server Ping] Deleting Agents:"+toBeDeleted.toString());
92+
LOG.debug(format("[Server Ping] Deleting Agents: {0}", toBeDeleted.toString()));
9193
if (toBeDeleted.isEmpty()) {
9294
return;
9395
}

0 commit comments

Comments
 (0)