Skip to content

Commit

Permalink
Update CHEATSHEET and tests CustomResource POJOs
Browse files Browse the repository at this point in the history
+ Remove unnecessary `implements KubernetesResource` from CustomResource
  POJOs
+ Remove unnecessary `@JsonDeserialize` from test CustomResource POJOs
+ Update CHEATSHEET Typed API samples as per 5.x
  • Loading branch information
rohanKanojia authored and manusa committed Mar 12, 2021
1 parent f76c644 commit 698eda1
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 158 deletions.
108 changes: 22 additions & 86 deletions doc/CHEATSHEET.md
Original file line number Diff line number Diff line change
Expand Up @@ -1666,128 +1666,64 @@ spec:
For a CustomResource like this one, we should have a `CronTab` java class like this:

**Note:** Please make sure that your CustomResource POJO is implementing `Namespaced` interface if it's a namespaced resource. Otherwise it would be considered a Cluster scoped resource.
```
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
```java
package io.fabric8.kubernetes.client.mock.crd;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

public class CronTab extends CustomResource implements Namespaced {
private CronTabSpec spec;
private CronTabStatus status;
@Override
public ObjectMeta getMetadata() {
return super.getMetadata();
}
public CronTabSpec getSpec() {
return spec;
}
public void setSpec(CronTabSpec spec) {
this.spec = spec;
}
public CronTabStatus getStatus() {
return status;
}
public void setStatus(CronTabStatus status) {
this.status = status;
}
@Override
public String getApiVersion() {
return "stable.example.com/v1";
}
@Override
public String toString() {
return "CronTab{"+
"apiVersion='" + getApiVersion() + "'" +
", metadata=" + getMetadata() +
", spec=" + spec +
", status=" + status +
"}";
}
@Version("v1")
@Group("stable.example.com")
public class CronTab extends CustomResource<CronTabSpec, CronTabStatus> implements Namespaced {
}
```
You can find other helper classes related to `CronTab` in our [tests](https://github.com/fabric8io/kubernetes-client/tree/master/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/crd). For now, we can proceed with it's common usage examples:

- Get Instance of client for our `CustomResource`:
```
// Alternatively use CustomResourceDefinitionContext.fromCrd(crd) if you already have a CustomResourceDefinition
CustomResourceDefinitionContext context = new CustomResourceDefinitionContext.Builder()
.withGroup("stable.example.com)
.withVersion("v1")
.withScope("Namespaced")
.withName("crontabs.stable.example.com)
.withPlural("crontabs")
.withKind("CronTab")
.build()
MixedOperation<CronTab, CronTabList, Resource<CronTab>> cronTabClient = client
.customResources(cronTabCrd, CronTab.class, CronTabList.class);
```
- Register your `CustomResource` to `KubernetesDeserializer`:
```
KubernetesDeserializer.registerCustomKind("stable.example.com/v1", "CronTab", CronTab.class);
```java
MixedOperation<CronTab, KubernetesResourceList<CronTab>, Resource<CronTab>> cronTabClient = client.customResources(CronTab.class);
```
- Get `CustomResource` from Kubernetes APIServer:
```
```java
CronTab ct = cronTabClient.inNamespace("default").withName("my-second-cron-object").get();
```
- Create `CustomResource`:
```
```java
cronTabClient.inNamespace("default").create(cronTab1);
```
- List `CustomResource`:
```
```java
CronTabList cronTabList = cronTabClient.inNamespace("default").list();
```
- Delete `CustomResource`:
```
```java
Boolean isDeleted = cronTabClient.inNamespace("default").withName("my-third-cron-object").delete();
```
- Update Status of `CustomResource`:
```
```java
cronTabClient.inNamespace("default").updateStatus(updatedCronTab);
```
- Watch `CustomResource`, (*note:* You need to register your `CustomResource` to `KubernetesDeserializer` otherwise you won't be able to use watch):
```
```java
cronTabClient.inNamespace("default").watch(new Watcher<CronTab>() {
@Override
public void eventReceived(Action action, CronTab resource) {
// Do something depending upon action type
}
@Override
public void eventReceived(Action action, CronTab resource) {
}

@Override
public void onClose(KubernetesClientException cause) {
@Override
public void onClose(WatcherException cause) {

}
}
});
```

### CustomResource Typeless API
Although, you should be using Typed API since it's type-safe. But it can get a bit complicated to maintain your `CustomResource` POJOs and sometimes people don't even have them. Kubernetes Client also provides a typeless/raw API to handle your `CustomResource` objects in form of HashMaps. In order to use it, you need to provide it with a `CustomResourceDefinitionContext`, which carries necessary information about `CustomResource`. Here is an example on how to create one:
- Create `CustomResourceDefinitionContext`:
```
```java
CustomResourceDefinitionContext customResourceDefinitionContext = new CustomResourceDefinitionContext.Builder()
.withName("animals.jungle.example.com")
.withGroup("jungle.example.com")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;


import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class AnimalSpec implements KubernetesResource {
public class AnimalSpec {
public String getOrder() {
return order;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class AnimalStatus implements KubernetesResource {
public class AnimalStatus {
private String currentName;

public String getCurrentName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class CronTabSpec implements KubernetesResource {
public class CronTabSpec {
public String getCronSpec() {
return cronSpec;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class CronTabStatus implements KubernetesResource {
public class CronTabStatus {
public int getReplicas() {
return replicas;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;


import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class EntandoBundleReleaseSpec implements KubernetesResource {
public class EntandoBundleReleaseSpec {
private String databaseType;

public String getDatabaseType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

/*
*/
@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class PodSetSpec implements KubernetesResource {
public class PodSetSpec {
public int getReplicas() {
return replicas;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class PodSetStatus implements KubernetesResource {
public class PodSetStatus {
public int getAvailableReplicas() {
return availableReplicas;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;

@JsonDeserialize(
using = JsonDeserializer.None.class
)
public class StarSpec implements KubernetesResource {
public class StarSpec {
private String type;

private String location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package io.fabric8.kubernetes.client.mock.crd;

import io.fabric8.kubernetes.api.model.KubernetesResource;

public class StarStatus implements KubernetesResource {
public class StarStatus {
private String location;

public String getLocation() {
Expand Down

0 comments on commit 698eda1

Please sign in to comment.