-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
tlsroute.go
149 lines (118 loc) · 4.01 KB
/
tlsroute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package routechecks
import (
"context"
"fmt"
"reflect"
"time"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/operator/pkg/gateway-api/helpers"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)
// TLSRouteInput is used to implement the Input interface for TLSRoute
type TLSRouteInput struct {
Ctx context.Context
Logger *logrus.Entry
Client client.Client
Grants *gatewayv1beta1.ReferenceGrantList
TLSRoute *gatewayv1alpha2.TLSRoute
gateways map[gatewayv1.ParentReference]*gatewayv1.Gateway
}
func (t *TLSRouteInput) SetParentCondition(ref gatewayv1.ParentReference, condition metav1.Condition) {
// fill in the condition
condition.LastTransitionTime = metav1.NewTime(time.Now())
condition.ObservedGeneration = t.TLSRoute.GetGeneration()
t.mergeStatusConditions(ref, []metav1.Condition{
condition,
})
}
func (t *TLSRouteInput) SetAllParentCondition(condition metav1.Condition) {
// fill in the condition
condition.LastTransitionTime = metav1.NewTime(time.Now())
condition.ObservedGeneration = t.TLSRoute.GetGeneration()
for _, parent := range t.TLSRoute.Spec.ParentRefs {
t.mergeStatusConditions(parent, []metav1.Condition{
condition,
})
}
}
func (t *TLSRouteInput) mergeStatusConditions(parentRef gatewayv1alpha2.ParentReference, updates []metav1.Condition) {
index := -1
for i, parent := range t.TLSRoute.Status.RouteStatus.Parents {
if reflect.DeepEqual(parent.ParentRef, parentRef) {
index = i
break
}
}
if index != -1 {
t.TLSRoute.Status.RouteStatus.Parents[index].Conditions = merge(t.TLSRoute.Status.RouteStatus.Parents[index].Conditions, updates...)
return
}
t.TLSRoute.Status.RouteStatus.Parents = append(t.TLSRoute.Status.RouteStatus.Parents, gatewayv1alpha2.RouteParentStatus{
ParentRef: parentRef,
ControllerName: controllerName,
Conditions: updates,
})
}
func (t *TLSRouteInput) GetGrants() []gatewayv1beta1.ReferenceGrant {
return t.Grants.Items
}
func (t *TLSRouteInput) GetNamespace() string {
return t.TLSRoute.GetNamespace()
}
func (t *TLSRouteInput) GetGVK() schema.GroupVersionKind {
return gatewayv1alpha2.SchemeGroupVersion.WithKind("TLSRoute")
}
func (t *TLSRouteInput) GetRules() []GenericRule {
var rules []GenericRule
for _, rule := range t.TLSRoute.Spec.Rules {
rules = append(rules, &TLSRouteRule{rule})
}
return rules
}
func (t *TLSRouteInput) GetClient() client.Client {
return t.Client
}
func (t *TLSRouteInput) GetContext() context.Context {
return t.Ctx
}
// TLSRouteRule is used to implement the GenericRule interface for TLSRoute
type TLSRouteRule struct {
Rule gatewayv1alpha2.TLSRouteRule
}
func (t *TLSRouteRule) GetBackendRefs() []gatewayv1.BackendRef {
return t.Rule.BackendRefs
}
func (t *TLSRouteInput) GetHostnames() []gatewayv1.Hostname {
return t.TLSRoute.Spec.Hostnames
}
func (t *TLSRouteInput) GetGateway(parent gatewayv1.ParentReference) (*gatewayv1.Gateway, error) {
if t.gateways == nil {
t.gateways = make(map[gatewayv1.ParentReference]*gatewayv1.Gateway)
}
if gw, exists := t.gateways[parent]; exists {
return gw, nil
}
ns := helpers.NamespaceDerefOr(parent.Namespace, t.GetNamespace())
gw := &gatewayv1.Gateway{}
if err := t.Client.Get(t.Ctx, client.ObjectKey{Namespace: ns, Name: string(parent.Name)}, gw); err != nil {
if !k8serrors.IsNotFound(err) {
// if it is not just a not found error, we should return the error as something is bad
return nil, fmt.Errorf("error while getting gateway: %w", err)
}
// Gateway does not exist skip further checks
return nil, fmt.Errorf("gateway %q does not exist: %w", parent.Name, err)
}
t.gateways[parent] = gw
return gw, nil
}
func (t *TLSRouteInput) Log() *logrus.Entry {
return t.Logger
}