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
602aaaf0
Commit
602aaaf0
authored
Feb 21, 2018
by
Maciej Pytel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validation for HPA external metrics
parent
079f3f18
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
214 additions
and
1 deletion
+214
-1
validation.go
pkg/apis/autoscaling/validation/validation.go
+34
-1
validation_test.go
pkg/apis/autoscaling/validation/validation_test.go
+180
-0
No files found.
pkg/apis/autoscaling/validation/validation.go
View file @
602aaaf0
...
@@ -115,7 +115,7 @@ func validateMetrics(metrics []autoscaling.MetricSpec, fldPath *field.Path) fiel
...
@@ -115,7 +115,7 @@ func validateMetrics(metrics []autoscaling.MetricSpec, fldPath *field.Path) fiel
return
allErrs
return
allErrs
}
}
var
validMetricSourceTypes
=
sets
.
NewString
(
string
(
autoscaling
.
ObjectMetricSourceType
),
string
(
autoscaling
.
PodsMetricSourceType
),
string
(
autoscaling
.
ResourceMetricSourceType
))
var
validMetricSourceTypes
=
sets
.
NewString
(
string
(
autoscaling
.
ObjectMetricSourceType
),
string
(
autoscaling
.
PodsMetricSourceType
),
string
(
autoscaling
.
ResourceMetricSourceType
)
,
string
(
autoscaling
.
ExternalMetricSourceType
)
)
var
validMetricSourceTypesList
=
validMetricSourceTypes
.
List
()
var
validMetricSourceTypesList
=
validMetricSourceTypes
.
List
()
func
validateMetricSpec
(
spec
autoscaling
.
MetricSpec
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
func
validateMetricSpec
(
spec
autoscaling
.
MetricSpec
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
...
@@ -137,6 +137,13 @@ func validateMetricSpec(spec autoscaling.MetricSpec, fldPath *field.Path) field.
...
@@ -137,6 +137,13 @@ func validateMetricSpec(spec autoscaling.MetricSpec, fldPath *field.Path) field.
}
}
}
}
if
spec
.
External
!=
nil
{
typesPresent
.
Insert
(
"external"
)
if
typesPresent
.
Len
()
==
1
{
allErrs
=
append
(
allErrs
,
validateExternalSource
(
spec
.
External
,
fldPath
.
Child
(
"external"
))
...
)
}
}
if
spec
.
Pods
!=
nil
{
if
spec
.
Pods
!=
nil
{
typesPresent
.
Insert
(
"pods"
)
typesPresent
.
Insert
(
"pods"
)
if
typesPresent
.
Len
()
==
1
{
if
typesPresent
.
Len
()
==
1
{
...
@@ -183,6 +190,32 @@ func validateObjectSource(src *autoscaling.ObjectMetricSource, fldPath *field.Pa
...
@@ -183,6 +190,32 @@ func validateObjectSource(src *autoscaling.ObjectMetricSource, fldPath *field.Pa
return
allErrs
return
allErrs
}
}
func
validateExternalSource
(
src
*
autoscaling
.
ExternalMetricSource
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
if
len
(
src
.
MetricName
)
==
0
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"metricName"
),
"must specify a metric name"
))
}
if
src
.
TargetValue
==
nil
&&
src
.
TargetAverageValue
==
nil
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"targetValue"
),
"must set either a target value for metric or a per-pod target"
))
}
if
src
.
TargetValue
!=
nil
&&
src
.
TargetAverageValue
!=
nil
{
allErrs
=
append
(
allErrs
,
field
.
Forbidden
(
fldPath
.
Child
(
"targetValue"
),
"may not set both a target value for metric and a per-pod target"
))
}
if
src
.
TargetAverageValue
!=
nil
&&
src
.
TargetAverageValue
.
Sign
()
!=
1
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"targetAverageValue"
),
src
.
TargetAverageValue
,
"must be positive"
))
}
if
src
.
TargetValue
!=
nil
&&
src
.
TargetValue
.
Sign
()
!=
1
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"targetValue"
),
src
.
TargetValue
,
"must be positive"
))
}
return
allErrs
}
func
validatePodsSource
(
src
*
autoscaling
.
PodsMetricSource
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
func
validatePodsSource
(
src
*
autoscaling
.
PodsMetricSource
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
allErrs
:=
field
.
ErrorList
{}
...
...
pkg/apis/autoscaling/validation/validation_test.go
View file @
602aaaf0
...
@@ -202,6 +202,62 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
...
@@ -202,6 +202,62 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
},
},
},
},
},
},
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
,
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricName
:
"some/metric"
,
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
TargetValue
:
resource
.
NewMilliQuantity
(
300
,
resource
.
DecimalSI
),
},
},
},
},
},
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
,
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricName
:
"some/metric"
,
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
TargetAverageValue
:
resource
.
NewMilliQuantity
(
300
,
resource
.
DecimalSI
),
},
},
},
},
},
}
}
for
_
,
successCase
:=
range
successCases
{
for
_
,
successCase
:=
range
successCases
{
if
errs
:=
ValidateHorizontalPodAutoscaler
(
&
successCase
);
len
(
errs
)
!=
0
{
if
errs
:=
ValidateHorizontalPodAutoscaler
(
&
successCase
);
len
(
errs
)
!=
0
{
...
@@ -495,6 +551,130 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
...
@@ -495,6 +551,130 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
MinReplicas
:
newInt32
(
1
),
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
TargetValue
:
resource
.
NewMilliQuantity
(
300
,
resource
.
DecimalSI
),
},
},
},
},
},
msg
:
"must specify a metric name"
,
},
{
horizontalPodAutoscaler
:
autoscaling
.
HorizontalPodAutoscaler
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Name
:
"myrc"
,
Kind
:
"ReplicationController"
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricName
:
"some/metric"
,
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
},
},
},
},
},
msg
:
"must set either a target value for metric or a per-pod target"
,
},
{
horizontalPodAutoscaler
:
autoscaling
.
HorizontalPodAutoscaler
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Name
:
"myrc"
,
Kind
:
"ReplicationController"
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricName
:
"some/metric"
,
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
TargetValue
:
resource
.
NewMilliQuantity
(
-
300
,
resource
.
DecimalSI
),
},
},
},
},
},
msg
:
"must be positive"
,
},
{
horizontalPodAutoscaler
:
autoscaling
.
HorizontalPodAutoscaler
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Name
:
"myrc"
,
Kind
:
"ReplicationController"
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricName
:
"some/metric"
,
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
TargetAverageValue
:
resource
.
NewMilliQuantity
(
-
300
,
resource
.
DecimalSI
),
},
},
},
},
},
msg
:
"must be positive"
,
},
{
horizontalPodAutoscaler
:
autoscaling
.
HorizontalPodAutoscaler
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Name
:
"myrc"
,
Kind
:
"ReplicationController"
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{
Type
:
autoscaling
.
ExternalMetricSourceType
,
External
:
&
autoscaling
.
ExternalMetricSource
{
MetricName
:
"some/metric"
,
MetricSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"label"
:
"value"
,
},
},
TargetValue
:
resource
.
NewMilliQuantity
(
300
,
resource
.
DecimalSI
),
TargetAverageValue
:
resource
.
NewMilliQuantity
(
300
,
resource
.
DecimalSI
),
},
},
},
},
},
msg
:
"may not set both a target value for metric and a per-pod target"
,
},
{
horizontalPodAutoscaler
:
autoscaling
.
HorizontalPodAutoscaler
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
metav1
.
NamespaceDefault
},
Spec
:
autoscaling
.
HorizontalPodAutoscalerSpec
{
ScaleTargetRef
:
autoscaling
.
CrossVersionObjectReference
{
Name
:
"myrc"
,
Kind
:
"ReplicationController"
},
MinReplicas
:
newInt32
(
1
),
MaxReplicas
:
5
,
Metrics
:
[]
autoscaling
.
MetricSpec
{
{},
{},
},
},
},
},
...
...
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