/
start.go
155 lines (130 loc) · 4.66 KB
/
start.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
/*
Copyright AppsCode Inc. and 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 server
import (
"context"
"fmt"
"io"
"net"
uiapi "go.openviz.dev/apimachinery/apis/ui/v1alpha1"
"go.openviz.dev/grafana-tools/pkg/apiserver"
"github.com/spf13/pflag"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apiserver/pkg/endpoints/openapi"
"k8s.io/apiserver/pkg/features"
genericapiserver "k8s.io/apiserver/pkg/server"
genericoptions "k8s.io/apiserver/pkg/server/options"
"k8s.io/apiserver/pkg/util/feature"
"kmodules.xyz/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
const defaultEtcdPathPrefix = "/registry/k8s.appscode.com"
// UIServerOptions contains state for master/api server
type UIServerOptions struct {
RecommendedOptions *genericoptions.RecommendedOptions
ExtraOptions *ExtraOptions
StdOut io.Writer
StdErr io.Writer
}
// NewUIServerOptions returns a new UIServerOptions
func NewUIServerOptions(out, errOut io.Writer) *UIServerOptions {
_ = feature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", features.APIPriorityAndFairness))
o := &UIServerOptions{
RecommendedOptions: genericoptions.NewRecommendedOptions(
defaultEtcdPathPrefix,
apiserver.Codecs.LegacyCodec(
uiapi.SchemeGroupVersion,
),
),
ExtraOptions: NewExtraOptions(),
StdOut: out,
StdErr: errOut,
}
o.RecommendedOptions.Etcd = nil
o.RecommendedOptions.Admission = nil
return o
}
func (o UIServerOptions) AddFlags(fs *pflag.FlagSet) {
o.RecommendedOptions.AddFlags(fs)
o.ExtraOptions.AddFlags(fs)
}
// Validate validates UIServerOptions
func (o UIServerOptions) Validate(args []string) error {
var errors []error
errors = append(errors, o.RecommendedOptions.Validate()...)
errors = append(errors, o.ExtraOptions.Validate()...)
return utilerrors.NewAggregate(errors)
}
// Complete fills in fields required to have valid data
func (o *UIServerOptions) Complete() error {
return nil
}
// Config returns config for the api server given UIServerOptions
func (o *UIServerOptions) Config() (*apiserver.Config, error) {
// TODO have a "real" external address
if err := o.RecommendedOptions.SecureServing.MaybeDefaultWithSelfSignedCerts("localhost", nil, []net.IP{net.ParseIP("127.0.0.1")}); err != nil {
return nil, fmt.Errorf("error creating self-signed certificates: %v", err)
}
serverConfig := genericapiserver.NewRecommendedConfig(apiserver.Codecs)
if err := o.RecommendedOptions.ApplyTo(serverConfig); err != nil {
return nil, err
}
// Fixes https://github.com/Azure/AKS/issues/522
clientcmd.Fix(serverConfig.ClientConfig)
serverConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(
uiapi.GetOpenAPIDefinitions,
openapi.NewDefinitionNamer(apiserver.Scheme))
serverConfig.OpenAPIConfig.Info.Title = "grafana-webhook-server"
serverConfig.OpenAPIConfig.Info.Version = "v0.0.1"
serverConfig.OpenAPIV3Config = genericapiserver.DefaultOpenAPIV3Config(
uiapi.GetOpenAPIDefinitions,
openapi.NewDefinitionNamer(apiserver.Scheme))
serverConfig.OpenAPIV3Config.Info.Title = "grafana-webhook-server"
serverConfig.OpenAPIV3Config.Info.Version = "v0.0.1"
extraConfig := apiserver.ExtraConfig{
ClientConfig: serverConfig.ClientConfig,
}
if err := o.ExtraOptions.ApplyTo(&extraConfig); err != nil {
return nil, err
}
config := &apiserver.Config{
GenericConfig: serverConfig,
ExtraConfig: extraConfig,
}
return config, nil
}
// RunUIServer starts a new GrafanaOperator given UIServerOptions
func (o UIServerOptions) RunUIServer(ctx context.Context) error {
config, err := o.Config()
if err != nil {
return err
}
server, err := config.Complete().New(ctx)
if err != nil {
return err
}
server.GenericAPIServer.AddPostStartHookOrDie("start-ui-server-informers", func(context genericapiserver.PostStartHookContext) error {
config.GenericConfig.SharedInformerFactory.Start(context.StopCh)
return nil
})
err = server.Manager.Add(manager.RunnableFunc(func(ctx context.Context) error {
return server.GenericAPIServer.PrepareRun().Run(ctx.Done())
}))
if err != nil {
return err
}
setupLog := log.Log.WithName("setup")
setupLog.Info("starting manager")
return server.Manager.Start(ctx)
}