-
Notifications
You must be signed in to change notification settings - Fork 7
/
runner.go
133 lines (106 loc) · 2.49 KB
/
runner.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
package apps
import (
"context"
"io"
"io/ioutil"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/kubectl-gs/pkg/app"
"github.com/giantswarm/kubectl-gs/pkg/commonconfig"
)
const (
defaultNamespace = metav1.NamespaceDefault
)
type runner struct {
flag *flag
logger micrologger.Logger
service app.Interface
stdout io.Writer
stderr io.Writer
}
func (r *runner) Run(cmd *cobra.Command, args []string) error {
ctx := context.Background()
err := r.flag.Validate()
if err != nil {
return microerror.Mask(err)
}
err = r.run(ctx, cmd, args)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) error {
var err error
namespace, _, err := r.flag.config.ToRawKubeConfigLoader().Namespace()
if err != nil {
return microerror.Mask(err)
}
// If the namespace is empty, set it to "default".
if namespace == "" {
namespace = defaultNamespace
}
// BUT if we want all namespaces, set it to 'metav1.NamespaceAll', aka ""
// again so the client gets all namespaces.
if r.flag.AllNamespaces {
namespace = metav1.NamespaceAll
}
labelSelector := r.flag.LabelSelector
valuesSchemaFilePath := r.flag.ValuesSchemaFile
var valuesSchema string
if valuesSchemaFilePath != "" {
valuesSchemaFile, err := ioutil.ReadFile(valuesSchemaFilePath)
if err != nil {
return microerror.Mask(err)
}
valuesSchema = string(valuesSchemaFile)
}
config := commonconfig.New(r.flag.config)
{
err = r.getService(config)
if err != nil {
return microerror.Mask(err)
}
}
var results app.ValidationResults
{
options := app.ValidateOptions{}
{
if len(args) > 0 {
options.Name = args[0]
}
options.Namespace = namespace
options.LabelSelector = labelSelector
options.ValuesSchema = valuesSchema
}
results, err = r.service.Validate(ctx, options)
if err != nil {
return microerror.Mask(err)
}
}
err = r.printOutput(results)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *runner) getService(config *commonconfig.CommonConfig) error {
if r.service != nil {
return nil
}
client, err := config.GetClient(r.logger)
if err != nil {
return microerror.Mask(err)
}
serviceConfig := app.Config{
Client: client,
Logger: r.logger,
}
r.service, err = app.New(serviceConfig)
if err != nil {
return microerror.Mask(err)
}
return nil
}