Skip to content

Commit d38c179

Browse files
authored
Replace default kubernetes client (#11)
* refactor: move KubernetesClient to composition of Fabric8 Client * refactor: move from DefaultKubernetesClient to KubernetesClientBuilder * fix: formatting
1 parent c1a30da commit d38c179

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

src/main/java/cws/k8s/scheduler/client/KubernetesClient.java renamed to src/main/java/cws/k8s/scheduler/client/CWSKubernetesClient.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,48 @@
33
import cws.k8s.scheduler.model.NodeWithAlloc;
44
import cws.k8s.scheduler.model.PodWithAge;
55
import io.fabric8.kubernetes.api.model.*;
6-
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
7-
import io.fabric8.kubernetes.client.KubernetesClientException;
8-
import io.fabric8.kubernetes.client.Watcher;
9-
import io.fabric8.kubernetes.client.WatcherException;
6+
import io.fabric8.kubernetes.client.*;
7+
import io.fabric8.kubernetes.client.Config;
8+
import io.fabric8.kubernetes.client.dsl.MixedOperation;
9+
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
10+
import io.fabric8.kubernetes.client.dsl.PodResource;
11+
import io.fabric8.kubernetes.client.dsl.Resource;
1012
import lombok.extern.slf4j.Slf4j;
1113

1214
import java.math.BigDecimal;
1315
import java.util.*;
1416

1517
@Slf4j
16-
public class KubernetesClient extends DefaultKubernetesClient {
18+
public class CWSKubernetesClient {
1719

18-
private final Map<String, NodeWithAlloc> nodeHolder= new HashMap<>();
20+
private final KubernetesClient client;
21+
22+
private final Map<String, NodeWithAlloc> nodeHolder = new HashMap<>();
1923
private final List<Informable> informables = new LinkedList<>();
2024

2125

22-
public KubernetesClient(){
26+
public CWSKubernetesClient() {
27+
KubernetesClientBuilder builder = new KubernetesClientBuilder();
28+
this.client = builder.build();
2329
for( Node node : this.nodes().list().getItems() ){
2430
nodeHolder.put( node.getMetadata().getName(), new NodeWithAlloc(node,this) );
2531
}
2632
this.pods().inAnyNamespace().watch( new PodWatcher( this ) );
2733
this.nodes().watch( new NodeWatcher( this ) );
2834
}
2935

36+
public NonNamespaceOperation<Node, NodeList, Resource<Node>> nodes() {
37+
return client.nodes();
38+
}
39+
40+
public MixedOperation<Pod, PodList, PodResource> pods() {
41+
return client.pods();
42+
}
43+
44+
public Config getConfiguration() {
45+
return client.getConfiguration();
46+
}
47+
3048
public void addInformable( Informable informable ){
3149
synchronized ( informables ){
3250
informables.add( informable );
@@ -83,7 +101,7 @@ public void assignPodToNode( PodWithAge pod, String node ) {
83101
.withApiVersion( nodeWithAlloc.getApiVersion() )
84102
.withName( node ).endTarget()
85103
.build();
86-
bindings()
104+
client.bindings()
87105
.inNamespace( pod.getMetadata().getNamespace() )
88106
.resource( build )
89107
.create();
@@ -106,7 +124,7 @@ public List<NodeWithAlloc> getAllNodes(){
106124
}
107125

108126
public BigDecimal getMemoryOfNode(NodeWithAlloc node ){
109-
final Quantity memory = this
127+
final Quantity memory = client
110128
.top()
111129
.nodes()
112130
.metrics(node.getName())
@@ -117,9 +135,9 @@ public BigDecimal getMemoryOfNode(NodeWithAlloc node ){
117135

118136
static class NodeWatcher implements Watcher<Node>{
119137

120-
private final KubernetesClient kubernetesClient;
138+
private final CWSKubernetesClient kubernetesClient;
121139

122-
public NodeWatcher(KubernetesClient kubernetesClient) {
140+
public NodeWatcher(CWSKubernetesClient kubernetesClient) {
123141
this.kubernetesClient = kubernetesClient;
124142
}
125143

@@ -173,9 +191,9 @@ public void onClose(WatcherException cause) {
173191

174192
static class PodWatcher implements Watcher<Pod> {
175193

176-
private final KubernetesClient kubernetesClient;
194+
private final CWSKubernetesClient kubernetesClient;
177195

178-
public PodWatcher(KubernetesClient kubernetesClient) {
196+
public PodWatcher(CWSKubernetesClient kubernetesClient) {
179197
this.kubernetesClient = kubernetesClient;
180198
}
181199

src/main/java/cws/k8s/scheduler/config/Beans.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cws.k8s.scheduler.config;
22

3-
import cws.k8s.scheduler.client.KubernetesClient;
3+
import cws.k8s.scheduler.client.CWSKubernetesClient;
44
import lombok.AccessLevel;
55
import lombok.RequiredArgsConstructor;
66
import org.springframework.context.annotation.Bean;
@@ -10,12 +10,12 @@
1010
@RequiredArgsConstructor( access = AccessLevel.PACKAGE )
1111
public class Beans {
1212

13-
private static KubernetesClient kubernetesClient;
13+
private static CWSKubernetesClient kubernetesClient;
1414

1515
@Bean
16-
KubernetesClient getClient(){
16+
CWSKubernetesClient getClient(){
1717
if(kubernetesClient == null) {
18-
kubernetesClient = new KubernetesClient();
18+
kubernetesClient = new CWSKubernetesClient();
1919
kubernetesClient.getConfiguration().setNamespace(null);
2020
}
2121
return kubernetesClient;

src/main/java/cws/k8s/scheduler/model/NodeWithAlloc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cws.k8s.scheduler.model;
22

3-
import cws.k8s.scheduler.client.KubernetesClient;
3+
import cws.k8s.scheduler.client.CWSKubernetesClient;
44
import io.fabric8.kubernetes.api.model.Node;
55
import io.fabric8.kubernetes.api.model.ObjectMeta;
66
import io.fabric8.kubernetes.api.model.Pod;
@@ -16,7 +16,7 @@
1616
@Slf4j
1717
public class NodeWithAlloc extends Node implements Comparable<NodeWithAlloc> {
1818

19-
private final transient KubernetesClient kubernetesClient;
19+
private final transient CWSKubernetesClient kubernetesClient;
2020

2121
private static final long serialVersionUID = 1L;
2222

@@ -34,7 +34,7 @@ public NodeWithAlloc( String name ) {
3434
this.getMetadata().setName( name );
3535
}
3636

37-
public NodeWithAlloc( Node node, KubernetesClient kubernetesClient ) {
37+
public NodeWithAlloc( Node node, CWSKubernetesClient kubernetesClient ) {
3838

3939
this.kubernetesClient = kubernetesClient;
4040

src/main/java/cws/k8s/scheduler/rest/SchedulerRestController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import cws.k8s.scheduler.dag.DAG;
44
import cws.k8s.scheduler.dag.InputEdge;
5-
import cws.k8s.scheduler.client.KubernetesClient;
5+
import cws.k8s.scheduler.client.CWSKubernetesClient;
66
import cws.k8s.scheduler.dag.Vertex;
77
import cws.k8s.scheduler.model.SchedulerConfig;
88
import cws.k8s.scheduler.model.TaskConfig;
@@ -37,7 +37,7 @@
3737
@EnableScheduling
3838
public class SchedulerRestController {
3939

40-
private final KubernetesClient client;
40+
private final CWSKubernetesClient client;
4141
private final boolean autoClose;
4242
private final ApplicationContext appContext;
4343
private long closedLastScheduler = -1;
@@ -50,7 +50,7 @@ public class SchedulerRestController {
5050
private static final Map<String, Scheduler> schedulerHolder = new HashMap<>();
5151

5252
public SchedulerRestController(
53-
@Autowired KubernetesClient client,
53+
@Autowired CWSKubernetesClient client,
5454
@Value("#{environment.AUTOCLOSE}") String autoClose,
5555
@Autowired ApplicationContext appContext ){
5656
this.client = client;

src/main/java/cws/k8s/scheduler/scheduler/PrioritizeAssignScheduler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import cws.k8s.scheduler.model.*;
44
import cws.k8s.scheduler.scheduler.prioritize.Prioritize;
55
import cws.k8s.scheduler.client.Informable;
6-
import cws.k8s.scheduler.client.KubernetesClient;
6+
import cws.k8s.scheduler.client.CWSKubernetesClient;
77
import cws.k8s.scheduler.scheduler.nodeassign.NodeAssign;
88
import cws.k8s.scheduler.util.NodeTaskAlignment;
99
import lombok.extern.slf4j.Slf4j;
@@ -17,7 +17,7 @@ public class PrioritizeAssignScheduler extends Scheduler {
1717
private final NodeAssign nodeAssigner;
1818

1919
public PrioritizeAssignScheduler( String execution,
20-
KubernetesClient client,
20+
CWSKubernetesClient client,
2121
String namespace,
2222
SchedulerConfig config,
2323
Prioritize prioritize,

src/main/java/cws/k8s/scheduler/scheduler/Scheduler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import cws.k8s.scheduler.model.*;
55
import cws.k8s.scheduler.util.Batch;
66
import cws.k8s.scheduler.client.Informable;
7-
import cws.k8s.scheduler.client.KubernetesClient;
7+
import cws.k8s.scheduler.client.CWSKubernetesClient;
88
import cws.k8s.scheduler.util.NodeTaskAlignment;
99
import io.fabric8.kubernetes.api.model.Pod;
1010
import io.fabric8.kubernetes.client.Watch;
@@ -40,7 +40,7 @@ public abstract class Scheduler implements Informable {
4040
private int currentBatch = 0;
4141
private Batch currentBatchInstance = null;
4242

43-
final KubernetesClient client;
43+
final CWSKubernetesClient client;
4444
private final Set<Task> upcomingTasks = new HashSet<>();
4545
private final List<Task> unscheduledTasks = new ArrayList<>( 100 );
4646
private final List<Task> unfinishedTasks = new ArrayList<>( 100 );
@@ -52,7 +52,7 @@ public abstract class Scheduler implements Informable {
5252

5353
final boolean traceEnabled;
5454

55-
Scheduler(String execution, KubernetesClient client, String namespace, SchedulerConfig config){
55+
Scheduler(String execution, CWSKubernetesClient client, String namespace, SchedulerConfig config){
5656
this.execution = execution;
5757
this.name = System.getenv( "SCHEDULER_NAME" ) + "-" + execution;
5858
this.namespace = namespace;

0 commit comments

Comments
 (0)