k3s/vendor/k8s.io/legacy-cloud-providers/gce/gce_routes.go

110 lines
3.5 KiB
Go
Raw Normal View History

2019-09-27 21:51:53 +00:00
// +build !providerless
2019-08-30 18:33:25 +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"
"fmt"
"net/http"
"path"
2020-03-26 21:07:15 +00:00
"time"
2019-08-30 18:33:25 +00:00
2020-03-26 21:07:15 +00:00
"google.golang.org/api/compute/v1"
2019-08-30 18:33:25 +00:00
"k8s.io/apimachinery/pkg/types"
2020-08-10 17:43:49 +00:00
"k8s.io/klog/v2"
2019-08-30 18:33:25 +00:00
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/filter"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
cloudprovider "k8s.io/cloud-provider"
)
func newRoutesMetricContext(request string) *metricContext {
return newGenericMetricContext("routes", request, unusedMetricLabel, unusedMetricLabel, computeV1Version)
}
// ListRoutes in the cloud environment.
func (g *Cloud) ListRoutes(ctx context.Context, clusterName string) ([]*cloudprovider.Route, error) {
2020-03-26 21:07:15 +00:00
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Hour)
2019-08-30 18:33:25 +00:00
defer cancel()
mc := newRoutesMetricContext("list")
prefix := truncateClusterName(clusterName)
f := filter.Regexp("name", prefix+"-.*").AndRegexp("network", g.NetworkURL()).AndRegexp("description", k8sNodeRouteTag)
2019-09-27 21:51:53 +00:00
routes, err := g.c.Routes().List(timeoutCtx, f)
2019-08-30 18:33:25 +00:00
if err != nil {
return nil, mc.Observe(err)
}
var croutes []*cloudprovider.Route
for _, r := range routes {
target := path.Base(r.NextHopInstance)
// TODO: Should we lastComponent(target) this?
targetNodeName := types.NodeName(target) // NodeName == Instance Name on GCE
croutes = append(croutes, &cloudprovider.Route{
Name: r.Name,
TargetNode: targetNodeName,
DestinationCIDR: r.DestRange,
})
}
return croutes, mc.Observe(nil)
}
// CreateRoute in the cloud environment.
func (g *Cloud) CreateRoute(ctx context.Context, clusterName string, nameHint string, route *cloudprovider.Route) error {
2020-03-26 21:07:15 +00:00
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Hour)
2019-08-30 18:33:25 +00:00
defer cancel()
mc := newRoutesMetricContext("create")
targetInstance, err := g.getInstanceByName(mapNodeNameToInstanceName(route.TargetNode))
if err != nil {
return mc.Observe(err)
}
cr := &compute.Route{
2019-09-27 21:51:53 +00:00
// TODO(thockin): generate a unique name for node + route cidr. Don't depend on name hints.
2019-08-30 18:33:25 +00:00
Name: truncateClusterName(clusterName) + "-" + nameHint,
DestRange: route.DestinationCIDR,
NextHopInstance: fmt.Sprintf("zones/%s/instances/%s", targetInstance.Zone, targetInstance.Name),
Network: g.NetworkURL(),
Priority: 1000,
Description: k8sNodeRouteTag,
}
2019-09-27 21:51:53 +00:00
err = g.c.Routes().Insert(timeoutCtx, meta.GlobalKey(cr.Name), cr)
2019-08-30 18:33:25 +00:00
if isHTTPErrorCode(err, http.StatusConflict) {
klog.Infof("Route %q already exists.", cr.Name)
err = nil
}
return mc.Observe(err)
}
// DeleteRoute from the cloud environment.
func (g *Cloud) DeleteRoute(ctx context.Context, clusterName string, route *cloudprovider.Route) error {
2020-03-26 21:07:15 +00:00
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Hour)
2019-08-30 18:33:25 +00:00
defer cancel()
mc := newRoutesMetricContext("delete")
2019-09-27 21:51:53 +00:00
return mc.Observe(g.c.Routes().Delete(timeoutCtx, meta.GlobalKey(route.Name)))
2019-08-30 18:33:25 +00:00
}
func truncateClusterName(clusterName string) string {
if len(clusterName) > 26 {
return clusterName[:26]
}
return clusterName
}