forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.go
191 lines (170 loc) · 5.24 KB
/
conversion.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
182
183
184
185
186
187
188
189
190
191
// Copyright 2017 Istio Authors
//
// 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 crd
import (
"bytes"
"fmt"
"io"
"reflect"
"strings"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/log"
)
// ConvertObject converts an IstioObject k8s-style object to the
// internal configuration model.
func ConvertObject(schema model.ProtoSchema, object IstioObject, domain string) (*model.Config, error) {
data, err := schema.FromJSONMap(object.GetSpec())
if err != nil {
return nil, err
}
meta := object.GetObjectMeta()
//// FIXME this is a gross hack to hardcode a service's domain name in kubernetes
//if domain == "" {
// domain = "svc.cluster.local"
//} else {
// domain = "svc." + domain
//}
return &model.Config{
ConfigMeta: model.ConfigMeta{
Type: schema.Type,
Group: ResourceGroup(&schema),
Version: schema.Version,
Name: meta.Name,
Namespace: meta.Namespace,
Domain: domain,
Labels: meta.Labels,
Annotations: meta.Annotations,
ResourceVersion: meta.ResourceVersion,
},
Spec: data,
}, nil
}
// ConvertConfig translates Istio config to k8s config JSON
func ConvertConfig(schema model.ProtoSchema, config model.Config) (IstioObject, error) {
spec, err := model.ToJSONMap(config.Spec)
if err != nil {
return nil, err
}
namespace := config.Namespace
if namespace == "" {
namespace = meta_v1.NamespaceDefault
}
out := knownTypes[schema.Type].object.DeepCopyObject().(IstioObject)
out.SetObjectMeta(meta_v1.ObjectMeta{
Name: config.Name,
Namespace: namespace,
ResourceVersion: config.ResourceVersion,
Labels: config.Labels,
Annotations: config.Annotations,
})
out.SetSpec(spec)
return out, nil
}
// ResourceName converts "my-name" to "myname".
// This is needed by k8s API server as dashes prevent kubectl from accessing CRDs
func ResourceName(s string) string {
return strings.Replace(s, "-", "", -1)
}
// ResourceGroup generates the k8s API group for each schema.
func ResourceGroup(schema *model.ProtoSchema) string {
return schema.Group + model.IstioAPIGroupDomain
}
// TODO - add special cases for type-to-kind and kind-to-type
// conversions with initial-isms. Consider adding additional type
// information to the abstract model and/or elevating k8s
// representation to first-class type to avoid extra conversions.
// KabobCaseToCamelCase converts "my-name" to "MyName"
func KabobCaseToCamelCase(s string) string {
switch s {
case "http-api-spec":
return "HTTPAPISpec"
case "http-api-spec-binding":
return "HTTPAPISpecBinding"
default:
words := strings.Split(s, "-")
out := ""
for _, word := range words {
out = out + strings.Title(word)
}
return out
}
}
// CamelCaseToKabobCase converts "MyName" to "my-name"
func CamelCaseToKabobCase(s string) string {
switch s {
case "HTTPAPISpec":
return "http-api-spec"
case "HTTPAPISpecBinding":
return "http-api-spec-binding"
default:
var out bytes.Buffer
for i := range s {
if 'A' <= s[i] && s[i] <= 'Z' {
if i > 0 {
out.WriteByte('-')
}
out.WriteByte(s[i] - 'A' + 'a')
} else {
out.WriteByte(s[i])
}
}
return out.String()
}
}
// ParseInputs reads multiple documents from `kubectl` output and checks with
// the schema. It also returns the list of unrecognized kinds as the second
// response.
//
// NOTE: This function only decodes a subset of the complete k8s
// ObjectMeta as identified by the fields in model.ConfigMeta. This
// would typically only be a problem if a user dumps an configuration
// object with kubectl and then re-ingests it.
func ParseInputs(inputs string) ([]model.Config, []IstioKind, error) {
var varr []model.Config
var others []IstioKind
reader := bytes.NewReader([]byte(inputs))
var empty = IstioKind{}
// We store configs as a YaML stream; there may be more than one decoder.
yamlDecoder := kubeyaml.NewYAMLOrJSONDecoder(reader, 512*1024)
for {
obj := IstioKind{}
err := yamlDecoder.Decode(&obj)
if err == io.EOF {
break
}
if err != nil {
return nil, nil, fmt.Errorf("cannot parse proto message: %v", err)
}
if reflect.DeepEqual(obj, empty) {
continue
}
schema, exists := model.IstioConfigTypes.GetByType(CamelCaseToKabobCase(obj.Kind))
if !exists {
log.Debugf("unrecognized type %v", obj.Kind)
others = append(others, obj)
continue
}
config, err := ConvertObject(schema, &obj, "")
if err != nil {
return nil, nil, fmt.Errorf("cannot parse proto message: %v", err)
}
if err := schema.Validate(config.Spec); err != nil {
return nil, nil, fmt.Errorf("configuration is invalid: %v", err)
}
varr = append(varr, *config)
}
return varr, others, nil
}