Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
legacy: check for machine deployment subnets when collection reserved…
Browse files Browse the repository at this point in the history
… subnets (#2679)
  • Loading branch information
calvix authored Aug 19, 2020
1 parent 1869385 commit 5337e8b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Check subnets from node pools clusters when collecting allocated subnets.

## [5.7.2] - 2020-07-29

- Add support for latest Container Linux AMIs.
Expand Down
6 changes: 6 additions & 0 deletions helm/aws-operator/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ rules:
resources:
- awsclusters
- awsclusters/status
- awscontrolplanes
- awscontrolplanes/status
- awsmachinedeployments
- awsmachinedeployments/status
- g8scontrolplanes
- g8scontrolplanes/status
verbs:
- "*"
- apiGroups:
Expand Down
4 changes: 2 additions & 2 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package project

var (
bundleVersion = "5.7.3"
bundleVersion = "5.7.4-dev"
description = "The aws-operator handles Kubernetes clusters running on a Kubernetes cluster inside of AWS."
gitSHA = "n/a"
name string = "aws-operator"
source string = "https://github.com/giantswarm/aws-operator"
version = "5.7.3"
version = "5.7.4-dev"
)

func BundleVersion() string {
Expand Down
11 changes: 11 additions & 0 deletions service/controller/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"time"

"github.com/giantswarm/apiextensions/pkg/apis/infrastructure/v1alpha2"
infrastructurev1alpha2 "github.com/giantswarm/apiextensions/pkg/apis/infrastructure/v1alpha2"
"github.com/giantswarm/apiextensions/pkg/apis/provider/v1alpha1"
releasev1alpha1 "github.com/giantswarm/apiextensions/pkg/apis/release/v1alpha1"
k8scloudconfig "github.com/giantswarm/k8scloudconfig/v6/pkg/template"
"github.com/giantswarm/microerror"

"github.com/giantswarm/aws-operator/pkg/annotation"
"github.com/giantswarm/aws-operator/pkg/label"
"github.com/giantswarm/aws-operator/pkg/project"
"github.com/giantswarm/aws-operator/service/controller/internal/templates/cloudconfig"
Expand Down Expand Up @@ -338,6 +340,10 @@ func LoadBalancerName(domainName string, cluster v1alpha1.AWSConfig) (string, er
return lbName, nil
}

func MachineDeploymentSubnet(cr infrastructurev1alpha2.AWSMachineDeployment) string {
return cr.Annotations[annotation.MachineDeploymentSubnet]
}

func MainGuestStackName(customObject v1alpha1.AWSConfig) string {
clusterID := ClusterID(customObject)

Expand Down Expand Up @@ -555,6 +561,11 @@ func StatusClusterNetworkCIDR(cluster v1alpha2.AWSCluster) string {
return cluster.Status.Provider.Network.CIDR
}

// StatusAWSInfrastructureClusterNetworkCIDR returns the allocated tenant cluster subnet CIDR.
func StatusAWSInfrastructureClusterNetworkCIDR(cluster infrastructurev1alpha2.AWSCluster) string {
return cluster.Status.Provider.Network.CIDR
}

// StatusNetworkCIDR returns the allocated tenant cluster subnet CIDR.
func StatusNetworkCIDR(customObject v1alpha1.AWSConfig) string {
return customObject.Status.Cluster.Network.CIDR
Expand Down
80 changes: 80 additions & 0 deletions service/controller/resource/ipam/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,38 @@ func (r *Resource) getReservedNetworks(ctx context.Context) ([]net.IPNet, error)
return nil
})

g.Go(func() error {
r.logger.LogCtx(ctx, "level", "debug", "message", "finding allocated subnets from AWSCluster CRs")

subnets, err := getSubnetsFromAWSClusters(r.g8sClient)
if err != nil {
return microerror.Mask(err)
}
mutex.Lock()
reservedSubnets = append(reservedSubnets, subnets...)
mutex.Unlock()

r.logger.LogCtx(ctx, "level", "debug", "message", "found allocated subnets from AWSCluster CRs")

return nil
})

g.Go(func() error {
r.logger.LogCtx(ctx, "level", "debug", "message", "finding allocated subnets from MachineDeployment CRs")

subnets, err := getSubnetsFromMachineDeployments(r.g8sClient)
if err != nil {
return microerror.Mask(err)
}
mutex.Lock()
reservedSubnets = append(reservedSubnets, subnets...)
mutex.Unlock()

r.logger.LogCtx(ctx, "level", "debug", "message", "found allocated subnets from MachineDeployment CRs")

return nil
})

err = g.Wait()
if err != nil {
return nil, microerror.Mask(err)
Expand Down Expand Up @@ -243,6 +275,54 @@ func getClusterSubnets(g8sClient versioned.Interface) ([]net.IPNet, error) {
return results, nil
}

func getSubnetsFromAWSClusters(g8sClient versioned.Interface) ([]net.IPNet, error) {
clusterList, err := g8sClient.InfrastructureV1alpha2().AWSClusters(metav1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
return nil, microerror.Mask(err)
}

var results []net.IPNet
for _, c := range clusterList.Items {
cidr := key.StatusAWSInfrastructureClusterNetworkCIDR(c)
if cidr == "" {
continue
}

_, n, err := net.ParseCIDR(cidr)
if err != nil {
return nil, microerror.Mask(err)
}

results = append(results, *n)
}

return results, nil
}

func getSubnetsFromMachineDeployments(g8sClient versioned.Interface) ([]net.IPNet, error) {
machineDeploymentList, err := g8sClient.InfrastructureV1alpha2().AWSMachineDeployments(metav1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
return nil, microerror.Mask(err)
}

var results []net.IPNet
for _, md := range machineDeploymentList.Items {
cidr := key.MachineDeploymentSubnet(md)
if cidr == "" {
continue
}

_, n, err := net.ParseCIDR(cidr)
if err != nil {
return nil, microerror.Mask(err)
}

results = append(results, *n)
}

return results, nil
}

func getVPCSubnets(ctx context.Context) ([]net.IPNet, error) {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
Expand Down

0 comments on commit 5337e8b

Please sign in to comment.