Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use no cache client to avoid watch permission #204

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor: use no cache client to avoid watch permission
zyy17 committed Nov 7, 2024
commit 95997ebfeb0fd2fb3e5ed1f585abe8607f2af052
6 changes: 5 additions & 1 deletion cmd/operator/app/command.go
Original file line number Diff line number Diff line change
@@ -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 {
11 changes: 9 additions & 2 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 4 additions & 1 deletion pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
@@ -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)