@@ -3,6 +3,7 @@ package agentdeploy
33import (
44 "context"
55 "fmt"
6+ "regexp"
67 "strings"
78
89 appsv1 "k8s.io/api/apps/v1"
@@ -167,17 +168,38 @@ func (s *healthCheckSyncer) probeAddonStatusByWorks(
167168 manifestConditions = append (manifestConditions , work .Status .ResourceStatus .Manifests ... )
168169 }
169170
170- probeFields , healthChecker , err := s .analyzeWorkProber (s .agentAddon , cluster , addon )
171+ probeFields , healthChecker , healthAllChecker , err := s .analyzeWorkProber (s .agentAddon , cluster , addon )
171172 if err != nil {
172173 // should not happen, return
173174 return err
174175 }
175176
177+ var resultFields []agent.ResultField
178+
176179 for _ , field := range probeFields {
177- result := findResultByIdentifier (field .ResourceIdentifier , manifestConditions )
180+ results := findResultsByIdentifier (field .ResourceIdentifier , manifestConditions )
181+
182+ // healthChecker will be ignored if healthAllChecker is set
183+ if healthAllChecker != nil {
184+ if len (results ) != 0 {
185+ resultFields = append (resultFields , results ... )
186+ }
187+ continue
188+ }
189+
190+ if healthChecker == nil {
191+ meta .SetStatusCondition (& addon .Status .Conditions , metav1.Condition {
192+ Type : addonapiv1alpha1 .ManagedClusterAddOnConditionAvailable ,
193+ Status : metav1 .ConditionFalse ,
194+ Reason : addonapiv1alpha1 .AddonAvailableReasonProbeUnavailable ,
195+ Message : fmt .Sprintf ("health checker function is not set %v" , err ),
196+ })
197+ return nil
198+ }
199+
178200 // if no results are returned. it is possible that work agent has not returned the feedback value.
179201 // mark condition to unknown
180- if result == nil {
202+ if len ( results ) == 0 {
181203 meta .SetStatusCondition (& addon .Status .Conditions , metav1.Condition {
182204 Type : addonapiv1alpha1 .ManagedClusterAddOnConditionAvailable ,
183205 Status : metav1 .ConditionUnknown ,
@@ -189,16 +211,29 @@ func (s *healthCheckSyncer) probeAddonStatusByWorks(
189211 return nil
190212 }
191213
192- err := healthChecker (field .ResourceIdentifier , * result )
193- if err != nil {
194- meta .SetStatusCondition (& addon .Status .Conditions , metav1.Condition {
195- Type : addonapiv1alpha1 .ManagedClusterAddOnConditionAvailable ,
196- Status : metav1 .ConditionFalse ,
197- Reason : addonapiv1alpha1 .AddonAvailableReasonProbeUnavailable ,
198- Message : fmt .Sprintf ("Probe addon unavailable with err %v" , err ),
199- })
200- return nil
214+ for _ , result := range results {
215+ err := healthChecker (result .ResourceIdentifier , result .FeedbackResult )
216+ if err != nil {
217+ meta .SetStatusCondition (& addon .Status .Conditions , metav1.Condition {
218+ Type : addonapiv1alpha1 .ManagedClusterAddOnConditionAvailable ,
219+ Status : metav1 .ConditionFalse ,
220+ Reason : addonapiv1alpha1 .AddonAvailableReasonProbeUnavailable ,
221+ Message : fmt .Sprintf ("Probe addon unavailable with err %v" , err ),
222+ })
223+ return nil
224+ }
201225 }
226+
227+ }
228+
229+ if healthAllChecker != nil && healthAllChecker (resultFields ) != nil {
230+ meta .SetStatusCondition (& addon .Status .Conditions , metav1.Condition {
231+ Type : addonapiv1alpha1 .ManagedClusterAddOnConditionAvailable ,
232+ Status : metav1 .ConditionFalse ,
233+ Reason : addonapiv1alpha1 .AddonAvailableReasonProbeUnavailable ,
234+ Message : fmt .Sprintf ("Probe addon unavailable with err %v" , err ),
235+ })
236+ return nil
202237 }
203238
204239 meta .SetStatusCondition (& addon .Status .Conditions , metav1.Condition {
@@ -214,21 +249,23 @@ func (s *healthCheckSyncer) analyzeWorkProber(
214249 agentAddon agent.AgentAddon ,
215250 cluster * clusterv1.ManagedCluster ,
216251 addon * addonapiv1alpha1.ManagedClusterAddOn ,
217- ) ([]agent.ProbeField , agent.AddonHealthCheckFunc , error ) {
252+ ) ([]agent.ProbeField , agent.AddonHealthCheckFunc , agent. AddonHealthCheckAllFunc , error ) {
218253
219254 switch agentAddon .GetAgentAddonOptions ().HealthProber .Type {
220255 case agent .HealthProberTypeWork :
221256 workProber := agentAddon .GetAgentAddonOptions ().HealthProber .WorkProber
222257 if workProber != nil {
223- return workProber .ProbeFields , workProber .HealthCheck , nil
258+ return workProber .ProbeFields , workProber .HealthCheck , workProber . HealthCheckAll , nil
224259 }
225- return nil , nil , fmt .Errorf ("work prober is not configured" )
260+ return nil , nil , nil , fmt .Errorf ("work prober is not configured" )
226261 case agent .HealthProberTypeDeploymentAvailability :
227- return s .analyzeDeploymentWorkProber (agentAddon , cluster , addon )
262+ probeFields , heathChecker , err := s .analyzeDeploymentWorkProber (agentAddon , cluster , addon )
263+ return probeFields , heathChecker , nil , err
228264 case agent .HealthProberTypeWorkloadAvailability :
229- return s .analyzeWorkloadsWorkProber (agentAddon , cluster , addon )
265+ probeFields , heathChecker , err := s .analyzeWorkloadsWorkProber (agentAddon , cluster , addon )
266+ return probeFields , heathChecker , nil , err
230267 default :
231- return nil , nil , fmt .Errorf ("unsupported health prober type %s" , agentAddon .GetAgentAddonOptions ().HealthProber .Type )
268+ return nil , nil , nil , fmt .Errorf ("unsupported health prober type %s" , agentAddon .GetAgentAddonOptions ().HealthProber .Type )
232269 }
233270}
234271
@@ -294,27 +331,46 @@ func (s *healthCheckSyncer) analyzeWorkloadsWorkProber(
294331 return probeFields , utils .WorkloadAvailabilityHealthCheck , nil
295332}
296333
297- func findResultByIdentifier (identifier workapiv1.ResourceIdentifier , manifestConditions []workapiv1.ManifestCondition ) * workapiv1.StatusFeedbackResult {
334+ func findResultsByIdentifier (identifier workapiv1.ResourceIdentifier ,
335+ manifestConditions []workapiv1.ManifestCondition ) []agent.ResultField {
336+ var results []agent.ResultField
298337 for _ , status := range manifestConditions {
299- if identifier .Group != status .ResourceMeta .Group {
300- continue
301- }
302- if identifier .Resource != status .ResourceMeta .Resource {
303- continue
304- }
305- if identifier .Name != status .ResourceMeta .Name {
306- continue
307- }
308- if identifier .Namespace != status .ResourceMeta .Namespace {
309- continue
338+ if resourceMatch (status .ResourceMeta , identifier ) && len (status .StatusFeedbacks .Values ) != 0 {
339+ results = append (results , agent.ResultField {
340+ ResourceIdentifier : workapiv1.ResourceIdentifier {
341+ Group : status .ResourceMeta .Group ,
342+ Resource : status .ResourceMeta .Resource ,
343+ Name : status .ResourceMeta .Name ,
344+ Namespace : status .ResourceMeta .Namespace ,
345+ },
346+ FeedbackResult : status .StatusFeedbacks ,
347+ })
310348 }
349+ }
311350
312- if len (status .StatusFeedbacks .Values ) == 0 {
313- return nil
314- }
351+ return results
352+ }
315353
316- return & status .StatusFeedbacks
354+ // compare two string, target may include *
355+ func wildcardMatch (resource , target string ) bool {
356+ if resource == target || target == "*" {
357+ return true
317358 }
318359
319- return nil
360+ pattern := "^" + regexp .QuoteMeta (target ) + "$"
361+ pattern = strings .ReplaceAll (pattern , "\\ *" , ".*" )
362+
363+ re , err := regexp .Compile (pattern )
364+ if err != nil {
365+ return false
366+ }
367+
368+ return re .MatchString (resource )
369+ }
370+
371+ func resourceMatch (resourceMeta workapiv1.ManifestResourceMeta , resource workapiv1.ResourceIdentifier ) bool {
372+ return resourceMeta .Group == resource .Group &&
373+ resourceMeta .Resource == resource .Resource &&
374+ wildcardMatch (resourceMeta .Namespace , resource .Namespace ) &&
375+ wildcardMatch (resourceMeta .Name , resource .Name )
320376}
0 commit comments