Skip to content

Commit 3f69284

Browse files
committed
форматный кондишен
1 parent bfa66f8 commit 3f69284

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

internal/controller/rules2s_controller.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -249,36 +249,6 @@ func (r *RuleS2SReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
249249
localAddressGroups := localService.AddressGroups.Items
250250
targetAddressGroups := targetService.AddressGroups.Items
251251

252-
if len(localAddressGroups) == 0 || len(targetAddressGroups) == 0 {
253-
// Определяем, у какого именно сервиса отсутствуют адресные группы
254-
var missingAddressGroupsMsg string
255-
if len(localAddressGroups) == 0 && len(targetAddressGroups) == 0 {
256-
missingAddressGroupsMsg = fmt.Sprintf("Both services have no address groups: localService '%s', targetService '%s'", localService.Name, targetService.Name)
257-
} else if len(localAddressGroups) == 0 {
258-
missingAddressGroupsMsg = fmt.Sprintf("LocalService '%s' has no address groups", localService.Name)
259-
} else {
260-
missingAddressGroupsMsg = fmt.Sprintf("TargetService '%s' has no address groups", targetService.Name)
261-
}
262-
263-
meta.SetStatusCondition(&ruleS2S.Status.Conditions, metav1.Condition{
264-
Type: netguardv1alpha1.ConditionReady,
265-
Status: metav1.ConditionTrue,
266-
Reason: "ValidConfiguration",
267-
Message: fmt.Sprintf("Rule is valid but inactive: %s", missingAddressGroupsMsg),
268-
})
269-
if err := UpdateStatusWithRetry(ctx, r.Client, ruleS2S, DefaultMaxRetries); err != nil {
270-
logger.Error(err, "Failed to update RuleS2S status")
271-
}
272-
273-
// Логируем информацию, но НЕ ставим в очередь повторно
274-
logger.Info(missingAddressGroupsMsg,
275-
"localService", localService.Name,
276-
"targetService", targetService.Name)
277-
278-
// Возвращаем пустой Result без RequeueAfter
279-
return ctrl.Result{}, nil
280-
}
281-
282252
// Determine which ports to use based on traffic direction
283253
// In both cases, we use ports from the service that receives the traffic
284254
var ports []netguardv1alpha1.IngressPort
@@ -290,30 +260,64 @@ func (r *RuleS2SReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
290260
ports = targetService.Spec.IngressPorts
291261
}
292262

263+
// Collect all inactive conditions
264+
var inactiveConditions []string
265+
266+
// Check address groups
267+
if len(localAddressGroups) == 0 && len(targetAddressGroups) == 0 {
268+
inactiveConditions = append(inactiveConditions,
269+
fmt.Sprintf("Both services have no address groups: localService '%s', targetService '%s'",
270+
localService.Name, targetService.Name))
271+
} else if len(localAddressGroups) == 0 {
272+
inactiveConditions = append(inactiveConditions,
273+
fmt.Sprintf("LocalService '%s' has no address groups", localService.Name))
274+
} else if len(targetAddressGroups) == 0 {
275+
inactiveConditions = append(inactiveConditions,
276+
fmt.Sprintf("TargetService '%s' has no address groups", targetService.Name))
277+
}
278+
279+
// Check ports
293280
if len(ports) == 0 {
294-
// Определяем, для какого сервиса не определены порты
295281
var serviceName string
296282
if strings.ToLower(ruleS2S.Spec.Traffic) == "ingress" {
297283
serviceName = fmt.Sprintf("local service '%s'", localService.Name)
298284
} else {
299285
serviceName = fmt.Sprintf("target service '%s'", targetService.Name)
300286
}
301287

302-
infoMsg := fmt.Sprintf("No ports defined for the %s (traffic direction: %s)",
303-
serviceName, ruleS2S.Spec.Traffic)
288+
inactiveConditions = append(inactiveConditions,
289+
fmt.Sprintf("No ports defined for the %s (traffic direction: %s)",
290+
serviceName, ruleS2S.Spec.Traffic))
291+
}
292+
293+
// If there are any inactive conditions, set status and return
294+
if len(inactiveConditions) > 0 {
295+
// Format the message with line breaks
296+
var formattedMessage strings.Builder
297+
formattedMessage.WriteString("Rule is valid but inactive due to the following reasons:\n")
298+
299+
for i, condition := range inactiveConditions {
300+
formattedMessage.WriteString(fmt.Sprintf("%d. %s", i+1, condition))
301+
if i < len(inactiveConditions)-1 {
302+
formattedMessage.WriteString("\n")
303+
}
304+
}
304305

305306
meta.SetStatusCondition(&ruleS2S.Status.Conditions, metav1.Condition{
306307
Type: netguardv1alpha1.ConditionReady,
307308
Status: metav1.ConditionTrue,
308309
Reason: "ValidConfiguration",
309-
Message: fmt.Sprintf("Rule is valid but inactive: %s", infoMsg),
310+
Message: formattedMessage.String(),
310311
})
311312
if err := UpdateStatusWithRetry(ctx, r.Client, ruleS2S, DefaultMaxRetries); err != nil {
312313
logger.Error(err, "Failed to update RuleS2S status")
313314
}
314315

315316
// Логируем информацию
316-
logger.Info(infoMsg, "traffic", ruleS2S.Spec.Traffic)
317+
logger.Info("Rule is valid but inactive",
318+
"conditions", strings.Join(inactiveConditions, "; "),
319+
"localService", localService.Name,
320+
"targetService", targetService.Name)
317321

318322
// Возвращаем пустой Result без RequeueAfter
319323
return ctrl.Result{}, nil

0 commit comments

Comments
 (0)