@@ -16,7 +16,6 @@ package apisix
1616
1717import (
1818 "context"
19- "errors"
2019 "fmt"
2120 "reflect"
2221 "strconv"
@@ -192,13 +191,6 @@ func (c *apisixUpstreamController) sync(ctx context.Context, ev *types.Event) er
192191 if au .Spec == nil {
193192 return nil
194193 }
195- svc , err := c .SvcLister .Services (namespace ).Get (name )
196- if err != nil {
197- log .Errorf ("failed to get service %s: %s" , key , err )
198- errRecord = err
199- goto updateStatus
200- }
201-
202194 // We will prioritize ExternalNodes and Discovery.
203195 if len (au .Spec .ExternalNodes ) != 0 || au .Spec .Discovery != nil {
204196 var newUps * apisixv1.Upstream
@@ -228,7 +220,7 @@ func (c *apisixUpstreamController) sync(ctx context.Context, ev *types.Event) er
228220 }
229221 // updateUpstream for real
230222 upsName := apisixv1 .ComposeExternalUpstreamName (au .Namespace , au .Name )
231- errRecord = c .updateUpstream (ctx , upsName , & au .Spec .ApisixUpstreamConfig , ev .Type .IsSyncEvent (), svc . Spec . ClusterIP )
223+ errRecord = c .updateUpstream (ctx , upsName , & au .Spec .ApisixUpstreamConfig , ev .Type .IsSyncEvent ())
232224 if err == apisix .ErrNotFound {
233225 errRecord = fmt .Errorf ("%s" , "upstream doesn't exist. It will be created after ApisixRoute is created referencing it." )
234226 }
@@ -242,6 +234,14 @@ func (c *apisixUpstreamController) sync(ctx context.Context, ev *types.Event) er
242234 portLevelSettings [port .Port ] = port .ApisixUpstreamConfig
243235 }
244236 }
237+
238+ svc , err := c .SvcLister .Services (namespace ).Get (name )
239+ if err != nil {
240+ log .Errorf ("failed to get service %s: %s" , key , err )
241+ errRecord = err
242+ goto updateStatus
243+ }
244+
245245 var subsets []configv2.ApisixUpstreamSubset
246246 subsets = append (subsets , configv2.ApisixUpstreamSubset {})
247247 if len (au .Spec .Subsets ) > 0 {
@@ -257,7 +257,7 @@ func (c *apisixUpstreamController) sync(ctx context.Context, ev *types.Event) er
257257 cfg = au .Spec .ApisixUpstreamConfig
258258 }
259259 }
260- err := c .updateUpstream (ctx , apisixv1 .ComposeUpstreamName (namespace , name , subset .Name , port .Port ), & cfg , ev .Type .IsSyncEvent (), svc . Spec . ClusterIP )
260+ err := c .updateUpstream (ctx , apisixv1 .ComposeUpstreamName (namespace , name , subset .Name , port .Port ), & cfg , ev .Type .IsSyncEvent ())
261261 if err != nil {
262262 if err == apisix .ErrNotFound {
263263 errRecord = fmt .Errorf ("%s" , "upstream doesn't exist. It will be created after ApisixRoute is created referencing it." )
@@ -325,7 +325,7 @@ func (c *apisixUpstreamController) updateStatus(obj kube.ApisixUpstream, statusE
325325 }
326326}
327327
328- func (c * apisixUpstreamController ) updateUpstream (ctx context.Context , upsName string , cfg * configv2.ApisixUpstreamConfig , shouldCompare bool , svcClusterIP string ) error {
328+ func (c * apisixUpstreamController ) updateUpstream (ctx context.Context , upsName string , cfg * configv2.ApisixUpstreamConfig , shouldCompare bool ) error {
329329 // TODO: multi cluster
330330 clusterName := c .Config .APISIX .DefaultClusterName
331331
@@ -348,24 +348,8 @@ func (c *apisixUpstreamController) updateUpstream(ctx context.Context, upsName s
348348 }
349349
350350 newUps .Metadata = ups .Metadata
351+ newUps .Nodes = ups .Nodes
351352
352- if cfg .Granularity == types .ResolveGranularity .Service {
353- if svcClusterIP == "" {
354- log .Errorw ("ApisixRoute refers to a headless service but want to use the service level resolve granularity" ,
355- zap .String ("ApisixUpstream name" , upsName ),
356- )
357- return errors .New ("conflict headless service and backend resolve granularity" )
358- }
359- for _ , node := range ups .Nodes {
360- newUps .Nodes = append (newUps .Nodes , apisixv1.UpstreamNode {
361- Host : svcClusterIP ,
362- Port : node .Port ,
363- Weight : node .Weight ,
364- })
365- }
366- } else {
367- newUps .Nodes = ups .Nodes
368- }
369353 log .Debugw ("updating upstream since ApisixUpstream changed" ,
370354 zap .Any ("upstream" , newUps ),
371355 zap .String ("ApisixUpstream name" , upsName ),
0 commit comments