Skip to content

Commit

Permalink
allow local zone override for NonGCP NEG
Browse files Browse the repository at this point in the history
  • Loading branch information
Minhan Xia committed Apr 16, 2020
1 parent bbb27b5 commit a73adbd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/glbc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
} 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")
Expand Down
35 changes: 35 additions & 0 deletions pkg/neg/types/simple_zone_getter.go
Original file line number Diff line number Diff line change
@@ -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}
}
49 changes: 49 additions & 0 deletions pkg/neg/types/simple_zone_getter_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit a73adbd

Please sign in to comment.