|
| 1 | +package ingress |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/karmada-io/dashboard/pkg/common/errors" |
| 6 | + "github.com/karmada-io/dashboard/pkg/common/helpers" |
| 7 | + "github.com/karmada-io/dashboard/pkg/common/types" |
| 8 | + "github.com/karmada-io/dashboard/pkg/dataselect" |
| 9 | + "github.com/karmada-io/dashboard/pkg/resource/common" |
| 10 | + v1 "k8s.io/api/networking/v1" |
| 11 | + client "k8s.io/client-go/kubernetes" |
| 12 | +) |
| 13 | + |
| 14 | +// Ingress - a single ingress returned to the frontend. |
| 15 | +type Ingress struct { |
| 16 | + types.ObjectMeta `json:"objectMeta"` |
| 17 | + types.TypeMeta `json:"typeMeta"` |
| 18 | + |
| 19 | + // External endpoints of this ingress. |
| 20 | + Endpoints []common.Endpoint `json:"endpoints"` |
| 21 | + Hosts []string `json:"hosts"` |
| 22 | +} |
| 23 | + |
| 24 | +// IngressList - response structure for a queried ingress list. |
| 25 | +type IngressList struct { |
| 26 | + types.ListMeta `json:"listMeta"` |
| 27 | + |
| 28 | + // Unordered list of Ingresss. |
| 29 | + Items []Ingress `json:"items"` |
| 30 | + |
| 31 | + // List of non-critical errors, that occurred during resource retrieval. |
| 32 | + Errors []error `json:"errors"` |
| 33 | +} |
| 34 | + |
| 35 | +// GetIngressList returns all ingresses in the given namespace. |
| 36 | +func GetIngressList(client client.Interface, namespace *common.NamespaceQuery, |
| 37 | + dsQuery *dataselect.DataSelectQuery) (*IngressList, error) { |
| 38 | + ingressList, err := client.NetworkingV1().Ingresses(namespace.ToRequestParam()).List(context.TODO(), helpers.ListEverything) |
| 39 | + |
| 40 | + nonCriticalErrors, criticalError := errors.ExtractErrors(err) |
| 41 | + if criticalError != nil { |
| 42 | + return nil, criticalError |
| 43 | + } |
| 44 | + |
| 45 | + return ToIngressList(ingressList.Items, nonCriticalErrors, dsQuery), nil |
| 46 | +} |
| 47 | + |
| 48 | +func getEndpoints(ingress *v1.Ingress) []common.Endpoint { |
| 49 | + endpoints := make([]common.Endpoint, 0) |
| 50 | + if len(ingress.Status.LoadBalancer.Ingress) > 0 { |
| 51 | + for _, status := range ingress.Status.LoadBalancer.Ingress { |
| 52 | + endpoint := common.Endpoint{} |
| 53 | + if status.Hostname != "" { |
| 54 | + endpoint.Host = status.Hostname |
| 55 | + } else if status.IP != "" { |
| 56 | + endpoint.Host = status.IP |
| 57 | + } |
| 58 | + endpoints = append(endpoints, endpoint) |
| 59 | + } |
| 60 | + } |
| 61 | + return endpoints |
| 62 | +} |
| 63 | + |
| 64 | +func getHosts(ingress *v1.Ingress) []string { |
| 65 | + hosts := make([]string, 0) |
| 66 | + set := make(map[string]struct{}) |
| 67 | + |
| 68 | + for _, rule := range ingress.Spec.Rules { |
| 69 | + if _, exists := set[rule.Host]; !exists && len(rule.Host) > 0 { |
| 70 | + hosts = append(hosts, rule.Host) |
| 71 | + } |
| 72 | + |
| 73 | + set[rule.Host] = struct{}{} |
| 74 | + } |
| 75 | + |
| 76 | + return hosts |
| 77 | +} |
| 78 | + |
| 79 | +func toIngress(ingress *v1.Ingress) Ingress { |
| 80 | + return Ingress{ |
| 81 | + ObjectMeta: types.NewObjectMeta(ingress.ObjectMeta), |
| 82 | + TypeMeta: types.NewTypeMeta(types.ResourceKindIngress), |
| 83 | + Endpoints: getEndpoints(ingress), |
| 84 | + Hosts: getHosts(ingress), |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func ToIngressList(ingresses []v1.Ingress, nonCriticalErrors []error, dsQuery *dataselect.DataSelectQuery) *IngressList { |
| 89 | + newIngressList := &IngressList{ |
| 90 | + ListMeta: types.ListMeta{TotalItems: len(ingresses)}, |
| 91 | + Items: make([]Ingress, 0), |
| 92 | + Errors: nonCriticalErrors, |
| 93 | + } |
| 94 | + |
| 95 | + ingresCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(ingresses), dsQuery) |
| 96 | + ingresses = fromCells(ingresCells) |
| 97 | + newIngressList.ListMeta = types.ListMeta{TotalItems: filteredTotal} |
| 98 | + |
| 99 | + for _, ingress := range ingresses { |
| 100 | + newIngressList.Items = append(newIngressList.Items, toIngress(&ingress)) |
| 101 | + } |
| 102 | + |
| 103 | + return newIngressList |
| 104 | +} |
0 commit comments