Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2017 The Kubernetes Authors.
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.
*/
package io.kubernetes.client.examples;

import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Container;
import io.kubernetes.client.models.V1ObjectMeta;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodSpec;
import io.kubernetes.client.util.Config;

import java.io.IOException;
import java.util.Collections;

/**
* A simple example of how to use the Java API
*
* Easiest way to run this:
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.CreatePodExample"
*
* From inside $REPO_DIR/kubernetes
*/
public class CreatePodExample {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
String namespace = "default";

V1Pod pod = new V1Pod();
pod.setApiVersion("v1");

V1ObjectMeta objectMeta = new V1ObjectMeta();
objectMeta.setName("demo-pod-nginx");
objectMeta.setNamespace(namespace);

pod.setMetadata(objectMeta);

V1PodSpec podSpec = new V1PodSpec();
V1Container container = new V1Container();
container.setImage("nginx");
container.setName("nginx");

podSpec.setContainers(Collections.singletonList(container));
pod.setSpec(podSpec);

CoreV1Api api = new CoreV1Api();
api.createNamespacedPod(namespace, pod, "false");
}
}
4 changes: 2 additions & 2 deletions kubernetes/src/main/java/io/kubernetes/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,10 @@ public String selectHeaderAccept(String[] accepts) {
*
* @param contentTypes The Content-Type array to select from
* @return The Content-Type header to use. If the given array is empty,
* JSON will be used.
* or matches any, JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do this, we'll need to make this change in the upstream swagger-codegen and I don't think we can land it there.

The right thing to do is to fix the Kubernetes swagger to not have this.

Copy link
Contributor

@mbohlool mbohlool Jul 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and I don't think we can land it there.

We did this for python client. swagger-api/swagger-codegen#4152
I think it is a reasonable thing to do because swagger client prefers json and when content type is */*, that includes json.

The right thing to do is to fix the Kubernetes swagger to not have this.

I also agree with this regardless of swagger-codegen fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My read of the Kubernetes swagger was that it was declaring that it, as a server, accepts anything (*/*). If this is true, it's up to the client to decide what it wants to send, and requires either swagger-codegen to be smarter about assigning content types or us as consumers of the generated code to be explicit in what we are sending. But of course, if Kubernetes only accepts json & yaml for example then the right fix is in the Kubernetes swagger.

An alternative to modifying ApiClient in place is to subclass it and override this method to avoid having our changes overwritten by running codegen. Although - when I tested this against the autoupdate script it left my changes in place, so I'm not sure what's going on there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's due the PR in upstream swagger-codegen. They've been great about quick turnaround in the past. Once that is merged, I'm happy to merge this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and let me know if you need help with the PR, I've done a bunch of them myself)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

And thanks - I've done one myself now too so this shouldn't be too bad.

if (contentTypes.length == 0 || contentTypes[0].equals("*/*")) {
return "application/json";
}
for (String contentType : contentTypes) {
Expand Down