|
| 1 | +/* |
| 2 | +Copyright 2023 The KusionStack Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "math/rand" |
| 22 | + _ "net/http/pprof" |
| 23 | + "os" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/spf13/pflag" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 29 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 30 | + _ "k8s.io/client-go/plugin/pkg/client/auth" |
| 31 | + "k8s.io/client-go/tools/leaderelection/resourcelock" |
| 32 | + "k8s.io/klog/v2" |
| 33 | + "k8s.io/klog/v2/klogr" |
| 34 | + ctrl "sigs.k8s.io/controller-runtime" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/healthz" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 37 | + |
| 38 | + appcontroller "github.com/KusionStack/kridge/e2e/customoperator/app/controller" |
| 39 | +) |
| 40 | + |
| 41 | +var ( |
| 42 | + scheme = runtime.NewScheme() |
| 43 | + setupLog = ctrl.Log.WithName("setup") |
| 44 | +) |
| 45 | + |
| 46 | +func init() { |
| 47 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 48 | + //+kubebuilder:scaffold:scheme |
| 49 | +} |
| 50 | + |
| 51 | +// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch;delete |
| 52 | + |
| 53 | +func main() { |
| 54 | + |
| 55 | + klog.InitFlags(nil) |
| 56 | + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) |
| 57 | + pflag.Parse() |
| 58 | + ctrl.SetLogger(klogr.New()) |
| 59 | + |
| 60 | + rand.Seed(time.Now().UnixNano()) |
| 61 | + |
| 62 | + cfg := ctrl.GetConfigOrDie() |
| 63 | + cfg.UserAgent = "testapp" |
| 64 | + |
| 65 | + mgr, err := manager.New(cfg, ctrl.Options{ |
| 66 | + Scheme: scheme, |
| 67 | + LeaderElection: true, |
| 68 | + LeaderElectionID: "testapp", |
| 69 | + LeaderElectionNamespace: os.Getenv("POD_NAMESPACE"), |
| 70 | + LeaderElectionResourceLock: resourcelock.LeasesResourceLock, |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + setupLog.Error(err, "unable to start manager") |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + ctx := ctrl.SetupSignalHandler() |
| 78 | + |
| 79 | + if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { |
| 80 | + setupLog.Error(err, "unable to set up health check") |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { |
| 84 | + setupLog.Error(err, "unable to set up ready check") |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + go func() { |
| 89 | + setupLog.Info("wait webhook ready") |
| 90 | + |
| 91 | + if err = (&appcontroller.PodReconciler{ |
| 92 | + Client: mgr.GetClient(), |
| 93 | + }).SetupWithManager(mgr); err != nil { |
| 94 | + setupLog.Error(err, "unable to create controller", "controller", "Recorder") |
| 95 | + os.Exit(1) |
| 96 | + } |
| 97 | + }() |
| 98 | + |
| 99 | + go func() { |
| 100 | + <-time.After(20 * time.Second) |
| 101 | + appcontroller.Checker(ctx, mgr.GetClient()) |
| 102 | + }() |
| 103 | + setupLog.Info("starting manager") |
| 104 | + if err := mgr.Start(ctx); err != nil { |
| 105 | + setupLog.Error(err, "problem running manager") |
| 106 | + os.Exit(1) |
| 107 | + } |
| 108 | +} |
0 commit comments