1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package storageclass
18
19import (
20	"context"
21
22	"k8s.io/apimachinery/pkg/runtime"
23	"k8s.io/apimachinery/pkg/util/validation/field"
24	"k8s.io/apiserver/pkg/storage/names"
25	"k8s.io/kubernetes/pkg/api/legacyscheme"
26	"k8s.io/kubernetes/pkg/apis/storage"
27	storageutil "k8s.io/kubernetes/pkg/apis/storage/util"
28	"k8s.io/kubernetes/pkg/apis/storage/validation"
29)
30
31// storageClassStrategy implements behavior for StorageClass objects
32type storageClassStrategy struct {
33	runtime.ObjectTyper
34	names.NameGenerator
35}
36
37// Strategy is the default logic that applies when creating and updating
38// StorageClass objects via the REST API.
39var Strategy = storageClassStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
40
41func (storageClassStrategy) NamespaceScoped() bool {
42	return false
43}
44
45// ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation.
46func (storageClassStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
47	class := obj.(*storage.StorageClass)
48
49	storageutil.DropDisabledFields(class, nil)
50}
51
52func (storageClassStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
53	storageClass := obj.(*storage.StorageClass)
54	return validation.ValidateStorageClass(storageClass)
55}
56
57// WarningsOnCreate returns warnings for the creation of the given object.
58func (storageClassStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
59	return nil
60}
61
62// Canonicalize normalizes the object after validation.
63func (storageClassStrategy) Canonicalize(obj runtime.Object) {
64}
65
66func (storageClassStrategy) AllowCreateOnUpdate() bool {
67	return false
68}
69
70// PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a PV
71func (storageClassStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
72	newClass := obj.(*storage.StorageClass)
73	oldClass := old.(*storage.StorageClass)
74
75	storageutil.DropDisabledFields(oldClass, newClass)
76}
77
78func (storageClassStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
79	errorList := validation.ValidateStorageClass(obj.(*storage.StorageClass))
80	return append(errorList, validation.ValidateStorageClassUpdate(obj.(*storage.StorageClass), old.(*storage.StorageClass))...)
81}
82
83// WarningsOnUpdate returns warnings for the given update.
84func (storageClassStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
85	return nil
86}
87
88func (storageClassStrategy) AllowUnconditionalUpdate() bool {
89	return true
90}
91