k3s/pkg/registry/experimental/controller/etcd/etcd_test.go

133 lines
3.8 KiB
Go
Raw Normal View History

2015-08-06 16:53:34 +00:00
/*
Copyright 2014 The Kubernetes Authors 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 etcd
import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
Support for both map-based and set-based selectors in extensions/v1beta1.Scale Here are a list of changes along with an explanation of how they work: 1. Add a new string field called TargetSelector to the external version of extensions Scale type (extensions/v1beta1.Scale). This is a serialized version of either the map-based selector (in case of ReplicationControllers) or the unversioned.LabelSelector struct (in case of Deployments and ReplicaSets). 2. Change the selector field in the internal Scale type (extensions.Scale) to unversioned.LabelSelector. 3. Add conversion functions to convert from two external selector fields to a single internal selector field. The rules for conversion are as follows: i. If the target resource that this scale targets supports LabelSelector (Deployments and ReplicaSets), then serialize the LabelSelector and store the string in the TargetSelector field in the external version and leave the map-based Selector field as nil. ii. If the target resource only supports a map-based selector (ReplicationControllers), then still serialize that selector and store the serialized string in the TargetSelector field. Also, set the the Selector map field in the external Scale type. iii. When converting from external to internal version, parse the TargetSelector string into LabelSelector struct if the string isn't empty. If it is empty, then check if the Selector map is set and just assign that map to the MatchLabels component of the LabelSelector. iv. When converting from internal to external version, serialize the LabelSelector and store it in the TargetSelector field. If only the MatchLabel component is set, then also copy that value to the Selector map field in the external version. 4. HPA now just converts the LabelSelector field to a Selector interface type to list the pods. 5. Scale Get and Update etcd methods for Deployments and ReplicaSets now return extensions.Scale instead of autoscaling.Scale. 6. Consequently, SubresourceGroupVersion override and is "autoscaling" enabled check is now removed from pkg/master/master.go 7. Other small changes to labels package, fuzzer and LabelSelector helpers to piece this all together. 8. Add unit tests to HPA targeting Deployments and ReplicaSets. 9. Add an e2e test to HPA targeting ReplicaSets.
2016-03-09 00:27:13 +00:00
"k8s.io/kubernetes/pkg/api/unversioned"
2015-10-09 22:04:41 +00:00
"k8s.io/kubernetes/pkg/apis/extensions"
2015-11-05 15:04:42 +00:00
"k8s.io/kubernetes/pkg/registry/generic"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/storage"
"k8s.io/kubernetes/pkg/storage/etcd/etcdtest"
etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
2015-08-06 16:53:34 +00:00
)
func newStorage(t *testing.T) (*ScaleREST, *etcdtesting.EtcdTestServer, storage.Interface) {
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
restOptions := generic.RESTOptions{Storage: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1}
return NewStorage(restOptions).Scale, server, etcdStorage
2015-08-06 16:53:34 +00:00
}
var validPodTemplate = api.PodTemplate{
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "test",
Image: "test_image",
ImagePullPolicy: api.PullIfNotPresent,
},
},
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
},
},
}
2016-04-27 04:35:14 +00:00
var validReplicas = int32(8)
2015-08-06 16:53:34 +00:00
var validControllerSpec = api.ReplicationControllerSpec{
Replicas: validReplicas,
Selector: validPodTemplate.Template.Labels,
Template: &validPodTemplate.Template,
}
var validController = api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
2015-08-06 16:53:34 +00:00
Spec: validControllerSpec,
}
2015-10-09 22:49:10 +00:00
var validScale = extensions.Scale{
2015-08-06 16:53:34 +00:00
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
2015-10-09 22:49:10 +00:00
Spec: extensions.ScaleSpec{
2015-08-06 16:53:34 +00:00
Replicas: validReplicas,
},
2015-10-09 22:49:10 +00:00
Status: extensions.ScaleStatus{
2015-08-06 16:53:34 +00:00
Replicas: 0,
Support for both map-based and set-based selectors in extensions/v1beta1.Scale Here are a list of changes along with an explanation of how they work: 1. Add a new string field called TargetSelector to the external version of extensions Scale type (extensions/v1beta1.Scale). This is a serialized version of either the map-based selector (in case of ReplicationControllers) or the unversioned.LabelSelector struct (in case of Deployments and ReplicaSets). 2. Change the selector field in the internal Scale type (extensions.Scale) to unversioned.LabelSelector. 3. Add conversion functions to convert from two external selector fields to a single internal selector field. The rules for conversion are as follows: i. If the target resource that this scale targets supports LabelSelector (Deployments and ReplicaSets), then serialize the LabelSelector and store the string in the TargetSelector field in the external version and leave the map-based Selector field as nil. ii. If the target resource only supports a map-based selector (ReplicationControllers), then still serialize that selector and store the serialized string in the TargetSelector field. Also, set the the Selector map field in the external Scale type. iii. When converting from external to internal version, parse the TargetSelector string into LabelSelector struct if the string isn't empty. If it is empty, then check if the Selector map is set and just assign that map to the MatchLabels component of the LabelSelector. iv. When converting from internal to external version, serialize the LabelSelector and store it in the TargetSelector field. If only the MatchLabel component is set, then also copy that value to the Selector map field in the external version. 4. HPA now just converts the LabelSelector field to a Selector interface type to list the pods. 5. Scale Get and Update etcd methods for Deployments and ReplicaSets now return extensions.Scale instead of autoscaling.Scale. 6. Consequently, SubresourceGroupVersion override and is "autoscaling" enabled check is now removed from pkg/master/master.go 7. Other small changes to labels package, fuzzer and LabelSelector helpers to piece this all together. 8. Add unit tests to HPA targeting Deployments and ReplicaSets. 9. Add an e2e test to HPA targeting ReplicaSets.
2016-03-09 00:27:13 +00:00
Selector: &unversioned.LabelSelector{
MatchLabels: validPodTemplate.Template.Labels,
},
2015-08-06 16:53:34 +00:00
},
}
func TestGet(t *testing.T) {
storage, server, si := newStorage(t)
defer server.Terminate(t)
2015-08-06 16:53:34 +00:00
ctx := api.WithNamespace(api.NewContext(), "test")
2015-08-06 16:53:34 +00:00
key := etcdtest.AddPrefix("/controllers/test/foo")
2016-03-25 13:24:58 +00:00
if err := si.Create(ctx, key, &validController, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
2015-08-06 16:53:34 +00:00
}
obj, err := storage.Get(ctx, "foo")
2015-08-06 16:53:34 +00:00
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
scale := obj.(*extensions.Scale)
if scale.Spec.Replicas != validReplicas {
t.Errorf("wrong replicas count expected: %d got: %d", validReplicas, scale.Spec.Replicas)
2015-08-06 16:53:34 +00:00
}
}
func TestUpdate(t *testing.T) {
storage, server, si := newStorage(t)
defer server.Terminate(t)
2015-08-06 16:53:34 +00:00
ctx := api.WithNamespace(api.NewContext(), "test")
2015-08-06 16:53:34 +00:00
key := etcdtest.AddPrefix("/controllers/test/foo")
2016-03-25 13:24:58 +00:00
if err := si.Create(ctx, key, &validController, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
2015-08-06 16:53:34 +00:00
}
2016-04-27 04:35:14 +00:00
replicas := int32(12)
2015-10-09 22:49:10 +00:00
update := extensions.Scale{
2015-08-06 16:53:34 +00:00
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
2015-10-09 22:49:10 +00:00
Spec: extensions.ScaleSpec{
2015-08-06 16:53:34 +00:00
Replicas: replicas,
},
}
if _, _, err := storage.Update(ctx, update.Name, rest.DefaultUpdatedObjectInfo(&update, api.Scheme)); err != nil {
t.Fatalf("unexpected error: %v", err)
2015-08-06 16:53:34 +00:00
}
obj, err := storage.Get(ctx, "foo")
2015-08-06 16:53:34 +00:00
if err != nil {
t.Fatalf("unexpected error: %v", err)
2015-08-06 16:53:34 +00:00
}
updated := obj.(*extensions.Scale)
if updated.Spec.Replicas != replicas {
t.Errorf("wrong replicas count expected: %d got: %d", replicas, updated.Spec.Replicas)
2015-08-06 16:53:34 +00:00
}
}