diff --git a/cmd/glbc/main.go b/cmd/glbc/main.go index ffc21df175..a1c67cd0b5 100644 --- a/cmd/glbc/main.go +++ b/cmd/glbc/main.go @@ -231,9 +231,21 @@ func runControllers(ctx *ingctx.ControllerContext) { go l4Controller.Run() klog.V(0).Infof("L4 controller started") } + var zoneGetter negtypes.ZoneGetter + zoneGetter = lbc.Translator + // In NonGCP mode, use the zone specified in gce.conf directly. + // This overrides the zone/fault-domain label on nodes for NEG controller. + if flags.F.EnableNonGCPMode { + zone, err := ctx.Cloud.GetZone(context.Background()) + if err != nil { + klog.Errorf("Failed to retrieve zone information from Cloud provider: %v; Please check if local-zone is specified in gce.conf.", err) + } else { + zoneGetter = negtypes.NewSimpleZoneGetter(zone.FailureDomain) + } + } // TODO: Refactor NEG to use cloud mocks so ctx.Cloud can be referenced within NewController. - negController := neg.NewController(negtypes.NewAdapter(ctx.Cloud), ctx, lbc.Translator, ctx.ClusterNamer, flags.F.ResyncPeriod, flags.F.NegGCPeriod, flags.F.EnableReadinessReflector, flags.F.RunIngressController, flags.F.RunL4Controller) + negController := neg.NewController(negtypes.NewAdapter(ctx.Cloud), ctx, zoneGetter, ctx.ClusterNamer, flags.F.ResyncPeriod, flags.F.NegGCPeriod, flags.F.EnableReadinessReflector, flags.F.RunIngressController, flags.F.RunL4Controller) go negController.Run(stopCh) klog.V(0).Infof("negController started") diff --git a/pkg/neg/syncers/endpoints_calculator.go b/pkg/neg/syncers/endpoints_calculator.go index d1316e8d0c..e3cbe95f50 100644 --- a/pkg/neg/syncers/endpoints_calculator.go +++ b/pkg/neg/syncers/endpoints_calculator.go @@ -164,5 +164,5 @@ func (l *L7EndpointsCalculator) Mode() types.EndpointsCalculatorMode { // CalculateEndpoints determines the endpoints in the NEGs based on the current service endpoints and the current NEGs. func (l *L7EndpointsCalculator) CalculateEndpoints(ep *v1.Endpoints, currentMap map[string]types.NetworkEndpointSet) (map[string]types.NetworkEndpointSet, types.EndpointPodMap, error) { - return toZoneNetworkEndpointMap(ep, l.zoneGetter, l.servicePortName, l.podLister, l.subsetLabels, "") + return toZoneNetworkEndpointMap(ep, l.zoneGetter, l.servicePortName, l.podLister, l.subsetLabels, l.networkEndpointType) } diff --git a/pkg/neg/types/simple_zone_getter.go b/pkg/neg/types/simple_zone_getter.go new file mode 100644 index 0000000000..d1e8817119 --- /dev/null +++ b/pkg/neg/types/simple_zone_getter.go @@ -0,0 +1,35 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package types + +// simpleZoneGetter implements ZoneGetter interface +// It always return its one single stored zone +type simpleZoneGetter struct { + zone string +} + +func (s *simpleZoneGetter) GetZoneForNode(string) (string, error) { + return s.zone, nil +} + +func (s *simpleZoneGetter) ListZones() ([]string, error) { + return []string{s.zone}, nil +} + +func NewSimpleZoneGetter(zone string) ZoneGetter { + return &simpleZoneGetter{zone: zone} +} diff --git a/pkg/neg/types/simple_zone_getter_test.go b/pkg/neg/types/simple_zone_getter_test.go new file mode 100644 index 0000000000..9e587f569e --- /dev/null +++ b/pkg/neg/types/simple_zone_getter_test.go @@ -0,0 +1,49 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package types + +import ( + "reflect" + "testing" +) + +func TestSimpleZoneGetter(t *testing.T) { + zone := "foo" + zoneGetter := NewSimpleZoneGetter(zone) + ret, err := zoneGetter.ListZones() + if err != nil { + t.Errorf("expect err = nil, but got %v", err) + } + expectZones := []string{zone} + if !reflect.DeepEqual(expectZones, ret) { + t.Errorf("expect list zones = %v, but got %v", expectZones, ret) + } + + validateGetZoneForNode := func(node string) { + retZone, err := zoneGetter.GetZoneForNode(node) + if err != nil { + t.Errorf("expect err = nil, but got %v", err) + } + + if retZone != zone { + t.Errorf("expect zone = %q, but got %q", zone, retZone) + } + } + + validateGetZoneForNode("foo-node") + validateGetZoneForNode("bar-node") +}