Skip to content

Commit

Permalink
Add container ID to api.Container
Browse files Browse the repository at this point in the history
Autogenerate an ID if not specified
  • Loading branch information
smarterclayton committed Jun 26, 2014
1 parent 2116466 commit 984334f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/examples/pod-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"desiredState": {
"manifest": {
"containers": [{
"id": "f81d4fae-7dec-11d0-a765-00a0c91e6bf5",
"image": "dockerfile/nginx",
"ports": [{
"hostPort": 8080,
Expand All @@ -30,6 +31,7 @@
"desiredState": {
"manifest": {
"containers": [{
"id": "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"image": "dockerfile/nginx",
"ports": [{
"hostPort": 8080,
Expand Down
1 change: 1 addition & 0 deletions api/examples/pod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"desiredState": {
"manifest": {
"containers": [{
"id": "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"image": "dockerfile/nginx",
"ports": [{
"containerPort": 80,
Expand Down
35 changes: 35 additions & 0 deletions api/kubernetes.html
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ <h4 class="modal-title" id="myModalLabel">





</div>


Expand Down Expand Up @@ -287,6 +289,7 @@ <h3>Body</h3>
"<span class="hljs-attribute">desiredState</span>": <span class="hljs-value">{
"<span class="hljs-attribute">manifest</span>": <span class="hljs-value">{
"<span class="hljs-attribute">containers</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">id</span>": <span class="hljs-value"><span class="hljs-string">"f81d4fae-7dec-11d0-a765-00a0c91e6bf5"</span></span>,
"<span class="hljs-attribute">image</span>": <span class="hljs-value"><span class="hljs-string">"dockerfile/nginx"</span></span>,
"<span class="hljs-attribute">ports</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">hostPort</span>": <span class="hljs-value"><span class="hljs-number">8080</span></span>,
Expand All @@ -308,6 +311,7 @@ <h3>Body</h3>
"<span class="hljs-attribute">desiredState</span>": <span class="hljs-value">{
"<span class="hljs-attribute">manifest</span>": <span class="hljs-value">{
"<span class="hljs-attribute">containers</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">id</span>": <span class="hljs-value"><span class="hljs-string">"f81d4fae-7dec-11d0-a765-00a0c91e6bf6"</span></span>,
"<span class="hljs-attribute">image</span>": <span class="hljs-value"><span class="hljs-string">"dockerfile/nginx"</span></span>,
"<span class="hljs-attribute">ports</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">hostPort</span>": <span class="hljs-value"><span class="hljs-number">8080</span></span>,
Expand Down Expand Up @@ -364,6 +368,8 @@ <h4 class="modal-title" id="myModalLabel">





<h3>Body</h3>

<p><strong>Type: application/json</strong></p>
Expand Down Expand Up @@ -466,6 +472,7 @@ <h3>Body</h3>
"<span class="hljs-attribute">desiredState</span>": <span class="hljs-value">{
"<span class="hljs-attribute">manifest</span>": <span class="hljs-value">{
"<span class="hljs-attribute">containers</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">id</span>": <span class="hljs-value"><span class="hljs-string">"f81d4fae-7dec-11d0-a765-00a0c91e6bf6"</span></span>,
"<span class="hljs-attribute">image</span>": <span class="hljs-value"><span class="hljs-string">"dockerfile/nginx"</span></span>,
"<span class="hljs-attribute">ports</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">containerPort</span>": <span class="hljs-value"><span class="hljs-number">80</span></span>,
Expand Down Expand Up @@ -575,6 +582,8 @@ <h3>URI Parameters</h3>





</div>


Expand All @@ -597,6 +606,7 @@ <h3>Body</h3>
"<span class="hljs-attribute">desiredState</span>": <span class="hljs-value">{
"<span class="hljs-attribute">manifest</span>": <span class="hljs-value">{
"<span class="hljs-attribute">containers</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">id</span>": <span class="hljs-value"><span class="hljs-string">"f81d4fae-7dec-11d0-a765-00a0c91e6bf6"</span></span>,
"<span class="hljs-attribute">image</span>": <span class="hljs-value"><span class="hljs-string">"dockerfile/nginx"</span></span>,
"<span class="hljs-attribute">ports</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">containerPort</span>": <span class="hljs-value"><span class="hljs-number">80</span></span>,
Expand Down Expand Up @@ -660,6 +670,8 @@ <h3>URI Parameters</h3>





<h3>Body</h3>

<p><strong>Type: application/json</strong></p>
Expand Down Expand Up @@ -762,6 +774,7 @@ <h3>Body</h3>
"<span class="hljs-attribute">desiredState</span>": <span class="hljs-value">{
"<span class="hljs-attribute">manifest</span>": <span class="hljs-value">{
"<span class="hljs-attribute">containers</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">id</span>": <span class="hljs-value"><span class="hljs-string">"f81d4fae-7dec-11d0-a765-00a0c91e6bf6"</span></span>,
"<span class="hljs-attribute">image</span>": <span class="hljs-value"><span class="hljs-string">"dockerfile/nginx"</span></span>,
"<span class="hljs-attribute">ports</span>": <span class="hljs-value">[{
"<span class="hljs-attribute">containerPort</span>": <span class="hljs-value"><span class="hljs-number">80</span></span>,
Expand Down Expand Up @@ -829,6 +842,8 @@ <h3>URI Parameters</h3>





</div>


Expand Down Expand Up @@ -947,6 +962,8 @@ <h4 class="modal-title" id="myModalLabel">





</div>


Expand Down Expand Up @@ -1036,6 +1053,8 @@ <h4 class="modal-title" id="myModalLabel">





<h3>Body</h3>

<p><strong>Type: application/json</strong></p>
Expand Down Expand Up @@ -1210,6 +1229,8 @@ <h3>URI Parameters</h3>





</div>


Expand Down Expand Up @@ -1295,6 +1316,8 @@ <h3>URI Parameters</h3>





<h3>Body</h3>

<p><strong>Type: application/json</strong></p>
Expand Down Expand Up @@ -1427,6 +1450,8 @@ <h3>URI Parameters</h3>





</div>


Expand Down Expand Up @@ -1545,6 +1570,8 @@ <h4 class="modal-title" id="myModalLabel">





</div>


Expand Down Expand Up @@ -1631,6 +1658,8 @@ <h4 class="modal-title" id="myModalLabel">





<h3>Body</h3>

<p><strong>Type: application/json</strong></p>
Expand Down Expand Up @@ -1787,6 +1816,8 @@ <h3>URI Parameters</h3>





</div>


Expand Down Expand Up @@ -1864,6 +1895,8 @@ <h3>URI Parameters</h3>





<h3>Body</h3>

<p><strong>Type: application/json</strong></p>
Expand Down Expand Up @@ -1978,6 +2011,8 @@ <h3>URI Parameters</h3>





</div>


Expand Down
1 change: 1 addition & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type EnvVar struct {

// Container represents a single container that is expected to be run on the host.
type Container struct {
ID string `yaml:"id,omitempty" json:"id,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Image string `yaml:"image,omitempty" json:"image,omitempty"`
Command []string `yaml:"command,omitempty" json:"command,omitempty"`
Expand Down
8 changes: 7 additions & 1 deletion pkg/registry/pod_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

"code.google.com/p/go-uuid/uuid"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
Expand Down Expand Up @@ -143,7 +144,12 @@ func (storage *PodRegistryStorage) Extract(body []byte) (interface{}, error) {
func (storage *PodRegistryStorage) Create(pod interface{}) (<-chan interface{}, error) {
podObj := pod.(api.Pod)
if len(podObj.ID) == 0 {
return nil, fmt.Errorf("id is unspecified: %#v", pod)
return nil, fmt.Errorf("id is unspecified: %#v", podObj)
}
for i := range podObj.DesiredState.Manifest.Containers {
if len(podObj.DesiredState.Manifest.Containers[i].ID) == 0 {
podObj.DesiredState.Manifest.Containers[i].ID = uuid.NewUUID().String()
}
}
machine, err := storage.scheduler.Schedule(podObj)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions pkg/registry/pod_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package registry
import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
Expand All @@ -32,6 +33,55 @@ func expectNoError(t *testing.T, err error) {
}
}

func TestCreatePodIdError(t *testing.T) {
storage := PodRegistryStorage{
registry: &MockPodRegistry{},
}
pod := api.Pod{}
_, err := storage.Create(pod)
if !strings.HasPrefix(err.Error(), "id is unspecified: ") {
t.Errorf("Expected id is unspecified error, Got %#v", err)
}
}

type MockScheduler struct {
err error
pod api.Pod
}

func (m *MockScheduler) Schedule(pod api.Pod) (string, error) {
m.pod = pod
return "", m.err
}

func TestCreatePodContainerIdError(t *testing.T) {
mockScheduler := MockScheduler{
err: fmt.Errorf("test error"),
}
storage := PodRegistryStorage{
scheduler: &mockScheduler,
}
pod := api.Pod{
JSONBase: api.JSONBase{
ID: "test",
},
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Containers: []api.Container{
api.Container{},
},
},
},
}
_, err := storage.Create(pod)
if err != mockScheduler.err {
t.Errorf("Expected %#v, Got %#v", mockScheduler.err, err)
}
if len(mockScheduler.pod.DesiredState.Manifest.Containers[0].ID) == 0 {
t.Errorf("Expected container[0] to have ID set, Got %#v", mockScheduler.pod)
}
}

func TestListPodsError(t *testing.T) {
mockRegistry := MockPodRegistry{
err: fmt.Errorf("test error"),
Expand Down
1 change: 1 addition & 0 deletions third_party/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ TOP_PACKAGES="
github.com/golang/glog
code.google.com/p/goauth2/compute/serviceaccount
code.google.com/p/goauth2/oauth
code.google.com/p/go-uuid/uuid
code.google.com/p/google-api-go-client/compute/v1
"

Expand Down

0 comments on commit 984334f

Please sign in to comment.