-
Notifications
You must be signed in to change notification settings - Fork 330
/
config.go
145 lines (125 loc) · 3.99 KB
/
config.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright 2022 The Koordinator Authors.
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 config
import (
"fmt"
"strings"
)
type FailurePolicyType string
const (
// PolicyFail returns error to caller when got an error cri hook server
PolicyFail FailurePolicyType = "Fail"
// PolicyIgnore transfer cri request to containerd/dockerd when got an error to cri serer
PolicyIgnore FailurePolicyType = "Ignore"
// PolicyNone when no Policy configured. Proxy would ignore errors for PolicyNone like PolicyIgnore.
PolicyNone = ""
)
func GetFailurePolicyType(typeString string) (FailurePolicyType, error) {
switch typeString {
case "Fail":
return PolicyFail, nil
case "Ignore":
return PolicyIgnore, nil
default:
return "", fmt.Errorf("failure policy type not supported")
}
}
type RuntimeHookType string
const (
defaultRuntimeHookConfigPath string = "/etc/runtime/hookserver.d"
)
const (
PreRunPodSandbox RuntimeHookType = "PreRunPodSandbox"
PostStopPodSandbox RuntimeHookType = "PostStopPodSandbox"
PreCreateContainer RuntimeHookType = "PreCreateContainer"
PreStartContainer RuntimeHookType = "PreStartContainer"
PostStartContainer RuntimeHookType = "PostStartContainer"
PreUpdateContainerResources RuntimeHookType = "PreUpdateContainerResources"
PostStopContainer RuntimeHookType = "PostStopContainer"
PreRemoveRunPodSandbox RuntimeHookType = "PreRemoveRunPodSandbox"
NoneRuntimeHookType RuntimeHookType = "NoneRuntimeHookType"
)
type RuntimeHookConfig struct {
RemoteEndpoint string `json:"remote-endpoint,omitempty"`
FailurePolicy FailurePolicyType `json:"failure-policy,omitempty"`
RuntimeHooks []RuntimeHookType `json:"runtime-hooks,omitempty"`
}
type RuntimeRequestPath string
const (
RunPodSandbox RuntimeRequestPath = "RunPodSandbox"
StopPodSandbox RuntimeRequestPath = "StopPodSandbox"
CreateContainer RuntimeRequestPath = "CreateContainer"
StartContainer RuntimeRequestPath = "StartContainer"
UpdateContainerResources RuntimeRequestPath = "UpdateContainerResources"
StopContainer RuntimeRequestPath = "StopContainer"
NoneRuntimeHookPath RuntimeRequestPath = "NoneRuntimeHookPath"
)
func (ht RuntimeHookType) OccursOn(path RuntimeRequestPath) bool {
switch ht {
case PreRunPodSandbox:
if path == RunPodSandbox {
return true
}
case PostStopPodSandbox:
if path == StopPodSandbox {
return true
}
case PreCreateContainer:
if path == CreateContainer {
return true
}
case PreStartContainer:
if path == StartContainer {
return true
}
case PostStartContainer:
if path == StartContainer {
return true
}
case PreUpdateContainerResources:
if path == UpdateContainerResources {
return true
}
case PostStopContainer:
if path == StopContainer {
return true
}
}
return false
}
func (hp RuntimeRequestPath) PreHookType() RuntimeHookType {
if hp == RunPodSandbox {
return PreRunPodSandbox
}
return NoneRuntimeHookType
}
func (hp RuntimeRequestPath) PostHookType() RuntimeHookType {
if hp == RunPodSandbox {
return NoneRuntimeHookType
}
return NoneRuntimeHookType
}
type RuntimeHookStage string
const (
PreHook RuntimeHookStage = "PreHook"
PostHook RuntimeHookStage = "PostHook"
UnknownHook RuntimeHookStage = "UnknownHook"
)
func (ht RuntimeHookType) HookStage() RuntimeHookStage {
if strings.HasPrefix(string(ht), "Pre") {
return PreHook
} else if strings.HasPrefix(string(ht), "Post") {
return PostHook
}
return UnknownHook
}