Skip to content

Commit

Permalink
refactor: use no cache client to avoid watch permission (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 authored Nov 7, 2024
1 parent e82ef3a commit 8e6b3d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cmd/operator/app/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8e6b3d7

Please sign in to comment.