
Here are a list of changes along with an explanation of how they work: 1. Add a new string field called TargetSelector to the external version of extensions Scale type (extensions/v1beta1.Scale). This is a serialized version of either the map-based selector (in case of ReplicationControllers) or the unversioned.LabelSelector struct (in case of Deployments and ReplicaSets). 2. Change the selector field in the internal Scale type (extensions.Scale) to unversioned.LabelSelector. 3. Add conversion functions to convert from two external selector fields to a single internal selector field. The rules for conversion are as follows: i. If the target resource that this scale targets supports LabelSelector (Deployments and ReplicaSets), then serialize the LabelSelector and store the string in the TargetSelector field in the external version and leave the map-based Selector field as nil. ii. If the target resource only supports a map-based selector (ReplicationControllers), then still serialize that selector and store the serialized string in the TargetSelector field. Also, set the the Selector map field in the external Scale type. iii. When converting from external to internal version, parse the TargetSelector string into LabelSelector struct if the string isn't empty. If it is empty, then check if the Selector map is set and just assign that map to the MatchLabels component of the LabelSelector. iv. When converting from internal to external version, serialize the LabelSelector and store it in the TargetSelector field. If only the MatchLabel component is set, then also copy that value to the Selector map field in the external version. 4. HPA now just converts the LabelSelector field to a Selector interface type to list the pods. 5. Scale Get and Update etcd methods for Deployments and ReplicaSets now return extensions.Scale instead of autoscaling.Scale. 6. Consequently, SubresourceGroupVersion override and is "autoscaling" enabled check is now removed from pkg/master/master.go 7. Other small changes to labels package, fuzzer and LabelSelector helpers to piece this all together. 8. Add unit tests to HPA targeting Deployments and ReplicaSets. 9. Add an e2e test to HPA targeting ReplicaSets.
155 lines
4.5 KiB
Go
155 lines
4.5 KiB
Go
/*
|
|
Copyright 2016 The Kubernetes Authors 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.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package unversioned
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
|
)
|
|
|
|
// LabelSelectorAsSelector converts the LabelSelector api type into a struct that implements
|
|
// labels.Selector
|
|
// Note: This function should be kept in sync with the selector methods in pkg/labels/selector.go
|
|
func LabelSelectorAsSelector(ps *LabelSelector) (labels.Selector, error) {
|
|
if ps == nil {
|
|
return labels.Nothing(), nil
|
|
}
|
|
if len(ps.MatchLabels)+len(ps.MatchExpressions) == 0 {
|
|
return labels.Everything(), nil
|
|
}
|
|
selector := labels.NewSelector()
|
|
for k, v := range ps.MatchLabels {
|
|
r, err := labels.NewRequirement(k, labels.EqualsOperator, sets.NewString(v))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selector = selector.Add(*r)
|
|
}
|
|
for _, expr := range ps.MatchExpressions {
|
|
var op labels.Operator
|
|
switch expr.Operator {
|
|
case LabelSelectorOpIn:
|
|
op = labels.InOperator
|
|
case LabelSelectorOpNotIn:
|
|
op = labels.NotInOperator
|
|
case LabelSelectorOpExists:
|
|
op = labels.ExistsOperator
|
|
case LabelSelectorOpDoesNotExist:
|
|
op = labels.DoesNotExistOperator
|
|
default:
|
|
return nil, fmt.Errorf("%q is not a valid pod selector operator", expr.Operator)
|
|
}
|
|
r, err := labels.NewRequirement(expr.Key, op, sets.NewString(expr.Values...))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selector = selector.Add(*r)
|
|
}
|
|
return selector, nil
|
|
}
|
|
|
|
// ParseToLabelSelector parses a string representing a selector into a LabelSelector object.
|
|
// Note: This function should be kept in sync with the parser in pkg/labels/selector.go
|
|
func ParseToLabelSelector(selector string) (*LabelSelector, error) {
|
|
reqs, err := labels.ParseToRequirements(selector)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("couldn't parse the selector string \"%s\": %v", selector, err)
|
|
}
|
|
|
|
labelSelector := &LabelSelector{
|
|
MatchLabels: map[string]string{},
|
|
MatchExpressions: []LabelSelectorRequirement{},
|
|
}
|
|
for _, req := range reqs {
|
|
var op LabelSelectorOperator
|
|
switch req.Operator() {
|
|
case labels.EqualsOperator, labels.DoubleEqualsOperator:
|
|
vals := req.Values()
|
|
if vals.Len() != 1 {
|
|
return nil, fmt.Errorf("equals operator must have exactly one value")
|
|
}
|
|
val, ok := vals.PopAny()
|
|
if !ok {
|
|
return nil, fmt.Errorf("equals operator has exactly one value but it cannot be retrieved")
|
|
}
|
|
labelSelector.MatchLabels[req.Key()] = val
|
|
continue
|
|
case labels.InOperator:
|
|
op = LabelSelectorOpIn
|
|
case labels.NotInOperator:
|
|
op = LabelSelectorOpNotIn
|
|
case labels.ExistsOperator:
|
|
op = LabelSelectorOpExists
|
|
case labels.DoesNotExistOperator:
|
|
op = LabelSelectorOpDoesNotExist
|
|
case labels.GreaterThanOperator, labels.LessThanOperator:
|
|
// Adding a separate case for these operators to indicate that this is deliberate
|
|
return nil, fmt.Errorf("%q isn't supported in label selectors", req.Operator())
|
|
default:
|
|
return nil, fmt.Errorf("%q is not a valid label selector operator", req.Operator())
|
|
}
|
|
labelSelector.MatchExpressions = append(labelSelector.MatchExpressions, LabelSelectorRequirement{
|
|
Key: req.Key(),
|
|
Operator: op,
|
|
Values: req.Values().List(),
|
|
})
|
|
}
|
|
return labelSelector, nil
|
|
}
|
|
|
|
// SetAsLabelSelector converts the labels.Set object into a LabelSelector api object.
|
|
func SetAsLabelSelector(ls labels.Set) *LabelSelector {
|
|
if ls == nil {
|
|
return nil
|
|
}
|
|
|
|
selector := &LabelSelector{
|
|
MatchLabels: make(map[string]string),
|
|
}
|
|
for label, value := range ls {
|
|
selector.MatchLabels[label] = value
|
|
}
|
|
|
|
return selector
|
|
}
|
|
|
|
// FormatLabelSelector convert labelSelector into plain string
|
|
func FormatLabelSelector(labelSelector *LabelSelector) string {
|
|
selector, err := LabelSelectorAsSelector(labelSelector)
|
|
if err != nil {
|
|
return "<error>"
|
|
}
|
|
|
|
l := selector.String()
|
|
if len(l) == 0 {
|
|
l = "<none>"
|
|
}
|
|
return l
|
|
}
|
|
|
|
func ExtractGroupVersions(l *APIGroupList) []string {
|
|
var groupVersions []string
|
|
for _, g := range l.Groups {
|
|
for _, gv := range g.Versions {
|
|
groupVersions = append(groupVersions, gv.GroupVersion)
|
|
}
|
|
}
|
|
return groupVersions
|
|
}
|