Merge pull request #70669 from pohly/e2e-create-secrets

e2e: create and patch items of type Secret, code cleanup
pull/58/head
k8s-ci-robot 2018-11-06 10:23:41 -08:00 committed by GitHub
commit 8dcdec0a67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 117 deletions

View File

@ -119,6 +119,7 @@ go_library(
"//staging/src/k8s.io/client-go/rest:go_default_library", "//staging/src/k8s.io/client-go/rest:go_default_library",
"//staging/src/k8s.io/client-go/restmapper:go_default_library", "//staging/src/k8s.io/client-go/restmapper:go_default_library",
"//staging/src/k8s.io/client-go/scale:go_default_library", "//staging/src/k8s.io/client-go/scale:go_default_library",
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
"//staging/src/k8s.io/client-go/tools/clientcmd:go_default_library", "//staging/src/k8s.io/client-go/tools/clientcmd:go_default_library",
"//staging/src/k8s.io/client-go/tools/clientcmd/api:go_default_library", "//staging/src/k8s.io/client-go/tools/clientcmd/api:go_default_library",
"//staging/src/k8s.io/client-go/tools/remotecommand:go_default_library", "//staging/src/k8s.io/client-go/tools/remotecommand:go_default_library",

View File

@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
clientset "k8s.io/client-go/kubernetes" clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest" restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/test/e2e/framework/testfiles" "k8s.io/kubernetes/test/e2e/framework/testfiles"
) )
@ -253,23 +254,16 @@ type ItemFactory interface {
// If the item is of an unsupported type, it must return // If the item is of an unsupported type, it must return
// an error that has ItemNotSupported as cause. // an error that has ItemNotSupported as cause.
Create(f *Framework, item interface{}) (func() error, error) Create(f *Framework, item interface{}) (func() error, error)
// UniqueName returns just the name (for global items)
// or <namespace>/<name> (for namespaced items) if the
// item has the right type for this factory, otherwise
// the empty string.
UniqueName(item interface{}) string
} }
// DescribeItem always returns a string that describes the item, // DescribeItem always returns a string that describes the item,
// usually by calling out to a factory that supports the item // usually by calling out to cache.MetaNamespaceKeyFunc which
// type. If that fails, the entire item gets converted to a string. // concatenates namespace (if set) and name. If that fails, the entire
// item gets converted to a string.
func DescribeItem(item interface{}) string { func DescribeItem(item interface{}) string {
for _, factory := range Factories { key, err := cache.MetaNamespaceKeyFunc(item)
name := factory.UniqueName(item) if err == nil && key != "" {
if name != "" { return fmt.Sprintf("%T: %s", item, key)
return fmt.Sprintf("%T: %s", item, name)
}
} }
return fmt.Sprintf("%T: %s", item, item) return fmt.Sprintf("%T: %s", item, item)
} }
@ -279,15 +273,16 @@ func DescribeItem(item interface{}) string {
var ItemNotSupported = errors.New("not supported") var ItemNotSupported = errors.New("not supported")
var Factories = map[What]ItemFactory{ var Factories = map[What]ItemFactory{
{"ClusterRole"}: &ClusterRoleFactory{}, {"ClusterRole"}: &clusterRoleFactory{},
{"ClusterRoleBinding"}: &ClusterRoleBindingFactory{}, {"ClusterRoleBinding"}: &clusterRoleBindingFactory{},
{"DaemonSet"}: &DaemonSetFactory{}, {"DaemonSet"}: &daemonSetFactory{},
{"Role"}: &RoleFactory{}, {"Role"}: &roleFactory{},
{"RoleBinding"}: &RoleBindingFactory{}, {"RoleBinding"}: &roleBindingFactory{},
{"ServiceAccount"}: &ServiceAccountFactory{}, {"Secret"}: &secretFactory{},
{"StatefulSet"}: &StatefulSetFactory{}, {"Service"}: &serviceFactory{},
{"StorageClass"}: &StorageClassFactory{}, {"ServiceAccount"}: &serviceAccountFactory{},
{"Service"}: &ServiceFactory{}, {"StatefulSet"}: &statefulSetFactory{},
{"StorageClass"}: &storageClassFactory{},
} }
// PatchName makes the name of some item unique by appending the // PatchName makes the name of some item unique by appending the
@ -331,6 +326,8 @@ func (f *Framework) patchItemRecursively(item interface{}) error {
f.PatchName(&item.Name) f.PatchName(&item.Name)
case *v1.ServiceAccount: case *v1.ServiceAccount:
f.PatchNamespace(&item.ObjectMeta.Namespace) f.PatchNamespace(&item.ObjectMeta.Namespace)
case *v1.Secret:
f.PatchNamespace(&item.ObjectMeta.Namespace)
case *rbac.ClusterRoleBinding: case *rbac.ClusterRoleBinding:
f.PatchName(&item.Name) f.PatchName(&item.Name)
for i := range item.Subjects { for i := range item.Subjects {
@ -368,13 +365,13 @@ func (f *Framework) patchItemRecursively(item interface{}) error {
// looked like the least dirty approach. Perhaps one day Go will have // looked like the least dirty approach. Perhaps one day Go will have
// generics. // generics.
type ServiceAccountFactory struct{} type serviceAccountFactory struct{}
func (f *ServiceAccountFactory) New() runtime.Object { func (f *serviceAccountFactory) New() runtime.Object {
return &v1.ServiceAccount{} return &v1.ServiceAccount{}
} }
func (*ServiceAccountFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*serviceAccountFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*v1.ServiceAccount) item, ok := i.(*v1.ServiceAccount)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -388,21 +385,13 @@ func (*ServiceAccountFactory) Create(f *Framework, i interface{}) (func() error,
}, nil }, nil
} }
func (*ServiceAccountFactory) UniqueName(i interface{}) string { type clusterRoleFactory struct{}
item, ok := i.(*v1.ServiceAccount)
if !ok {
return ""
}
return fmt.Sprintf("%s/%s", item.GetNamespace(), item.GetName())
}
type ClusterRoleFactory struct{} func (f *clusterRoleFactory) New() runtime.Object {
func (f *ClusterRoleFactory) New() runtime.Object {
return &rbac.ClusterRole{} return &rbac.ClusterRole{}
} }
func (*ClusterRoleFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*clusterRoleFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*rbac.ClusterRole) item, ok := i.(*rbac.ClusterRole)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -436,21 +425,13 @@ func (*ClusterRoleFactory) Create(f *Framework, i interface{}) (func() error, er
}, nil }, nil
} }
func (*ClusterRoleFactory) UniqueName(i interface{}) string { type clusterRoleBindingFactory struct{}
item, ok := i.(*rbac.ClusterRole)
if !ok {
return ""
}
return item.GetName()
}
type ClusterRoleBindingFactory struct{} func (f *clusterRoleBindingFactory) New() runtime.Object {
func (f *ClusterRoleBindingFactory) New() runtime.Object {
return &rbac.ClusterRoleBinding{} return &rbac.ClusterRoleBinding{}
} }
func (*ClusterRoleBindingFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*clusterRoleBindingFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*rbac.ClusterRoleBinding) item, ok := i.(*rbac.ClusterRoleBinding)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -465,21 +446,13 @@ func (*ClusterRoleBindingFactory) Create(f *Framework, i interface{}) (func() er
}, nil }, nil
} }
func (*ClusterRoleBindingFactory) UniqueName(i interface{}) string { type roleFactory struct{}
item, ok := i.(*rbac.ClusterRoleBinding)
if !ok {
return ""
}
return item.GetName()
}
type RoleFactory struct{} func (f *roleFactory) New() runtime.Object {
func (f *RoleFactory) New() runtime.Object {
return &rbac.Role{} return &rbac.Role{}
} }
func (*RoleFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*roleFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*rbac.Role) item, ok := i.(*rbac.Role)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -494,21 +467,13 @@ func (*RoleFactory) Create(f *Framework, i interface{}) (func() error, error) {
}, nil }, nil
} }
func (*RoleFactory) UniqueName(i interface{}) string { type roleBindingFactory struct{}
item, ok := i.(*rbac.Role)
if !ok {
return ""
}
return fmt.Sprintf("%s/%s", item.GetNamespace(), item.GetName())
}
type RoleBindingFactory struct{} func (f *roleBindingFactory) New() runtime.Object {
func (f *RoleBindingFactory) New() runtime.Object {
return &rbac.RoleBinding{} return &rbac.RoleBinding{}
} }
func (*RoleBindingFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*roleBindingFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*rbac.RoleBinding) item, ok := i.(*rbac.RoleBinding)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -523,21 +488,13 @@ func (*RoleBindingFactory) Create(f *Framework, i interface{}) (func() error, er
}, nil }, nil
} }
func (*RoleBindingFactory) UniqueName(i interface{}) string { type serviceFactory struct{}
item, ok := i.(*rbac.RoleBinding)
if !ok {
return ""
}
return fmt.Sprintf("%s/%s", item.GetNamespace(), item.GetName())
}
type ServiceFactory struct{} func (f *serviceFactory) New() runtime.Object {
func (f *ServiceFactory) New() runtime.Object {
return &v1.Service{} return &v1.Service{}
} }
func (*ServiceFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*serviceFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*v1.Service) item, ok := i.(*v1.Service)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -552,21 +509,13 @@ func (*ServiceFactory) Create(f *Framework, i interface{}) (func() error, error)
}, nil }, nil
} }
func (*ServiceFactory) UniqueName(i interface{}) string { type statefulSetFactory struct{}
item, ok := i.(*v1.Service)
if !ok {
return ""
}
return fmt.Sprintf("%s/%s", item.GetNamespace(), item.GetName())
}
type StatefulSetFactory struct{} func (f *statefulSetFactory) New() runtime.Object {
func (f *StatefulSetFactory) New() runtime.Object {
return &apps.StatefulSet{} return &apps.StatefulSet{}
} }
func (*StatefulSetFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*statefulSetFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*apps.StatefulSet) item, ok := i.(*apps.StatefulSet)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -581,21 +530,13 @@ func (*StatefulSetFactory) Create(f *Framework, i interface{}) (func() error, er
}, nil }, nil
} }
func (*StatefulSetFactory) UniqueName(i interface{}) string { type daemonSetFactory struct{}
item, ok := i.(*apps.StatefulSet)
if !ok {
return ""
}
return fmt.Sprintf("%s/%s", item.GetNamespace(), item.GetName())
}
type DaemonSetFactory struct{} func (f *daemonSetFactory) New() runtime.Object {
func (f *DaemonSetFactory) New() runtime.Object {
return &apps.DaemonSet{} return &apps.DaemonSet{}
} }
func (*DaemonSetFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*daemonSetFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*apps.DaemonSet) item, ok := i.(*apps.DaemonSet)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -610,21 +551,13 @@ func (*DaemonSetFactory) Create(f *Framework, i interface{}) (func() error, erro
}, nil }, nil
} }
func (*DaemonSetFactory) UniqueName(i interface{}) string { type storageClassFactory struct{}
item, ok := i.(*apps.DaemonSet)
if !ok {
return ""
}
return fmt.Sprintf("%s/%s", item.GetNamespace(), item.GetName())
}
type StorageClassFactory struct{} func (f *storageClassFactory) New() runtime.Object {
func (f *StorageClassFactory) New() runtime.Object {
return &storage.StorageClass{} return &storage.StorageClass{}
} }
func (*StorageClassFactory) Create(f *Framework, i interface{}) (func() error, error) { func (*storageClassFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*storage.StorageClass) item, ok := i.(*storage.StorageClass)
if !ok { if !ok {
return nil, ItemNotSupported return nil, ItemNotSupported
@ -639,12 +572,25 @@ func (*StorageClassFactory) Create(f *Framework, i interface{}) (func() error, e
}, nil }, nil
} }
func (*StorageClassFactory) UniqueName(i interface{}) string { type secretFactory struct{}
item, ok := i.(*storage.StorageClass)
func (f *secretFactory) New() runtime.Object {
return &v1.Secret{}
}
func (*secretFactory) Create(f *Framework, i interface{}) (func() error, error) {
item, ok := i.(*v1.Secret)
if !ok { if !ok {
return "" return nil, ItemNotSupported
} }
return item.GetName()
client := f.ClientSet.CoreV1().Secrets(f.Namespace.GetName())
if _, err := client.Create(item); err != nil {
return nil, errors.Wrap(err, "create Secret")
}
return func() error {
return client.Delete(item.GetName(), &metav1.DeleteOptions{})
}, nil
} }
// PrettyPrint returns a human-readable representation of an item. // PrettyPrint returns a human-readable representation of an item.