github.com/kubearmor/cilium@v1.6.12/pkg/source/source.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package source
    16  
    17  // Source describes the source of a definition
    18  type Source string
    19  
    20  const (
    21  	// Unspec is used when the source is unspecified
    22  	Unspec Source = "unspec"
    23  
    24  	// Local is the source used for state derived from local agent state.
    25  	// Local state has the strongest ownership and can only be overwritten
    26  	// by other local state.
    27  	Local Source = "local"
    28  
    29  	// KVStore is the source used for state derived from a key value store.
    30  	// State in the key value stored takes precendence over orchestration
    31  	// system state such as Kubernetes.
    32  	KVStore Source = "kvstore"
    33  
    34  	// Kubernetes is the source used for state derived from Kubernetes
    35  	Kubernetes Source = "k8s"
    36  
    37  	// CustomResource is the source used for state derived from Kubernetes
    38  	// custom resources
    39  	CustomResource Source = "custom-resource"
    40  
    41  	// Generated is the source used for generated state which can be
    42  	// overwritten by all other sources
    43  	Generated Source = "generated"
    44  )
    45  
    46  // AllowOverwrite returns true if new state from a particular source is allowed
    47  // to overwrite existing state from another source
    48  func AllowOverwrite(existing, new Source) bool {
    49  	switch existing {
    50  
    51  	// Kubernetes state can be overwritten by everything except generated
    52  	// and unspecified state
    53  	case Kubernetes:
    54  		return new != Generated && new != Unspec
    55  
    56  	// Custom-resource state can be overwritten everything except
    57  	// generated, unspecified and Kuberntes (non-CRD) state
    58  	case CustomResource:
    59  		return new != Generated && new != Unspec && new != Kubernetes
    60  
    61  	// KVStore can be overwritten by other kvstore or local state
    62  	case KVStore:
    63  		return new == KVStore || new == Local
    64  
    65  	// local state can only be overwritten by other local state
    66  	case Local:
    67  		return new == Local
    68  
    69  	// Generated and unspecified state can be overwritten by everything
    70  	case Generated:
    71  		return new != Unspec
    72  
    73  	// Unspecified state can be overwritten by everything
    74  	case Unspec:
    75  		return true
    76  	}
    77  
    78  	return true
    79  }