Skip to content

Commit

Permalink
Skip health check for deployments whose replicas is 0
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 committed Jul 3, 2024
1 parent fa77e9b commit 6bd739a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/addonmanager/controllers/agentdeploy/healthcheck_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions pkg/addonmanager/controllers/agentdeploy/healthcheck_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/probe_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 6bd739a

Please sign in to comment.