-
Notifications
You must be signed in to change notification settings - Fork 1
/
export.go
178 lines (145 loc) · 4.44 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
package export
// TODO: alpatel this will eventually make it into a library, I think
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
errorsutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd/api"
)
type ExportOptions struct {
configFlags *genericclioptions.ConfigFlags
ExportDir string
Context string
Namespace string
QPS float32
Burst int
genericclioptions.IOStreams
}
func (o *ExportOptions) Complete(c *cobra.Command, args []string) error {
// TODO: @alpatel
return nil
}
func (o *ExportOptions) Validate() error {
// TODO: @alpatel
return nil
}
func (o *ExportOptions) Run() error {
return o.run()
}
func NewExportCommand(streams genericclioptions.IOStreams) *cobra.Command {
o := &ExportOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
cmd := &cobra.Command{
Use: "export",
Short: "Export the namespace resources in an output directory",
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(c, args); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
if err := o.Run(); err != nil {
return err
}
return nil
},
}
addFlagsForOptions(o, cmd)
return cmd
}
func addFlagsForOptions(o *ExportOptions, cmd *cobra.Command) {
cmd.Flags().StringVar(&o.ExportDir, "export-dir", "export", "The path where files are to be exported")
cmd.Flags().StringVar(&o.Context, "context", "", "The kube context, if empty it will use the current context. If --namespace is set it will take precedence")
cmd.Flags().StringVar(&o.Namespace, "namespace", "", "The kube namespace to export.")
cmd.Flags().Float32VarP(&o.QPS, "qps", "q", 100, "Query Per Second Rate.")
cmd.Flags().IntVarP(&o.Burst, "burst", "b", 1000, "API Burst Rate.")
}
func (o *ExportOptions) run() error {
config := o.configFlags.ToRawKubeConfigLoader()
rawConfig, err := config.RawConfig()
if err != nil {
fmt.Printf("error in generating raw config")
os.Exit(1)
}
if o.Context == "" {
o.Context = rawConfig.CurrentContext
}
if o.Context == "" {
fmt.Printf("current kubecontext is empty and not kubecontext is specified")
os.Exit(1)
}
var currentContext *api.Context
contextName := ""
for name, ctx := range rawConfig.Contexts {
if name == o.Context {
currentContext = ctx
contextName = name
}
}
if currentContext == nil {
fmt.Printf("currentContext is nil\n")
os.Exit(1)
}
if o.Namespace == "" {
fmt.Printf("--namespace is empty, defaulting to current context namespace\n")
if currentContext.Namespace == "" {
fmt.Printf("current context %s namespace is empty, exiting\n", contextName)
os.Exit(1)
}
o.Namespace = currentContext.Namespace
}
fmt.Printf("current context is: %s\n", currentContext.AuthInfo)
// create export directory if it doesnt exist
err = os.MkdirAll(filepath.Join(o.ExportDir, "resources", o.Namespace), 0700)
switch {
case os.IsExist(err):
case err != nil:
fmt.Printf("error creating the resources directory: %#v", err)
os.Exit(1)
}
// create export directory if it doesnt exist
err = os.MkdirAll(filepath.Join(o.ExportDir, "failures", o.Namespace), 0700)
switch {
case os.IsExist(err):
case err != nil:
fmt.Printf("error creating the failures directory: %#v", err)
os.Exit(1)
}
discoveryclient, err := o.configFlags.ToDiscoveryClient()
if err != nil {
fmt.Printf("cannot create discovery client: %#v", err)
os.Exit(1)
}
// Always request fresh data from the server
discoveryclient.Invalidate()
restConfig, err := o.configFlags.ToRESTConfig()
restConfig.Burst = o.Burst
restConfig.QPS = o.QPS
if err != nil {
fmt.Printf("cannot create rest config: %#v", err)
}
dynamicClient := dynamic.NewForConfigOrDie(restConfig)
errs := []error{}
lists, err := discoveryclient.ServerPreferredNamespacedResources()
if err != nil {
fmt.Printf("unauthorized to get discovery service resources: %#v", err)
return err
}
resources, resourceErrs := resourceToExtract(o.Namespace, dynamicClient, lists)
for _, e := range errs {
fmt.Printf("error exporting resource: %#v\n", e)
}
errs = writeResources(resources, filepath.Join(o.ExportDir, "resources", o.Namespace))
for _, e := range errs {
fmt.Printf("error writing maniffest to file: %#v\n", e)
}
errs = writeErrors(resourceErrs, filepath.Join(o.ExportDir, "failures", o.Namespace))
return errorsutil.NewAggregate(errs)
}