1
1
package com .csviri .kubeapi ;
2
2
3
- import org .apache .commons .io .FileUtils ;
4
3
import org .slf4j .Logger ;
5
4
import org .slf4j .LoggerFactory ;
6
5
7
6
import java .io .*;
8
- import java .util .Scanner ;
9
- import java .util .concurrent .atomic .AtomicBoolean ;
10
7
11
8
public class APIServer {
12
9
@@ -18,9 +15,8 @@ public class APIServer {
18
15
private final BinaryManager binaryManager ;
19
16
private final CertManager certManager ;
20
17
private final KubeConfigManager kubeConfigManager ;
21
- private Process etcdProcess ;
22
- private volatile Process apiServerProcess ;
23
- private volatile boolean stopped = false ;
18
+ private final EtcdProcessManager etcdProcessManager ;
19
+ private final APIServerProcessManager apiServerProcessManager ;
24
20
25
21
public APIServer () {
26
22
this (new APIServerConfig ());
@@ -31,25 +27,26 @@ public APIServer(APIServerConfig config) {
31
27
this .binaryManager = new BinaryManager (config .getJenvtestDirectory (),config .getApiServerVersion ());
32
28
this .certManager = new CertManager (config .getJenvtestDirectory ());
33
29
this .kubeConfigManager = new KubeConfigManager (certManager ,binaryManager );
30
+ this .etcdProcessManager = new EtcdProcessManager (binaryManager ,config );
31
+ this .apiServerProcessManager = new APIServerProcessManager (certManager ,binaryManager ,config );
34
32
}
35
33
36
34
public void start () {
37
35
log .debug ("Stating API Server. Using jenvtest dir: {}" , config .getJenvtestDirectory ());
38
36
prepareLogDirectory ();
39
- cleanEtcdData ();
40
- startEtcd ();
41
- startApiServer ();
37
+ etcdProcessManager . cleanEtcdData ();
38
+ etcdProcessManager . startEtcd ();
39
+ apiServerProcessManager . startApiServer ();
42
40
kubeConfigManager .updateKubeConfig ();
43
- waitUntilDefaultNamespaceCreated ();
41
+ apiServerProcessManager . waitUntilDefaultNamespaceCreated ();
44
42
log .info ("API Server ready to use" );
45
43
}
46
44
47
45
public void stop () {
48
- stopped = true ;
49
- stopApiServer ();
50
- stopEtcd ();
46
+ apiServerProcessManager .stopApiServer ();
47
+ etcdProcessManager .stopEtcd ();
51
48
kubeConfigManager .cleanupFromKubeConfig ();
52
- cleanEtcdData ();
49
+ etcdProcessManager . cleanEtcdData ();
53
50
log .debug ("Fully stopped." );
54
51
}
55
52
@@ -62,117 +59,4 @@ private void prepareLogDirectory() {
62
59
}
63
60
}
64
61
}
65
-
66
-
67
- private void stopApiServer () {
68
- if (apiServerProcess != null ) {
69
- apiServerProcess .destroyForcibly ();
70
- }
71
- log .debug ("API Server stopped" );
72
- }
73
-
74
- private void cleanEtcdData () {
75
- try {
76
- FileUtils .deleteDirectory (new File ("default.etcd" ));
77
- } catch (IOException e ) {
78
- throw new KubeApiException (e );
79
- }
80
- }
81
-
82
- private void stopEtcd () {
83
- if (etcdProcess != null ) {
84
- etcdProcess .destroy ();
85
- }
86
- log .debug ("etcd stopped" );
87
- }
88
-
89
- private void waitUntilDefaultNamespaceCreated () {
90
- try {
91
- AtomicBoolean started = new AtomicBoolean (false );
92
- var proc = new ProcessBuilder (binaryManager .binaries ().getKubectl ().getPath (),"get" ,"ns" ,"--watch" ).start ();
93
- var procWaiter = new Thread (() -> {
94
- try (Scanner sc = new Scanner (proc .getInputStream ())){
95
- while (sc .hasNextLine ()) {
96
- String line = sc .nextLine ();
97
- if (line .contains ("default" )) {
98
- started .set (true );
99
- return ;
100
- }
101
- }
102
- }
103
- });
104
- procWaiter .start ();
105
- procWaiter .join (APIServer .STARTUP_TIMEOUT );
106
- if (!started .get ()) {
107
- throw new KubeApiException ("API Server did not start properly. Check the log files." );
108
- }
109
- } catch (IOException e ) {
110
- throw new KubeApiException (e );
111
- } catch (InterruptedException e ) {
112
- Thread .currentThread ().interrupt ();
113
- throw new KubeApiException (e );
114
- }
115
- }
116
-
117
- private void startEtcd () {
118
- var etcdBinary = binaryManager .binaries ().getEtcd ();
119
- try {
120
- if (!etcdBinary .exists ()) {
121
- throw new KubeApiException ("Missing binary for etcd on path: " + etcdBinary .getAbsolutePath ());
122
- }
123
- var logsFile = new File (config .logDirectory (), "etcd.logs" );
124
-
125
- etcdProcess = new ProcessBuilder (etcdBinary .getAbsolutePath (),
126
- "--listen-client-urls=http://0.0.0.0:2379" ,
127
- "--advertise-client-urls=http://0.0.0.0:2379" )
128
- // todo log to a different logger on debug level
129
- .redirectOutput (logsFile )
130
- .redirectError (logsFile )
131
- .start ();
132
- etcdProcess .onExit ().thenApply (p -> {
133
- if (!stopped ) {
134
- throw new KubeApiException ("Etcd stopped unexpectedly" );
135
- }
136
- return null ;
137
- });
138
- log .debug ("etcd started" );
139
- } catch (IOException e ) {
140
- throw new KubeApiException (e );
141
- }
142
- }
143
-
144
- private void startApiServer () {
145
- certManager .ensureAPIServerCertificates ();
146
- var apiServerBinary = binaryManager .binaries ().getApiServer ();
147
- try {
148
- if (!apiServerBinary .exists ()) {
149
- throw new KubeApiException ("Missing binary for API Server on path: " + apiServerBinary .getAbsolutePath ());
150
- }
151
-
152
- apiServerProcess = new ProcessBuilder (apiServerBinary .getAbsolutePath (),
153
- "--cert-dir" , config .getJenvtestDirectory (),
154
- "--etcd-servers" , "http://0.0.0.0:2379" ,
155
- "--authorization-mode" , "RBAC" ,
156
- "--service-account-issuer" , "https://localhost" ,
157
- "--service-account-signing-key-file" , certManager .getAPIServerKeyPath (),
158
- "--service-account-signing-key-file" , certManager .getAPIServerKeyPath (),
159
- "--service-account-key-file" , certManager .getAPIServerKeyPath (),
160
- "--service-account-issuer" , certManager .getAPIServerCertPath (),
161
- "--disable-admission-plugins" , "ServiceAccount" ,
162
- "--client-ca-file" , certManager .getClientCertPath (),
163
- "--service-cluster-ip-range" , "10.0.0.0/24" ,
164
- "--allow-privileged"
165
- )
166
- .start ();
167
- apiServerProcess .onExit ().thenApply (p -> {
168
- if (!stopped ) {
169
- throw new KubeApiException ("APIServer stopped unexpectedly." );
170
- }
171
- return null ;
172
- });
173
- log .debug ("API Server started" );
174
- } catch (IOException e ) {
175
- throw new RuntimeException (e );
176
- }
177
- }
178
62
}
0 commit comments