This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
/
main.go
133 lines (108 loc) · 4.14 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Copyright 2018 SUSE LINUX GmbH, Nuernberg, Germany..
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 (
"flag"
"fmt"
"io"
"os"
"github.com/golang/glog"
"github.com/kubic-project/registries-operator/pkg/apis"
"github.com/kubic-project/registries-operator/pkg/controller"
"github.com/renstrom/dedent"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
utilflag "k8s.io/apiserver/pkg/util/flag"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
regcfg "github.com/kubic-project/registries-operator/pkg/config"
)
// Version is set from the build process
var Version string
// Build is set from the build process
var Build string
// newCmdManager runs the manager
func newCmdManager(out io.Writer) *cobra.Command {
var kubeconfigFile = ""
cmd := &cobra.Command{
Use: "manager",
Short: "Run the Kubic controller manager.",
Run: func(cmd *cobra.Command, args []string) {
var err error
glog.V(1).Infof("[kubic] getting a kubeconfig to talk to the API server")
if len(kubeconfigFile) > 0 {
glog.V(3).Infof("[kubic] setting KUBECONFIG to '%s'", kubeconfigFile)
os.Setenv("KUBECONFIG", kubeconfigFile)
}
kubeconfig, err := config.GetConfig()
kubeadmutil.CheckErr(err)
glog.V(1).Infof("[kubic] creating a new manager to provide shared dependencies and start components")
mgr, err := manager.New(kubeconfig, manager.Options{
//LeaderElection: true,
//LeaderElectionNamespace: metav1.NamespaceSystem,
})
kubeadmutil.CheckErr(err)
glog.V(1).Infof("[kubic] setting up the scheme for all the resources")
err = apis.AddToScheme(mgr.GetScheme())
kubeadmutil.CheckErr(err)
glog.V(1).Infof("[kubic] setting up all the controllers")
err = controller.AddToManager(mgr)
kubeadmutil.CheckErr(err)
glog.V(1).Infof("[kubic] starting the controller")
err = mgr.Start(signals.SetupSignalHandler())
kubeadmutil.CheckErr(err)
},
}
flagSet := cmd.PersistentFlags()
flagSet.StringVar(&kubeconfigFile, "kubeconfig", "", "Use this kubeconfig file for talking to the API server (not necessary when running in the kuberentes cluster).")
flagSet.StringVar(®cfg.DefaultPrefix, "prefix", regcfg.DefaultPrefix, "A prefix for all the resources created by the operator.")
flagSet.IntVar(®cfg.DefaultDeployNumReplicas, "replicas", regcfg.DefaultDeployNumReplicas, "Default number of replicas in the Dex Deployment.")
return cmd
}
func newCmdVersion(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version of registries-operator",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(out, "registries-operator version: %s (build: %s)\n", Version, Build)
},
}
return cmd
}
func main() {
pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// see https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
flag.CommandLine.Parse([]string{})
pflag.Set("logtostderr", "true")
cmds := &cobra.Command{
Use: "registries-operator",
Short: "registries-operator: a kubernetes operator for configuring images registries",
Long: dedent.Dedent(`
registries-operator: a kubernetes operator for configuring images registries.
`),
}
cmds.ResetFlags()
cmds.AddCommand(newCmdManager(os.Stdout))
cmds.AddCommand(newCmdVersion(os.Stdout))
err := cmds.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}