Search in sources :

Example 1 with Serialization.unmarshal

use of io.fabric8.kubernetes.client.utils.Serialization.unmarshal in project kubernetes-client by fabric8io.

the class BackwardsCompatibilityInterceptor method afterFailure.

@Override
public boolean afterFailure(Builder builder, HttpResponse<?> response) {
    ResourceKey target = findNewTarget(builder, response);
    if (target == null) {
        return false;
    }
    HttpRequest request = response.request();
    if (request.bodyString() != null && !request.method().equalsIgnoreCase(PATCH)) {
        Object object = Serialization.unmarshal(request.bodyString());
        if (object instanceof HasMetadata) {
            HasMetadata h = (HasMetadata) object;
            h.setApiVersion(target.group + "/" + target.version);
            switch(request.method()) {
                case "POST":
                    builder.post(JSON, Serialization.asJson(h));
                    break;
                case "PUT":
                    builder.put(JSON, Serialization.asJson(h));
                    break;
                case "DELETE":
                    builder.delete(JSON, Serialization.asJson(h));
                    break;
                default:
                    return false;
            }
        }
    }
    return true;
}
Also used : HttpRequest(io.fabric8.kubernetes.client.http.HttpRequest) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata)

Example 2 with Serialization.unmarshal

use of io.fabric8.kubernetes.client.utils.Serialization.unmarshal in project kubernetes-client by fabric8io.

the class CustomResourceTest method testCreateOrReplace.

@Test
void testCreateOrReplace() throws IOException {
    String jsonObject = "{\"apiVersion\": \"test.fabric8.io/v1alpha1\",\"kind\": \"Hello\"," + "\"metadata\": {\"resourceVersion\":\"1\", \"name\": \"example-hello\"},\"spec\": {\"size\": 3}}";
    server.expect().post().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos").andReturn(HttpURLConnection.HTTP_INTERNAL_ERROR, new StatusBuilder().build()).once();
    server.expect().post().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos").andReturn(HttpURLConnection.HTTP_CREATED, jsonObject).once();
    server.expect().post().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos").andReturn(HttpURLConnection.HTTP_CONFLICT, jsonObject).once();
    server.expect().put().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos/example-hello").andReturn(HttpURLConnection.HTTP_OK, jsonObject).once();
    server.expect().get().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos/example-hello").andReturn(HttpURLConnection.HTTP_OK, jsonObject).once();
    GenericKubernetesResource resource = client.genericKubernetesResources(customResourceDefinitionContext).inNamespace("ns1").createOrReplace(Serialization.unmarshal(jsonObject, GenericKubernetesResource.class));
    assertEquals("example-hello", resource.getMetadata().getName());
    resource = client.genericKubernetesResources(customResourceDefinitionContext).inNamespace("ns1").createOrReplace(Serialization.unmarshal(jsonObject, GenericKubernetesResource.class));
    assertEquals("example-hello", resource.getMetadata().getName());
}
Also used : StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) Test(org.junit.jupiter.api.Test)

Example 3 with Serialization.unmarshal

use of io.fabric8.kubernetes.client.utils.Serialization.unmarshal in project kubernetes-client by fabric8io.

the class AdmissionReviewTest method testJacksonParsingWithUpdateOptions.

@Test
@DisplayName("Should be able to deserialize from AdmissionRequest option set to UpdateOption")
void testJacksonParsingWithUpdateOptions() {
    InputStream jsonStream = getClass().getResourceAsStream("/admissionreview-withupdateoptions.json");
    AdmissionReview admissionReview = Serialization.unmarshal(jsonStream, AdmissionReview.class);
    assertEquals("UPDATE", admissionReview.getRequest().getOperation());
    assertRequest(admissionReview);
}
Also used : InputStream(java.io.InputStream) AdmissionReview(io.fabric8.kubernetes.api.model.admission.v1beta1.AdmissionReview) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with Serialization.unmarshal

use of io.fabric8.kubernetes.client.utils.Serialization.unmarshal in project kubernetes-client by fabric8io.

the class AdmissionReviewTest method testJacksonParsingWithPathOptions.

@Test
@DisplayName("Should be able to deserialize from AdmissionRequest option set to PatchOption")
void testJacksonParsingWithPathOptions() {
    InputStream jsonStream = getClass().getResourceAsStream("/admissionreview-withpatchoptions.json");
    AdmissionReview admissionReview = Serialization.unmarshal(jsonStream, AdmissionReview.class);
    assertEquals("PATCH", admissionReview.getRequest().getOperation());
    assertRequest(admissionReview);
}
Also used : InputStream(java.io.InputStream) AdmissionReview(io.fabric8.kubernetes.api.model.admission.v1beta1.AdmissionReview) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with Serialization.unmarshal

use of io.fabric8.kubernetes.client.utils.Serialization.unmarshal in project kubernetes-client by fabric8io.

the class V1SubjectAccessReviewAuthTest method testCreateSelfSubjectRulesReview.

@Test
@DisplayName("Create SelfSubjectRulesReview")
void testCreateSelfSubjectRulesReview() {
    // Given
    SelfSubjectRulesReview selfSubjectRulesReview = new SelfSubjectRulesReviewBuilder().withNewMetadata().withName("foo").endMetadata().withNewSpec().withNamespace("test").endSpec().build();
    server.expect().post().withPath("/apis/authorization.k8s.io/v1/selfsubjectrulesreviews").andReply(HttpURLConnection.HTTP_OK, recordedRequest -> {
        SelfSubjectRulesReview reviewResponse = Serialization.unmarshal(recordedRequest.getBody().readString(Charset.defaultCharset()), SelfSubjectRulesReview.class);
        List<NonResourceRule> nonResourceRuleList = new ArrayList<>();
        nonResourceRuleList.add(new NonResourceRuleBuilder().withNonResourceURLs("*").withVerbs("*").build());
        nonResourceRuleList.add(new NonResourceRuleBuilder().withNonResourceURLs("/healthz", "/livez", "/readyz", "/version", "/version/").withVerbs("get").build());
        nonResourceRuleList.add(new NonResourceRuleBuilder().withNonResourceURLs("/api", "/api/*", "/apis", "/apis/*", "/healthz", "/livez", "/openapi", "/openapi/*", "/readyz", "/version", "/version/").withVerbs("get").build());
        List<ResourceRule> resourceRuleList = new ArrayList<>();
        resourceRuleList.add(new ResourceRuleBuilder().withApiGroups("*").withResources("*").withVerbs("*").build());
        resourceRuleList.add(new ResourceRuleBuilder().withApiGroups("authorization.k8s.io").withResources("selfsubjectaccessreviews", "selfsubjectrulesreviews").withVerbs("create").build());
        reviewResponse.setStatus(new SubjectRulesReviewStatusBuilder().withIncomplete(false).withNonResourceRules(nonResourceRuleList).withResourceRules(resourceRuleList).build());
        return reviewResponse;
    }).once();
    // When
    SelfSubjectRulesReview reviewResponse = client.authorization().v1().selfSubjectRulesReview().create(selfSubjectRulesReview);
    // Then
    assertNotNull(reviewResponse);
    assertEquals("test", reviewResponse.getSpec().getNamespace());
    assertNotNull(reviewResponse.getStatus());
    assertFalse(reviewResponse.getStatus().getIncomplete());
    assertEquals(3, reviewResponse.getStatus().getNonResourceRules().size());
    assertEquals(2, reviewResponse.getStatus().getResourceRules().size());
}
Also used : SelfSubjectRulesReview(io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectRulesReview) HttpURLConnection(java.net.HttpURLConnection) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) SelfSubjectAccessReviewBuilder(io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReviewBuilder) SelfSubjectAccessReview(io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReview) ArrayList(java.util.ArrayList) ResourceRule(io.fabric8.kubernetes.api.model.authorization.v1.ResourceRule) NonResourceRule(io.fabric8.kubernetes.api.model.authorization.v1.NonResourceRule) SubjectAccessReview(io.fabric8.kubernetes.api.model.authorization.v1.SubjectAccessReview) SubjectRulesReviewStatusBuilder(io.fabric8.kubernetes.api.model.authorization.v1.SubjectRulesReviewStatusBuilder) Charset(java.nio.charset.Charset) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) EnableKubernetesMockClient(io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient) Serialization(io.fabric8.kubernetes.client.utils.Serialization) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NonResourceRuleBuilder(io.fabric8.kubernetes.api.model.authorization.v1.NonResourceRuleBuilder) LocalSubjectAccessReview(io.fabric8.kubernetes.api.model.authorization.v1.LocalSubjectAccessReview) SubjectAccessReviewStatus(io.fabric8.kubernetes.api.model.authorization.v1.SubjectAccessReviewStatus) ResourceRuleBuilder(io.fabric8.kubernetes.api.model.authorization.v1.ResourceRuleBuilder) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) LocalSubjectAccessReviewBuilder(io.fabric8.kubernetes.api.model.authorization.v1.LocalSubjectAccessReviewBuilder) SubjectAccessReviewBuilder(io.fabric8.kubernetes.api.model.authorization.v1.SubjectAccessReviewBuilder) List(java.util.List) KubernetesMockServer(io.fabric8.kubernetes.client.server.mock.KubernetesMockServer) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) SelfSubjectRulesReviewBuilder(io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectRulesReviewBuilder) NonResourceRuleBuilder(io.fabric8.kubernetes.api.model.authorization.v1.NonResourceRuleBuilder) ResourceRuleBuilder(io.fabric8.kubernetes.api.model.authorization.v1.ResourceRuleBuilder) SubjectRulesReviewStatusBuilder(io.fabric8.kubernetes.api.model.authorization.v1.SubjectRulesReviewStatusBuilder) NonResourceRuleBuilder(io.fabric8.kubernetes.api.model.authorization.v1.NonResourceRuleBuilder) ArrayList(java.util.ArrayList) List(java.util.List) SelfSubjectRulesReviewBuilder(io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectRulesReviewBuilder) SelfSubjectRulesReview(io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectRulesReview) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Test (org.junit.jupiter.api.Test)37 DisplayName (org.junit.jupiter.api.DisplayName)23 Serialization (io.fabric8.kubernetes.client.utils.Serialization)16 ArrayList (java.util.ArrayList)13 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)12 List (java.util.List)12 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)11 InputStream (java.io.InputStream)11 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)11 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)11 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)11 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)10 EnableKubernetesMockClient (io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient)9 KubernetesMockServer (io.fabric8.kubernetes.client.server.mock.KubernetesMockServer)9 HttpURLConnection (java.net.HttpURLConnection)9 Charset (java.nio.charset.Charset)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 IOException (java.io.IOException)7