Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
369fd81c
Commit
369fd81c
authored
Nov 14, 2017
by
Joe Betz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Align admission metric names with prometheus guidelines
parent
375e2d03
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
46 deletions
+46
-46
chain.go
staging/src/k8s.io/apiserver/pkg/admission/chain.go
+7
-7
chain_test.go
staging/src/k8s.io/apiserver/pkg/admission/chain_test.go
+8
-8
metrics.go
staging/src/k8s.io/apiserver/pkg/admission/metrics.go
+17
-17
metrics_test.go
staging/src/k8s.io/apiserver/pkg/admission/metrics_test.go
+13
-13
admission.go
...8s.io/apiserver/pkg/admission/plugin/webhook/admission.go
+1
-1
No files found.
staging/src/k8s.io/apiserver/pkg/admission/chain.go
View file @
369fd81c
...
...
@@ -18,7 +18,7 @@ package admission
import
"time"
// chainAdmissionHandler is an instance of admission.
Interface
that performs admission control using
// chainAdmissionHandler is an instance of admission.
NamedHandler
that performs admission control using
// a chain of admission handlers
type
chainAdmissionHandler
[]
NamedHandler
...
...
@@ -35,15 +35,15 @@ func NewNamedHandler(name string, i Interface) NamedHandler {
}
const
(
stepValidat
ing
=
"validating
"
step
Mutating
=
"mutating
"
stepValidat
e
=
"validate
"
step
Admit
=
"admit
"
)
// Admit performs an admission control check using a chain of handlers, and returns immediately on first error
func
(
admissionHandler
chainAdmissionHandler
)
Admit
(
a
Attributes
)
error
{
start
:=
time
.
Now
()
err
:=
admissionHandler
.
admit
(
a
)
Metrics
.
ObserveAdmissionStep
(
time
.
Since
(
start
),
err
!=
nil
,
a
,
step
Mutating
)
Metrics
.
ObserveAdmissionStep
(
time
.
Since
(
start
),
err
!=
nil
,
a
,
step
Admit
)
return
err
}
...
...
@@ -55,7 +55,7 @@ func (admissionHandler chainAdmissionHandler) admit(a Attributes) error {
if
mutator
,
ok
:=
handler
.
Interface
()
.
(
MutationInterface
);
ok
{
t
:=
time
.
Now
()
err
:=
mutator
.
Admit
(
a
)
Metrics
.
ObserveAdmissionController
(
time
.
Since
(
t
),
err
!=
nil
,
handler
,
a
,
step
Mutating
)
Metrics
.
ObserveAdmissionController
(
time
.
Since
(
t
),
err
!=
nil
,
handler
,
a
,
step
Admit
)
if
err
!=
nil
{
return
err
}
...
...
@@ -68,7 +68,7 @@ func (admissionHandler chainAdmissionHandler) admit(a Attributes) error {
func
(
admissionHandler
chainAdmissionHandler
)
Validate
(
a
Attributes
)
error
{
start
:=
time
.
Now
()
err
:=
admissionHandler
.
validate
(
a
)
Metrics
.
ObserveAdmissionStep
(
time
.
Since
(
start
),
err
!=
nil
,
a
,
stepValidat
ing
)
Metrics
.
ObserveAdmissionStep
(
time
.
Since
(
start
),
err
!=
nil
,
a
,
stepValidat
e
)
return
err
}
...
...
@@ -80,7 +80,7 @@ func (admissionHandler chainAdmissionHandler) validate(a Attributes) (err error)
if
validator
,
ok
:=
handler
.
Interface
()
.
(
ValidationInterface
);
ok
{
t
:=
time
.
Now
()
err
:=
validator
.
Validate
(
a
)
Metrics
.
ObserveAdmissionController
(
time
.
Since
(
t
),
err
!=
nil
,
handler
,
a
,
stepValidat
ing
)
Metrics
.
ObserveAdmissionController
(
time
.
Since
(
t
),
err
!=
nil
,
handler
,
a
,
stepValidat
e
)
if
err
!=
nil
{
return
err
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/chain_test.go
View file @
369fd81c
...
...
@@ -103,12 +103,12 @@ func TestAdmitAndValidate(t *testing.T) {
t
.
Errorf
(
"validate handler %s called during admit"
,
h
.
Name
())
}
//
reset value for validation test
// reset value for validation test
fake
.
admitCalled
=
false
}
labelFilter
:=
map
[
string
]
string
{
"type"
:
"
mutating
"
,
"type"
:
"
admit
"
,
}
checkAdmitAndValidateMetrics
(
t
,
labelFilter
,
test
.
accept
,
test
.
calls
)
...
...
@@ -134,7 +134,7 @@ func TestAdmitAndValidate(t *testing.T) {
}
labelFilter
=
map
[
string
]
string
{
"type"
:
"validat
ing
"
,
"type"
:
"validat
e
"
,
}
checkAdmitAndValidateMetrics
(
t
,
labelFilter
,
test
.
accept
,
test
.
calls
)
...
...
@@ -154,22 +154,22 @@ func checkAdmitAndValidateMetrics(t *testing.T, labelFilter map[string]string, a
if
accept
{
// Ensure exactly one admission end-to-end admission accept should have been recorded.
expectHistogramCountTotal
(
t
,
"apiserver_admission_step_
latencie
s"
,
acceptFilter
,
1
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_step_
admission_latencies_second
s"
,
acceptFilter
,
1
)
// Ensure the expected count of admission controllers have been executed.
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
latencie
s"
,
acceptFilter
,
len
(
calls
))
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
admission_latencies_second
s"
,
acceptFilter
,
len
(
calls
))
}
else
{
// When not accepted, ensure exactly one end-to-end rejection has been recorded.
expectHistogramCountTotal
(
t
,
"apiserver_admission_step_
latencie
s"
,
rejectFilter
,
1
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_step_
admission_latencies_second
s"
,
rejectFilter
,
1
)
if
len
(
calls
)
>
0
{
if
len
(
calls
)
>
1
{
// When not accepted, ensure that all but the last controller had been accepted, since
// the chain stops execution at the first rejection.
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
latencie
s"
,
acceptFilter
,
len
(
calls
)
-
1
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
admission_latencies_second
s"
,
acceptFilter
,
len
(
calls
)
-
1
)
}
// When not accepted, ensure exactly one controller has been rejected.
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
latencie
s"
,
rejectFilter
,
1
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
admission_latencies_second
s"
,
rejectFilter
,
1
)
}
}
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/metrics.go
View file @
369fd81c
...
...
@@ -47,9 +47,9 @@ type NamedHandler interface {
// AdmissionMetrics instruments admission with prometheus metrics.
type
AdmissionMetrics
struct
{
step
*
metricSet
controller
*
metricSet
externalWebhook
*
metricSet
step
*
metricSet
controller
*
metricSet
webhook
*
metricSet
}
// newAdmissionMetrics create a new AdmissionMetrics, configured with default metric names.
...
...
@@ -58,28 +58,28 @@ func newAdmissionMetrics() *AdmissionMetrics {
// Each step is identified by a distinct type label value.
step
:=
newMetricSet
(
"step"
,
[]
string
{
"type"
,
"operation"
,
"group"
,
"version"
,
"resource"
,
"subresource"
,
"rejected"
},
"Admission sub-step %s, broken out for each operation and API resource and step type (validat
ing or mutating
)."
)
"Admission sub-step %s, broken out for each operation and API resource and step type (validat
e or admit
)."
)
// Built-in admission controller metrics. Each admission controller is identified by name.
controller
:=
newMetricSet
(
"controller"
,
[]
string
{
"name"
,
"type"
,
"operation"
,
"group"
,
"version"
,
"resource"
,
"subresource"
,
"rejected"
},
"Admission controller %s, identified by name and broken out for each operation and API resource and type (validat
ing or mutating
)."
)
"Admission controller %s, identified by name and broken out for each operation and API resource and type (validat
e or admit
)."
)
//
External a
dmission webhook metrics. Each webhook is identified by name.
externalWebhook
:=
newMetricSet
(
"external_
webhook"
,
//
A
dmission webhook metrics. Each webhook is identified by name.
webhook
:=
newMetricSet
(
"
webhook"
,
[]
string
{
"name"
,
"type"
,
"operation"
,
"group"
,
"version"
,
"resource"
,
"subresource"
,
"rejected"
},
"
External admission webhook %s, identified by name and broken out for each operation and API resource and type (validating or mutating
)."
)
"
Admission webhook %s, identified by name and broken out for each operation and API resource and type (validate or admit
)."
)
step
.
mustRegister
()
controller
.
mustRegister
()
externalW
ebhook
.
mustRegister
()
return
&
AdmissionMetrics
{
step
:
step
,
controller
:
controller
,
externalWebhook
:
externalW
ebhook
}
w
ebhook
.
mustRegister
()
return
&
AdmissionMetrics
{
step
:
step
,
controller
:
controller
,
webhook
:
w
ebhook
}
}
func
(
m
*
AdmissionMetrics
)
reset
()
{
m
.
step
.
reset
()
m
.
controller
.
reset
()
m
.
externalW
ebhook
.
reset
()
m
.
w
ebhook
.
reset
()
}
// ObserveAdmissionStep records admission related metrics for a admission step, identified by step type.
...
...
@@ -94,11 +94,11 @@ func (m *AdmissionMetrics) ObserveAdmissionController(elapsed time.Duration, rej
m
.
controller
.
observe
(
elapsed
,
handler
.
Name
(),
stepType
,
string
(
attr
.
GetOperation
()),
gvr
.
Group
,
gvr
.
Version
,
gvr
.
Resource
,
attr
.
GetSubresource
(),
strconv
.
FormatBool
(
rejected
))
}
// Observe
ExternalWebhook records admission related metrics for a external
admission webhook.
func
(
m
*
AdmissionMetrics
)
Observe
External
Webhook
(
elapsed
time
.
Duration
,
rejected
bool
,
hook
*
v1alpha1
.
Webhook
,
attr
Attributes
)
{
t
:=
"
validating"
// TODO: pass in type (validating|mutating
) once mutating webhook functionality has been implemented
// Observe
Webhook records admission related metrics for a
admission webhook.
func
(
m
*
AdmissionMetrics
)
ObserveWebhook
(
elapsed
time
.
Duration
,
rejected
bool
,
hook
*
v1alpha1
.
Webhook
,
attr
Attributes
)
{
t
:=
"
admit"
// TODO: pass in type (validate|admit
) once mutating webhook functionality has been implemented
gvr
:=
attr
.
GetResource
()
m
.
externalW
ebhook
.
observe
(
elapsed
,
hook
.
Name
,
t
,
string
(
attr
.
GetOperation
()),
gvr
.
Group
,
gvr
.
Version
,
gvr
.
Resource
,
attr
.
GetSubresource
(),
strconv
.
FormatBool
(
rejected
))
m
.
w
ebhook
.
observe
(
elapsed
,
hook
.
Name
,
t
,
string
(
attr
.
GetOperation
()),
gvr
.
Group
,
gvr
.
Version
,
gvr
.
Resource
,
attr
.
GetSubresource
(),
strconv
.
FormatBool
(
rejected
))
}
type
metricSet
struct
{
...
...
@@ -112,7 +112,7 @@ func newMetricSet(name string, labels []string, helpTemplate string) *metricSet
prometheus
.
HistogramOpts
{
Namespace
:
namespace
,
Subsystem
:
subsystem
,
Name
:
fmt
.
Sprintf
(
"%s_
latencie
s"
,
name
),
Name
:
fmt
.
Sprintf
(
"%s_
admission_latencies_second
s"
,
name
),
Help
:
fmt
.
Sprintf
(
helpTemplate
,
"latency histogram"
),
Buckets
:
latencyBuckets
,
},
...
...
@@ -122,7 +122,7 @@ func newMetricSet(name string, labels []string, helpTemplate string) *metricSet
prometheus
.
SummaryOpts
{
Namespace
:
namespace
,
Subsystem
:
subsystem
,
Name
:
fmt
.
Sprintf
(
"%s_
latencie
s_summary"
,
name
),
Name
:
fmt
.
Sprintf
(
"%s_
admission_latencies_second
s_summary"
,
name
),
Help
:
fmt
.
Sprintf
(
helpTemplate
,
"latency summary"
),
MaxAge
:
latencySummaryMaxAge
,
},
...
...
staging/src/k8s.io/apiserver/pkg/admission/metrics_test.go
View file @
369fd81c
...
...
@@ -33,24 +33,24 @@ var (
func
TestObserveAdmissionStep
(
t
*
testing
.
T
)
{
Metrics
.
reset
()
Metrics
.
ObserveAdmissionStep
(
2
*
time
.
Second
,
false
,
attr
,
"
mutating
"
)
Metrics
.
ObserveAdmissionStep
(
2
*
time
.
Second
,
false
,
attr
,
"
admit
"
)
wantLabels
:=
map
[
string
]
string
{
"operation"
:
string
(
Create
),
"group"
:
resource
.
Group
,
"version"
:
resource
.
Version
,
"resource"
:
resource
.
Resource
,
"subresource"
:
"subresource"
,
"type"
:
"
mutating
"
,
"type"
:
"
admit
"
,
"rejected"
:
"false"
,
}
expectHistogramCountTotal
(
t
,
"apiserver_admission_step_
latencie
s"
,
wantLabels
,
1
)
expectFindMetric
(
t
,
"apiserver_admission_step_
latencie
s_summary"
,
wantLabels
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_step_
admission_latencies_second
s"
,
wantLabels
,
1
)
expectFindMetric
(
t
,
"apiserver_admission_step_
admission_latencies_second
s_summary"
,
wantLabels
)
}
func
TestObserveAdmissionController
(
t
*
testing
.
T
)
{
Metrics
.
reset
()
handler
:=
makeValidatingNamedHandler
(
"a"
,
true
,
Create
)
Metrics
.
ObserveAdmissionController
(
2
*
time
.
Second
,
false
,
handler
,
attr
,
"validat
ing
"
)
Metrics
.
ObserveAdmissionController
(
2
*
time
.
Second
,
false
,
handler
,
attr
,
"validat
e
"
)
wantLabels
:=
map
[
string
]
string
{
"name"
:
"a"
,
"operation"
:
string
(
Create
),
...
...
@@ -58,17 +58,17 @@ func TestObserveAdmissionController(t *testing.T) {
"version"
:
resource
.
Version
,
"resource"
:
resource
.
Resource
,
"subresource"
:
"subresource"
,
"type"
:
"validat
ing
"
,
"type"
:
"validat
e
"
,
"rejected"
:
"false"
,
}
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
latencie
s"
,
wantLabels
,
1
)
expectFindMetric
(
t
,
"apiserver_admission_controller_
latencie
s_summary"
,
wantLabels
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_controller_
admission_latencies_second
s"
,
wantLabels
,
1
)
expectFindMetric
(
t
,
"apiserver_admission_controller_
admission_latencies_second
s_summary"
,
wantLabels
)
}
func
TestObserve
External
Webhook
(
t
*
testing
.
T
)
{
func
TestObserveWebhook
(
t
*
testing
.
T
)
{
Metrics
.
reset
()
hook
:=
&
v1alpha1
.
Webhook
{
Name
:
"x"
}
Metrics
.
Observe
External
Webhook
(
2
*
time
.
Second
,
false
,
hook
,
attr
)
Metrics
.
ObserveWebhook
(
2
*
time
.
Second
,
false
,
hook
,
attr
)
wantLabels
:=
map
[
string
]
string
{
"name"
:
"x"
,
"operation"
:
string
(
Create
),
...
...
@@ -76,9 +76,9 @@ func TestObserveExternalWebhook(t *testing.T) {
"version"
:
resource
.
Version
,
"resource"
:
resource
.
Resource
,
"subresource"
:
"subresource"
,
"type"
:
"
validating
"
,
"type"
:
"
admit
"
,
"rejected"
:
"false"
,
}
expectHistogramCountTotal
(
t
,
"apiserver_admission_
external_webhook_latencie
s"
,
wantLabels
,
1
)
expectFindMetric
(
t
,
"apiserver_admission_
external_webhook_latencie
s_summary"
,
wantLabels
)
expectHistogramCountTotal
(
t
,
"apiserver_admission_
webhook_admission_latencies_second
s"
,
wantLabels
,
1
)
expectFindMetric
(
t
,
"apiserver_admission_
webhook_admission_latencies_second
s_summary"
,
wantLabels
)
}
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/admission.go
View file @
369fd81c
...
...
@@ -309,7 +309,7 @@ func (a *GenericAdmissionWebhook) Admit(attr admission.Attributes) error {
t
:=
time
.
Now
()
err
:=
a
.
callHook
(
ctx
,
hook
,
versionedAttr
)
admission
.
Metrics
.
Observe
External
Webhook
(
time
.
Since
(
t
),
err
!=
nil
,
hook
,
attr
)
admission
.
Metrics
.
ObserveWebhook
(
time
.
Since
(
t
),
err
!=
nil
,
hook
,
attr
)
if
err
==
nil
{
return
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment