forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (97 loc) · 3.3 KB
/
main.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 main
import (
"fmt"
"io/ioutil"
"os"
"github.com/urfave/cli"
"go.uber.org/zap"
"github.com/kubermatic/kubermatic/api/pkg/controller/operator/common"
operatorv1alpha1 "github.com/kubermatic/kubermatic/api/pkg/crd/operator/v1alpha1"
"github.com/kubermatic/kubermatic/api/pkg/log"
yamlutil "github.com/kubermatic/kubermatic/api/pkg/util/yaml"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
var logger *zap.SugaredLogger
func main() {
app := cli.NewApp()
app.Name = "Kubermatic Operator Utility"
app.Version = "v1.0.0"
defaultLogFormat := log.FormatConsole
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "log-debug",
Usage: "Enables more verbose logging",
},
cli.GenericFlag{
Name: "log-format",
Value: &defaultLogFormat,
Usage: fmt.Sprintf("Use one of [%v] to change the log output", log.AvailableFormats),
},
}
app.Commands = append([]cli.Command{
{
Name: "defaults",
Usage: "Outputs a KubermaticConfiguration with all default values, optionally applied to a given configuration manifest (YAML)",
Action: defaultsAction,
ArgsUsage: "[MANIFEST_FILE]",
},
}, extraCommands()...)
// setup logging
app.Before = func(c *cli.Context) error {
format := c.GlobalGeneric("log-format").(*log.Format)
rawLog := log.New(c.GlobalBool("log-debug"), *format)
logger = rawLog.Sugar()
return nil
}
err := app.Run(os.Args)
// Only log failures when the logger has been setup, otherwise
// we know it's been a CLI parsing failure and the cli package
// has already output the error and printed the usage hints.
if err != nil && logger != nil {
logger.Fatalw("Failed to run command", zap.Error(err))
}
}
func defaultsAction(ctx *cli.Context) error {
config := &operatorv1alpha1.KubermaticConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "operator.kubermatic.io/v1alpha1",
Kind: "KubermaticConfiguration",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kubermatic",
Namespace: "kubermatic",
},
}
configFile := ctx.Args().First()
if configFile != "" {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to read file %s: %v", configFile, err), 1)
}
if err := yaml.Unmarshal(content, &config); err != nil {
return cli.NewExitError(fmt.Errorf("failed to parse file %s as YAML: %v", configFile, err), 1)
}
}
logger := zap.NewNop().Sugar()
defaulted, err := common.DefaultConfiguration(config, logger)
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to create KubermaticConfiguration: %v", err), 1)
}
if err := yamlutil.Encode(defaulted, os.Stdout); err != nil {
return cli.NewExitError(fmt.Errorf("failed to create YAML: %v", err), 1)
}
return nil
}