Skip to content

Commit

Permalink
Add an example of how to create a pod, fix a bug.
Browse files Browse the repository at this point in the history
Signed-off-by: Brendan Burns <[email protected]>
  • Loading branch information
brendandburns committed Mar 16, 2019
1 parent b56e290 commit 344d41f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
54 changes: 54 additions & 0 deletions examples/create-pod/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2019 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.
*/

// Note: the example only works with the code within the same release/branch.
package main

import (
"context"

"github.com/kubernetes-client/go/kubernetes/client"
"github.com/kubernetes-client/go/kubernetes/config"
)

func main() {
c, err := config.LoadKubeConfig()
if err != nil {
panic(err.Error())
}

// create the clientset
clientset := client.NewAPIClient(c)
pod := client.V1Pod{
Metadata: &client.V1ObjectMeta{
Name: "my-pod",
Namespace: "default",
},
Spec: &client.V1PodSpec{
Containers: []client.V1Container{
client.V1Container{
Name: "www",
Image: "nginx",
},
},
},
}

_, _, err = clientset.CoreV1Api.CreateNamespacedPod(context.Background(), "default", pod, nil)
if err != nil {
panic(err)
}
}
3 changes: 3 additions & 0 deletions kubernetes/client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func selectHeaderContentType(contentTypes []string) string {
if contains(contentTypes, "application/json") {
return "application/json"
}
if contains(contentTypes, "*/*") {
return "application/json"
}
return contentTypes[0] // use the first content type specified in 'consumes'
}

Expand Down

0 comments on commit 344d41f

Please sign in to comment.