Skip to content

Commit 1b63efa

Browse files
committed
support create, update, delete, get, list with yaml
1 parent 98cf06c commit 1b63efa

2 files changed

Lines changed: 168 additions & 1 deletion

File tree

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,4 @@
244244
</profile>
245245
</profiles>
246246

247-
248247
</project>

src/main/java/io/github/kubesys/client/KubernetesClient.java

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.fasterxml.jackson.databind.ObjectMapper;
6060
import com.fasterxml.jackson.databind.node.ArrayNode;
6161
import com.fasterxml.jackson.databind.node.ObjectNode;
62+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
6263
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
6364

6465
import io.github.kubesys.client.core.KubernetesRuleBase;
@@ -209,6 +210,16 @@ public KubernetesClient(String url, String username, String password, Kubernetes
209210
*
210211
**********************************************************/
211212

213+
/**
214+
* @param yaml yaml
215+
* @return String
216+
* @throws Exception Exception
217+
*/
218+
public String createResourceUsingYaml(String yaml) throws Exception {
219+
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
220+
JsonNode jsonNode = mapper.readTree(yaml);
221+
return new YAMLMapper().writeValueAsString(createResource(jsonNode));
222+
}
212223
/**
213224
* create a Kubernetes resource using JSON. <br>
214225
*
@@ -284,6 +295,17 @@ public JsonNode deleteResource(String json) throws Exception {
284295
return deleteResource(new ObjectMapper().readTree(json));
285296
}
286297

298+
/**
299+
* @param yaml yaml
300+
* @return string
301+
* @throws Exception Exception
302+
*/
303+
public String deleteResourceUsingYaml(String yaml) throws Exception {
304+
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
305+
JsonNode jsonNode = mapper.readTree(yaml);
306+
return new YAMLMapper().writeValueAsString(deleteResource(jsonNode));
307+
}
308+
287309
/**
288310
* delete a Kubernetes resource using JSON <br>
289311
*
@@ -341,6 +363,20 @@ public JsonNode deleteResource(String kind, String namespace, String name) throw
341363
HttpDelete request = ReqUtil.delete(requester, uri);
342364
return requester.getResponse(request);
343365
}
366+
367+
/**
368+
* delete a Kubernetes resource using kind, namespace and name see
369+
* https://kubernetes.io/docs/reference/kubectl/overview/
370+
*
371+
* @param kind kind or fullKind
372+
* @param namespace resource namespace, and "" means all-namespaces
373+
* @param name resource name
374+
* @return json the deleted object with json style
375+
* @throws Exception see HttpCaller.getResponse
376+
*/
377+
public String deleteResourceUsingYaml(String kind, String namespace, String name) throws Exception {
378+
return new YAMLMapper().writeValueAsString(deleteResource(kind, namespace, name));
379+
}
344380

345381
/**
346382
* update a Kubernetes resource using JSON
@@ -366,6 +402,17 @@ public JsonNode updateResource(String json) throws Exception {
366402
return updateResource(new ObjectMapper().readTree(json));
367403
}
368404

405+
/**
406+
* @param yaml yaml
407+
* @return string
408+
* @throws Exception Exception
409+
*/
410+
public String updateResourceUsingYaml(String yaml) throws Exception {
411+
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
412+
JsonNode jsonNode = mapper.readTree(yaml);
413+
return new YAMLMapper().writeValueAsString(updateResource(jsonNode));
414+
}
415+
369416
/**
370417
* update a Kubernetes resource using JSON
371418
*
@@ -414,6 +461,18 @@ public JsonNode getResource(String kind, String name) throws Exception {
414461
return getResource(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, name);
415462
}
416463

464+
/**
465+
* get a Kubernetes resource using kind, namespace and name
466+
*
467+
* @param kind kind or fullkind
468+
* @param name name
469+
* @return json json
470+
* @throws Exception Kubernetes cannot parsing this jsonStr
471+
*/
472+
public String getResourceUsingYaml(String kind, String name) throws Exception {
473+
return new YAMLMapper().writeValueAsString(getResource(kind, name));
474+
}
475+
417476
/**
418477
* get a Kubernetes resource using kind, namespace and name
419478
*
@@ -429,6 +488,19 @@ public JsonNode getResource(String kind, String namespace, String name) throws E
429488
HttpGet request = ReqUtil.get(requester, uri);
430489
return requester.getResponse(request);
431490
}
491+
492+
/**
493+
* get a Kubernetes resource using kind, namespace and name
494+
*
495+
* @param kind kind or fullkind
496+
* @param namespace namespace, if this kind unsupports namespace, it is ""
497+
* @param name name
498+
* @return json json
499+
* @throws Exception Kubernetes cannot parsing this jsonStr
500+
*/
501+
public String getResourceUsingYaml(String kind, String namespace, String name) throws Exception {
502+
return new YAMLMapper().writeValueAsString(getResource(kind, namespace, name));
503+
}
432504

433505
/**
434506
* get a Kubernetes resource using kind, namespace and name
@@ -462,6 +534,18 @@ public JsonNode listResources(String kind) throws Exception {
462534
return listResources(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, null, null, 0, null);
463535
}
464536

537+
/**
538+
* list all Kubernetes resources using kind
539+
*
540+
* @param kind kind
541+
* @return json json
542+
* @throws Exception Kubernetes cannot parsing this jsonStr
543+
*/
544+
public String listResourcesUsingYamml(String kind) throws Exception {
545+
return new YAMLMapper().writeValueAsString(listResources(
546+
kind, KubernetesConstants.VALUE_ALL_NAMESPACES, null, null, 0, null));
547+
}
548+
465549
/**
466550
* list all Kubernetes resources using kind
467551
*
@@ -474,6 +558,19 @@ public JsonNode listResourcesWithField(String kind, Map<String, String> fields)
474558
return listResources(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, URLUtil.fromMap(fields), null, 0, null);
475559
}
476560

561+
/**
562+
* list all Kubernetes resources using kind
563+
*
564+
* @param kind kind
565+
* @param fields fields
566+
* @return json json
567+
* @throws Exception Kubernetes cannot parsing this jsonStr
568+
*/
569+
public String listResourcesWithFieldUsingYaml(String kind, Map<String, String> fields) throws Exception {
570+
return new YAMLMapper().writeValueAsString(listResources(
571+
kind, KubernetesConstants.VALUE_ALL_NAMESPACES, URLUtil.fromMap(fields), null, 0, null));
572+
}
573+
477574
/**
478575
* list all Kubernetes resources using kind
479576
*
@@ -486,6 +583,18 @@ public JsonNode listResourcesWithLabel(String kind, Map<String, String> labels)
486583
return listResources(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, null, URLUtil.fromMap(labels), 0, null);
487584
}
488585

586+
/**
587+
* list all Kubernetes resources using kind
588+
*
589+
* @param kind kind
590+
* @param labels labels
591+
* @return json json
592+
* @throws Exception Kubernetes cannot parsing this jsonStr
593+
*/
594+
public String listResourcesWithLabelUsingYaml(String kind, Map<String, String> labels) throws Exception {
595+
return new YAMLMapper().writeValueAsString(listResources(
596+
kind, KubernetesConstants.VALUE_ALL_NAMESPACES, null, URLUtil.fromMap(labels), 0, null));
597+
}
489598
/**
490599
* list all Kubernetes resources using kind and namespace
491600
*
@@ -498,6 +607,19 @@ public JsonNode listResources(String kind, String namespace) throws Exception {
498607
return listResources(kind, namespace, null, null, 0, null);
499608
}
500609

610+
/**
611+
* list all Kubernetes resources using kind
612+
*
613+
* @param kind kind
614+
* @param namespace namespace
615+
* @return json json
616+
* @throws Exception Kubernetes cannot parsing this jsonStr
617+
*/
618+
public String listResourcesUsingYaml(String kind, String namespace) throws Exception {
619+
return new YAMLMapper().writeValueAsString(listResources(
620+
kind, namespace, null, null, 0, null));
621+
}
622+
501623
/**
502624
* list all Kubernetes resources using kind
503625
*
@@ -510,6 +632,20 @@ public JsonNode listResourcesWithField(String kind, String namespace, Map<Strin
510632
return listResources(kind, namespace, URLUtil.fromMap(fields), null, 0, null);
511633
}
512634

635+
/**
636+
* list all Kubernetes resources using kind
637+
*
638+
* @param kind kind
639+
* @param namespace namespace
640+
* @param fields fields
641+
* @return json json
642+
* @throws Exception Kubernetes cannot parsing this jsonStr
643+
*/
644+
public String listResourcesWithFieldUsingYaml(String kind, String namespace, Map<String, String> fields) throws Exception {
645+
return new YAMLMapper().writeValueAsString(listResources(
646+
kind, namespace, URLUtil.fromMap(fields), null, 0, null));
647+
}
648+
513649
/**
514650
* list all Kubernetes resources using kind and namespace
515651
*
@@ -521,6 +657,20 @@ public JsonNode listResourcesWithField(String kind, String namespace, Map<Strin
521657
public JsonNode listResourcesWithLabel(String kind, String namespace, Map<String, String> labels) throws Exception {
522658
return listResources(kind, namespace, null, URLUtil.fromMap(labels), 0, null);
523659
}
660+
661+
/**
662+
* list all Kubernetes resources using kind
663+
*
664+
* @param kind kind
665+
* @param namespace namespace
666+
* @param labels labels
667+
* @return json json
668+
* @throws Exception Kubernetes cannot parsing this jsonStr
669+
*/
670+
public String listResourcesWithLabelUsingYaml(String kind, String namespace, Map<String, String> labels) throws Exception {
671+
return new YAMLMapper().writeValueAsString(listResources(
672+
kind, namespace, null, URLUtil.fromMap(labels), 0, null));
673+
}
524674

525675
/**
526676
* list all Kubernetes resources using kind, namespace, fieldSelector and
@@ -579,6 +729,24 @@ public JsonNode listResources(String kind, String namespace, String fieldSelecto
579729
return requester.getResponse(request);
580730
}
581731

732+
/**
733+
* list all Kubernetes resources using kind, namespace, fieldSelector,
734+
* labelSelector, limit and nextId
735+
*
736+
* @param kind kind
737+
* @param namespace namespace
738+
* @param fieldSelector fieldSelector
739+
* @param labelSelector labelSelector
740+
* @param limit limit
741+
* @param nextId nextId
742+
* @return json json
743+
* @throws Exception Kubernetes cannot parsing this jsonStr
744+
*/
745+
public String listResourcesUsingYaml(String kind, String namespace, String fieldSelector,
746+
String labelSelector, int limit, String nextId) throws Exception {
747+
return new YAMLMapper().writeValueAsString(listResources(
748+
kind, namespace, fieldSelector, labelSelector, limit, nextId));
749+
}
582750

583751
/**
584752
* update a Kubernetes resource status using JSON

0 commit comments

Comments
 (0)