Skip to content

Commit

Permalink
fix a bug where AggregatedListNEG will throw error when non zonal NEG…
Browse files Browse the repository at this point in the history
… is included in the API response
  • Loading branch information
freehan committed Aug 9, 2019
1 parent 2fe3b2f commit 03de628
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pkg/neg/types/cloudprovideradapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ limitations under the License.
package types

import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"google.golang.org/api/compute/v1"
"k8s.io/klog"
"k8s.io/legacy-cloud-providers/gce"
)

const (
// aggregatedListZonalKeyPrefix is the prefix for the zonal key from AggregatedList
aggregatedListZonalKeyPrefix = "zones"
// aggregatedListGlobalKey is the global key from AggregatedList
aggregatedListGlobalKey = "global"
)

// NewAdapter takes a Cloud and returns a NetworkEndpointGroupCloud.
func NewAdapter(g *gce.Cloud) NetworkEndpointGroupCloud {
return &cloudProviderAdapter{
Expand Down Expand Up @@ -72,10 +79,19 @@ func (a *cloudProviderAdapter) AggregatedListNetworkEndpointGroup() (map[string]
}
ret := map[string][]*compute.NetworkEndpointGroup{}
for key, byZone := range all {
// key is "zones/<zone name>"
// key is scope
// zonal key is "zones/<zone name>"
// regional key is "regions/<region name>"
// global key is "global"
// TODO: use cloud provider meta.KeyType and scope name as key
parts := strings.Split(key, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid key for AggregatedListNetworkEndpointGroup: %q", key)
if len(parts) == 1 && parts[0] == aggregatedListGlobalKey {
klog.V(4).Infof("Ignoring key %q as it is global", key)
continue
}
if len(parts) != 2 || parts[0] != aggregatedListZonalKeyPrefix {
klog.Warningf("Key %q is not in a known format, ignoring", key)
continue
}
zone := parts[1]
ret[zone] = append(ret[zone], byZone...)
Expand Down

0 comments on commit 03de628

Please sign in to comment.