forked from maxlaverse/kubernetes-cli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 47
/
GenericBuildStep.java
90 lines (73 loc) · 3.26 KB
/
GenericBuildStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.jenkinsci.plugins.kubernetes.cli;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.FilePath;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.kubernetes.cli.kubeconfig.KubeConfigWriter;
import org.jenkinsci.plugins.kubernetes.cli.kubeconfig.KubeConfigWriterFactory;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import javax.annotation.Nonnull;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class GenericBuildStep extends AbstractStepExecutionImpl {
private static final long serialVersionUID = 1L;
@SuppressFBWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "not needed on deserialization")
private transient List<KubectlCredential> kubectlCredentials;
public GenericBuildStep(List<KubectlCredential> credentials, StepContext context) {
super(context);
this.kubectlCredentials = credentials;
}
/**
* {@inheritDoc}
*/
@Override
public boolean start() throws Exception {
List<String> configFiles = new ArrayList<String>();
for(KubectlCredential cred: this.kubectlCredentials) {
KubeConfigWriter kubeConfigWriter = KubeConfigWriterFactory.get(
cred.serverUrl,
cred.credentialsId,
cred.caCertificate,
cred.clusterName,
cred.contextName,
cred.namespace,
getContext());
configFiles.add(kubeConfigWriter.writeKubeConfig());
}
// Prepare a new environment
String configFileList = String.join(File.pathSeparator, configFiles);
EnvironmentExpander envExpander = EnvironmentExpander.merge(
getContext().get(EnvironmentExpander.class),
new KubeConfigExpander(configFileList));
// Execute the commands in the body within this environment
getContext().get(TaskListener.class).getLogger().println("[kubernetes-cli] !!!DEBUG!!! KUBECONFIG variable set to '"+configFileList +"' (len:"+configFileList.length() + ")");
getContext().newBodyInvoker()
.withContext(envExpander)
.withCallback(new Callback(configFiles))
.start();
return false;
}
/**
* {@inheritDoc}
*/
@Override
public void stop(@Nonnull Throwable cause) throws Exception {
getContext().onFailure(cause);
}
private static final class Callback extends BodyExecutionCallback.TailCall {
private static final long serialVersionUID = 1L;
private final List<String> configFiles;
Callback(List<String> configFiles) {
this.configFiles = configFiles;
}
protected void finished(StepContext context) throws Exception {
for(String configFile : configFiles) {
context.get(FilePath.class).child(configFile).delete();
}
context.get(TaskListener.class).getLogger().println("[kubernetes-cli] kubectl configuration cleaned up");
}
}
}