This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathserver.go
More file actions
176 lines (147 loc) · 5.12 KB
/
server.go
File metadata and controls
176 lines (147 loc) · 5.12 KB
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
169
170
171
172
173
174
175
176
// Copyright 2019 Istio Authors
//
// 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 (
"context"
"fmt"
"os"
"istio.io/operator/pkg/apis/istio/v1alpha2"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
//"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"
drm "github.com/openshift/cluster-network-operator/pkg/util/k8s"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"istio.io/operator/pkg/apis"
"istio.io/operator/pkg/controller"
"istio.io/operator/pkg/controller/istiocontrolplane"
"istio.io/pkg/ctrlz"
"istio.io/pkg/log"
)
// Should match deploy/service.yaml
const (
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
)
func serverCmd() *cobra.Command {
loggingOptions := log.DefaultOptions()
introspectionOptions := ctrlz.DefaultOptions()
serverCmd := &cobra.Command{
Use: "server",
Short: "Starts the Istio operator server",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := log.Configure(loggingOptions); err != nil {
log.Errorf("Unable to configure logging: %v", err)
}
if cs, err := ctrlz.Run(introspectionOptions, nil); err == nil {
defer cs.Close()
} else {
log.Errorf("Unable to initialize ControlZ: %v", err)
}
run()
return nil
},
}
loggingOptions.AttachCobraFlags(serverCmd)
introspectionOptions.AttachCobraFlags(serverCmd)
istiocontrolplane.AttachCobraFlags(serverCmd)
return serverCmd
}
// getWatchNamespace returns the namespace the operator should be watching for changes
func getWatchNamespace() (string, error) {
ns, found := os.LookupEnv("WATCH_NAMESPACE")
if !found {
return "", fmt.Errorf("WATCH_NAMESPACE must be set")
}
return ns, nil
}
// getLeaderElectionNamespace returns the namespace in which the leader election configmap will be created
func getLeaderElectionNamespace() (string, error) {
ns, found := os.LookupEnv("LEADER_ELECTION_NAMESPACE")
if !found {
return "", fmt.Errorf("LEADER_ELECTION_NAMESPACE must be set")
}
return ns, nil
}
func run() {
watchNS, err := getWatchNamespace()
if err != nil {
log.Fatalf("Failed to get watch namespace: %v", err)
}
leaderElectionNS, err := getLeaderElectionNamespace()
if err != nil {
log.Fatalf("Failed to get leader election namespace: %v", err)
}
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
log.Fatalf("Could not get apiserver config: %v", err)
}
// Create a new Cmd to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{
Namespace: watchNS,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
// Workaround for https://github.com/kubernetes-sigs/controller-runtime/issues/321
MapperProvider: drm.NewDynamicRESTMapper,
LeaderElection: true,
LeaderElectionNamespace: leaderElectionNS,
LeaderElectionID: "istio-operator-lock",
})
if err != nil {
log.Fatalf("Could not create a controller manager: %v", err)
}
log.Info("Registering Components.")
// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Fatalf("Could not add manager scheme: %v", err)
}
// Setup all Controllers
if err := controller.AddToManager(mgr); err != nil {
log.Fatalf("Could not add all controllers to operator manager: %v", err)
}
// setup webhooks
log.Info("setting up webhook server")
// Method 1
validatingHook := &webhook.Admission{
Handler: admission.HandlerFunc(func(ctx context.Context, req webhook.AdmissionRequest) webhook.AdmissionResponse {
log.Info("enterring admission handler!")
return webhook.Denied("Test only: none shall pass!")
}),
}
crv := mgr.GetWebhookServer()
crv.CertDir = "/tmp/k8s-webhook-server/serving-certs"
//crv.Port = 8443
crv.Register("/validate", validatingHook)
// method 2
//crv.Register("/validate-install-istio-io-v1alpha2-istiocontrolplane",
// &webhook.Admission{Handler: &iscpwebhook.IscpValidator{}})
// Method3(implement the validator inside v1alpha2.IstioControlPlane)
/* err = controllerruntime.NewWebhookManagedBy(mgr).
For(&v1alpha2.IstioControlPlane{}).
Complete()
if err != nil {
os.Exit(1)
}*/
log.Info("Starting the Cmd.")
// Start the Cmd
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Fatalf("Manager exited non-zero: %v", err)
}
}