[cozystack-api] Add field index for Service spec.type and filter by LoadBalancer type
Signed-off-by: Kirill Ilin <stitch14@yandex.ru>
This commit is contained in:
parent
ded52c1279
commit
40dc20f0f1
2 changed files with 17 additions and 4 deletions
|
|
@ -149,6 +149,17 @@ func (c completedConfig) New() (*CozyServer, error) {
|
|||
return nil, fmt.Errorf("failed to build manager: %w", err)
|
||||
}
|
||||
|
||||
if err := mgr.GetFieldIndexer().IndexField(
|
||||
context.Background(),
|
||||
&corev1.Service{},
|
||||
"spec.type",
|
||||
func(rawObj client.Object) []string {
|
||||
svc := rawObj.(*corev1.Service)
|
||||
return []string{string(svc.Spec.Type)}
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("failed to index service spec.type field: %w", err)
|
||||
}
|
||||
|
||||
ctx := ctrl.SetupSignalHandler()
|
||||
|
||||
if err = mustGetInformers(ctx, mgr,
|
||||
|
|
|
|||
|
|
@ -1278,16 +1278,18 @@ func (r *REST) countTenantExternalIPs(ctx context.Context, namespace string) (in
|
|||
}
|
||||
|
||||
var services corev1.ServiceList
|
||||
if err := r.c.List(ctx, &services, client.InNamespace(namespace)); err != nil {
|
||||
if err := r.c.List(
|
||||
ctx,
|
||||
&services,
|
||||
client.InNamespace(namespace),
|
||||
client.MatchingFields{"spec.type": string(corev1.ServiceTypeLoadBalancer)},
|
||||
); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var count int32
|
||||
for i := range services.Items {
|
||||
svc := &services.Items[i]
|
||||
if svc.Spec.Type != corev1.ServiceTypeLoadBalancer {
|
||||
continue
|
||||
}
|
||||
for _, ingress := range svc.Status.LoadBalancer.Ingress {
|
||||
if ingress.IP != "" {
|
||||
count++
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue