Skip to content

Commit

Permalink
Tighten validation of Name and Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin authored and smarterclayton committed Jan 27, 2015
1 parent 358ace6 commit a480794
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 43 deletions.
8 changes: 4 additions & 4 deletions api/examples/controller-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"apiVersion": "v1beta1",
"items": [
{
"id": "testRun",
"id": "test-run",
"desiredState": {
"replicas": 2,
"replicaSelector": {
"name": "testRun"
"name": "test-run"
},
"podTemplate": {
"desiredState": {
Expand All @@ -23,12 +23,12 @@
}
},
"labels": {
"name": "testRun"
"name": "test-run"
}
}
},
"labels": {
"name": "testRun"
"name": "test-run"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions api/examples/controller.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "nginxController",
"id": "nginx-controller",
"apiVersion": "v1beta1",
"kind": "ReplicationController",
"desiredState": {
Expand All @@ -9,7 +9,7 @@
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "nginxController",
"id": "nginx-controller",
"containers": [{
"name": "nginx",
"image": "dockerfile/nginx",
Expand Down
8 changes: 4 additions & 4 deletions api/examples/pod-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"id": "my-pod-1",
"labels": {
"name": "testRun",
"replicationcontroller": "testRun"
"name": "test-run",
"replicationcontroller": "test-run"
},
"desiredState": {
"manifest": {
Expand All @@ -29,8 +29,8 @@
{
"id": "my-pod-2",
"labels": {
"name": "testRun",
"replicationcontroller": "testRun"
"name": "test-run",
"replicationcontroller": "test-run"
},
"desiredState": {
"manifest": {
Expand Down
22 changes: 11 additions & 11 deletions examples/guestbook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Use the file `examples/guestbook/redis-slave-controller.json`:
```js
{
"id": "redisSlaveController",
"id": "redis-slave-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
Expand All @@ -131,7 +131,7 @@ Use the file `examples/guestbook/redis-slave-controller.json`:
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "redisSlaveController",
"id": "redis-slave-controller",
"containers": [{
"name": "slave",
"image": "brendanburns/redis-slave",
Expand All @@ -153,11 +153,11 @@ to create the replication controller by running:
```shell
$ cluster/kubectl.sh create -f examples/guestbook/redis-slave-controller.json
redisSlaveController
redis-slave-controller
# cluster/kubectl.sh get replicationcontrollers
NAME IMAGE(S) SELECTOR REPLICAS
redisSlaveController brendanburns/redis-slave name=redisslave 2
NAME IMAGE(S) SELECTOR REPLICAS
redis-slave-controller brendanburns/redis-slave name=redisslave 2
```
The redis slave configures itself by looking for the Kubernetes service environment variables in the container environment. In particular, the redis slave is started with the following command:
Expand Down Expand Up @@ -225,7 +225,7 @@ The pod is described in the file `examples/guestbook/frontend-controller.json`:
```js
{
"id": "frontendController",
"id": "frontend-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
Expand All @@ -235,7 +235,7 @@ The pod is described in the file `examples/guestbook/frontend-controller.json`:
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "frontendController",
"id": "frontend-controller",
"containers": [{
"name": "php-redis",
"image": "kubernetes/example-guestbook-php-redis",
Expand All @@ -258,12 +258,12 @@ Using this file, you can turn up your frontend with:
```shell
$ cluster/kubectl.sh create -f examples/guestbook/frontend-controller.json
frontendController
frontend-controller
$ cluster/kubectl.sh get replicationcontrollers
NAME IMAGE(S) SELECTOR REPLICAS
redisSlaveController brendanburns/redis-slave name=redisslave 2
frontendController kubernetes/example-guestbook-php-redis name=frontend 3
NAME IMAGE(S) SELECTOR REPLICAS
redis-slave-controller brendanburns/redis-slave name=redisslave 2
frontend-controller kubernetes/example-guestbook-php-redis name=frontend 3
```
Once that's up (it may take ten to thirty seconds to create the pods) you can list the pods in the cluster, to verify that the master, slaves and frontends are running:
Expand Down
4 changes: 2 additions & 2 deletions examples/guestbook/frontend-controller.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "frontendController",
"id": "frontend-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
Expand All @@ -9,7 +9,7 @@
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "frontendController",
"id": "frontend-controller",
"containers": [{
"name": "php-redis",
"image": "kubernetes/example-guestbook-php-redis",
Expand Down
4 changes: 2 additions & 2 deletions examples/guestbook/redis-slave-controller.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "redisSlaveController",
"id": "redis-slave-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
Expand All @@ -9,7 +9,7 @@
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "redisSlaveController",
"id": "redis-slave-controller",
"containers": [{
"name": "slave",
"image": "brendanburns/redis-slave",
Expand Down
2 changes: 1 addition & 1 deletion examples/walkthrough/k8s201.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Replication controllers are the objects to answer these questions. A replicatio
An example replica controller that instantiates two pods running nginx looks like:

```yaml
id: nginxController
id: nginx-controller
apiVersion: v1beta1
kind: ReplicationController
desiredState:
Expand Down
2 changes: 1 addition & 1 deletion examples/walkthrough/replication-controller.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id: nginxController
id: nginx-controller
apiVersion: v1beta1
kind: ReplicationController
desiredState:
Expand Down
4 changes: 2 additions & 2 deletions hack/e2e-suite/guestbook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ sleep 5
POD_LIST_1=$($KUBECFG '-template={{range.items}}{{.id}} {{end}}' list pods)
echo "Pods running: ${POD_LIST_1}"

$KUBECFG stop redisSlaveController
$KUBECFG stop redis-slave-controller
# Needed until issue #103 gets fixed
sleep 25
$KUBECFG rm redisSlaveController
$KUBECFG rm redis-slave-controller
$KUBECFG delete services/redis-master
$KUBECFG delete pods/redis-master

Expand Down
4 changes: 2 additions & 2 deletions hack/test-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ __EOF__
kubectl get replicationcontrollers "${kube_flags[@]}"
kubectl create -f examples/guestbook/frontend-controller.json "${kube_flags[@]}"
kubectl get replicationcontrollers "${kube_flags[@]}"
kubectl describe replicationcontroller frontendController "${kube_flags[@]}" | grep -q 'Replicas:.*3 desired'
kubectl delete rc frontendController "${kube_flags[@]}"
kubectl describe replicationcontroller frontend-controller "${kube_flags[@]}" | grep -q 'Replicas:.*3 desired'
kubectl delete rc frontend-controller "${kube_flags[@]}"

kube::log::status "Testing kubectl(${version}:nodes)"
kubectl get nodes "${kube_flags[@]}"
Expand Down
18 changes: 13 additions & 5 deletions pkg/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ func ValidateService(service *api.Service, lister ServiceLister, ctx api.Context
} else if !util.IsDNS952Label(service.Name) {
allErrs = append(allErrs, errs.NewFieldInvalid("name", service.Name, ""))
}
if !util.IsDNSSubdomain(service.Namespace) {
if len(service.Namespace) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("namespace", service.Namespace))
} else if !util.IsDNSSubdomain(service.Namespace) {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", service.Namespace, ""))
}
if !util.IsValidPortNum(service.Spec.Port) {
Expand Down Expand Up @@ -499,8 +501,12 @@ func ValidateReplicationController(controller *api.ReplicationController) errs.V
allErrs := errs.ValidationErrorList{}
if len(controller.Name) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("name", controller.Name))
} else if !util.IsDNSSubdomain(controller.Name) {
allErrs = append(allErrs, errs.NewFieldInvalid("name", controller.Name, ""))
}
if !util.IsDNSSubdomain(controller.Namespace) {
if len(controller.Namespace) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("namespace", controller.Namespace))
} else if !util.IsDNSSubdomain(controller.Namespace) {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", controller.Namespace, ""))
}
allErrs = append(allErrs, ValidateReplicationControllerSpec(&controller.Spec).Prefix("spec")...)
Expand Down Expand Up @@ -582,11 +588,13 @@ func ValidateBoundPod(pod *api.BoundPod) errs.ValidationErrorList {
// ValidateMinion tests if required fields in the minion are set.
func ValidateMinion(minion *api.Node) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
if len(minion.Namespace) != 0 {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", minion.Namespace, ""))
}
if len(minion.Name) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("name", minion.Name))
} else if !util.IsDNSSubdomain(minion.Name) {
allErrs = append(allErrs, errs.NewFieldInvalid("name", minion.Name, ""))
}
if len(minion.Namespace) != 0 {
allErrs = append(allErrs, errs.NewFieldInvalid("namespace", minion.Namespace, ""))
}
allErrs = append(allErrs, ValidateLabels(minion.Labels, "labels")...)
allErrs = append(allErrs, ValidateLabels(minion.Annotations, "annotations")...)
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/config_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
}
},
{
"id": "frontendController",
"name": "frontendController",
"id": "frontend-controller",
"name": "frontend-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
Expand Down Expand Up @@ -97,8 +97,8 @@
"labels": {"name": "frontend"}
},
{
"id": "redisSlaveController",
"name": "redisSlaveController",
"id": "redis-slave-controller",
"name": "redis-slave-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestCreateDirectory(t *testing.T) {
cmd.Flags().Set("filename", "../../../examples/guestbook")
cmd.Run(cmd, []string{})

if buf.String() != "frontendController\nfrontend\nredis-master\nredis-master\nredisSlaveController\nredisslave\n" {
if buf.String() != "frontend-controller\nfrontend\nredis-master\nredis-master\nredis-slave-controller\nredisslave\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestDeleteDirectory(t *testing.T) {
cmd.Flags().Set("filename", "../../../examples/guestbook")
cmd.Run(cmd, []string{})

if buf.String() != "frontendController\nfrontend\nredis-master\nredis-master\nredisSlaveController\nredisslave\n" {
if buf.String() != "frontend-controller\nfrontend\nredis-master\nredis-master\nredis-slave-controller\nredisslave\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/controller/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestControllerDecode(t *testing.T) {
func TestControllerParsing(t *testing.T) {
expectedController := api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "nginxController",
Name: "nginx-controller",
Labels: map[string]string{
"name": "nginx",
},
Expand Down

0 comments on commit a480794

Please sign in to comment.