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
e76caf43
Commit
e76caf43
authored
Feb 06, 2016
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #20703 from mwielgus/hpa-cm-validation
Auto commit by PR queue bot
parents
9e1d7645
9a74a604
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
134 additions
and
4 deletions
+134
-4
validation.go
pkg/apis/extensions/validation/validation.go
+27
-0
validation_test.go
pkg/apis/extensions/validation/validation_test.go
+103
-0
horizontal.go
pkg/controller/podautoscaler/horizontal.go
+3
-3
horizontal_test.go
pkg/controller/podautoscaler/horizontal_test.go
+1
-1
No files found.
pkg/apis/extensions/validation/validation.go
View file @
e76caf43
...
...
@@ -17,6 +17,7 @@ limitations under the License.
package
validation
import
(
"encoding/json"
"net"
"regexp"
"strconv"
...
...
@@ -27,6 +28,7 @@ import (
unversionedvalidation
"k8s.io/kubernetes/pkg/api/unversioned/validation"
apivalidation
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/controller/podautoscaler"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/intstr"
"k8s.io/kubernetes/pkg/util/sets"
...
...
@@ -85,9 +87,34 @@ func ValidateSubresourceReference(ref extensions.SubresourceReference, fldPath *
return
allErrs
}
func
validateHorizontalPodAutoscalerAnnotations
(
annotations
map
[
string
]
string
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
if
annotationValue
,
found
:=
annotations
[
podautoscaler
.
HpaCustomMetricsTargetAnnotationName
];
found
{
// Try to parse the annotation
var
targetList
extensions
.
CustomMetricTargetList
if
err
:=
json
.
Unmarshal
([]
byte
(
annotationValue
),
&
targetList
);
err
!=
nil
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"annotations"
),
annotations
,
"failed to parse custom metrics target annotation"
))
}
else
{
if
len
(
targetList
.
Items
)
==
0
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"annotations"
,
"items"
),
"custom metrics target must not be empty"
))
}
for
_
,
target
:=
range
targetList
.
Items
{
if
target
.
Name
==
""
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"annotations"
,
"items"
,
"name"
),
"missing custom metric target name"
))
}
if
target
.
TargetValue
.
MilliValue
()
<=
0
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"annotations"
,
"items"
,
"value"
),
target
.
TargetValue
,
"custom metric target value must be greater than 0"
))
}
}
}
}
return
allErrs
}
func
ValidateHorizontalPodAutoscaler
(
autoscaler
*
extensions
.
HorizontalPodAutoscaler
)
field
.
ErrorList
{
allErrs
:=
apivalidation
.
ValidateObjectMeta
(
&
autoscaler
.
ObjectMeta
,
true
,
ValidateHorizontalPodAutoscalerName
,
field
.
NewPath
(
"metadata"
))
allErrs
=
append
(
allErrs
,
validateHorizontalPodAutoscalerSpec
(
autoscaler
.
Spec
,
field
.
NewPath
(
"spec"
))
...
)
allErrs
=
append
(
allErrs
,
validateHorizontalPodAutoscalerAnnotations
(
autoscaler
.
Annotations
,
field
.
NewPath
(
"metadata"
))
...
)
return
allErrs
}
...
...
pkg/apis/extensions/validation/validation_test.go
View file @
e76caf43
...
...
@@ -24,6 +24,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/controller/podautoscaler"
"k8s.io/kubernetes/pkg/util/intstr"
)
...
...
@@ -60,6 +61,24 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
MaxReplicas
:
5
,
},
},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
api
.
NamespaceDefault
,
Annotations
:
map
[
string
]
string
{
podautoscaler
.
HpaCustomMetricsTargetAnnotationName
:
"{
\"
items
\"
:[{
\"
name
\"
:
\"
qps
\"
,
\"
value
\"
:
\"
20
\"
}]}"
,
},
},
Spec
:
extensions
.
HorizontalPodAutoscalerSpec
{
ScaleRef
:
extensions
.
SubresourceReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
Subresource
:
"scale"
,
},
MinReplicas
:
newInt
(
1
),
MaxReplicas
:
5
,
},
},
}
for
_
,
successCase
:=
range
successCases
{
if
errs
:=
ValidateHorizontalPodAutoscaler
(
&
successCase
);
len
(
errs
)
!=
0
{
...
...
@@ -204,6 +223,90 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
},
msg
:
"must be greater than 0"
,
},
{
horizontalPodAutoscaler
:
extensions
.
HorizontalPodAutoscaler
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
api
.
NamespaceDefault
,
Annotations
:
map
[
string
]
string
{
podautoscaler
.
HpaCustomMetricsTargetAnnotationName
:
"broken"
,
},
},
Spec
:
extensions
.
HorizontalPodAutoscalerSpec
{
ScaleRef
:
extensions
.
SubresourceReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
Subresource
:
"scale"
,
},
MinReplicas
:
newInt
(
1
),
MaxReplicas
:
5
,
},
},
msg
:
"failed to parse custom metrics target annotation"
,
},
{
horizontalPodAutoscaler
:
extensions
.
HorizontalPodAutoscaler
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
api
.
NamespaceDefault
,
Annotations
:
map
[
string
]
string
{
podautoscaler
.
HpaCustomMetricsTargetAnnotationName
:
"{}"
,
},
},
Spec
:
extensions
.
HorizontalPodAutoscalerSpec
{
ScaleRef
:
extensions
.
SubresourceReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
Subresource
:
"scale"
,
},
MinReplicas
:
newInt
(
1
),
MaxReplicas
:
5
,
},
},
msg
:
"custom metrics target must not be empty"
,
},
{
horizontalPodAutoscaler
:
extensions
.
HorizontalPodAutoscaler
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
api
.
NamespaceDefault
,
Annotations
:
map
[
string
]
string
{
podautoscaler
.
HpaCustomMetricsTargetAnnotationName
:
"{
\"
items
\"
:[{
\"
value
\"
:
\"
20
\"
}]}"
,
},
},
Spec
:
extensions
.
HorizontalPodAutoscalerSpec
{
ScaleRef
:
extensions
.
SubresourceReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
Subresource
:
"scale"
,
},
MinReplicas
:
newInt
(
1
),
MaxReplicas
:
5
,
},
},
msg
:
"missing custom metric target name"
,
},
{
horizontalPodAutoscaler
:
extensions
.
HorizontalPodAutoscaler
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"myautoscaler"
,
Namespace
:
api
.
NamespaceDefault
,
Annotations
:
map
[
string
]
string
{
podautoscaler
.
HpaCustomMetricsTargetAnnotationName
:
"{
\"
items
\"
:[{
\"
name
\"
:
\"
qps
\"
,
\"
value
\"
:
\"
0
\"
}]}"
,
},
},
Spec
:
extensions
.
HorizontalPodAutoscalerSpec
{
ScaleRef
:
extensions
.
SubresourceReference
{
Kind
:
"ReplicationController"
,
Name
:
"myrc"
,
Subresource
:
"scale"
,
},
MinReplicas
:
newInt
(
1
),
MaxReplicas
:
5
,
},
},
msg
:
"custom metric target value must be greater than 0"
,
},
}
for
_
,
c
:=
range
errorCases
{
...
...
pkg/controller/podautoscaler/horizontal.go
View file @
e76caf43
...
...
@@ -39,8 +39,8 @@ const (
// TODO: make it a flag or HPA spec element.
tolerance
=
0.1
HpaCustomMetrics
DefinitionAnnotationName
=
"alpha/definiton
.custom-metrics.podautoscaler.kubernetes.io"
HpaCustomMetricsStatusAnnotationName
=
"alpha/status.custom-metrics.podautoscaler.kubernetes.io"
HpaCustomMetrics
TargetAnnotationName
=
"alpha/target
.custom-metrics.podautoscaler.kubernetes.io"
HpaCustomMetricsStatusAnnotationName
=
"alpha/status.custom-metrics.podautoscaler.kubernetes.io"
)
type
HorizontalController
struct
{
...
...
@@ -190,7 +190,7 @@ func (a *HorizontalController) reconcileAutoscaler(hpa extensions.HorizontalPodA
}
}
if
cmAnnotation
,
cmAnnotationFound
:=
hpa
.
Annotations
[
HpaCustomMetrics
Definition
AnnotationName
];
cmAnnotationFound
{
if
cmAnnotation
,
cmAnnotationFound
:=
hpa
.
Annotations
[
HpaCustomMetrics
Target
AnnotationName
];
cmAnnotationFound
{
cmDesiredReplicas
,
cmStatus
,
cmTimestamp
,
err
=
a
.
computeReplicasForCustomMetrics
(
hpa
,
scale
,
cmAnnotation
)
if
err
!=
nil
{
a
.
eventRecorder
.
Event
(
&
hpa
,
api
.
EventTypeWarning
,
"FailedComputeCMReplicas"
,
err
.
Error
())
...
...
pkg/controller/podautoscaler/horizontal_test.go
View file @
e76caf43
...
...
@@ -110,7 +110,7 @@ func (tc *testCase) prepareTestClient(t *testing.T) *fake.Clientset {
t
.
Fatalf
(
"Failed to marshal cm: %v"
,
err
)
}
obj
.
Items
[
0
]
.
Annotations
=
make
(
map
[
string
]
string
)
obj
.
Items
[
0
]
.
Annotations
[
HpaCustomMetrics
Definition
AnnotationName
]
=
string
(
b
)
obj
.
Items
[
0
]
.
Annotations
[
HpaCustomMetrics
Target
AnnotationName
]
=
string
(
b
)
}
return
true
,
obj
,
nil
})
...
...
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