...

Source file src/k8s.io/kubernetes/pkg/registry/networking/networkpolicy/strategy.go

Documentation: k8s.io/kubernetes/pkg/registry/networking/networkpolicy

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package networkpolicy
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	"k8s.io/apiserver/pkg/storage/names"
    26  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    27  	"k8s.io/kubernetes/pkg/apis/networking"
    28  	"k8s.io/kubernetes/pkg/apis/networking/validation"
    29  )
    30  
    31  // networkPolicyStrategy implements verification logic for NetworkPolicies
    32  type networkPolicyStrategy struct {
    33  	runtime.ObjectTyper
    34  	names.NameGenerator
    35  }
    36  
    37  // Strategy is the default logic that applies when creating and updating NetworkPolicy objects.
    38  var Strategy = networkPolicyStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
    39  
    40  // NamespaceScoped returns true because all NetworkPolicies need to be within a namespace.
    41  func (networkPolicyStrategy) NamespaceScoped() bool {
    42  	return true
    43  }
    44  
    45  // PrepareForCreate clears the status of a NetworkPolicy before creation.
    46  func (networkPolicyStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
    47  	networkPolicy := obj.(*networking.NetworkPolicy)
    48  	networkPolicy.Generation = 1
    49  }
    50  
    51  // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
    52  func (networkPolicyStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
    53  	newNetworkPolicy := obj.(*networking.NetworkPolicy)
    54  	oldNetworkPolicy := old.(*networking.NetworkPolicy)
    55  
    56  	// Any changes to the spec increment the generation number, any changes to the
    57  	// status should reflect the generation number of the corresponding object.
    58  	// See metav1.ObjectMeta description for more information on Generation.
    59  	if !reflect.DeepEqual(oldNetworkPolicy.Spec, newNetworkPolicy.Spec) {
    60  		newNetworkPolicy.Generation = oldNetworkPolicy.Generation + 1
    61  	}
    62  }
    63  
    64  // Validate validates a new NetworkPolicy.
    65  func (networkPolicyStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
    66  	networkPolicy := obj.(*networking.NetworkPolicy)
    67  	ops := validation.ValidationOptionsForNetworking(networkPolicy, nil)
    68  	return validation.ValidateNetworkPolicy(networkPolicy, ops)
    69  }
    70  
    71  // WarningsOnCreate returns warnings for the creation of the given object.
    72  func (networkPolicyStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
    73  	return nil
    74  }
    75  
    76  // Canonicalize normalizes the object after validation.
    77  func (networkPolicyStrategy) Canonicalize(obj runtime.Object) {}
    78  
    79  // AllowCreateOnUpdate is false for NetworkPolicy; this means POST is needed to create one.
    80  func (networkPolicyStrategy) AllowCreateOnUpdate() bool {
    81  	return false
    82  }
    83  
    84  // ValidateUpdate is the default update validation for an end user.
    85  func (networkPolicyStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
    86  	opts := validation.ValidationOptionsForNetworking(obj.(*networking.NetworkPolicy), old.(*networking.NetworkPolicy))
    87  	validationErrorList := validation.ValidateNetworkPolicy(obj.(*networking.NetworkPolicy), opts)
    88  	updateErrorList := validation.ValidateNetworkPolicyUpdate(obj.(*networking.NetworkPolicy), old.(*networking.NetworkPolicy), opts)
    89  	return append(validationErrorList, updateErrorList...)
    90  }
    91  
    92  // WarningsOnUpdate returns warnings for the given update.
    93  func (networkPolicyStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
    94  	return nil
    95  }
    96  
    97  // AllowUnconditionalUpdate is the default update policy for NetworkPolicy objects.
    98  func (networkPolicyStrategy) AllowUnconditionalUpdate() bool {
    99  	return true
   100  }
   101  

View as plain text