1
- package io .javaoperatorsdk .webhook .sample . springboot ;
1
+ package io .javaoperatorsdk .webhook .sample ;
2
2
3
- import java .io .IOException ;
4
- import java .io .InputStream ;
5
- import java .net .URL ;
6
3
import java .time .Duration ;
7
4
import java .util .List ;
8
5
import java .util .Map ;
6
+ import java .util .concurrent .TimeUnit ;
7
+ import java .util .function .Supplier ;
9
8
import java .util .function .UnaryOperator ;
10
9
11
- import org .junit .jupiter .api .BeforeAll ;
12
10
import org .junit .jupiter .api .Test ;
13
11
14
12
import io .fabric8 .kubernetes .api .model .HasMetadata ;
15
13
import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
16
14
import io .fabric8 .kubernetes .api .model .apiextensions .v1 .*;
17
- import io .fabric8 .kubernetes .api .model .networking .v1 .*;
18
15
import io .fabric8 .kubernetes .client .KubernetesClient ;
19
16
import io .fabric8 .kubernetes .client .KubernetesClientBuilder ;
20
17
import io .fabric8 .kubernetes .client .KubernetesClientException ;
23
20
import io .javaoperatorsdk .webhook .sample .commons .customresource .MultiVersionCustomResourceV2 ;
24
21
25
22
import static io .javaoperatorsdk .webhook .sample .commons .AdmissionControllers .MUTATION_TARGET_LABEL ;
23
+ import static io .javaoperatorsdk .webhook .sample .commons .ConversionControllers .CONVERSION_PATH ;
26
24
import static io .javaoperatorsdk .webhook .sample .commons .Utils .*;
27
- import static io .javaoperatorsdk .webhook .sample .springboot . conversion . ConversionEndpoint . CONVERSION_PATH ;
28
- import static org .assertj .core .api .Assertions .assertThat ;
25
+ import static io .javaoperatorsdk .webhook .sample .commons . Utils . SPIN_UP_GRACE_PERIOD ;
26
+ import static org .assertj .core .api .AssertionsForInterfaceTypes .assertThat ;
29
27
import static org .awaitility .Awaitility .await ;
30
28
import static org .junit .jupiter .api .Assertions .assertThrows ;
31
29
32
- class WebhooksE2E {
30
+ public class EndToEndTestBase {
33
31
34
- private KubernetesClient client = new KubernetesClientBuilder ().build ();
32
+ protected KubernetesClient client = new KubernetesClientBuilder ().build ();
35
33
36
34
public static final String TEST_CR_NAME = "test-cr" ;
37
35
public static final int CR_SPEC_VALUE = 5 ;
38
36
39
- @ BeforeAll
40
- static void deployService () throws IOException {
41
- try (KubernetesClient client = new KubernetesClientBuilder ().build ();
42
- InputStream certManager =
43
- new URL (
44
- "https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml" )
45
- .openStream ()) {
46
- applyAndWait (client , certManager );
47
- applyAndWait (client , "target/classes/META-INF/dekorate/kubernetes.yml" );
48
- applyAndWait (client , "k8s/validating-webhook-configuration.yml" );
49
- applyAndWait (client , "k8s/mutating-webhook-configuration.yml" );
50
- applyAndWait (client ,
51
- "../commons/target/classes/META-INF/fabric8/multiversioncustomresources.sample.javaoperatorsdk-v1.yml" ,
52
- addConversionHookEndpointToCustomResource ());
53
- }
54
- }
55
-
56
37
@ Test
57
38
void validationHook () {
58
39
var ingressWithLabel = testIngress ("normal-add-test" );
59
40
addRequiredLabels (ingressWithLabel );
60
41
await ().atMost (Duration .ofSeconds (SPIN_UP_GRACE_PERIOD )).untilAsserted (() -> {
61
- Ingress res = null ;
62
- try {
63
- // this can be since coredns in minikube can take some time
64
- res = client .network ().v1 ().ingresses ().resource (ingressWithLabel ).createOrReplace ();
65
- } catch (KubernetesClientException e ) {
66
- }
42
+ var res = avoidRequestTimeout (
43
+ () -> client .network ().v1 ().ingresses ().resource (ingressWithLabel ).createOrReplace ());
67
44
assertThat (res ).isNotNull ();
68
45
});
69
46
assertThrows (KubernetesClientException .class ,
@@ -76,11 +53,8 @@ void mutationHook() {
76
53
var ingressWithLabel = testIngress ("mutation-test" );
77
54
addRequiredLabels (ingressWithLabel );
78
55
await ().atMost (Duration .ofSeconds (SPIN_UP_GRACE_PERIOD )).untilAsserted (() -> {
79
- Ingress res = null ;
80
- try {
81
- res = client .network ().v1 ().ingresses ().resource (ingressWithLabel ).createOrReplace ();
82
- } catch (KubernetesClientException e ) {
83
- }
56
+ var res = avoidRequestTimeout (
57
+ () -> client .network ().v1 ().ingresses ().resource (ingressWithLabel ).createOrReplace ());
84
58
assertThat (res ).isNotNull ();
85
59
assertThat (res .getMetadata ().getLabels ()).containsKey (MUTATION_TARGET_LABEL );
86
60
});
@@ -89,11 +63,7 @@ void mutationHook() {
89
63
@ Test
90
64
void conversionHook () {
91
65
await ().atMost (Duration .ofSeconds (SPIN_UP_GRACE_PERIOD )).untilAsserted (() -> {
92
- try {
93
- // this can be since coredns in minikube can take some time
94
- createV1Resource (TEST_CR_NAME );
95
- } catch (KubernetesClientException e ) {
96
- }
66
+ avoidRequestTimeout (() -> createV1Resource (TEST_CR_NAME ));
97
67
});
98
68
MultiVersionCustomResourceV2 v2 =
99
69
client .resources (MultiVersionCustomResourceV2 .class ).withName (TEST_CR_NAME ).get ();
@@ -110,15 +80,30 @@ private MultiVersionCustomResource createV1Resource(String name) {
110
80
return client .resource (res ).createOrReplace ();
111
81
}
112
82
113
- static UnaryOperator <HasMetadata > addConversionHookEndpointToCustomResource () {
83
+ <T > T avoidRequestTimeout (Supplier <T > operator ) {
84
+ try {
85
+ return operator .get ();
86
+ } catch (KubernetesClientException e ) {
87
+ return null ;
88
+ }
89
+ }
90
+
91
+ /** On minikube CoreDNS can take some time to start */
92
+ public static void waitForCoreDNS (KubernetesClient client ) {
93
+ client .apps ().deployments ().inNamespace ("kube-system" ).withName ("coredns" ).waitUntilReady (2 ,
94
+ TimeUnit .MINUTES );
95
+ }
96
+
97
+ public static UnaryOperator <HasMetadata > addConversionHookEndpointToCustomResource (
98
+ String serviceName ) {
114
99
return r -> {
115
100
if (!(r instanceof CustomResourceDefinition )) {
116
101
return r ;
117
102
}
118
103
var crd = (CustomResourceDefinition ) r ;
119
104
var crc = new CustomResourceConversion ();
120
105
crd .getMetadata ()
121
- .setAnnotations (Map .of ("cert-manager.io/inject-ca-from" , "default/spring-boot-sample" ));
106
+ .setAnnotations (Map .of ("cert-manager.io/inject-ca-from" , "default/" + serviceName ));
122
107
crd .getSpec ().setConversion (crc );
123
108
crc .setStrategy ("Webhook" );
124
109
@@ -127,8 +112,9 @@ static UnaryOperator<HasMetadata> addConversionHookEndpointToCustomResource() {
127
112
.withClientConfig (new WebhookClientConfigBuilder ()
128
113
.withService (new ServiceReferenceBuilder ()
129
114
.withPath ("/" + CONVERSION_PATH )
130
- .withName ("spring-boot-sample" )
115
+ .withName (serviceName )
131
116
.withNamespace ("default" )
117
+ .withPort (443 )
132
118
.build ())
133
119
.build ())
134
120
.build ();
0 commit comments