Skip to content

Commit

Permalink
filter out subnets from other zones from the resource
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Apr 21, 2023
1 parent 5834475 commit 6df9486
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
33 changes: 29 additions & 4 deletions internal/provisioner/crossplane_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
crossplaneV1Alpha1 "github.com/mattermost/mattermost-cloud-crossplane/apis/crossplane/v1alpha1"
"github.com/mattermost/mattermost-cloud/internal/supervisor"
"github.com/mattermost/mattermost-cloud/internal/tools/aws"
"github.com/mattermost/mattermost-cloud/internal/tools/utils"
"github.com/mattermost/mattermost-cloud/k8s"
"github.com/mattermost/mattermost-cloud/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -102,9 +103,19 @@ func (provisioner *CrossplaneProvisioner) PrepareCluster(cluster *model.Cluster)
provisioner.logger.WithError(err).WithField("vpc", metadata.VPC).Error("Failed to claim VPC resources")
return false
}

metadata.VPC = resources.VpcID
metadata.PublicSubnets = resources.PublicSubnetsIDs
metadata.PrivateSubnets = resources.PrivateSubnetIDs
for _, subnet := range resources.PublicSubnets {
if utils.Contains[string](cluster.ProviderMetadataAWS.Zones, *subnet.AvailabilityZone) {
metadata.Subnets = append(metadata.Subnets, *subnet.SubnetId)
}
}
for _, subnet := range resources.PrivateSubnets {
if utils.Contains[string](cluster.ProviderMetadataAWS.Zones, *subnet.AvailabilityZone) {
metadata.Subnets = append(metadata.Subnets, *subnet.SubnetId)
metadata.PrivateSubnets = append(metadata.PrivateSubnets, *subnet.SubnetId)
}
}
metadata.AccountID = provisioner.kube2IAMAccountID

return true
Expand Down Expand Up @@ -137,7 +148,7 @@ func (provisioner *CrossplaneProvisioner) CreateCluster(cluster *model.Cluster)
EndpointPrivateAccess: true, // TODO
EndpointPublicAccess: false, // TODO
VpcID: cluster.ProvisionerMetadataCrossplane.VPC,
SubnetIds: cluster.ProvisionerMetadataCrossplane.PublicSubnets,
SubnetIds: cluster.ProvisionerMetadataCrossplane.Subnets,
PrivateSubnetIds: cluster.ProvisionerMetadataCrossplane.PrivateSubnets,
NodeCount: int(cluster.ProvisionerMetadataCrossplane.NodeCount),
InstanceType: cluster.ProvisionerMetadataCrossplane.InstanceType,
Expand All @@ -163,16 +174,30 @@ func (provisioner *CrossplaneProvisioner) CreateCluster(cluster *model.Cluster)

// CheckClusterCreated checks if cluster creation finished.
func (provisioner *CrossplaneProvisioner) CheckClusterCreated(cluster *model.Cluster) (bool, error) {
resource, err := provisioner.kubeClient.CrossplaneClient.CloudV1alpha1().MMK8Ss(crossplaneProvisionerNamespace).Get(context.TODO(), cluster.ID, metav1.GetOptions{})
resources, err := provisioner.kubeClient.CrossplaneClient.CloudV1alpha1().MMK8Ss(crossplaneProvisionerNamespace).List(context.TODO(), metav1.ListOptions{
FieldSelector: fmt.Sprintf("metadata.name=%s", cluster.ProvisionerMetadataCrossplane.Name),
}) //.Get(context.TODO(), cluster.ID, metav1.GetOptions{})
if err != nil && !k8sErrors.IsNotFound(err) {
return false, errors.Wrap(err, "error getting crossplane resource information")
}

if len(resources.Items) == 0 {
return false, fmt.Errorf("no crossplane resource found")
}

if len(resources.Items) > 1 {
return false, fmt.Errorf("expected one eks cluster, found %d", len(resources.Items))
}

resource := resources.Items[0]
ready, err := resource.Status.GetReadyCondition()
if err != nil && !errors.Is(err, crossplaneV1Alpha1.ErrConditionNotFound) {
return false, errors.Wrap(err, "error getting crossplane cluster ready status")
}

provisioner.logger.Warnf("Conditions: %v", resource.Status.Conditions)
provisioner.logger.Warnf("Ready: %v", ready)

if ready == nil {
return false, nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/tools/aws/cluster_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
type ClusterResources struct {
VpcID string
VpcCIDR string
PrivateSubnets []ec2Types.Subnet
PrivateSubnetIDs []string
PublicSubnets []ec2Types.Subnet
PublicSubnetsIDs []string
MasterSecurityGroupIDs []string
WorkerSecurityGroupIDs []string
Expand Down Expand Up @@ -138,6 +140,7 @@ func (a *Client) getClusterResourcesForVPC(vpcID, vpcCIDR string, logger log.Fie
}

for _, subnet := range privateSubnets {
clusterResources.PrivateSubnets = append(clusterResources.PrivateSubnets, subnet)
clusterResources.PrivateSubnetIDs = append(clusterResources.PrivateSubnetIDs, *subnet.SubnetId)
}

Expand All @@ -152,6 +155,7 @@ func (a *Client) getClusterResourcesForVPC(vpcID, vpcCIDR string, logger log.Fie
}

for _, subnet := range publicSubnets {
clusterResources.PublicSubnets = append(clusterResources.PublicSubnets, subnet)
clusterResources.PublicSubnetsIDs = append(clusterResources.PublicSubnetsIDs, *subnet.SubnetId)
}

Expand Down
15 changes: 15 additions & 0 deletions internal/tools/utils/generics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//

package utils

// Contains generic function to check if an item is in an array of items
func Contains[T comparable](haystack []T, needle T) bool {
for _, item := range haystack {
if item == needle {
return true
}
}
return false
}

0 comments on commit 6df9486

Please sign in to comment.