This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
/
reader.go
179 lines (146 loc) · 5.54 KB
/
reader.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
/*
Copyright 2020 GramLabs, Inc.
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 commander
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
redskyappsv1alpha1 "github.com/thestormforge/optimize-controller/api/apps/v1alpha1"
redskyv1alpha1 "github.com/thestormforge/optimize-controller/api/v1alpha1"
redskyv1beta1 "github.com/thestormforge/optimize-controller/api/v1beta1"
"github.com/thestormforge/optimize-controller/internal/controller"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// ResourceReader helps properly decode Kubernetes resources on the CLI. It is meant to be a
// lighter weight alternative to the cli-runtime resource.Builder.
type ResourceReader struct {
Scheme *runtime.Scheme
PreferredVersion runtime.GroupVersioner
}
// NewResourceReader returns a new resource reader for the supplied byte stream.
func NewResourceReader() *ResourceReader {
rr := &ResourceReader{
Scheme: runtime.NewScheme(),
PreferredVersion: OnlyVersion,
}
// Always add our types
_ = redskyv1beta1.AddToScheme(rr.Scheme)
_ = redskyv1alpha1.AddToScheme(rr.Scheme)
_ = redskyappsv1alpha1.AddToScheme(rr.Scheme)
// Allow single experiments to target an experiment list
_ = addExperimentListConversions(rr.Scheme)
return rr
}
// ReadInto decodes the supplied byte stream into the target runtime object. The default values
// and type information of the target object will be populated.
func (r *ResourceReader) ReadInto(reader io.ReadCloser, target runtime.Object) error {
// Determine the GVK for the type we are supposed to be populating
gvk, err := r.objectKind(target)
if err != nil {
return err
}
mt := mediaType(reader)
decoder, err := r.decoder(mt)
if err != nil {
return err
}
// TODO For lists we should consider yaml.NewDocumentDecoder(reader) so we can read a stream
// Read in the raw bytes
defer func() { _ = reader.Close() }()
data, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
// Decode the raw data
obj, _, err := decoder.Decode(data, &gvk, target)
if err != nil {
return err
}
// If decode returned an object of a different type we should try to force the conversion
if obj != target {
if err := r.Scheme.Convert(obj, target, r.PreferredVersion); err != nil {
return err
}
}
// Fill in the default values for the target
target.GetObjectKind().SetGroupVersionKind(gvk)
r.Scheme.Default(target)
return nil
}
func (r *ResourceReader) objectKind(obj runtime.Object) (schema.GroupVersionKind, error) {
gvks, _, err := r.Scheme.ObjectKinds(obj)
if err != nil {
return schema.GroupVersionKind{}, err
}
gvk, ok := r.PreferredVersion.KindForGroupVersionKinds(gvks)
if !ok {
// Your code must supply a GroupVersioner to disambiguate this case
panic("read destination type is ambiguous in scheme")
}
return gvk, nil
}
func (r *ResourceReader) decoder(mediaType string) (runtime.Decoder, error) {
// Create the conversion serializer with our negotiation logic in it
cs := controller.NewConversionSerializer(r.Scheme)
info, ok := runtime.SerializerInfoForMediaType(cs.SupportedMediaTypes(), mediaType)
if !ok {
return nil, fmt.Errorf("could not find serializer for %s", mediaType)
}
return cs.DecoderToVersion(info.Serializer, r.PreferredVersion), nil
}
// mediaType returns the presumed media type for the supplied read closer.
func mediaType(r io.ReadCloser) string {
// For now just assume YAML unless we got a JSON file
mt := runtime.ContentTypeYAML
if f, ok := r.(*os.File); ok {
switch filepath.Ext(f.Name()) {
case "json":
mt = runtime.ContentTypeJSON
}
}
return mt
}
// OnlyVersion is a group version that only resolves if there is a single possible kind
var OnlyVersion runtime.GroupVersioner = onlyVersion{}
type onlyVersion struct{}
func (onlyVersion) Identifier() string { return "only" }
func (onlyVersion) KindForGroupVersionKinds(kinds []schema.GroupVersionKind) (schema.GroupVersionKind, bool) {
if len(kinds) == 1 {
return kinds[0], true
}
return schema.GroupVersionKind{}, false
}
// addExperimentListConversions adds conversions from a single experiment to a list of experiments; for example
// when a command can handle a list but the user only supplies a single experiment.
func addExperimentListConversions(s *runtime.Scheme) error {
// Convert from a single experiment to a list of experiments
if err := s.AddConversionFunc((*redskyv1beta1.Experiment)(nil), (*redskyv1beta1.ExperimentList)(nil), func(a, b interface{}, scope conversion.Scope) error {
b.(*redskyv1beta1.ExperimentList).Items = []redskyv1beta1.Experiment{*a.(*redskyv1beta1.Experiment)}
return nil
}); err != nil {
return err
}
// Convert from a single v1alpha1 experiment to a list of experiments
if err := s.AddConversionFunc((*redskyv1alpha1.Experiment)(nil), (*redskyv1beta1.ExperimentList)(nil), func(a, b interface{}, scope conversion.Scope) error {
l := b.(*redskyv1beta1.ExperimentList)
l.Items = make([]redskyv1beta1.Experiment, 1)
return scope.Convert(a, &l.Items[0], scope.Flags())
}); err != nil {
return err
}
return nil
}