forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
master.go
167 lines (141 loc) · 5.24 KB
/
master.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
package validate
import (
"errors"
"fmt"
"io"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
configapilatest "github.com/openshift/origin/pkg/cmd/server/api/latest"
"github.com/openshift/origin/pkg/cmd/server/api/validation"
)
const (
ValidateMasterConfigRecommendedName = "master-config"
validateMasterConfigDeprecationMessage = `This command is deprecated and will be removed. Use 'oc adm diagnostics MasterConfigCheck --master-config=path/to/config.yaml' instead.`
)
var (
validateMasterConfigLong = templates.LongDesc(`
Validate the configuration file for a master server.
This command validates that a configuration file intended to be used for a master server is valid.`)
validateMasterConfigExample = templates.Examples(`
# Validate master server configuration file
%s openshift.local.config/master/master-config.yaml`)
)
type ValidateMasterConfigOptions struct {
// MasterConfigFile is the location of the config file to be validated
MasterConfigFile string
// Out is the writer to write output to
Out io.Writer
}
// NewCommandValidateMasterConfig provides a CLI handler for the `validate all-in-one` command
func NewCommandValidateMasterConfig(name, fullName string, out io.Writer) *cobra.Command {
options := &ValidateMasterConfigOptions{
Out: out,
}
cmd := &cobra.Command{
Use: fmt.Sprintf("%s SOURCE", name),
Short: "Validate the configuration file for a master server",
Long: validateMasterConfigLong,
Example: fmt.Sprintf(validateMasterConfigExample, fullName),
Deprecated: validateMasterConfigDeprecationMessage,
Run: func(c *cobra.Command, args []string) {
if err := options.Complete(args); err != nil {
cmdutil.CheckErr(cmdutil.UsageErrorf(c, err.Error()))
}
ok, err := options.Run()
cmdutil.CheckErr(err)
if !ok {
fmt.Fprintf(options.Out, "FAILURE: Validation failed for file: %s\n", options.MasterConfigFile)
os.Exit(1)
}
fmt.Fprintf(options.Out, "SUCCESS: Validation succeeded for file: %s\n", options.MasterConfigFile)
},
}
return cmd
}
func (o *ValidateMasterConfigOptions) Complete(args []string) error {
if len(args) != 1 {
return errors.New("exactly one source file is required")
}
o.MasterConfigFile = args[0]
return nil
}
// Run runs the master config validation and returns the result of the validation as a boolean as well as any errors
// that occurred trying to validate the file
func (o *ValidateMasterConfigOptions) Run() (bool, error) {
masterConfig, err := configapilatest.ReadAndResolveMasterConfig(o.MasterConfigFile)
if err != nil {
return true, err
}
results := validation.ValidateMasterConfig(masterConfig, nil)
writer := tabwriter.NewWriter(o.Out, minColumnWidth, tabWidth, padding, padchar, flags)
err = prettyPrintValidationResults(results, writer)
if err != nil {
return len(results.Errors) == 0, fmt.Errorf("could not print results: %v", err)
}
writer.Flush()
return len(results.Errors) == 0, nil
}
const (
minColumnWidth = 4
tabWidth = 4
padding = 2
padchar = byte(' ')
flags = 0
validationErrorHeadings = "ERROR\tFIELD\tVALUE\tDETAILS\n"
validationWarningHeadings = "WARNING\tFIELD\tVALUE\tDETAILS\n"
)
// prettyPrintValidationResults prints the contents of the ValidationResults into the buffer of a tabwriter.Writer.
// The writer must be Flush()ed after calling this to write the buffered data.
func prettyPrintValidationResults(results validation.ValidationResults, writer *tabwriter.Writer) error {
if len(results.Errors) > 0 {
fmt.Fprintf(writer, "VALIDATION ERRORS:\t\t\t\n")
err := prettyPrintValidationErrorList(validationErrorHeadings, results.Errors, writer)
if err != nil {
return err
}
}
if len(results.Warnings) > 0 {
fmt.Fprintf(writer, "VALIDATION WARNINGS:\t\t\t\n")
err := prettyPrintValidationErrorList(validationWarningHeadings, results.Warnings, writer)
if err != nil {
return err
}
}
return nil
}
// prettyPrintValidationErrorList prints the contents of the ValidationErrorList into the buffer of a tabwriter.Writer.
// The writer must be Flush()ed after calling this to write the buffered data.
func prettyPrintValidationErrorList(headings string, validationErrors field.ErrorList, writer *tabwriter.Writer) error {
if len(validationErrors) > 0 {
fmt.Fprintf(writer, headings)
for _, err := range validationErrors {
err := prettyPrintValidationError(err, writer)
if err != nil {
return err
}
}
}
return nil
}
// prettyPrintValidationError prints the contents of the ValidationError into the buffer of a tabwriter.Writer.
// The writer must be Flush()ed after calling this to write the buffered data.
func prettyPrintValidationError(validationError *field.Error, writer *tabwriter.Writer) error {
_, printError := fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n",
toString(validationError.Type),
validationError.Field,
toString(validationError.BadValue),
validationError.Detail)
return printError
}
const missingValue = "<none>"
func toString(v interface{}) string {
value := fmt.Sprintf("%v", v)
if len(value) == 0 {
value = missingValue
}
return value
}