This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
export.go
248 lines (224 loc) · 6.34 KB
/
export.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package export
import (
"encoding/json"
"fmt"
"strings"
"github.com/rancher/rio/cli/pkg/clicontext"
clitypes "github.com/rancher/rio/cli/pkg/types"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/riofile"
"github.com/rancher/wrangler/pkg/gvk"
name "github.com/rancher/wrangler/pkg/name"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
type Export struct {
Format string `desc:"Specify output format, yaml/json. Defaults to yaml" default:"yaml"`
Riofile bool `desc:"Export riofile format. (example: rio export --riofile namespace/default)"`
}
func (e *Export) Run(ctx *clicontext.CLIContext) error {
output := &strings.Builder{}
var (
svcs []*riov1.Service
other []runtime.Object
)
for _, arg := range ctx.CLI.Args() {
r, err := ctx.ByID(arg)
if err != nil {
return err
}
switch r.Type {
case clitypes.ServiceType:
svc := r.Object.(*riov1.Service)
svcs = append(svcs, svc)
case clitypes.NamespaceType:
ns := r.Object.(*corev1.Namespace)
objects, err := collectObjectsFromNs(ctx, ns.Name)
if err != nil {
return err
}
if err := exportObjects(objects, output, e.Riofile, e.Format); err != nil {
return err
}
case clitypes.StackType:
stack := r.Object.(*riov1.Stack)
objects, err := collectObjectsFromStack(ctx, stack)
if err != nil {
return err
}
if err := exportObjects(objects, output, e.Riofile, e.Format); err != nil {
return err
}
default:
other = append(other, r.Object)
}
}
if len(other) > 0 {
if err := exportObjects(other, output, e.Riofile, e.Format); err != nil {
return err
}
}
if len(svcs) > 0 {
var objects []runtime.Object
for _, svc := range svcs {
cms, err := configmapsFromService(ctx, *svc)
if err != nil {
return err
}
objects = append(objects, cms...)
}
for _, svc := range svcs {
objects = append(objects, svc)
}
if err := exportObjects(objects, output, e.Riofile, e.Format); err != nil {
return err
}
}
fmt.Println(output.String())
return nil
}
func collectObjectsFromStack(ctx *clicontext.CLIContext, stack *riov1.Stack) ([]runtime.Object, error) {
var objects []runtime.Object
client, err := dynamic.NewForConfig(ctx.Config.RestConfig)
if err != nil {
return nil, err
}
for _, gvk := range stack.Spec.AdditionalGroupVersionKinds {
gvr := schema.GroupVersionResource{
Group: gvk.Group,
Version: gvk.Version,
Resource: strings.ToLower(name.GuessPluralName(gvk.Kind)),
}
resourceClient := client.Resource(gvr)
ul, err := resourceClient.Namespace(stack.Namespace).List(metav1.ListOptions{
LabelSelector: "rio.cattle.io/stack=" + stack.Name,
})
if err != nil {
return nil, err
}
for _, item := range ul.Items {
if item.GroupVersionKind().Group == "rio.cattle.io" {
data, err := item.MarshalJSON()
if err != nil {
return nil, err
}
if item.GetKind() == "Service" {
svc := riov1.Service{}
if err := json.Unmarshal(data, &svc); err != nil {
return nil, err
}
objects = append(objects, &svc)
} else if item.GetKind() == "ExternalService" {
es := &riov1.ExternalService{}
if err := json.Unmarshal(data, es); err != nil {
return nil, err
}
objects = append(objects, es)
} else if item.GetKind() == "ConfigMap" {
cm := &corev1.ConfigMap{}
if err := json.Unmarshal(data, cm); err != nil {
return nil, err
}
objects = append(objects, cm)
} else if item.GetKind() == "Router" {
rt := &riov1.Router{}
if err := json.Unmarshal(data, rt); err != nil {
return nil, err
}
objects = append(objects, rt)
}
} else {
// From here down we only want to export non-rio created objects which would already be exported above
stackObj := true
for k, v := range item.GetAnnotations() {
if stackObj == true && k == "objectset.rio.cattle.io/owner-gvk" && v != "rio.cattle.io/v1, Kind=Stack" {
stackObj = false
break
}
}
if stackObj == true {
objects = append(objects, item.DeepCopyObject())
}
}
}
}
return objects, nil
}
func collectObjectsFromNs(ctx *clicontext.CLIContext, ns string) ([]runtime.Object, error) {
var objects []runtime.Object
services, err := ctx.Rio.Services(ns).List(metav1.ListOptions{})
if err != nil {
return objects, err
}
for i := range services.Items {
objects = append(objects, &services.Items[i])
}
for i := range services.Items {
configs, err := configmapsFromService(ctx, services.Items[i])
if err != nil {
return objects, err
}
objects = append(objects, configs...)
}
externalServices, err := ctx.Rio.ExternalServices(ns).List(metav1.ListOptions{})
if err != nil {
return objects, err
}
for i := range externalServices.Items {
objects = append(objects, &externalServices.Items[i])
}
routers, err := ctx.Rio.Routers(ns).List(metav1.ListOptions{})
if err != nil {
return objects, err
}
for i := range routers.Items {
objects = append(objects, &routers.Items[i])
}
return objects, nil
}
func configmapsFromService(ctx *clicontext.CLIContext, svc riov1.Service) ([]runtime.Object, error) {
var objects []runtime.Object
for _, cm := range svc.Spec.Configs {
configMap, err := ctx.Core.ConfigMaps(svc.Namespace).Get(cm.Name, metav1.GetOptions{})
if err != nil {
return objects, err
}
if err := gvk.Set(configMap); err != nil {
return objects, err
}
objects = append(objects, configMap)
}
return objects, nil
}
func exportObjects(objects []runtime.Object, output *strings.Builder, riofileFormat bool, format string) error {
if !riofileFormat {
return exportObjectsNative(objects, output, format)
}
return exportObjectStack(objects, output)
}
func exportObjectStack(objects []runtime.Object, output *strings.Builder) error {
content, err := riofile.Render(objects)
if err != nil {
return err
}
output.Write(content)
output.Write([]byte("\n"))
return nil
}
func exportObjectsNative(objects []runtime.Object, output *strings.Builder, format string) error {
for _, obj := range objects {
result, err := riofile.ObjToYaml(obj, format)
if err != nil {
return err
}
output.WriteString(result)
output.WriteString("\n")
if format != "json" {
output.WriteString("---\n")
}
}
return nil
}