-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
168 lines (144 loc) · 4.95 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
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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"runtime"
"github.com/jenkins-x/sso-operator/pkg/dex"
"github.com/jenkins-x/sso-operator/pkg/kubernetes"
"github.com/jenkins-x/sso-operator/pkg/operator"
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
sdkVersion "github.com/operator-framework/operator-sdk/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
watchNamespaceEnv = "WATCH_NAMESPACE"
operatorNamespaceEnv = "OPERATOR_NAMESPACE"
port = "8080"
)
// OperatorOptions holds the command options for SSO operator
type OperatorOptions struct {
Namespace string
WatchNamespace string
DexGrpcHostAndPort string
DexGrpcClientCrt string
DexGrpcClientKey string
DexGrpcClientCA string
ClusterRoleName string
}
func printVersion(namespace string, watchNamespace string) {
logrus.Infof("Go Version: %s", runtime.Version())
logrus.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
logrus.Infof("operator-sdk Version: %v", sdkVersion.Version)
logrus.Infof("operator namespace: %s", namespace)
if watchNamespace == "" {
logrus.Info("operator watching entire cluster")
} else {
logrus.Infof("operator watching namespace: %s", watchNamespace)
}
}
func handleLiveness() {
logrus.Infof("Liveness probe listening on: %s", port)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
logrus.Debug("ping")
})
err := http.ListenAndServe(":"+port, nil) // #nosec
if err != nil {
logrus.Errorf("failed to start health probe: %v\n", err)
}
}
// Run starts the SSO operator
func (o *OperatorOptions) Run() {
namespace := o.Namespace
if namespace == "" {
namespace = os.Getenv(operatorNamespaceEnv)
}
watchNamespace := o.WatchNamespace
if watchNamespace == "" {
watchNamespace = os.Getenv(watchNamespaceEnv)
}
printVersion(namespace, watchNamespace)
// validate the command line options
err := o.Validate()
if err != nil {
logrus.Errorf("invalid options: %v", err)
os.Exit(2)
}
opts := &dex.Options{
HostAndPort: o.DexGrpcHostAndPort,
ClientCrt: o.DexGrpcClientCrt,
ClientKey: o.DexGrpcClientKey,
ClientCA: o.DexGrpcClientCA,
}
dexClient, err := dex.NewClient(opts)
if err != nil {
logrus.Errorf("failed to crate dex client: %v", err)
os.Exit(2)
}
logrus.Infof("Connected to Dex gRPC server: %s", o.DexGrpcHostAndPort)
// Register the CRDs
apiclient, err := kubernetes.GetAPIExtensionsClient()
if err != nil {
logrus.Errorf("failed to register the k8s API extensions client: %v", err)
os.Exit(2)
}
err = kubernetes.RegisterSSOCRD(apiclient)
if err != nil {
logrus.Errorf("failed to register the SSO CRD: %v", err)
os.Exit(2)
}
// configure the operator
sdk.Watch("jenkins.io/v1", "SSO", watchNamespace, 5)
handler, err := operator.NewHandler(dexClient, namespace, o.ClusterRoleName)
if err != nil {
logrus.Errorf("failed to create the operator handler: %v", err)
os.Exit(2)
}
sdk.Handle(handler)
// start the health probe
go handleLiveness()
// start the operator
sdk.Run(context.TODO())
}
// Validate validates the provided command options
func (o *OperatorOptions) Validate() error {
if o.DexGrpcHostAndPort == "" {
return errors.New("dex gRPC server host and port is empty")
}
if _, err := os.Stat(o.DexGrpcClientCrt); os.IsNotExist(err) {
return fmt.Errorf("provided dex gRPC client cert file '%s' does not exist", o.DexGrpcClientCrt)
}
if _, err := os.Stat(o.DexGrpcClientKey); os.IsNotExist(err) {
return fmt.Errorf("provided dex gRPC client key file '%s' does not exists", o.DexGrpcClientKey)
}
if _, err := os.Stat(o.DexGrpcClientCA); os.IsNotExist(err) {
return fmt.Errorf("provided dex gRPC CA cert file '%s' does not exists", o.DexGrpcClientCA)
}
return nil
}
func commandRoot() *cobra.Command {
options := &OperatorOptions{}
rootCmd := &cobra.Command{
Use: "sso-operator",
Run: func(cmd *cobra.Command, args []string) {
options.Run()
},
}
rootCmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace where the operator where the operator is deployed")
rootCmd.Flags().StringVarP(&options.WatchNamespace, "watch-namespace", "", "", "Namespace where the operator will watch for resources (leave empty to watch the entire cluster)")
rootCmd.Flags().StringVarP(&options.DexGrpcHostAndPort, "dex-grpc-host-port", "", "", "Host and port of Dex gRPC server")
rootCmd.Flags().StringVarP(&options.DexGrpcClientCrt, "dex-grpc-client-crt", "", "", "Certificate for Dex gRPC client")
rootCmd.Flags().StringVarP(&options.DexGrpcClientKey, "dex-grpc-client-key", "", "", "Key for Dex gRPC client")
rootCmd.Flags().StringVarP(&options.DexGrpcClientCA, "dex-grpc-client-ca", "", "", "CA certificate for Dex gRPC client")
rootCmd.Flags().StringVarP(&options.ClusterRoleName, "cluster-role-name", "", "", "Cluster role name which has the required permissions for operator")
return rootCmd
}
func main() {
if err := commandRoot().Execute(); err != nil {
logrus.Error(err)
os.Exit(2)
}
}