mirror of https://github.com/kubernetes/kops.git
Refactor ListResources to not require passing the Cluster object
This commit is contained in:
parent
3b2947143c
commit
f7d434ee2c
|
@ -32,7 +32,7 @@ import (
|
||||||
"github.com/aws/aws-sdk-go/service/route53"
|
"github.com/aws/aws-sdk-go/service/route53"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
kopsapi "k8s.io/kops/pkg/apis/kops"
|
"k8s.io/kops/pkg/dns"
|
||||||
"k8s.io/kops/pkg/featureflag"
|
"k8s.io/kops/pkg/featureflag"
|
||||||
"k8s.io/kops/pkg/resources"
|
"k8s.io/kops/pkg/resources"
|
||||||
"k8s.io/kops/pkg/resources/spotinst"
|
"k8s.io/kops/pkg/resources/spotinst"
|
||||||
|
@ -50,8 +50,10 @@ const (
|
||||||
|
|
||||||
type listFn func(fi.Cloud, string) ([]*resources.Resource, error)
|
type listFn func(fi.Cloud, string) ([]*resources.Resource, error)
|
||||||
|
|
||||||
func ListResourcesAWS(cloud awsup.AWSCloud, cluster *kopsapi.Cluster) (map[string]*resources.Resource, error) {
|
func ListResourcesAWS(cloud awsup.AWSCloud, clusterInfo resources.ClusterInfo) (map[string]*resources.Resource, error) {
|
||||||
clusterName := cluster.Name
|
clusterName := clusterInfo.Name
|
||||||
|
clusterUsesNoneDNS := clusterInfo.UsesNoneDNS
|
||||||
|
|
||||||
resourceTrackers := make(map[string]*resources.Resource)
|
resourceTrackers := make(map[string]*resources.Resource)
|
||||||
|
|
||||||
// These are the functions that are used for looking up
|
// These are the functions that are used for looking up
|
||||||
|
@ -87,7 +89,7 @@ func ListResourcesAWS(cloud awsup.AWSCloud, cluster *kopsapi.Cluster) (map[strin
|
||||||
ListEventBridgeRules,
|
ListEventBridgeRules,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cluster.IsGossip() && !cluster.UsesNoneDNS() {
|
if !dns.IsGossipClusterName(clusterName) && !clusterUsesNoneDNS {
|
||||||
// Route 53
|
// Route 53
|
||||||
listFunctions = append(listFunctions, ListRoute53Records)
|
listFunctions = append(listFunctions, ListRoute53Records)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ import (
|
||||||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-05-01/network"
|
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-05-01/network"
|
||||||
authz "github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2020-04-01-preview/authorization"
|
authz "github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2020-04-01-preview/authorization"
|
||||||
azureresources "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2021-04-01/resources"
|
azureresources "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2021-04-01/resources"
|
||||||
kopsapi "k8s.io/kops/pkg/apis/kops"
|
|
||||||
"k8s.io/kops/pkg/resources"
|
"k8s.io/kops/pkg/resources"
|
||||||
"k8s.io/kops/upup/pkg/fi"
|
"k8s.io/kops/upup/pkg/fi"
|
||||||
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
|
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
|
||||||
|
@ -44,21 +43,21 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ListResourcesAzure lists all resources for the cluster by quering Azure.
|
// ListResourcesAzure lists all resources for the cluster by quering Azure.
|
||||||
func ListResourcesAzure(cloud azure.AzureCloud, cluster *kopsapi.Cluster) (map[string]*resources.Resource, error) {
|
func ListResourcesAzure(cloud azure.AzureCloud, clusterInfo resources.ClusterInfo) (map[string]*resources.Resource, error) {
|
||||||
g := resourceGetter{
|
g := resourceGetter{
|
||||||
cloud: cloud,
|
cloud: cloud,
|
||||||
cluster: cluster,
|
clusterInfo: clusterInfo,
|
||||||
}
|
}
|
||||||
return g.listResourcesAzure()
|
return g.listResourcesAzure()
|
||||||
}
|
}
|
||||||
|
|
||||||
type resourceGetter struct {
|
type resourceGetter struct {
|
||||||
cloud azure.AzureCloud
|
cloud azure.AzureCloud
|
||||||
cluster *kopsapi.Cluster
|
clusterInfo resources.ClusterInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *resourceGetter) resourceGroupName() string {
|
func (g *resourceGetter) resourceGroupName() string {
|
||||||
return g.cluster.AzureResourceGroupName()
|
return g.clusterInfo.AzureResourceGroupName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *resourceGetter) listResourcesAzure() (map[string]*resources.Resource, error) {
|
func (g *resourceGetter) listResourcesAzure() (map[string]*resources.Resource, error) {
|
||||||
|
@ -129,7 +128,7 @@ func (g *resourceGetter) toResourceGroupResource(rg *azureresources.Group) *reso
|
||||||
ID: *rg.Name,
|
ID: *rg.Name,
|
||||||
Name: *rg.Name,
|
Name: *rg.Name,
|
||||||
Deleter: g.deleteResourceGroup,
|
Deleter: g.deleteResourceGroup,
|
||||||
Shared: g.cluster.IsSharedAzureResourceGroup(),
|
Shared: g.clusterInfo.AzureResourceGroupShared,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +167,7 @@ func (g *resourceGetter) toVirtualNetworkResource(vnet *network.VirtualNetwork)
|
||||||
Name: *vnet.Name,
|
Name: *vnet.Name,
|
||||||
Deleter: g.deleteVirtualNetwork,
|
Deleter: g.deleteVirtualNetwork,
|
||||||
Blocks: []string{toKey(typeResourceGroup, g.resourceGroupName())},
|
Blocks: []string{toKey(typeResourceGroup, g.resourceGroupName())},
|
||||||
Shared: g.cluster.SharedVPC(),
|
Shared: g.clusterInfo.AzureNetworkShared,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +201,7 @@ func (g *resourceGetter) toSubnetResource(subnet *network.Subnet, vnetName strin
|
||||||
toKey(typeVirtualNetwork, vnetName),
|
toKey(typeVirtualNetwork, vnetName),
|
||||||
toKey(typeResourceGroup, g.resourceGroupName()),
|
toKey(typeResourceGroup, g.resourceGroupName()),
|
||||||
},
|
},
|
||||||
Shared: g.cluster.SharedVPC(),
|
Shared: g.clusterInfo.AzureNetworkShared,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,7 +234,7 @@ func (g *resourceGetter) toRouteTableResource(rt *network.RouteTable) *resources
|
||||||
Name: *rt.Name,
|
Name: *rt.Name,
|
||||||
Deleter: g.deleteRouteTable,
|
Deleter: g.deleteRouteTable,
|
||||||
Blocks: []string{toKey(typeResourceGroup, g.resourceGroupName())},
|
Blocks: []string{toKey(typeResourceGroup, g.resourceGroupName())},
|
||||||
Shared: g.cluster.IsSharedAzureRouteTable(),
|
Shared: g.clusterInfo.AzureRouteTableShared,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,7 +468,7 @@ func (g *resourceGetter) deletePublicIPAddress(_ fi.Cloud, r *resources.Resource
|
||||||
// isOwnedByCluster returns true if the resource is owned by the cluster.
|
// isOwnedByCluster returns true if the resource is owned by the cluster.
|
||||||
func (g *resourceGetter) isOwnedByCluster(tags map[string]*string) bool {
|
func (g *resourceGetter) isOwnedByCluster(tags map[string]*string) bool {
|
||||||
for k, v := range tags {
|
for k, v := range tags {
|
||||||
if k == azure.TagClusterName && *v == g.cluster.Name {
|
if k == azure.TagClusterName && *v == g.clusterInfo.Name {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,6 @@ import (
|
||||||
authz "github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2020-04-01-preview/authorization"
|
authz "github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2020-04-01-preview/authorization"
|
||||||
azureresources "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2021-04-01/resources"
|
azureresources "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2021-04-01/resources"
|
||||||
"github.com/Azure/go-autorest/autorest/to"
|
"github.com/Azure/go-autorest/autorest/to"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/kops/pkg/apis/kops"
|
|
||||||
"k8s.io/kops/pkg/resources"
|
"k8s.io/kops/pkg/resources"
|
||||||
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
|
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
|
||||||
"k8s.io/kops/upup/pkg/fi/cloudup/azuretasks"
|
"k8s.io/kops/upup/pkg/fi/cloudup/azuretasks"
|
||||||
|
@ -176,17 +174,10 @@ func TestListResourcesAzure(t *testing.T) {
|
||||||
// Call listResourcesAzure.
|
// Call listResourcesAzure.
|
||||||
g := resourceGetter{
|
g := resourceGetter{
|
||||||
cloud: cloud,
|
cloud: cloud,
|
||||||
cluster: &kops.Cluster{
|
clusterInfo: resources.ClusterInfo{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
Name: clusterName,
|
||||||
Name: clusterName,
|
AzureResourceGroupName: rgName,
|
||||||
},
|
AzureResourceGroupShared: true,
|
||||||
Spec: kops.ClusterSpec{
|
|
||||||
CloudProvider: kops.CloudProviderSpec{
|
|
||||||
Azure: &kops.AzureSpec{
|
|
||||||
ResourceGroupName: rgName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := g.listResourcesAzure()
|
actual, err := g.listResourcesAzure()
|
||||||
|
@ -310,10 +301,8 @@ func TestIsOwnedByCluster(t *testing.T) {
|
||||||
for i, tc := range testCases {
|
for i, tc := range testCases {
|
||||||
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
|
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
|
||||||
g := &resourceGetter{
|
g := &resourceGetter{
|
||||||
cluster: &kops.Cluster{
|
clusterInfo: resources.ClusterInfo{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
Name: clusterName,
|
||||||
Name: clusterName,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
a := g.isOwnedByCluster(tc.tags)
|
a := g.isOwnedByCluster(tc.tags)
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022 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 resources
|
||||||
|
|
||||||
|
type ClusterInfo struct {
|
||||||
|
Name string
|
||||||
|
Region string
|
||||||
|
UsesNoneDNS bool
|
||||||
|
// Azure specific
|
||||||
|
AzureResourceGroupName string
|
||||||
|
AzureResourceGroupShared bool
|
||||||
|
AzureNetworkShared bool
|
||||||
|
AzureRouteTableShared bool
|
||||||
|
}
|
|
@ -45,7 +45,9 @@ const (
|
||||||
|
|
||||||
type listFn func(fi.Cloud, string) ([]*resources.Resource, error)
|
type listFn func(fi.Cloud, string) ([]*resources.Resource, error)
|
||||||
|
|
||||||
func ListResources(cloud do.DOCloud, clusterName string) (map[string]*resources.Resource, error) {
|
func ListResources(cloud do.DOCloud, clusterInfo resources.ClusterInfo) (map[string]*resources.Resource, error) {
|
||||||
|
clusterName := clusterInfo.Name
|
||||||
|
|
||||||
resourceTrackers := make(map[string]*resources.Resource)
|
resourceTrackers := make(map[string]*resources.Resource)
|
||||||
|
|
||||||
listFunctions := []listFn{
|
listFunctions := []listFn{
|
||||||
|
|
|
@ -61,7 +61,11 @@ const maxPrefixTokens = 5
|
||||||
// Maximum length of a GCE route name
|
// Maximum length of a GCE route name
|
||||||
const maxGCERouteNameLength = 63
|
const maxGCERouteNameLength = 63
|
||||||
|
|
||||||
func ListResourcesGCE(gceCloud gce.GCECloud, clusterName string, region string) (map[string]*resources.Resource, error) {
|
func ListResourcesGCE(gceCloud gce.GCECloud, clusterInfo resources.ClusterInfo) (map[string]*resources.Resource, error) {
|
||||||
|
clusterName := clusterInfo.Name
|
||||||
|
clusterUsesNoneDNS := clusterInfo.UsesNoneDNS
|
||||||
|
region := clusterInfo.Region
|
||||||
|
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
if region == "" {
|
if region == "" {
|
||||||
|
@ -105,7 +109,6 @@ func ListResourcesGCE(gceCloud gce.GCECloud, clusterName string, region string)
|
||||||
d.listForwardingRules,
|
d.listForwardingRules,
|
||||||
d.listFirewallRules,
|
d.listFirewallRules,
|
||||||
d.listGCEDisks,
|
d.listGCEDisks,
|
||||||
d.listGCEDNSZone,
|
|
||||||
// TODO: Find routes via instances (via instance groups)
|
// TODO: Find routes via instances (via instance groups)
|
||||||
d.listAddresses,
|
d.listAddresses,
|
||||||
d.listSubnets,
|
d.listSubnets,
|
||||||
|
@ -115,6 +118,10 @@ func ListResourcesGCE(gceCloud gce.GCECloud, clusterName string, region string)
|
||||||
d.listBackendServices,
|
d.listBackendServices,
|
||||||
d.listHealthchecks,
|
d.listHealthchecks,
|
||||||
}
|
}
|
||||||
|
if !dns.IsGossipClusterName(clusterName) && !clusterUsesNoneDNS {
|
||||||
|
listFunctions = append(listFunctions, d.listGCEDNSZone)
|
||||||
|
}
|
||||||
|
|
||||||
for _, fn := range listFunctions {
|
for _, fn := range listFunctions {
|
||||||
resourceTrackers, err := fn()
|
resourceTrackers, err := fn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1282,10 +1289,6 @@ func (d *clusterDiscoveryGCE) isKopsManagedDNSName(name string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *clusterDiscoveryGCE) listGCEDNSZone() ([]*resources.Resource, error) {
|
func (d *clusterDiscoveryGCE) listGCEDNSZone() ([]*resources.Resource, error) {
|
||||||
if dns.IsGossipClusterName(d.clusterName) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var resourceTrackers []*resources.Resource
|
var resourceTrackers []*resources.Resource
|
||||||
|
|
||||||
managedZones, err := d.gceCloud.CloudDNS().ManagedZones().List(d.gceCloud.Project())
|
managedZones, err := d.gceCloud.CloudDNS().ManagedZones().List(d.gceCloud.Project())
|
||||||
|
|
|
@ -39,7 +39,9 @@ const (
|
||||||
|
|
||||||
type listFn func(fi.Cloud, string) ([]*resources.Resource, error)
|
type listFn func(fi.Cloud, string) ([]*resources.Resource, error)
|
||||||
|
|
||||||
func ListResources(cloud hetzner.HetznerCloud, clusterName string) (map[string]*resources.Resource, error) {
|
func ListResources(cloud hetzner.HetznerCloud, clusterInfo resources.ClusterInfo) (map[string]*resources.Resource, error) {
|
||||||
|
clusterName := clusterInfo.Name
|
||||||
|
|
||||||
resourceTrackers := make(map[string]*resources.Resource)
|
resourceTrackers := make(map[string]*resources.Resource)
|
||||||
|
|
||||||
listFunctions := []listFn{
|
listFunctions := []listFn{
|
||||||
|
|
|
@ -31,13 +31,13 @@ type clusterDiscoveryOS struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListResources lists the OpenStack resources kops manages
|
// ListResources lists the OpenStack resources kops manages
|
||||||
func ListResources(cloud openstack.OpenstackCloud, clusterName string) (map[string]*resources.Resource, error) {
|
func ListResources(cloud openstack.OpenstackCloud, clusterInfo resources.ClusterInfo) (map[string]*resources.Resource, error) {
|
||||||
resources := make(map[string]*resources.Resource)
|
resources := make(map[string]*resources.Resource)
|
||||||
|
|
||||||
os := &clusterDiscoveryOS{
|
os := &clusterDiscoveryOS{
|
||||||
cloud: cloud,
|
cloud: cloud,
|
||||||
osCloud: cloud,
|
osCloud: cloud,
|
||||||
clusterName: clusterName,
|
clusterName: clusterInfo.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
listFunctions := []openstackListFn{
|
listFunctions := []openstackListFn{
|
||||||
|
|
|
@ -38,20 +38,29 @@ import (
|
||||||
|
|
||||||
// ListResources collects the resources from the specified cloud
|
// ListResources collects the resources from the specified cloud
|
||||||
func ListResources(cloud fi.Cloud, cluster *kops.Cluster, region string) (map[string]*resources.Resource, error) {
|
func ListResources(cloud fi.Cloud, cluster *kops.Cluster, region string) (map[string]*resources.Resource, error) {
|
||||||
clusterName := cluster.Name
|
clusterInfo := resources.ClusterInfo{
|
||||||
|
Name: cluster.Name,
|
||||||
|
Region: region,
|
||||||
|
UsesNoneDNS: cluster.UsesNoneDNS(),
|
||||||
|
}
|
||||||
|
|
||||||
switch cloud.ProviderID() {
|
switch cloud.ProviderID() {
|
||||||
case kops.CloudProviderAWS:
|
case kops.CloudProviderAWS:
|
||||||
return aws.ListResourcesAWS(cloud.(awsup.AWSCloud), cluster)
|
return aws.ListResourcesAWS(cloud.(awsup.AWSCloud), clusterInfo)
|
||||||
case kops.CloudProviderDO:
|
case kops.CloudProviderDO:
|
||||||
return digitalocean.ListResources(cloud.(clouddo.DOCloud), clusterName)
|
return digitalocean.ListResources(cloud.(clouddo.DOCloud), clusterInfo)
|
||||||
case kops.CloudProviderGCE:
|
case kops.CloudProviderGCE:
|
||||||
return gce.ListResourcesGCE(cloud.(cloudgce.GCECloud), clusterName, region)
|
return gce.ListResourcesGCE(cloud.(cloudgce.GCECloud), clusterInfo)
|
||||||
case kops.CloudProviderHetzner:
|
case kops.CloudProviderHetzner:
|
||||||
return hetzner.ListResources(cloud.(cloudhetzner.HetznerCloud), clusterName)
|
return hetzner.ListResources(cloud.(cloudhetzner.HetznerCloud), clusterInfo)
|
||||||
case kops.CloudProviderOpenstack:
|
case kops.CloudProviderOpenstack:
|
||||||
return openstack.ListResources(cloud.(cloudopenstack.OpenstackCloud), clusterName)
|
return openstack.ListResources(cloud.(cloudopenstack.OpenstackCloud), clusterInfo)
|
||||||
case kops.CloudProviderAzure:
|
case kops.CloudProviderAzure:
|
||||||
return azure.ListResourcesAzure(cloud.(cloudazure.AzureCloud), cluster)
|
clusterInfo.AzureResourceGroupName = cluster.AzureResourceGroupName()
|
||||||
|
clusterInfo.AzureResourceGroupShared = cluster.IsSharedAzureResourceGroup()
|
||||||
|
clusterInfo.AzureNetworkShared = cluster.SharedVPC()
|
||||||
|
clusterInfo.AzureRouteTableShared = cluster.IsSharedAzureRouteTable()
|
||||||
|
return azure.ListResourcesAzure(cloud.(cloudazure.AzureCloud), clusterInfo)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("delete on clusters on %q not (yet) supported", cloud.ProviderID())
|
return nil, fmt.Errorf("delete on clusters on %q not (yet) supported", cloud.ProviderID())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue