Skip to content

Commit

Permalink
Add support for creating a bulk of resources via kubectl apply
Browse files Browse the repository at this point in the history
  • Loading branch information
mfojtik committed Nov 4, 2014
1 parent d6e36a4 commit c8f88a3
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 0 deletions.
92 changes: 92 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2014 Google Inc. All rights reserved.
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 config

import (
errs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

// ClientFunc returns the RESTClient defined for given resource
type ClientFunc func(mapping *meta.RESTMapping) (*client.RESTClient, error)

// ApplyItems creates bulk of resources provided by items list. Each item must
// be valid API type. It requires ObjectTyper to parse the Version and Kind and
// RESTMapper to get the resource URI and REST client that knows how to create
// given type
func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientFunc, objects []runtime.Object) errs.ValidationErrorList {
allErrors := errs.ValidationErrorList{}
for i, obj := range objects {
version, kind, err := typer.ObjectVersionAndKind(obj)
if err != nil {
reportError(&allErrors, i, errs.NewFieldInvalid("kind", obj))
continue
}

mapping, err := mapper.RESTMapping(version, kind)
if err != nil {
reportError(&allErrors, i, errs.NewFieldNotSupported("mapping", err))
continue
}

client, err := clientFor(mapping)
if err != nil {
reportError(&allErrors, i, errs.NewFieldNotSupported("client", obj))
continue
}

if err := CreateObject(client, mapping, obj); err != nil {
reportError(&allErrors, i, *err)
}
}

return allErrors.Prefix("Config")
}

// Apply creates the obj using the provided clients and the resource URI
// mapping. It reports ValidationError when the object is missing the Metadata
// or the Name and it will report any error occured during create REST call
func CreateObject(client *client.RESTClient, mapping *meta.RESTMapping, obj runtime.Object) *errs.ValidationError {
name, err := mapping.MetadataAccessor.Name(obj)
if err != nil || name == "" {
e := errs.NewFieldRequired("name", err)
return &e
}

namespace, err := mapping.Namespace(obj)
if err != nil {
e := errs.NewFieldRequired("namespace", err)
return &e
}

// TODO: This should be using RESTHelper
err = client.Post().Path(mapping.Resource).Namespace(namespace).Body(obj).Do().Error()
if err != nil {
return &errs.ValidationError{errs.ValidationErrorTypeInvalid, name, err}
}

return nil
}

// reportError reports the single item validation error and properly set the
// prefix and index to match the Config item JSON index
func reportError(allErrs *errs.ValidationErrorList, index int, err errs.ValidationError) {
i := errs.ValidationErrorList{}
*allErrs = append(*allErrs, append(i, err).PrefixIndex(index).Prefix("item")...)
}
164 changes: 164 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Copyright 2014 Google Inc. All rights reserved.
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 config

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

func getTyperAndMapper() (runtime.ObjectTyper, meta.RESTMapper) {
return api.Scheme, latest.RESTMapper
}

func getFakeClient(t *testing.T, validURLs []string) (ClientFunc, *httptest.Server) {
handlerFunc := func(w http.ResponseWriter, r *http.Request) {
for _, u := range validURLs {
if u == r.RequestURI {
return
}
}
t.Errorf("Unexpected HTTP request: %s, expected %v", r.RequestURI, validURLs)
}
server := httptest.NewServer(http.HandlerFunc(handlerFunc))
return func(mapping *meta.RESTMapping) (*client.RESTClient, error) {
fakeCodec := runtime.CodecFor(api.Scheme, "v1beta1")
fakeUri, _ := url.Parse(server.URL + "/api/v1beta1")
return client.NewRESTClient(fakeUri, fakeCodec), nil
}, server
}

func TestCreateObjects(t *testing.T) {
items := []runtime.Object{}

items = append(items, &api.Pod{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Pod"},
ObjectMeta: api.ObjectMeta{Name: "test-pod"},
})

items = append(items, &api.Service{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Service"},
ObjectMeta: api.ObjectMeta{Name: "test-service"},
})

typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{"/api/v1beta1/pods", "/api/v1beta1/services"})

errs := CreateObjects(typer, mapper, client, items)
s.Close()
if len(errs) != 0 {
t.Errorf("Unexpected errors during config.Create(): %v", errs)
}
}

func TestCreateNoNameItem(t *testing.T) {
items := []runtime.Object{}

items = append(items, &api.Service{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Service"},
})

typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{"/api/v1beta1/services"})

errs := CreateObjects(typer, mapper, client, items)
s.Close()

if len(errs) == 0 {
t.Errorf("Expected required value error for missing name")
}

e := errs[0].(errors.ValidationError)
if errors.ValueOf(e.Type) != "required value" {
t.Errorf("Expected ValidationErrorTypeRequired error, got %#v", e)
}

if e.Field != "Config.item[0].name" {
t.Errorf("Expected 'Config.item[0].name' as error field, got '%#v'", e.Field)
}
}

type InvalidItem struct{}

func (*InvalidItem) IsAnAPIObject() {}

func TestCreateInvalidItem(t *testing.T) {
items := []runtime.Object{
&InvalidItem{},
}

typer, mapper := getTyperAndMapper()
client, s := getFakeClient(t, []string{})

errs := CreateObjects(typer, mapper, client, items)
s.Close()

if len(errs) == 0 {
t.Errorf("Expected invalid value error for kind")
}

e := errs[0].(errors.ValidationError)
if errors.ValueOf(e.Type) != "invalid value" {
t.Errorf("Expected ValidationErrorTypeInvalid error, got %#v", e)
}

if e.Field != "Config.item[0].kind" {
t.Errorf("Expected 'Config.item[0].kind' as error field, got '%#v'", e.Field)
}
}

func TestCreateNoClientItems(t *testing.T) {
items := []runtime.Object{}

items = append(items, &api.Pod{
TypeMeta: api.TypeMeta{APIVersion: "v1beta1", Kind: "Pod"},
ObjectMeta: api.ObjectMeta{Name: "test-pod"},
})

typer, mapper := getTyperAndMapper()
_, s := getFakeClient(t, []string{"/api/v1beta1/pods", "/api/v1beta1/services"})

noClientFunc := func(mapping *meta.RESTMapping) (*client.RESTClient, error) {
return nil, fmt.Errorf("no client")
}

errs := CreateObjects(typer, mapper, noClientFunc, items)
s.Close()

if len(errs) == 0 {
t.Errorf("Expected invalid value error for client")
}

e := errs[0].(errors.ValidationError)
if errors.ValueOf(e.Type) != "unsupported value" {
t.Errorf("Expected ValidationErrorTypeUnsupported error, got %#v", e)
}

if e.Field != "Config.item[0].client" {
t.Errorf("Expected 'Config.item[0].client' as error field, got '%#v'", e.Field)
}
}
128 changes: 128 additions & 0 deletions pkg/config/config_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
[
{
"id": "frontend",
"name": "frontend",
"kind": "Service",
"apiVersion": "v1beta2",
"port": 5432,
"selector": {
"name": "frontend"
}
},
{
"id": "redismaster",
"name": "redismaster",
"kind": "Service",
"apiVersion": "v1beta1",
"port": 10000,
"selector": {
"name": "redis-master"
}
},
{
"id": "redisslave",
"name": "redisslave",
"kind": "Service",
"apiVersion": "v1beta1",
"port": 10001,
"labels": {
"name": "redisslave"
},
"selector": {
"name": "redisslave"
}
},
{
"id": "redis-master-2",
"name": "redis-master-2",
"kind": "Pod",
"apiVersion": "v1beta1",
"desiredState": {
"manifest": {
"version": "v1beta1",
"containers": [{
"name": "master",
"image": "dockerfile/redis",
"env": [
{
"name": "REDIS_PASSWORD",
"value": "secret"
}
],
"ports": [{
"containerPort": 6379
}]
}]
}
},
"labels": {
"name": "redis-master"
}
},
{
"id": "frontendController",
"name": "frontendController",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
"replicas": 3,
"replicaSelector": {"name": "frontend"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"containers": [{
"name": "php-redis",
"image": "brendanburns/php-redis",
"env": [
{
"name": "ADMIN_USERNAME",
"value": "admin"
},
{
"name": "ADMIN_PASSWORD",
"value": "secret"
},
{
"name": "REDIS_PASSWORD",
"value": "secret"
}
],
"ports": [{"containerPort": 80}]
}]
}
},
"labels": {"name": "frontend"}
}},
"labels": {"name": "frontend"}
},
{
"id": "redisSlaveController",
"name": "redisSlaveController",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
"replicas": 2,
"replicaSelector": {"name": "redisslave"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"containers": [{
"name": "slave",
"image": "brendanburns/redis-slave",
"env": [
{
"name": "REDIS_PASSWORD",
"value": "secret"
}
],
"ports": [{"containerPort": 6379}]
}]
}
},
"labels": {"name": "redisslave"}
}},
"labels": {"name": "redisslave"}
}
]
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`,
cmds.AddCommand(NewCmdDelete(out))
cmds.AddCommand(NewCmdNamespace(out))
cmds.AddCommand(NewCmdLog(out))
cmds.AddCommand(NewCmdCreateAll(out))

if err := cmds.Execute(); err != nil {
os.Exit(1)
Expand Down
Loading

0 comments on commit c8f88a3

Please sign in to comment.