fix openpitrix return non nil client when enpoint is empty (#2165)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user