|
1 | 1 | package routeutils |
2 | 2 |
|
3 | | -import v1 "k8s.io/api/core/v1" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/pkg/errors" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/types" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 11 | +) |
4 | 12 |
|
5 | | -type BackendDescription interface { |
6 | | - GetService() *v1.Service |
7 | | - GetWeight() int32 |
8 | | - GetPort() int |
| 13 | +type Backend struct { |
| 14 | + Service *corev1.Service |
| 15 | + ServicePort *corev1.ServicePort |
| 16 | + Weight int |
| 17 | + // Add TG config here // |
9 | 18 | } |
10 | 19 |
|
11 | | -var _ BackendDescription = &backendDescriptionImpl{} |
| 20 | +func commonBackendLoader(ctx context.Context, k8sClient client.Client, backendRef gwv1.BackendRef, routeIdentifier types.NamespacedName, routeKind string) (*Backend, error) { |
| 21 | + if backendRef.Port == nil { |
| 22 | + return nil, errors.Errorf("Missing port in backend reference") |
| 23 | + } |
| 24 | + var namespace string |
| 25 | + if backendRef.Namespace == nil { |
| 26 | + namespace = routeIdentifier.Namespace |
| 27 | + } else { |
| 28 | + namespace = string(*backendRef.Namespace) |
| 29 | + } |
12 | 30 |
|
13 | | -type backendDescriptionImpl struct { |
14 | | -} |
| 31 | + svcName := types.NamespacedName{ |
| 32 | + Namespace: namespace, |
| 33 | + Name: string(backendRef.Name), |
| 34 | + } |
| 35 | + svc := &corev1.Service{} |
| 36 | + err := k8sClient.Get(ctx, svcName, svc) |
| 37 | + if err != nil { |
| 38 | + return nil, errors.Wrap(err, fmt.Sprintf("Unable to fetch svc object %+v", svcName)) |
| 39 | + } |
15 | 40 |
|
16 | | -func (b backendDescriptionImpl) GetService() *v1.Service { |
17 | | - //TODO implement me |
18 | | - panic("implement me") |
19 | | -} |
| 41 | + // TODO -- Is this correct? |
| 42 | + var servicePort *corev1.ServicePort |
20 | 43 |
|
21 | | -func (b backendDescriptionImpl) GetWeight() int32 { |
22 | | - //TODO implement me |
23 | | - panic("implement me") |
24 | | -} |
| 44 | + for _, svcPort := range svc.Spec.Ports { |
| 45 | + if svcPort.Port == int32(*backendRef.Port) { |
| 46 | + servicePort = &svcPort |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if servicePort == nil { |
| 52 | + return nil, errors.Errorf("Unable to find service port for port %d", *backendRef.Port) |
| 53 | + } |
| 54 | + |
| 55 | + // look up target group config here |
25 | 56 |
|
26 | | -func (b backendDescriptionImpl) GetPort() int { |
27 | | - //TODO implement me |
28 | | - panic("implement me") |
| 57 | + // Weight specifies the proportion of requests forwarded to the referenced |
| 58 | + // backend. This is computed as weight/(sum of all weights in this |
| 59 | + // BackendRefs list). For non-zero values, there may be some epsilon from |
| 60 | + // the exact proportion defined here depending on the precision an |
| 61 | + // implementation supports. Weight is not a percentage and the sum of |
| 62 | + // weights does not need to equal 100. |
| 63 | + // |
| 64 | + // If only one backend is specified and it has a weight greater than 0, 100% |
| 65 | + // of the traffic is forwarded to that backend. If weight is set to 0, no |
| 66 | + // traffic should be forwarded for this entry. If unspecified, weight |
| 67 | + // defaults to 1. |
| 68 | + weight := 1 |
| 69 | + if backendRef.Weight != nil { |
| 70 | + weight = int(*backendRef.Weight) |
| 71 | + } |
| 72 | + return &Backend{ |
| 73 | + Service: svc, |
| 74 | + ServicePort: servicePort, |
| 75 | + Weight: weight, |
| 76 | + }, nil |
29 | 77 | } |
0 commit comments