2017-08-22 01:12:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 gce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-01-18 05:58:07 +00:00
|
|
|
"fmt"
|
2017-08-22 01:12:27 +00:00
|
|
|
"strings"
|
2018-01-18 05:58:07 +00:00
|
|
|
|
|
|
|
computealpha "google.golang.org/api/compute/v0.alpha"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/filter"
|
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta"
|
2017-08-22 01:12:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NEGLoadBalancerType = "LOAD_BALANCING"
|
|
|
|
NEGIPPortNetworkEndpointType = "GCE_VM_IP_PORT"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newNetworkEndpointGroupMetricContext(request string, zone string) *metricContext {
|
|
|
|
return newGenericMetricContext("networkendpointgroup_", request, unusedMetricLabel, zone, computeAlphaVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) GetNetworkEndpointGroup(name string, zone string) (*computealpha.NetworkEndpointGroup, error) {
|
2018-01-18 05:58:07 +00:00
|
|
|
mc := newNetworkEndpointGroupMetricContext("get", zone)
|
2017-08-22 01:12:27 +00:00
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
2018-01-18 05:58:07 +00:00
|
|
|
return nil, mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
v, err := gce.c.AlphaNetworkEndpointGroups().Get(context.Background(), meta.ZonalKey(name, zone))
|
2017-08-22 01:12:27 +00:00
|
|
|
return v, mc.Observe(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) ListNetworkEndpointGroup(zone string) ([]*computealpha.NetworkEndpointGroup, error) {
|
2018-01-18 05:58:07 +00:00
|
|
|
mc := newNetworkEndpointGroupMetricContext("list", zone)
|
2017-08-22 01:12:27 +00:00
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
2018-01-18 05:58:07 +00:00
|
|
|
return nil, mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
negs, err := gce.c.AlphaNetworkEndpointGroups().List(context.Background(), zone, filter.None)
|
|
|
|
return negs, mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 05:58:07 +00:00
|
|
|
// AggregatedListNetworkEndpointGroup returns a map of zone -> endpoint group.
|
2017-08-22 01:12:27 +00:00
|
|
|
func (gce *GCECloud) AggregatedListNetworkEndpointGroup() (map[string][]*computealpha.NetworkEndpointGroup, error) {
|
2018-01-18 05:58:07 +00:00
|
|
|
mc := newNetworkEndpointGroupMetricContext("aggregated_list", "")
|
2017-08-22 01:12:27 +00:00
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
2018-01-18 05:58:07 +00:00
|
|
|
return nil, mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
// TODO: filter for the region the cluster is in.
|
|
|
|
all, err := gce.c.AlphaNetworkEndpointGroups().AggregatedList(context.Background(), filter.None)
|
|
|
|
if err != nil {
|
|
|
|
return nil, mc.Observe(err)
|
|
|
|
}
|
|
|
|
ret := map[string][]*computealpha.NetworkEndpointGroup{}
|
|
|
|
for key, byZone := range all {
|
|
|
|
// key is "zones/<zone name>"
|
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, mc.Observe(fmt.Errorf("invalid key for AggregatedListNetworkEndpointGroup: %q", key))
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
zone := parts[1]
|
|
|
|
ret[zone] = append(ret[zone], byZone...)
|
|
|
|
}
|
|
|
|
return ret, mc.Observe(nil)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) CreateNetworkEndpointGroup(neg *computealpha.NetworkEndpointGroup, zone string) error {
|
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mc := newNetworkEndpointGroupMetricContext("create", zone)
|
2018-01-18 05:58:07 +00:00
|
|
|
return mc.Observe(gce.c.AlphaNetworkEndpointGroups().Insert(context.Background(), meta.ZonalKey(neg.Name, zone), neg))
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) DeleteNetworkEndpointGroup(name string, zone string) error {
|
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mc := newNetworkEndpointGroupMetricContext("delete", zone)
|
2018-01-18 05:58:07 +00:00
|
|
|
return mc.Observe(gce.c.AlphaNetworkEndpointGroups().Delete(context.Background(), meta.ZonalKey(name, zone)))
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) AttachNetworkEndpoints(name, zone string, endpoints []*computealpha.NetworkEndpoint) error {
|
2018-01-18 05:58:07 +00:00
|
|
|
mc := newNetworkEndpointGroupMetricContext("attach", zone)
|
2017-08-22 01:12:27 +00:00
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
2018-01-18 05:58:07 +00:00
|
|
|
return mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
req := &computealpha.NetworkEndpointGroupsAttachEndpointsRequest{
|
2017-08-22 01:12:27 +00:00
|
|
|
NetworkEndpoints: endpoints,
|
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
return mc.Observe(gce.c.AlphaNetworkEndpointGroups().AttachNetworkEndpoints(context.Background(), meta.ZonalKey(name, zone), req))
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) DetachNetworkEndpoints(name, zone string, endpoints []*computealpha.NetworkEndpoint) error {
|
2018-01-18 05:58:07 +00:00
|
|
|
mc := newNetworkEndpointGroupMetricContext("detach", zone)
|
2017-08-22 01:12:27 +00:00
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
2018-01-18 05:58:07 +00:00
|
|
|
return mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
req := &computealpha.NetworkEndpointGroupsDetachEndpointsRequest{
|
2017-08-22 01:12:27 +00:00
|
|
|
NetworkEndpoints: endpoints,
|
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
return mc.Observe(gce.c.AlphaNetworkEndpointGroups().DetachNetworkEndpoints(context.Background(), meta.ZonalKey(name, zone), req))
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gce *GCECloud) ListNetworkEndpoints(name, zone string, showHealthStatus bool) ([]*computealpha.NetworkEndpointWithHealthStatus, error) {
|
2018-01-18 05:58:07 +00:00
|
|
|
mc := newNetworkEndpointGroupMetricContext("list_networkendpoints", zone)
|
2017-08-22 01:12:27 +00:00
|
|
|
if err := gce.alphaFeatureEnabled(AlphaFeatureNetworkEndpointGroup); err != nil {
|
2018-01-18 05:58:07 +00:00
|
|
|
return nil, mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|
|
|
|
healthStatus := "SKIP"
|
|
|
|
if showHealthStatus {
|
|
|
|
healthStatus = "SHOW"
|
|
|
|
}
|
2018-01-18 05:58:07 +00:00
|
|
|
req := &computealpha.NetworkEndpointGroupsListEndpointsRequest{
|
2017-08-22 01:12:27 +00:00
|
|
|
HealthStatus: healthStatus,
|
2018-01-18 05:58:07 +00:00
|
|
|
}
|
|
|
|
l, err := gce.c.AlphaNetworkEndpointGroups().ListNetworkEndpoints(context.Background(), meta.ZonalKey(name, zone), req, filter.None)
|
|
|
|
return l, mc.Observe(err)
|
2017-08-22 01:12:27 +00:00
|
|
|
}
|