| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package extensionsv1beta1 |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
|
| 22 |
v1beta1 "k8s.io/api/extensions/v1beta1" |
| 23 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 |
"k8s.io/apimachinery/pkg/conversion" |
| 25 |
scheme "k8s.io/client-go/scale/scheme" |
| 26 |
) |
| 27 |
|
| 28 |
func Convert_scheme_ScaleStatus_To_v1beta1_ScaleStatus(in *scheme.ScaleStatus, out *v1beta1.ScaleStatus, s conversion.Scope) error { |
| 29 |
out.Replicas = in.Replicas |
| 30 |
out.Selector = nil |
| 31 |
out.TargetSelector = "" |
| 32 |
if in.Selector != nil { |
| 33 |
if in.Selector.MatchExpressions == nil || len(in.Selector.MatchExpressions) == 0 { |
| 34 |
out.Selector = in.Selector.MatchLabels |
| 35 |
} |
| 36 |
|
| 37 |
selector, err := metav1.LabelSelectorAsSelector(in.Selector) |
| 38 |
if err != nil { |
| 39 |
return fmt.Errorf("invalid label selector: %v", err) |
| 40 |
} |
| 41 |
out.TargetSelector = selector.String() |
| 42 |
} |
| 43 |
|
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
func Convert_v1beta1_ScaleStatus_To_scheme_ScaleStatus(in *v1beta1.ScaleStatus, out *scheme.ScaleStatus, s conversion.Scope) error { |
| 48 |
out.Replicas = in.Replicas |
| 49 |
|
| 50 |
// Normally when 2 fields map to the same internal value we favor the old field, since |
| 51 |
// old clients can't be expected to know about new fields but clients that know about the |
| 52 |
// new field can be expected to know about the old field (though that's not quite true, due |
| 53 |
// to kubectl apply). However, these fields are readonly, so any non-nil value should work. |
| 54 |
if in.TargetSelector != "" { |
| 55 |
labelSelector, err := metav1.ParseToLabelSelector(in.TargetSelector) |
| 56 |
if err != nil { |
| 57 |
out.Selector = nil |
| 58 |
return fmt.Errorf("failed to parse target selector: %v", err) |
| 59 |
} |
| 60 |
out.Selector = labelSelector |
| 61 |
} else if in.Selector != nil { |
| 62 |
out.Selector = new(metav1.LabelSelector) |
| 63 |
selector := make(map[string]string) |
| 64 |
for key, val := range in.Selector { |
| 65 |
selector[key] = val |
| 66 |
} |
| 67 |
out.Selector.MatchLabels = selector |
| 68 |
} else { |
| 69 |
out.Selector = nil |
| 70 |
} |
| 71 |
|
| 72 |
return nil |
| 73 |
} |