2015-04-28 03:50:56 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-04-28 03:50:56 +00:00
|
|
|
|
|
|
|
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 secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-01-13 17:48:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2017-02-02 09:25:56 +00:00
|
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
2017-01-31 16:47:19 +00:00
|
|
|
apistorage "k8s.io/apiserver/pkg/storage"
|
2017-01-13 19:56:52 +00:00
|
|
|
"k8s.io/apiserver/pkg/storage/names"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/core/validation"
|
2015-04-28 03:50:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// strategy implements behavior for Secret objects
|
|
|
|
type strategy struct {
|
|
|
|
runtime.ObjectTyper
|
2017-01-13 19:56:52 +00:00
|
|
|
names.NameGenerator
|
2015-04-28 03:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Strategy is the default logic that applies when creating and updating Secret
|
|
|
|
// objects via the REST API.
|
2017-10-16 11:41:50 +00:00
|
|
|
var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
|
2015-04-28 03:50:56 +00:00
|
|
|
|
|
|
|
var _ = rest.RESTCreateStrategy(Strategy)
|
|
|
|
|
|
|
|
var _ = rest.RESTUpdateStrategy(Strategy)
|
|
|
|
|
|
|
|
func (strategy) NamespaceScoped() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (strategy) PrepareForCreate(ctx genericapirequest.Context, obj runtime.Object) {
|
2015-04-28 03:50:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (strategy) Validate(ctx genericapirequest.Context, obj runtime.Object) field.ErrorList {
|
2015-04-28 03:50:56 +00:00
|
|
|
return validation.ValidateSecret(obj.(*api.Secret))
|
|
|
|
}
|
|
|
|
|
2015-11-13 05:13:16 +00:00
|
|
|
func (strategy) Canonicalize(obj runtime.Object) {
|
|
|
|
}
|
|
|
|
|
2015-04-28 03:50:56 +00:00
|
|
|
func (strategy) AllowCreateOnUpdate() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (strategy) PrepareForUpdate(ctx genericapirequest.Context, obj, old runtime.Object) {
|
2015-04-28 03:50:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (strategy) ValidateUpdate(ctx genericapirequest.Context, obj, old runtime.Object) field.ErrorList {
|
2015-11-04 07:47:11 +00:00
|
|
|
return validation.ValidateSecretUpdate(obj.(*api.Secret), old.(*api.Secret))
|
2015-04-28 03:50:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:42:01 +00:00
|
|
|
func (strategy) AllowUnconditionalUpdate() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (s strategy) Export(ctx genericapirequest.Context, obj runtime.Object, exact bool) error {
|
2015-12-01 23:45:29 +00:00
|
|
|
t, ok := obj.(*api.Secret)
|
|
|
|
if !ok {
|
|
|
|
// unexpected programmer error
|
|
|
|
return fmt.Errorf("unexpected object: %v", obj)
|
|
|
|
}
|
2016-08-08 20:15:33 +00:00
|
|
|
s.PrepareForCreate(ctx, obj)
|
2015-12-01 23:45:29 +00:00
|
|
|
if exact {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// secrets that are tied to the UID of a service account cannot be exported anyway
|
|
|
|
if t.Type == api.SecretTypeServiceAccountToken || len(t.Annotations[api.ServiceAccountUIDKey]) > 0 {
|
|
|
|
errs := []*field.Error{
|
|
|
|
field.Invalid(field.NewPath("type"), t, "can not export service account secrets"),
|
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
return errors.NewInvalid(api.Kind("Secret"), t.Name, errs)
|
2015-12-01 23:45:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:19:55 +00:00
|
|
|
// GetAttrs returns labels and fields of a given object for filtering purposes.
|
2017-05-26 22:43:42 +00:00
|
|
|
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
|
2016-11-16 09:19:55 +00:00
|
|
|
secret, ok := obj.(*api.Secret)
|
|
|
|
if !ok {
|
2017-05-26 22:43:42 +00:00
|
|
|
return nil, nil, false, fmt.Errorf("not a secret")
|
2016-11-16 09:19:55 +00:00
|
|
|
}
|
2017-05-26 22:43:42 +00:00
|
|
|
return labels.Set(secret.Labels), SelectableFields(secret), secret.Initializers != nil, nil
|
2016-11-16 09:19:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 03:50:56 +00:00
|
|
|
// Matcher returns a generic matcher for a given label and field selector.
|
2016-08-23 03:41:21 +00:00
|
|
|
func Matcher(label labels.Selector, field fields.Selector) apistorage.SelectionPredicate {
|
|
|
|
return apistorage.SelectionPredicate{
|
2016-11-16 09:19:55 +00:00
|
|
|
Label: label,
|
|
|
|
Field: field,
|
|
|
|
GetAttrs: GetAttrs,
|
2016-08-12 18:50:51 +00:00
|
|
|
}
|
2015-04-28 03:50:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 18:50:51 +00:00
|
|
|
// SelectableFields returns a field set that can be used for filter selection
|
|
|
|
func SelectableFields(obj *api.Secret) fields.Set {
|
2016-08-23 14:34:02 +00:00
|
|
|
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true)
|
2015-10-29 16:34:59 +00:00
|
|
|
secretSpecificFieldsSet := fields.Set{
|
2015-04-28 03:50:56 +00:00
|
|
|
"type": string(obj.Type),
|
|
|
|
}
|
2016-08-12 18:50:51 +00:00
|
|
|
return generic.MergeFieldsSets(objectMetaFieldsSet, secretSpecificFieldsSet)
|
2015-04-28 03:50:56 +00:00
|
|
|
}
|