/
kube_replicaset_to_koki_replicaset.go
181 lines (150 loc) · 5.73 KB
/
kube_replicaset_to_koki_replicaset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package converters
import (
"reflect"
appsv1beta2 "k8s.io/api/apps/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/koki/short/parser"
"github.com/koki/short/parser/expressions"
"github.com/koki/short/types"
"github.com/koki/short/yaml"
serrors "github.com/koki/structurederrors"
)
func Convert_Kube_ReplicaSet_to_Koki_ReplicaSet(kubeRS runtime.Object) (*types.ReplicaSetWrapper, error) {
groupVersionKind := kubeRS.GetObjectKind().GroupVersionKind()
groupVersionString := groupVersionKind.GroupVersion().String()
groupVersionKind.Version = "v1beta2"
groupVersionKind.Group = "apps"
kubeRS.GetObjectKind().SetGroupVersionKind(groupVersionKind)
// Serialize as v1beta2
b, err := yaml.Marshal(kubeRS)
if err != nil {
return nil, serrors.InvalidInstanceContextErrorf(err, kubeRS, "couldn't serialize kube ReplicaSet after setting apiVersion to apps/v1beta2")
}
// Deserialize the "generic" kube ReplicaSet
genericReplicaSet, err := parser.ParseSingleKubeNativeFromBytes(b)
if err != nil {
return nil, serrors.InvalidValueContextErrorf(err, string(b), "couldn't deserialize 'generic' kube ReplicaSet")
}
if genericReplicaSet, ok := genericReplicaSet.(*appsv1beta2.ReplicaSet); ok {
kokiWrapper, err := Convert_Kube_v1beta2_ReplicaSet_to_Koki_ReplicaSet(genericReplicaSet)
if err != nil {
return nil, err
}
kokiRS := &kokiWrapper.ReplicaSet
kokiRS.Version = groupVersionString
// Perform version-specific initialization here.
return kokiWrapper, nil
}
return nil, serrors.InvalidInstanceErrorf(genericReplicaSet, "didn't deserialize 'generic' ReplicaSet as apps/v1beta2.ReplicaSet")
}
func Convert_Kube_v1beta2_ReplicaSet_to_Koki_ReplicaSet(kubeRS *appsv1beta2.ReplicaSet) (*types.ReplicaSetWrapper, error) {
kokiRS := &types.ReplicaSet{}
kokiRS.Name = kubeRS.Name
kokiRS.Namespace = kubeRS.Namespace
kokiRS.Version = kubeRS.APIVersion
kokiRS.Cluster = kubeRS.ClusterName
kokiRS.Labels = kubeRS.Labels
kokiRS.Annotations = kubeRS.Annotations
kubeSpec := &kubeRS.Spec
kokiRS.Replicas = kubeSpec.Replicas
kokiRS.MinReadySeconds = kubeSpec.MinReadySeconds
// Fill out the Selector and Template.Labels.
// If kubeRS only has Template.Labels, we pull it up to Selector.
selector, templateLabelsOverride, err := convertRSLabelSelector(kubeSpec.Selector, kubeSpec.Template.Labels)
if err != nil {
return nil, err
}
if selector != nil && (selector.Labels != nil || selector.Shorthand != "") {
kokiRS.Selector = selector
}
// Build a Pod from the kube Template. Use it to set the koki Template.
meta, template, err := convertTemplate(kubeSpec.Template)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "pod template")
}
kokiRS.TemplateMetadata = applyTemplateLabelsOverride(templateLabelsOverride, meta)
kokiRS.PodTemplate = template
// End Selector/Template section.
kokiRS.ReplicaSetStatus, err = convertReplicaSetStatus(kubeRS.Status)
if err != nil {
return nil, err
}
return &types.ReplicaSetWrapper{
ReplicaSet: *kokiRS,
}, nil
}
func convertReplicaSetStatus(kubeStatus appsv1beta2.ReplicaSetStatus) (types.ReplicaSetStatus, error) {
conditions, err := convertReplicaSetConditions(kubeStatus.Conditions)
if err != nil {
return types.ReplicaSetStatus{}, err
}
return types.ReplicaSetStatus{
ObservedGeneration: kubeStatus.ObservedGeneration,
Replicas: types.ReplicaSetReplicasStatus{
Total: kubeStatus.Replicas,
FullyLabeled: kubeStatus.FullyLabeledReplicas,
Ready: kubeStatus.ReadyReplicas,
Available: kubeStatus.AvailableReplicas,
},
Conditions: conditions,
}, nil
}
func convertReplicaSetConditions(kubeConditions []appsv1beta2.ReplicaSetCondition) ([]types.ReplicaSetCondition, error) {
if len(kubeConditions) == 0 {
return nil, nil
}
kokiConditions := make([]types.ReplicaSetCondition, len(kubeConditions))
for i, condition := range kubeConditions {
status, err := convertConditionStatus(condition.Status)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "replica-set conditions[%d]", i)
}
conditionType, err := convertReplicaSetConditionType(condition.Type)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "replica-set conditions[%d]", i)
}
kokiConditions[i] = types.ReplicaSetCondition{
Type: conditionType,
Status: status,
LastTransitionTime: condition.LastTransitionTime,
Reason: condition.Reason,
Message: condition.Message,
}
}
return kokiConditions, nil
}
func convertReplicaSetConditionType(kubeType appsv1beta2.ReplicaSetConditionType) (types.ReplicaSetConditionType, error) {
switch kubeType {
case appsv1beta2.ReplicaSetReplicaFailure:
return types.ReplicaSetReplicaFailure, nil
default:
return types.ReplicaSetReplicaFailure, serrors.InvalidValueErrorf(kubeType, "unrecognized replica-set condition type")
}
}
func convertRSLabelSelector(kubeSelector *metav1.LabelSelector, kubeTemplateLabels map[string]string) (*types.RSSelector, map[string]string, error) {
// If the Selector is unspecified, it defaults to the Template's Labels.
if kubeSelector == nil {
return &types.RSSelector{
Labels: kubeTemplateLabels,
}, nil, nil
}
if len(kubeSelector.MatchExpressions) == 0 {
if reflect.DeepEqual(kubeSelector.MatchLabels, kubeTemplateLabels) {
// Selector and template labels are identical. Just keep the selector.
return &types.RSSelector{
Labels: kubeSelector.MatchLabels,
}, nil, nil
}
return &types.RSSelector{
Labels: kubeSelector.MatchLabels,
}, kubeTemplateLabels, nil
}
selectorString, err := expressions.UnparseLabelSelector(kubeSelector)
if err != nil {
return nil, nil, err
}
return &types.RSSelector{
Shorthand: selectorString,
}, kubeTemplateLabels, nil
}