@@ -7,21 +7,26 @@ import (
7
7
"github.com/go-logr/logr"
8
8
"github.com/nginxinc/nginx-gateway-kubernetes/internal/state"
9
9
"github.com/nginxinc/nginx-gateway-kubernetes/internal/status"
10
+ apiv1 "k8s.io/api/core/v1"
11
+ "k8s.io/apimachinery/pkg/types"
10
12
"sigs.k8s.io/gateway-api/apis/v1alpha2"
11
13
)
12
14
13
15
// EventLoop is the main event loop of the Gateway.
14
16
type EventLoop struct {
15
17
conf state.Configuration
18
+ serviceStore state.ServiceStore
16
19
eventCh <- chan interface {}
17
20
logger logr.Logger
18
21
statusUpdater status.Updater
19
22
}
20
23
21
24
// NewEventLoop creates a new EventLoop.
22
- func NewEventLoop (conf state.Configuration , eventCh <- chan interface {}, statusUpdater status.Updater , logger logr.Logger ) * EventLoop {
25
+ func NewEventLoop (conf state.Configuration , serviceStore state.ServiceStore , eventCh <- chan interface {},
26
+ statusUpdater status.Updater , logger logr.Logger ) * EventLoop {
23
27
return & EventLoop {
24
28
conf : conf ,
29
+ serviceStore : serviceStore ,
25
30
eventCh : eventCh ,
26
31
statusUpdater : statusUpdater ,
27
32
logger : logger .WithName ("eventLoop" ),
@@ -75,8 +80,13 @@ func (el *EventLoop) propagateUpsert(e *UpsertEvent) ([]state.Change, []state.St
75
80
case * v1alpha2.HTTPRoute :
76
81
changes , statusUpdates := el .conf .UpsertHTTPRoute (r )
77
82
return changes , statusUpdates , nil
83
+ case * apiv1.Service :
84
+ el .serviceStore .Upsert (r )
85
+ // TO-DO: make sure the affected hosts are updated
86
+ return nil , nil , nil
78
87
}
79
88
89
+ // TO-DO: panic
80
90
return nil , nil , fmt .Errorf ("unknown resource type %T" , e .Resource )
81
91
}
82
92
@@ -85,8 +95,13 @@ func (el *EventLoop) propagateDelete(e *DeleteEvent) ([]state.Change, []state.St
85
95
case * v1alpha2.HTTPRoute :
86
96
changes , statusUpdates := el .conf .DeleteHTTPRoute (e .NamespacedName )
87
97
return changes , statusUpdates , nil
98
+ case * apiv1.Service :
99
+ el .serviceStore .Delete (e .NamespacedName )
100
+ // TO-DO: make sure the affected hosts are updated
101
+ return nil , nil , nil
88
102
}
89
103
104
+ // TO-DO: panic
90
105
return nil , nil , fmt .Errorf ("unknown resource type %T" , e .Type )
91
106
}
92
107
@@ -97,6 +112,43 @@ func (el *EventLoop) processChangesAndStatusUpdates(ctx context.Context, changes
97
112
98
113
// TO-DO: This code is temporary. We will remove it once we have a component that processes changes.
99
114
fmt .Printf ("%+v\n " , c )
115
+
116
+ if c .Op == state .Upsert {
117
+ // The code below resolves service backend refs into their cluster IPs
118
+ // TO-DO: this code will be removed once we have the component that generates NGINX config and
119
+ // uses the ServiceStore to resolve services.
120
+ for _ , g := range c .Host .PathRouteGroups {
121
+ for _ , r := range g .Routes {
122
+ for _ , b := range r .Source .Spec .Rules [r .RuleIdx ].BackendRefs {
123
+ if b .BackendRef .Kind == nil || * b .BackendRef .Kind == "Service" {
124
+ ns := r .Source .Namespace
125
+ if b .BackendRef .Namespace != nil {
126
+ ns = string (* b .BackendRef .Namespace )
127
+ }
128
+
129
+ address , err := el .serviceStore .Resolve (types.NamespacedName {
130
+ Namespace : ns ,
131
+ Name : string (b .BackendRef .Name ),
132
+ })
133
+
134
+ if err != nil {
135
+ fmt .Printf ("Service %s/%s error: %v\n " , ns , b .BackendRef .Name , err )
136
+ continue
137
+ }
138
+
139
+ var port int32 = 80
140
+ if b .BackendRef .Port != nil {
141
+ port = int32 (* b .BackendRef .Port )
142
+ }
143
+
144
+ address = fmt .Sprintf ("%s:%d" , address , port )
145
+
146
+ fmt .Printf ("Service %s/%s: %s\n " , ns , b .BackendRef .Name , address )
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
100
152
}
101
153
102
154
el .statusUpdater .ProcessStatusUpdates (ctx , updates )
0 commit comments