Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create ingress in Kubernetes V1.23.1 version ? #3999

Closed
fengqiao117 opened this issue Mar 24, 2022 · 6 comments
Closed

How to create ingress in Kubernetes V1.23.1 version ? #3999

fengqiao117 opened this issue Mar 24, 2022 · 6 comments

Comments

@fengqiao117
Copy link

fengqiao117 commented Mar 24, 2022

I referred to the sample code, but it did not compile successfully. I am using Kubernetes-Client version 5.12.1

Ingress ingress = new IngressBuilder() .withNewMetadata().withName("test-ingress").addToAnnotations("nginx.ingress.kubernetes.io/rewrite-target", "/").endMetadata() .withNewSpec() .addNewRule() .withNewHttp() .addNewPath() .withPath("/testPath").withNewBackend().withServiceName("test").withServicePort(new IntOrString(80)).endBackend() .endPath() .endHttp() .endRule() .endSpec() .build(); client.network().ingress().inNamespace("default").create(ingress);

But I compiled successfully in V4.12.0 using the following methods:
client.extensions().ingresses().createorreplace (ingress);

@rohanKanojia
Copy link
Member

@fengqiao117 : You might be looking for io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder . Does this work for you:

client.network().v1().ingresses().inNamespace("ns1").createOrReplace(ingress)

@fengqiao117
Copy link
Author

For example the ingress case would be like this:

Ingress ingress = new IngressBuilder()
                .withNewMetadata().withName("test-ingress").addToAnnotations("nginx.ingress.kubernetes.io/rewrite-target", "/").endMetadata()
                .withNewSpec()
                .addNewRule()
                .withNewHttp()
                .addNewPath().withPathType("Prefix")
                .withPath("/"+"fims-web"+"/(.+)").withBackend(new IngressBackend())
                .endPath()
                .endHttp()
                .endRule()
                .endSpec()
                .build();
        client.network().v1().ingresses().inNamespace("default").createOrReplace(ingress);

The error message is as follows

Exception in thread "main" io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://10.1.22.98:6443/apis/networking.k8s.io/v1/namespaces/default/ingresses. Message: Ingress.extensions "test-ingress" is invalid: spec.rules[0].http.paths[0].backend: Invalid value: "": resource or service backend is required. Received status: Status(apiVersion=v1, code=422, details=StatusDetails(causes=[StatusCause(field=spec.rules[0].http.paths[0].backend, message=Invalid value: "": resource or service backend is required, reason=FieldValueInvalid, additionalProperties={})], group=extensions, kind=Ingress, name=test-ingress, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=Ingress.extensions "test-ingress" is invalid: spec.rules[0].http.paths[0].backend: Invalid value: "": resource or service backend is required, metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid, status=Failure, additionalProperties={}).
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:682)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:661)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.assertResponseCode(OperationSupport.java:612)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:555)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:518)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleCreate(OperationSupport.java:305)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.handleCreate(BaseOperation.java:644)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.handleCreate(BaseOperation.java:83)
at io.fabric8.kubernetes.client.dsl.base.CreateOnlyResourceOperation.create(CreateOnlyResourceOperation.java:61)
at io.fabric8.kubernetes.client.utils.CreateOrReplaceHelper.createOrReplace(CreateOrReplaceHelper.java:48)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:318)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:83)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:308)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:83)
at fdec.deploy.IngressExample.main(IngressExample.java:30)

Kubernetes API Server version

v1.22.1

Fabric8 Kubernetes Client version

5.12.1

Can you give me a successful example ?

ingress controller version

image: k8s.gcr.io/ingress-nginx/controller:v1.1.1

1 similar comment
@fengqiao117
Copy link
Author

For example the ingress case would be like this:

Ingress ingress = new IngressBuilder()
                .withNewMetadata().withName("test-ingress").addToAnnotations("nginx.ingress.kubernetes.io/rewrite-target", "/").endMetadata()
                .withNewSpec()
                .addNewRule()
                .withNewHttp()
                .addNewPath().withPathType("Prefix")
                .withPath("/"+"fims-web"+"/(.+)").withBackend(new IngressBackend())
                .endPath()
                .endHttp()
                .endRule()
                .endSpec()
                .build();
        client.network().v1().ingresses().inNamespace("default").createOrReplace(ingress);

The error message is as follows

Exception in thread "main" io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://10.1.22.98:6443/apis/networking.k8s.io/v1/namespaces/default/ingresses. Message: Ingress.extensions "test-ingress" is invalid: spec.rules[0].http.paths[0].backend: Invalid value: "": resource or service backend is required. Received status: Status(apiVersion=v1, code=422, details=StatusDetails(causes=[StatusCause(field=spec.rules[0].http.paths[0].backend, message=Invalid value: "": resource or service backend is required, reason=FieldValueInvalid, additionalProperties={})], group=extensions, kind=Ingress, name=test-ingress, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=Ingress.extensions "test-ingress" is invalid: spec.rules[0].http.paths[0].backend: Invalid value: "": resource or service backend is required, metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid, status=Failure, additionalProperties={}).
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:682)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:661)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.assertResponseCode(OperationSupport.java:612)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:555)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:518)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleCreate(OperationSupport.java:305)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.handleCreate(BaseOperation.java:644)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.handleCreate(BaseOperation.java:83)
at io.fabric8.kubernetes.client.dsl.base.CreateOnlyResourceOperation.create(CreateOnlyResourceOperation.java:61)
at io.fabric8.kubernetes.client.utils.CreateOrReplaceHelper.createOrReplace(CreateOrReplaceHelper.java:48)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:318)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:83)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:308)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.createOrReplace(BaseOperation.java:83)
at fdec.deploy.IngressExample.main(IngressExample.java:30)

Kubernetes API Server version

v1.22.1

Fabric8 Kubernetes Client version

5.12.1

Can you give me a successful example ?

ingress controller version

image: k8s.gcr.io/ingress-nginx/controller:v1.1.1

@shawkins
Copy link
Contributor

shawkins commented Apr 1, 2022

The error message is related to PUT performed by createOrReplace. See #3896 and related issues. Fabric8 6 will have basic support for server side apply #3334 - until then you may want to use something like:

// build the ingress keeping the other parts of the existing spec
Ingress buildOrEdit(IngressBuilder builder) {
  return builder.withNewMetadata().withName("test-ingress").addToAnnotations("nginx.ingress.kubernetes.io/rewrite-target", "/").endMetadata()
                .editOrNewSpec()
                .withRules(new IngressRuleBuilder()
                .withNewHttp()
                .addNewPath().withPathType("Prefix")
                .withPath("/"+"fims-web"+"/(.+)").withBackend(new IngressBackend())
                .endPath()
                .endHttp()
                .build())
                .endSpec()
                .build();
}

var ingresses = client.network().v1().ingresses().inNamespace("default");

try {
  ingresses.edit(i -> buildOrEdit(new IngressBuilder(i)));
} catch (KubernetesClientException e) {
  if (e.getCode() != 404) {
    throw e;
  }
  ingresses.create(buildOrEdit(new IngressBuilder());
}

This avoids a PUT on replace and when the diff is computed for an edit the buildOrEdit function will make sure that generated fields are retained.

@manusa manusa added the Waiting on feedback Issues that require feedback from User/Other community members label Apr 4, 2022
@manusa
Copy link
Member

manusa commented Apr 4, 2022

I'm not sure, but maybe the IngressBackend definition is also incomplete.

@fengqiao117
Copy link
Author

fengqiao117 commented Jun 3, 2022

Resolved,Modify the code as follows:
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder;

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants