github.com/fafucoder/cilium@v1.6.11/pkg/monitor/api/types.go (about) 1 // Copyright 2018-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 api 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "strconv" 21 "strings" 22 "time" 23 24 "github.com/cilium/cilium/pkg/monitor/notifications" 25 ) 26 27 // Must be synchronized with <bpf/lib/common.h> 28 const ( 29 // 0-128 are reserved for BPF datapath events 30 MessageTypeUnspec = iota 31 MessageTypeDrop 32 MessageTypeDebug 33 MessageTypeCapture 34 MessageTypeTrace 35 36 // 129-255 are reserved for agent level events 37 38 // MessageTypeAccessLog contains a pkg/proxy/accesslog.LogRecord 39 MessageTypeAccessLog = 129 40 41 // MessageTypeAgent is an agent notification carrying a AgentNotify 42 MessageTypeAgent = 130 43 ) 44 45 type MessageTypeFilter []int 46 47 var ( 48 // MessageTypeNames is a map of all type names 49 MessageTypeNames = map[string]int{ 50 "drop": MessageTypeDrop, 51 "debug": MessageTypeDebug, 52 "capture": MessageTypeCapture, 53 "trace": MessageTypeTrace, 54 "l7": MessageTypeAccessLog, 55 "agent": MessageTypeAgent, 56 } 57 ) 58 59 func type2name(typ int) string { 60 for name, value := range MessageTypeNames { 61 if value == typ { 62 return name 63 } 64 } 65 66 return strconv.Itoa(typ) 67 } 68 69 func (m *MessageTypeFilter) String() string { 70 pieces := make([]string, 0, len(*m)) 71 for _, typ := range *m { 72 pieces = append(pieces, type2name(typ)) 73 } 74 75 return strings.Join(pieces, ",") 76 } 77 78 func (m *MessageTypeFilter) Set(value string) error { 79 i, err := MessageTypeNames[value] 80 if !err { 81 return fmt.Errorf("Unknown type (%s). Please use one of the following ones %v", 82 value, MessageTypeNames) 83 } 84 85 *m = append(*m, i) 86 return nil 87 } 88 89 func (m *MessageTypeFilter) Type() string { 90 return "[]string" 91 } 92 93 func (m *MessageTypeFilter) Contains(typ int) bool { 94 for _, v := range *m { 95 if v == typ { 96 return true 97 } 98 } 99 100 return false 101 } 102 103 // AgentNotify is a notification from the agent 104 type AgentNotify struct { 105 Type AgentNotification 106 Text string 107 } 108 109 // AgentNotification specifies the type of agent notification 110 type AgentNotification uint32 111 112 const ( 113 AgentNotifyUnspec AgentNotification = iota 114 AgentNotifyGeneric 115 AgentNotifyStart 116 AgentNotifyEndpointRegenerateSuccess 117 AgentNotifyEndpointRegenerateFail 118 AgentNotifyPolicyUpdated 119 AgentNotifyPolicyDeleted 120 AgentNotifyEndpointCreated 121 AgentNotifyEndpointDeleted 122 ) 123 124 var notifyTable = map[AgentNotification]string{ 125 AgentNotifyUnspec: "unspecified", 126 AgentNotifyGeneric: "Message", 127 AgentNotifyStart: "Cilium agent started", 128 AgentNotifyEndpointRegenerateSuccess: "Endpoint regenerated", 129 AgentNotifyEndpointCreated: "Endpoint created", 130 AgentNotifyEndpointDeleted: "Endpoint deleted", 131 AgentNotifyEndpointRegenerateFail: "Failed endpoint regeneration", 132 AgentNotifyPolicyUpdated: "Policy updated", 133 AgentNotifyPolicyDeleted: "Policy deleted", 134 } 135 136 func resolveAgentType(t AgentNotification) string { 137 if n, ok := notifyTable[t]; ok { 138 return n 139 } 140 141 return fmt.Sprintf("%d", t) 142 } 143 144 // DumpInfo dumps an agent notification 145 func (n *AgentNotify) DumpInfo() { 146 fmt.Printf(">> %s: %s\n", resolveAgentType(n.Type), n.Text) 147 } 148 149 func (n *AgentNotify) getJSON() string { 150 return fmt.Sprintf(`{"type":"agent","subtype":"%s","message":%s}`, resolveAgentType(n.Type), n.Text) 151 } 152 153 // DumpJSON prints notification in json format 154 func (n *AgentNotify) DumpJSON() { 155 fmt.Println(n.getJSON()) 156 } 157 158 // PolicyUpdateNotification structures update notification 159 type PolicyUpdateNotification struct { 160 Labels []string `json:"labels,omitempty"` 161 Revision uint64 `json:"revision,omitempty"` 162 RuleCount int `json:"rule_count"` 163 } 164 165 // PolicyUpdateRepr returns string representation of monitor notification 166 func PolicyUpdateRepr(numRules int, labels []string, revision uint64) (string, error) { 167 notification := PolicyUpdateNotification{ 168 Labels: labels, 169 Revision: revision, 170 RuleCount: numRules, 171 } 172 173 repr, err := json.Marshal(notification) 174 175 return string(repr), err 176 } 177 178 // PolicyDeleteRepr returns string representation of monitor notification 179 func PolicyDeleteRepr(deleted int, labels []string, revision uint64) (string, error) { 180 notification := PolicyUpdateNotification{ 181 Labels: labels, 182 Revision: revision, 183 RuleCount: deleted, 184 } 185 repr, err := json.Marshal(notification) 186 187 return string(repr), err 188 } 189 190 // EndpointRegenNotification structures regeneration notification 191 type EndpointRegenNotification struct { 192 ID uint64 `json:"id,omitempty"` 193 Labels []string `json:"labels,omitempty"` 194 Error string `json:"error,omitempty"` 195 } 196 197 // EndpointRegenRepr returns string representation of monitor notification 198 func EndpointRegenRepr(e notifications.RegenNotificationInfo, err error) (string, error) { 199 notification := EndpointRegenNotification{ 200 ID: e.GetID(), 201 Labels: e.GetOpLabels(), 202 } 203 204 if err != nil { 205 notification.Error = err.Error() 206 } 207 208 repr, err := json.Marshal(notification) 209 210 return string(repr), err 211 } 212 213 // EndpointCreateNotification structures the endpoint create notification 214 type EndpointCreateNotification struct { 215 EndpointRegenNotification 216 PodName string `json:"pod-name,omitempty"` 217 Namespace string `json:"namespace,omitempty"` 218 } 219 220 // EndpointCreateRepr returns string representation of monitor notification 221 func EndpointCreateRepr(e notifications.RegenNotificationInfo) (string, error) { 222 notification := EndpointCreateNotification{ 223 EndpointRegenNotification: EndpointRegenNotification{ 224 ID: e.GetID(), 225 Labels: e.GetOpLabels(), 226 }, 227 PodName: e.GetK8sPodName(), 228 Namespace: e.GetK8sNamespace(), 229 } 230 231 repr, err := json.Marshal(notification) 232 233 return string(repr), err 234 } 235 236 // EndpointDeleteNotification structures the an endpoint delete notification 237 type EndpointDeleteNotification struct { 238 EndpointRegenNotification 239 PodName string `json:"pod-name,omitempty"` 240 Namespace string `json:"namespace,omitempty"` 241 } 242 243 // EndpointDeleteRepr returns string representation of monitor notification 244 func EndpointDeleteRepr(e notifications.RegenNotificationInfo) (string, error) { 245 notification := EndpointDeleteNotification{ 246 EndpointRegenNotification: EndpointRegenNotification{ 247 ID: e.GetID(), 248 Labels: e.GetOpLabels(), 249 }, 250 PodName: e.GetK8sPodName(), 251 Namespace: e.GetK8sNamespace(), 252 } 253 254 repr, err := json.Marshal(notification) 255 256 return string(repr), err 257 } 258 259 // TimeNotification structures agent start notification 260 type TimeNotification struct { 261 Time string `json:"time"` 262 } 263 264 // TimeRepr returns string representation of monitor notification 265 func TimeRepr(t time.Time) (string, error) { 266 notification := TimeNotification{ 267 Time: t.String(), 268 } 269 repr, err := json.Marshal(notification) 270 return string(repr), err 271 }