From 6bd739aec5622719e2917621120c77f6edd1ec21 Mon Sep 17 00:00:00 2001 From: zhujian Date: Wed, 3 Jul 2024 08:35:36 +0000 Subject: [PATCH] Skip health check for deployments whose replicas is 0 Signed-off-by: zhujian --- .../agentdeploy/healthcheck_sync.go | 20 ++++++++--- .../agentdeploy/healthcheck_sync_test.go | 34 +++++++++++++++++++ pkg/utils/probe_helper.go | 8 +++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync.go b/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync.go index d01842e7..8ca3e0a2 100644 --- a/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync.go +++ b/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" @@ -178,10 +179,12 @@ func (s *healthCheckSyncer) probeAddonStatusByWorks( // mark condition to unknown if result == nil { meta.SetStatusCondition(&addon.Status.Conditions, metav1.Condition{ - Type: addonapiv1alpha1.ManagedClusterAddOnConditionAvailable, - Status: metav1.ConditionUnknown, - Reason: addonapiv1alpha1.AddonAvailableReasonNoProbeResult, - Message: "Probe results are not returned", + Type: addonapiv1alpha1.ManagedClusterAddOnConditionAvailable, + Status: metav1.ConditionUnknown, + Reason: addonapiv1alpha1.AddonAvailableReasonNoProbeResult, + Message: fmt.Sprintf("Probe results are not returned for %s/%s: %s/%s", + field.ResourceIdentifier.Group, field.ResourceIdentifier.Resource, + field.ResourceIdentifier.Namespace, field.ResourceIdentifier.Name), }) return nil } @@ -271,8 +274,17 @@ func (s *healthCheckSyncer) analyzeWorkloadsWorkProber( workloads := utils.FilterWorkloads(manifests) for _, workload := range workloads { + // Not probe the deployment with zero replicas + if workload.GroupResource.Group == appsv1.GroupName && + workload.GroupResource.Resource == "deployments" && + workload.DeploymentSpec != nil && + workload.DeploymentSpec.Replicas == 0 { + continue + } + manifestConfig := utils.WellKnowManifestConfig(workload.Group, workload.Resource, workload.Namespace, workload.Name) + probeFields = append(probeFields, agent.ProbeField{ ResourceIdentifier: manifestConfig.ResourceIdentifier, ProbeRules: manifestConfig.FeedbackRules, diff --git a/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync_test.go b/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync_test.go index 0b84799b..8bd18581 100644 --- a/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync_test.go +++ b/pkg/addonmanager/controllers/agentdeploy/healthcheck_sync_test.go @@ -44,6 +44,7 @@ func (t *healthCheckTestAgent) Manifests(cluster *clusterv1.ManagedCluster, return []runtime.Object{ NewFakeDeployment("test-deployment", "default"), + NewFakeZeroReplicasDeployment("test-zero-replicas-deployment", "default"), NewFakeDaemonSet("test-daemonset", "default"), }, nil } @@ -88,6 +89,39 @@ func NewFakeDeployment(namespace, name string) *appsv1.Deployment { } } +func NewFakeZeroReplicasDeployment(namespace, name string) *appsv1.Deployment { + var zero int32 = 0 + return &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Namespace: name, + }, + Spec: appsv1.DeploymentSpec{ + Replicas: &zero, + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "test", + }, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + "addon": "test", + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "test", + Image: "test", + }, + }, + }, + }, + }, + } +} + func NewFakeDaemonSet(namespace, name string) *appsv1.DaemonSet { return &appsv1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/utils/probe_helper.go b/pkg/utils/probe_helper.go index b43c6152..0d782a13 100644 --- a/pkg/utils/probe_helper.go +++ b/pkg/utils/probe_helper.go @@ -134,6 +134,11 @@ func FilterDeployments(objects []runtime.Object) []*appsv1.Deployment { type WorkloadMetadata struct { schema.GroupResource types.NamespacedName + DeploymentSpec *DeploymentSpec +} + +type DeploymentSpec struct { + Replicas int32 } func FilterWorkloads(objects []runtime.Object) []WorkloadMetadata { @@ -150,6 +155,9 @@ func FilterWorkloads(objects []runtime.Object) []WorkloadMetadata { Namespace: deployment.Namespace, Name: deployment.Name, }, + DeploymentSpec: &DeploymentSpec{ + Replicas: *deployment.Spec.Replicas, + }, }) } daemonset, err := ConvertToDaemonSet(obj)