From ff761ceb81cbf09505d1f66360551b94671d6fda Mon Sep 17 00:00:00 2001 From: lutzowguo Date: Tue, 9 Jul 2024 16:48:45 +0800 Subject: [PATCH] init add healthcheck in tkestack add health check to status --- api/application/v1/types.go | 16 ++++++ .../controller/app/action/healtchcheck.go | 49 +++++++++++++++++++ .../controller/app/app_controller.go | 3 +- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 pkg/application/controller/app/action/healtchcheck.go diff --git a/api/application/v1/types.go b/api/application/v1/types.go index bcd9a0775..320ffd287 100644 --- a/api/application/v1/types.go +++ b/api/application/v1/types.go @@ -152,6 +152,15 @@ type AppStatus struct { // Dryrun result. // +optional Manifest string `json:"manifest" protobuf:"bytes,10,opt,name=manifest"` + // The healthStatus for the application + // +optional + HealthStatus string `json:"healthStatus,omitempty" protobuf:"bytes,3,opt,name=healthStatus"` + // lastHealthCheckTime is the last health check time for the application + // +optional + LastHealthCheckTime metav1.Time `json:"lastHealthCheckTime,omitempty" protobuf:"bytes,4,opt,name=lastHealthCheckTime"` + // healthCheckDetails is the detail info of last health check result + // +optional + HealthCheckDetails []HealthCheckDetail `json:"healthCheckDetails" protobuf:"bytes,1,opt,name=healthCheckDetails"` } // +genclient @@ -258,6 +267,13 @@ const ( // AppPhase indicates the phase of app. type AppPhase string +type HealthCheckDetail struct { + WorkloadType string `json:"workloadType,omitempty" protobuf:"bytes,3,opt,name=workloadType"` + WorkloadName string `json:"workloadName,omitempty" protobuf:"bytes,3,opt,name=workloadName"` + PodDesiredNum int64 `json:"podDesiredNum,omitempty" protobuf:"varint,2,opt,name=podDesiredNum"` + PodReadyNum int64 `json:"podReadyNum,omitempty" protobuf:"varint,2,opt,name=podReadyNum"` +} + const ( // Installing means the installation for the App is running. AppPhaseInstalling AppPhase = "Installing" diff --git a/pkg/application/controller/app/action/healtchcheck.go b/pkg/application/controller/app/action/healtchcheck.go new file mode 100644 index 000000000..a6a9e53d6 --- /dev/null +++ b/pkg/application/controller/app/action/healtchcheck.go @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making TKEStack + * available. + * + * Copyright (C) 2012-2019 Tencent. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * https://opensource.org/licenses/Apache-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package action + +import ( + "context" + "fmt" + + applicationv1 "tkestack.io/tke/api/application/v1" + applicationversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/application/v1" + platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" + appconfig "tkestack.io/tke/pkg/application/config" + "tkestack.io/tke/pkg/util/log" +) + +// Install installs a chart archive +func HealthCheck(ctx context.Context, + applicationClient applicationversionedclient.ApplicationV1Interface, + platformClient platformversionedclient.PlatformV1Interface, + app *applicationv1.App, + repo appconfig.RepoConfiguration) (*applicationv1.App, error) { + hooks := getHooks(app) + + err := hooks.HealthCheck(ctx, applicationClient, platformClient, app, repo) + if err != nil { + log.Errorf(fmt.Sprintf("ERROR: healthcheck cluster %s app %s failed with err :%s", app.Spec.TargetCluster, app.Name, err)) + return nil, err + } + + //updateStatusFunc + return app, nil +} + diff --git a/pkg/application/controller/app/app_controller.go b/pkg/application/controller/app/app_controller.go index 8aa707835..e3bdaae04 100644 --- a/pkg/application/controller/app/app_controller.go +++ b/pkg/application/controller/app/app_controller.go @@ -370,7 +370,8 @@ func (c *Controller) handlePhase(ctx context.Context, key string, cachedApp *cac case applicationv1.AppPhaseSucceeded: c.startAppHealthCheck(ctx, key) // sync release status - return c.syncAppFromRelease(ctx, cachedApp, app) + c.syncAppFromRelease(ctx, cachedApp, app) + return action.HealthCheck(ctx, c.client.ApplicationV1(), c.platformClient, app, c.repo) case applicationv1.AppPhaseUpgradFailed: return action.Upgrade(ctx, c.client.ApplicationV1(), c.platformClient, app, c.repo, c.updateStatus) case applicationv1.AppPhaseRollingBack: