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

90 lines
2.6 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 2018 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 (
2019-09-27 21:51:53 +00:00
"context"
2019-08-30 18:33:25 +00:00
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
compute "google.golang.org/api/compute/v1"
2020-01-15 01:52:20 +00:00
option "google.golang.org/api/option"
2019-08-30 18:33:25 +00:00
"k8s.io/client-go/tools/cache"
)
// TestClusterValues holds the config values for the fake/test gce cloud object.
type TestClusterValues struct {
ProjectID string
Region string
ZoneName string
SecondaryZoneName string
ClusterID string
ClusterName string
2020-03-26 21:07:15 +00:00
OnXPN bool
2019-08-30 18:33:25 +00:00
}
// DefaultTestClusterValues Creates a reasonable set of default cluster values
// for generating a new test fake GCE cloud instance.
func DefaultTestClusterValues() TestClusterValues {
return TestClusterValues{
ProjectID: "test-project",
Region: "us-central1",
ZoneName: "us-central1-b",
SecondaryZoneName: "us-central1-c",
ClusterID: "test-cluster-id",
ClusterName: "Test Cluster Name",
}
}
// Stubs ClusterID so that ClusterID.getOrInitialize() does not require calling
// gce.Initialize()
func fakeClusterID(clusterID string) ClusterID {
return ClusterID{
clusterID: &clusterID,
store: cache.NewStore(func(obj interface{}) (string, error) {
return "", nil
}),
}
}
// NewFakeGCECloud constructs a fake GCE Cloud from the cluster values.
func NewFakeGCECloud(vals TestClusterValues) *Cloud {
2020-01-15 01:52:20 +00:00
service, err := compute.NewService(context.Background(), option.WithoutAuthentication())
if err != nil {
panic(err)
}
2019-08-30 18:33:25 +00:00
gce := &Cloud{
region: vals.Region,
service: service,
managedZones: []string{vals.ZoneName},
projectID: vals.ProjectID,
networkProjectID: vals.ProjectID,
ClusterID: fakeClusterID(vals.ClusterID),
2020-03-26 21:07:15 +00:00
onXPN: vals.OnXPN,
2020-08-10 17:43:49 +00:00
metricsCollector: newLoadBalancerMetrics(),
2019-08-30 18:33:25 +00:00
}
c := cloud.NewMockGCE(&gceProjectRouter{gce})
gce.c = c
return gce
}
2020-03-26 21:07:15 +00:00
// UpdateFakeGCECloud updates the fake GCE cloud with the specified values. Currently only the onXPN value is updated.
func UpdateFakeGCECloud(g *Cloud, vals TestClusterValues) {
g.onXPN = vals.OnXPN
}