/
source.go
91 lines (71 loc) · 2.92 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package source
// Source describes the source of a definition
type Source string
const (
// Unspec is used when the source is unspecified
Unspec Source = "unspec"
// KubeAPIServer is the source used for state which represents the
// kube-apiserver, such as the IPs associated with it. This is not to be
// confused with the Kubernetes source.
// KubeAPIServer state has the strongest ownership and can only be
// overwritten by itself.
KubeAPIServer Source = "kube-apiserver"
// Local is the source used for state derived from local agent state.
// Local state has the second strongest ownership, behind KubeAPIServer.
Local Source = "local"
// KVStore is the source used for state derived from a key value store.
// State in the key value stored takes precedence over orchestration
// system state such as Kubernetes.
KVStore Source = "kvstore"
// Kubernetes is the source used for state derived from Kubernetes
Kubernetes Source = "k8s"
// CustomResource is the source used for state derived from Kubernetes
// custom resources
CustomResource Source = "custom-resource"
// Generated is the source used for generated state which can be
// overwritten by all other sources, except for restored (and unspec).
Generated Source = "generated"
// Restored is the source used for restored state from data left behind
// by the previous agent instance. Can be overwritten by all other
// sources (except for unspec).
Restored Source = "restored"
)
// AllowOverwrite returns true if new state from a particular source is allowed
// to overwrite existing state from another source
func AllowOverwrite(existing, new Source) bool {
switch existing {
// KubeAPIServer state can only be overwritten by other kube-apiserver
// state.
case KubeAPIServer:
return new == KubeAPIServer
// Local state can only be overwritten by other local state or
// kube-apiserver state.
case Local:
return new == Local || new == KubeAPIServer
// KVStore can be overwritten by other kvstore, local state, or
// kube-apiserver state.
case KVStore:
return new == KVStore || new == Local || new == KubeAPIServer
// Custom-resource state can be overwritten by everything except
// restored, generated, unspecified and Kubernetes (non-CRD) state
case CustomResource:
return new != Restored && new != Generated && new != Unspec && new != Kubernetes
// Kubernetes state can be overwritten by everything except restored,
// generated and unspecified state
case Kubernetes:
return new != Restored && new != Generated && new != Unspec
// Generated can be overwritten by everything except by Restored and
// Unspecified
case Generated:
return new != Restored && new != Unspec
// Restored can be overwritten by everything except by Unspecified
case Restored:
return new != Unspec
// Unspecified state can be overwritten by everything
case Unspec:
return true
}
return true
}