From 9b16c0589ce3754dc19db5f77f79e3f27a56391c Mon Sep 17 00:00:00 2001 From: zryfish Date: Sun, 7 Jun 2020 20:00:27 +0800 Subject: [PATCH] fix openpitrix return non nil client when enpoint is empty (#2165) --- cmd/ks-apiserver/app/options/options.go | 2 +- pkg/models/monitoring/monitoring.go | 34 +++++++------ pkg/models/openpitrix/interface.go | 4 ++ pkg/simple/client/openpitrix/openpitrix.go | 59 +++++++++++++++++++--- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/cmd/ks-apiserver/app/options/options.go b/cmd/ks-apiserver/app/options/options.go index 67ff5e7dd..219ff56b6 100644 --- a/cmd/ks-apiserver/app/options/options.go +++ b/cmd/ks-apiserver/app/options/options.go @@ -179,7 +179,7 @@ func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIS apiServer.AuditingClient = auditingClient } - if s.OpenPitrixOptions != nil { + if s.OpenPitrixOptions != nil && !s.OpenPitrixOptions.IsEmpty() { opClient, err := openpitrix.NewClient(s.OpenPitrixOptions) if err != nil { return nil, err diff --git a/pkg/models/monitoring/monitoring.go b/pkg/models/monitoring/monitoring.go index 4aa1ef811..7fecaf943 100644 --- a/pkg/models/monitoring/monitoring.go +++ b/pkg/models/monitoring/monitoring.go @@ -188,24 +188,26 @@ func (mo monitoringOperator) GetKubeSphereStats() Metrics { openpitrix.RepoId: openpitrix.BuiltinRepoId, }, } - tmpls, err := mo.op.ListApps(cond, "", false, 0, 0) - if err != nil { - res.Results = append(res.Results, monitoring.Metric{ - MetricName: KubeSphereAppTmplCount, - Error: err.Error(), - }) - } else { - res.Results = append(res.Results, monitoring.Metric{ - MetricName: KubeSphereAppTmplCount, - MetricData: monitoring.MetricData{ - MetricType: monitoring.MetricTypeVector, - MetricValues: []monitoring.MetricValue{ - { - Sample: &monitoring.Point{now, float64(tmpls.TotalCount)}, + if mo.op != nil { + tmpl, err := mo.op.ListApps(cond, "", false, 0, 0) + if err != nil { + res.Results = append(res.Results, monitoring.Metric{ + MetricName: KubeSphereAppTmplCount, + Error: err.Error(), + }) + } else { + res.Results = append(res.Results, monitoring.Metric{ + MetricName: KubeSphereAppTmplCount, + MetricData: monitoring.MetricData{ + MetricType: monitoring.MetricTypeVector, + MetricValues: []monitoring.MetricValue{ + { + Sample: &monitoring.Point{now, float64(tmpl.TotalCount)}, + }, }, }, - }, - }) + }) + } } return res diff --git a/pkg/models/openpitrix/interface.go b/pkg/models/openpitrix/interface.go index 9eae5a1c2..63f414c47 100644 --- a/pkg/models/openpitrix/interface.go +++ b/pkg/models/openpitrix/interface.go @@ -37,6 +37,10 @@ type openpitrixOperator struct { } func NewOpenpitrixOperator(informers informers.SharedInformerFactory, opClient openpitrix.Client) Interface { + if opClient == nil { + return nil + } + return &openpitrixOperator{ ApplicationInterface: newApplicationOperator(informers, opClient), AppTemplateInterface: newAppTemplateOperator(opClient), diff --git a/pkg/simple/client/openpitrix/openpitrix.go b/pkg/simple/client/openpitrix/openpitrix.go index 28e497659..96dcc2327 100644 --- a/pkg/simple/client/openpitrix/openpitrix.go +++ b/pkg/simple/client/openpitrix/openpitrix.go @@ -74,7 +74,15 @@ func parseToHostPort(endpoint string) (string, int, error) { } func newRuntimeManagerClient(endpoint string) (pb.RuntimeManagerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } + conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -82,7 +90,14 @@ func newRuntimeManagerClient(endpoint string) (pb.RuntimeManagerClient, error) { return pb.NewRuntimeManagerClient(conn), nil } func newClusterManagerClient(endpoint string) (pb.ClusterManagerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -90,7 +105,14 @@ func newClusterManagerClient(endpoint string) (pb.ClusterManagerClient, error) { return pb.NewClusterManagerClient(conn), nil } func newCategoryManagerClient(endpoint string) (pb.CategoryManagerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -99,7 +121,14 @@ func newCategoryManagerClient(endpoint string) (pb.CategoryManagerClient, error) } func newAttachmentManagerClient(endpoint string) (pb.AttachmentManagerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -108,7 +137,14 @@ func newAttachmentManagerClient(endpoint string) (pb.AttachmentManagerClient, er } func newRepoManagerClient(endpoint string) (pb.RepoManagerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -117,7 +153,14 @@ func newRepoManagerClient(endpoint string) (pb.RepoManagerClient, error) { } func newRepoIndexer(endpoint string) (pb.RepoIndexerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -126,7 +169,14 @@ func newRepoIndexer(endpoint string) (pb.RepoIndexerClient, error) { } func newAppManagerClient(endpoint string) (pb.AppManagerClient, error) { + if len(endpoint) == 0 { + return nil, nil + } + host, port, err := parseToHostPort(endpoint) + if err != nil { + return nil, err + } conn, err := manager.NewClient(host, port) if err != nil { return nil, err @@ -134,52 +184,45 @@ func newAppManagerClient(endpoint string) (pb.AppManagerClient, error) { return pb.NewAppManagerClient(conn), nil } +// will return a nil client and nil error if endpoint is empty func NewClient(options *Options) (Client, error) { - runtimeMangerClient, err := newRuntimeManagerClient(options.RuntimeManagerEndpoint) - if err != nil { klog.Error(err) return nil, err } clusterManagerClient, err := newClusterManagerClient(options.ClusterManagerEndpoint) - if err != nil { klog.Error(err) return nil, err } repoManagerClient, err := newRepoManagerClient(options.RepoManagerEndpoint) - if err != nil { klog.Error(err) return nil, err } repoIndexerClient, err := newRepoIndexer(options.RepoIndexerEndpoint) - if err != nil { klog.Error(err) return nil, err } appManagerClient, err := newAppManagerClient(options.AppManagerEndpoint) - if err != nil { klog.Error(err) return nil, err } categoryManagerClient, err := newCategoryManagerClient(options.CategoryManagerEndpoint) - if err != nil { klog.Error(err) return nil, err } attachmentManagerClient, err := newAttachmentManagerClient(options.AttachmentManagerEndpoint) - if err != nil { klog.Error(err) return nil, err