26
26
* SOFTWARE.
27
27
*/
28
28
29
+ import org .slf4j .Logger ;
30
+ import org .slf4j .LoggerFactory ;
29
31
import org .testcontainers .containers .BindMode ;
30
32
33
+ import java .util .ArrayList ;
31
34
import java .util .HashMap ;
35
+ import java .util .List ;
36
+ import java .util .function .Function ;
32
37
33
38
import static com .dajudge .kindcontainer .KubernetesVersionEnum .latest ;
34
39
import static com .dajudge .kindcontainer .client .KubeConfigUtils .replaceServerInKubeconfig ;
35
40
import static com .github .dockerjava .api .model .DockerObjectAccessor .overrideRawValue ;
41
+ import static java .util .Arrays .asList ;
36
42
37
43
public class K3sContainer <SELF extends K3sContainer <SELF >> extends KubernetesWithKubeletContainer <SELF > {
44
+ private static final Logger LOG = LoggerFactory .getLogger (K3sContainer .class );
38
45
private static final int INTERNAL_API_SERVER_PORT = 6443 ;
39
46
private static final HashMap <String , String > TMP_FILESYSTEMS = new HashMap <String , String >() {{
40
47
put ("/run" , "" );
@@ -43,6 +50,7 @@ public class K3sContainer<SELF extends K3sContainer<SELF>> extends KubernetesWit
43
50
private final K3sContainerVersion version ;
44
51
private int minNodePort = 30000 ;
45
52
private int maxNodePort = 32767 ;
53
+ private Function <List <String >, List <String >> cmdLineModifier = Function .identity ();
46
54
47
55
public K3sContainer () {
48
56
this (latest (K3sContainerVersion .class ));
@@ -65,12 +73,14 @@ public K3sContainer(final KubernetesImageSpec<K3sContainerVersion> imageSpec) {
65
73
66
74
@ Override
67
75
public void start () {
68
- this . withCommand (
76
+ final List < String > cmdLine = cmdLineModifier . apply ( new ArrayList <>( asList (
69
77
"server" ,
70
78
getDisabledComponentsCmdlineArg (),
71
- "--tls-san=" + this .getHost (),
79
+ String . format ( "--tls-san=%s" , this .getHost () ),
72
80
String .format ("--service-node-port-range=%d-%d" , minNodePort , maxNodePort )
73
- );
81
+ )));
82
+ LOG .debug ("K3s command line: {}" , cmdLine );
83
+ this .withCommand (cmdLine .toArray (new String [0 ]));
74
84
super .start ();
75
85
}
76
86
@@ -82,12 +92,23 @@ private String getDisabledComponentsCmdlineArg() {
82
92
}
83
93
84
94
@ Override
85
- public SELF withNodePortRange (int minPort , int maxPort ) {
95
+ public SELF withNodePortRange (final int minPort , final int maxPort ) {
86
96
this .minNodePort = minPort ;
87
97
this .maxNodePort = maxPort ;
88
98
return self ();
89
99
}
90
100
101
+ /**
102
+ * Sets a command line modifier for the K3s container, e.g. to configure etcd or the K8s API server.
103
+ *
104
+ * @param modifier the command line modifier
105
+ * @return <code>this</code>
106
+ */
107
+ public SELF withCommandLineModifier (final Function <List <String >, List <String >> modifier ) {
108
+ this .cmdLineModifier = modifier ;
109
+ return self ();
110
+ }
111
+
91
112
@ Override
92
113
public int getInternalPort () {
93
114
return INTERNAL_API_SERVER_PORT ;
0 commit comments