1
1
package io .javaoperatorsdk .jenvtest .junit ;
2
2
3
3
import java .lang .reflect .AnnotatedElement ;
4
+ import java .lang .reflect .Field ;
4
5
import java .util .Arrays ;
5
6
import java .util .List ;
6
7
import java .util .Optional ;
@@ -26,7 +27,7 @@ public class KubeAPIServerExtension
26
27
27
28
@ Override
28
29
public void beforeAll (ExtensionContext extensionContext ) {
29
- startIfAnnotationPresent (extensionContext );
30
+ initialize (extensionContext , true );
30
31
}
31
32
32
33
@ Override
@@ -36,23 +37,69 @@ public void afterAll(ExtensionContext extensionContext) {
36
37
37
38
@ Override
38
39
public void beforeEach (ExtensionContext extensionContext ) {
39
- startIfAnnotationPresent (extensionContext );
40
+ initialize (extensionContext , false );
40
41
}
41
42
42
43
@ Override
43
44
public void afterEach (ExtensionContext extensionContext ) {
44
45
stopIfAnnotationPresent (extensionContext );
45
46
}
46
47
47
- private void startIfAnnotationPresent (ExtensionContext extensionContext ) {
48
+
49
+ private void initialize (ExtensionContext extensionContext , boolean staticContext ) {
50
+ var kubeConfigField = getFieldForKubeConfigInjection (extensionContext , staticContext );
51
+ startIfAnnotationPresent (extensionContext , kubeConfigField .isEmpty ());
52
+ kubeConfigField .ifPresent (f -> setKubeConfigYamlToField (extensionContext , f ));
53
+ }
54
+
55
+ private void setKubeConfigYamlToField (ExtensionContext extensionContext , Field kubeConfigField ) {
56
+ try {
57
+ var target = extensionContext .getTestInstance ()
58
+ .orElseGet (() -> extensionContext .getTestClass ().orElseThrow ());
59
+ kubeConfigField .setAccessible (true );
60
+ kubeConfigField .set (target ,
61
+ kubeApiServer .getKubeConfigYaml ());
62
+ } catch (IllegalAccessException e ) {
63
+ throw new JenvtestException (e );
64
+ }
65
+ }
66
+
67
+ private Optional <Field > getFieldForKubeConfigInjection (ExtensionContext extensionContext ,
68
+ boolean findStatic ) {
69
+ Class <?> clazz = extensionContext .getTestClass ().orElseThrow ();
70
+ var kubeConfigFields = Arrays .stream (clazz .getDeclaredFields ())
71
+ .filter (f -> f .getAnnotationsByType (KubeConfig .class ).length > 0 )
72
+ .collect (Collectors .toList ());
73
+ if (kubeConfigFields .isEmpty ()) {
74
+ return Optional .empty ();
75
+ }
76
+ if (kubeConfigFields .size () > 1 ) {
77
+ throw new JenvtestException (
78
+ "More fields annotation with @" + KubeConfig .class .getSimpleName () + " annotation" );
79
+ }
80
+ var field = kubeConfigFields .get (0 );
81
+ if (!field .getType ().equals (String .class )) {
82
+ throw new JenvtestException (
83
+ "Field annotated with @" + KubeConfig .class .getSimpleName () + " is not a String" );
84
+ }
85
+
86
+ if (java .lang .reflect .Modifier .isStatic (field .getModifiers ()) != findStatic ) {
87
+ return Optional .empty ();
88
+ } else {
89
+ return Optional .of (field );
90
+ }
91
+ }
92
+
93
+ private void startIfAnnotationPresent (ExtensionContext extensionContext ,
94
+ boolean updateKubeConfig ) {
48
95
extensionContext .getElement ().ifPresent (ae -> {
49
96
var annotation = getExtensionAnnotationInstance (ae );
50
- annotation .ifPresent (this :: startApiServer );
97
+ annotation .ifPresent (a -> startApiServer ( a , updateKubeConfig ) );
51
98
});
52
99
}
53
100
54
- private void startApiServer (EnableKubeAPIServer annotation ) {
55
- kubeApiServer = new KubeAPIServer (annotationToConfig (annotation ));
101
+ private void startApiServer (EnableKubeAPIServer annotation , boolean updateKubeConfig ) {
102
+ kubeApiServer = new KubeAPIServer (annotationToConfig (annotation , updateKubeConfig ));
56
103
kubeApiServer .start ();
57
104
}
58
105
@@ -64,7 +111,8 @@ private void stopIfAnnotationPresent(ExtensionContext extensionContext) {
64
111
});
65
112
}
66
113
67
- private KubeAPIServerConfig annotationToConfig (EnableKubeAPIServer annotation ) {
114
+ private KubeAPIServerConfig annotationToConfig (EnableKubeAPIServer annotation ,
115
+ boolean updateKubeConfig ) {
68
116
var builder = KubeAPIServerConfigBuilder .anAPIServerConfig ();
69
117
var version = annotation .kubeAPIVersion ();
70
118
if (!NOT_SET .equals (version )) {
@@ -73,6 +121,7 @@ private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation) {
73
121
if (annotation .apiServerFlags ().length > 0 ) {
74
122
builder .withApiServerFlags (List .of (annotation .apiServerFlags ()));
75
123
}
124
+ builder .withUpdateKubeConfig (updateKubeConfig );
76
125
return builder .build ();
77
126
}
78
127
0 commit comments