-
Notifications
You must be signed in to change notification settings - Fork 9
/
probe.go
138 lines (111 loc) · 3.22 KB
/
probe.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cuefrontendopaque
import (
"bytes"
"encoding/json"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type cueProbe struct {
Http *cueHttpProbe `json:"http"`
Exec *cueExecProbe `json:"exec"`
}
type cueHttpProbe struct {
Port int `json:"port"`
Path string `json:"path"`
}
type cueExecProbe struct {
Command []string
}
var _ json.Unmarshaler = &cueExecProbe{}
func (e *cueExecProbe) UnmarshalJSON(data []byte) error {
d := json.NewDecoder(bytes.NewReader(data))
tok, err := d.Token()
if err != nil {
return err
}
if str, ok := tok.(string); ok {
e.Command = []string{str}
return nil
}
if tok == json.Delim('[') {
return json.Unmarshal(data, &e.Command)
}
return fnerrors.BadInputError("failed to parse exec probe, unexpected token %v", tok)
}
func parseProbes(loc pkggraph.Location, base []*schema.Probe, server cueServer) ([]*schema.Probe, error) {
if server.ReadinessProbe != nil && server.Probes != nil {
return nil, fnerrors.AttachLocation(loc, fnerrors.BadInputError("probes and probe are exclusive"))
}
probes := base
if server.ReadinessProbe != nil {
probe, err := toProbe(loc, runtime.FnServiceReadyz, *server.ReadinessProbe)
if err != nil {
return nil, err
}
probes = append(probes, probe)
}
if server.Probes != nil {
for name, probe := range server.Probes {
kind, err := parseProbeKind(name)
if err != nil {
return nil, fnerrors.AttachLocation(loc, err)
}
probe, err := toProbe(loc, kind, probe)
if err != nil {
return nil, err
}
probes = append(probes, probe)
}
}
// Check for collisions.
index := make(map[string]*schema.Probe)
for _, probe := range probes {
if existing, ok := index[probe.Kind]; ok {
if proto.Equal(probe, existing) {
continue
}
return nil, fnerrors.AttachLocation(loc, fnerrors.BadInputError("found conflicting probe definitions for %q (%v vs %v)", probe.Kind, probe, existing))
}
index[probe.Kind] = probe
}
return probes, nil
}
func toProbe(loc pkggraph.Location, kind string, probe cueProbe) (*schema.Probe, error) {
if probe.Http != nil && probe.Exec != nil {
return nil, fnerrors.AttachLocation(loc, fnerrors.BadInputError("probes: http and exec are exclusive"))
}
if probe.Http != nil {
return &schema.Probe{
Kind: kind,
Http: &schema.Probe_Http{
Path: probe.Http.Path,
ContainerPort: int32(probe.Http.Port),
},
}, nil
}
if probe.Exec != nil {
return &schema.Probe{
Kind: kind,
Exec: &schema.Probe_Exec{
Command: probe.Exec.Command,
},
}, nil
}
return nil, fnerrors.AttachLocation(loc, fnerrors.BadInputError("unknown probe type"))
}
func parseProbeKind(name string) (string, error) {
switch name {
case "readiness":
return runtime.FnServiceReadyz, nil
case "liveness":
return runtime.FnServiceLivez, nil
default:
return "", fnerrors.BadInputError("%s: unsupported probe kind", name)
}
}