From 0ff8557376d0543ee31357fd0e474381f7fba9ac Mon Sep 17 00:00:00 2001 From: wangao1236 <908622977@qq.com> Date: Sat, 22 Aug 2020 11:08:06 +0800 Subject: [PATCH] feat(monitor): decouple monitor and business --- api/monitor/types.go | 8 ++++++++ cmd/tke-installer/app/installer/installer.go | 9 +++++---- .../tke-monitor-api/tke-monitor-api.yaml | 5 +++++ cmd/tke-monitor-api/app/config/config.go | 16 +++++++++------ cmd/tke-monitor-api/app/options/options.go | 2 +- .../overview/cluster/storage/storage.go | 20 ++++++++++++++----- pkg/monitor/util/cache/cache.go | 4 +++- 7 files changed, 47 insertions(+), 17 deletions(-) diff --git a/api/monitor/types.go b/api/monitor/types.go index 28b51824b..7a97f8048 100644 --- a/api/monitor/types.go +++ b/api/monitor/types.go @@ -134,6 +134,14 @@ const ( AddonPhaseUnknown AddonPhase = "Unknown" ) +type OverviewProjectStatus int32 + +const ( + OverviewProjectStatusNotFound OverviewProjectStatus = 0 + OverviewProjectStatusError OverviewProjectStatus = -1 + OverviewProjectStatusDisable OverviewProjectStatus = -2 +) + // +genclient // +genclient:nonNamespaced // +genclient:onlyVerbs=create diff --git a/cmd/tke-installer/app/installer/installer.go b/cmd/tke-installer/app/installer/installer.go index 1ad5862bd..ed0bffaca 100644 --- a/cmd/tke-installer/app/installer/installer.go +++ b/cmd/tke-installer/app/installer/installer.go @@ -1767,10 +1767,11 @@ func (t *TKE) installInfluxDB(ctx context.Context) error { func (t *TKE) installTKEMonitorAPI(ctx context.Context) error { options := map[string]interface{}{ - "Replicas": t.Config.Replicas, - "Image": images.Get().TKEMonitorAPI.FullName(), - "EnableAuth": t.Para.Config.Auth.TKEAuth != nil, - "EnableAudit": t.auditEnabled(), + "Replicas": t.Config.Replicas, + "Image": images.Get().TKEMonitorAPI.FullName(), + "EnableAuth": t.Para.Config.Auth.TKEAuth != nil, + "EnableBusiness": t.businessEnabled(), + "EnableAudit": t.auditEnabled(), } if t.Para.Config.Auth.OIDCAuth != nil { options["OIDCClientID"] = t.Para.Config.Auth.OIDCAuth.ClientID diff --git a/cmd/tke-installer/app/installer/manifests/tke-monitor-api/tke-monitor-api.yaml b/cmd/tke-installer/app/installer/manifests/tke-monitor-api/tke-monitor-api.yaml index 783f79334..ec8dd8982 100644 --- a/cmd/tke-installer/app/installer/manifests/tke-monitor-api/tke-monitor-api.yaml +++ b/cmd/tke-installer/app/installer/manifests/tke-monitor-api/tke-monitor-api.yaml @@ -142,9 +142,11 @@ data: api_server = "https://tke-platform-api" api_server_client_config = "/app/conf/tke-platform-config.yaml" +{{- if .EnableBusiness }} [client.business] api_server = "https://tke-business-api" api_server_client_config = "/app/conf/tke-business-config.yaml" +{{- end }} tke-auth-webhook.yaml: | apiVersion: v1 @@ -186,6 +188,8 @@ data: user: admin-cert name: tke + +{{- if .EnableBusiness }} tke-business-config.yaml: | apiVersion: v1 kind: Config @@ -205,6 +209,7 @@ data: cluster: tke user: admin-cert name: tke +{{- end }} tke-monitor-config.yaml: | apiVersion: monitor.config.tkestack.io/v1 diff --git a/cmd/tke-monitor-api/app/config/config.go b/cmd/tke-monitor-api/app/config/config.go index 73f03d7eb..cbd8e9210 100644 --- a/cmd/tke-monitor-api/app/config/config.go +++ b/cmd/tke-monitor-api/app/config/config.go @@ -175,12 +175,16 @@ func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config, if err != nil { return nil, err } - if !ok { - return nil, fmt.Errorf("failed to initialize client config of platform API server") + if !ok && opts.BusinessAPIClient.Required { + return nil, fmt.Errorf("failed to initialize client config of business API server") } - businessClient, err := versionedclientset.NewForConfig(rest.AddUserAgent(businessAPIServerClientConfig, "tke-monitor-api")) - if err != nil { - return nil, err + var businessClientV1 businessversionedclient.BusinessV1Interface + if ok { + businessClient, err := versionedclientset.NewForConfig(rest.AddUserAgent(businessAPIServerClientConfig, "tke-monitor-api")) + if err != nil { + return nil, err + } + businessClientV1 = businessClient.BusinessV1() } return &Config{ @@ -189,7 +193,7 @@ func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config, VersionedSharedInformerFactory: versionedInformers, StorageFactory: storageFactory, PlatformClient: platformClient.PlatformV1(), - BusinessClient: businessClient.BusinessV1(), + BusinessClient: businessClientV1, PrivilegedUsername: opts.Authentication.PrivilegedUsername, MonitorConfig: monitorConfig, }, nil diff --git a/cmd/tke-monitor-api/app/options/options.go b/cmd/tke-monitor-api/app/options/options.go index 8551b50f3..38b794228 100644 --- a/cmd/tke-monitor-api/app/options/options.go +++ b/cmd/tke-monitor-api/app/options/options.go @@ -63,7 +63,7 @@ func NewOptions(serverName string) *Options { Authentication: apiserveroptions.NewAuthenticationWithAPIOptions(), Authorization: apiserveroptions.NewAuthorizationOptions(), PlatformAPIClient: controlleroptions.NewAPIServerClientOptions("platform", true), - BusinessAPIClient: controlleroptions.NewAPIServerClientOptions("business", true), + BusinessAPIClient: controlleroptions.NewAPIServerClientOptions("business", false), Audit: genericapiserveroptions.NewAuditOptions(), } } diff --git a/pkg/monitor/registry/overview/cluster/storage/storage.go b/pkg/monitor/registry/overview/cluster/storage/storage.go index e9294b4c7..9ada15acd 100644 --- a/pkg/monitor/registry/overview/cluster/storage/storage.go +++ b/pkg/monitor/registry/overview/cluster/storage/storage.go @@ -97,17 +97,27 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, _ rest.ValidateOb } clusterOverview.Result = r.cacher.GetClusterOverviewResult(clusterIDs) - projectCount := 0 - projectAbnormal := 0 + if r.businessClient == nil { + log.Info("The client for Business API Server is not installed") + clusterOverview.Result.ProjectCount = int32(monitor.OverviewProjectStatusDisable) + clusterOverview.Result.ProjectAbnormal = int32(monitor.OverviewProjectStatusDisable) + return clusterOverview, nil + } + + projectCount := int32(0) + projectAbnormal := int32(0) if projectList, err := r.businessClient.Projects().List(ctx, listOptions); err == nil && projectList != nil { - projectCount = len(projectList.Items) + projectCount = int32(len(projectList.Items)) for _, prj := range projectList.Items { if prj.Status.Phase == businessv1.ProjectFailed { projectAbnormal++ } } + } else { + projectCount = int32(monitor.OverviewProjectStatusError) + projectAbnormal = int32(monitor.OverviewProjectStatusError) } - clusterOverview.Result.ProjectCount = int32(projectCount) - clusterOverview.Result.ProjectAbnormal = int32(projectAbnormal) + clusterOverview.Result.ProjectCount = projectCount + clusterOverview.Result.ProjectAbnormal = projectAbnormal return clusterOverview, nil } diff --git a/pkg/monitor/util/cache/cache.go b/pkg/monitor/util/cache/cache.go index a2d203078..d53673dfb 100644 --- a/pkg/monitor/util/cache/cache.go +++ b/pkg/monitor/util/cache/cache.go @@ -85,7 +85,9 @@ type Cacher interface { type cacher struct { sync.RWMutex - platformClient platformversionedclient.PlatformV1Interface + platformClient platformversionedclient.PlatformV1Interface + + // businessClient is not required, and needed to determine if it's nil businessClient businessversionedclient.BusinessV1Interface clusterStatisticSet util.ClusterStatisticSet clusterClientSets util.ClusterClientSets