Skip to content

Commit 7f61072

Browse files
authored
Merge pull request #1396 from yue9944882/csr
Feat: CSR support and e2e test
2 parents e28c5c0 + b4e494e commit 7f61072

File tree

11 files changed

+638
-75
lines changed

11 files changed

+638
-75
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.kubernetes.client.e2e.csr
2+
3+
import io.kubernetes.client.openapi.ApiClient
4+
import io.kubernetes.client.openapi.models.V1CertificateSigningRequest
5+
import io.kubernetes.client.util.CSRUtils
6+
import io.kubernetes.client.util.ClientBuilder
7+
import io.kubernetes.client.util.version.Version
8+
import spock.lang.Specification
9+
import spock.util.concurrent.PollingConditions
10+
11+
import java.security.KeyPairGenerator
12+
import java.security.SecureRandom
13+
14+
class CSRTest extends Specification {
15+
def "Build client instance via CSR should work"() {
16+
// initialize test environment
17+
given:
18+
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA")
19+
keyGen.initialize(2048, new SecureRandom())
20+
ApiClient bootstrapApiClient = ClientBuilder.defaultClient()
21+
22+
// generating key-pair
23+
when:
24+
def keyPair = keyGen.generateKeyPair()
25+
then:
26+
keyPair != null
27+
28+
// sign new CSR
29+
when:
30+
def csrBytes = CSRUtils.sign(keyPair, "foo")
31+
then:
32+
csrBytes != null
33+
34+
// creates CSR object against cluster
35+
when:
36+
def newCsr = CSRUtils.newV1CertificateSigningRequest("foo", csrBytes)
37+
def created = CSRUtils.createIfAbsent(bootstrapApiClient, newCsr)
38+
then:
39+
created
40+
41+
// reload certificates from CSR
42+
when:
43+
CSRUtils.approve(bootstrapApiClient, "foo")
44+
def signedApiClient = ClientBuilder.fromCertificateSigningRequest(bootstrapApiClient, keyPair.getPrivate(), newCsr)
45+
then:
46+
signedApiClient != null
47+
48+
// use the new client to get "/version"
49+
when:
50+
def v = new Version(signedApiClient).getVersion()
51+
then:
52+
v != null
53+
}
54+
}

extended/src/main/java/io/kubernetes/client/extended/wait/Wait.java

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,5 @@
1212
*/
1313
package io.kubernetes.client.extended.wait;
1414

15-
import java.time.Duration;
16-
import java.util.concurrent.Executors;
17-
import java.util.concurrent.ScheduledExecutorService;
18-
import java.util.concurrent.ScheduledFuture;
19-
import java.util.concurrent.TimeUnit;
20-
import java.util.concurrent.atomic.AtomicBoolean;
21-
import java.util.function.Supplier;
22-
23-
public class Wait {
24-
25-
/**
26-
* Poll tries a condition func until it returns true, an exception, or the timeout is reached.
27-
*
28-
* @param interval the check interval
29-
* @param timeout the timeout period
30-
* @param condition the condition func
31-
*/
32-
public static boolean poll(Duration interval, Duration timeout, Supplier<Boolean> condition) {
33-
return poll(interval, interval, timeout, condition);
34-
}
35-
36-
/**
37-
* Poll tries a condition func until w/ the initial delay specified.
38-
*
39-
* @param initialDelay the initial delay
40-
* @param interval the check interval
41-
* @param timeout the timeout period
42-
* @param condition the condition
43-
* @return returns true if gracefully finished
44-
*/
45-
public static boolean poll(
46-
Duration initialDelay, Duration interval, Duration timeout, Supplier<Boolean> condition) {
47-
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
48-
AtomicBoolean result = new AtomicBoolean(false);
49-
long dueDate = System.currentTimeMillis() + timeout.toMillis();
50-
ScheduledFuture<?> future =
51-
executorService.scheduleAtFixedRate(
52-
() -> {
53-
try {
54-
result.set(condition.get());
55-
} catch (Exception e) {
56-
result.set(false);
57-
}
58-
},
59-
initialDelay.toMillis(),
60-
interval.toMillis(),
61-
TimeUnit.MILLISECONDS);
62-
try {
63-
while (System.currentTimeMillis() < dueDate) {
64-
if (result.get()) {
65-
future.cancel(true);
66-
return true;
67-
}
68-
}
69-
} catch (Exception e) {
70-
return result.get();
71-
}
72-
future.cancel(true);
73-
return result.get();
74-
}
75-
}
15+
@Deprecated
16+
public class Wait extends io.kubernetes.client.util.wait.Wait {}

0 commit comments

Comments
 (0)