diff --git a/cmd/operator/app/command.go b/cmd/operator/app/command.go index 15df11b..e41e6c3 100644 --- a/cmd/operator/app/command.go +++ b/cmd/operator/app/command.go @@ -112,10 +112,14 @@ func NewOperatorCommand() *cobra.Command { } if o.EnableAPIServer { - server := apiserver.NewServer(mgr.GetClient(), &apiserver.Options{ + server, err := apiserver.NewServer(mgr, &apiserver.Options{ Port: o.APIServerPort, EnablePodMetrics: o.EnablePodMetrics, }) + if err != nil { + setupLog.Error(err, "unable to create API server") + os.Exit(1) + } go func() { if err := server.Run(); err != nil { diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 8155b5d..69a13b9 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -26,6 +26,7 @@ import ( "k8s.io/klog/v2" podmetricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/manager" greptimev1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" "github.com/GreptimeTeam/greptimedb-operator/controllers/common" @@ -143,12 +144,18 @@ type Response struct { } // NewServer creates a new Server with the given client and options. -func NewServer(client client.Client, opts *Options) *Server { +func NewServer(mgr manager.Manager, opts *Options) (*Server, error) { + // Create a NoCache client to avoid watching. + client, err := client.New(mgr.GetConfig(), client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()}) + if err != nil { + return nil, err + } + return &Server{ Client: client, port: opts.Port, enablePodMetrics: opts.EnablePodMetrics, - } + }, nil } // Run starts the HTTP service and listens on the specified port. diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index b686490..74f26b8 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -40,7 +40,10 @@ const ( func TestHTTPGetService(t *testing.T) { // Start the HTTP service. - svc := NewServer(&FakeClient{}, &Options{Port: TestPort}) + svc := &Server{ + Client: &FakeClient{}, + port: TestPort, + } go func() { if err := svc.Run(); err != nil { t.Errorf("failed to start HTTP service: %v", err)