get_cluster_info.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package aws
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/aws/aws-sdk-go-v2/aws"
  8. "github.com/aws/aws-sdk-go-v2/service/ec2"
  9. ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
  10. "github.com/aws/aws-sdk-go-v2/service/eks"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/apierrors"
  14. "github.com/porter-dev/porter/api/server/shared/config"
  15. "github.com/porter-dev/porter/api/types"
  16. "github.com/porter-dev/porter/internal/models"
  17. "gorm.io/gorm"
  18. )
  19. type GetClusterInfoHandler struct {
  20. handlers.PorterHandlerReadWriter
  21. }
  22. func NewGetClusterInfoHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *GetClusterInfoHandler {
  27. return &GetClusterInfoHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. }
  30. }
  31. func (c *GetClusterInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  32. ctx := r.Context()
  33. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  34. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  35. if cluster.AWSIntegrationID == 0 {
  36. c.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("no AWS cluster found with cluster ID: %d", cluster.ID)))
  37. return
  38. }
  39. awsInt, err := c.Repo().AWSIntegration().ReadAWSIntegration(proj.ID, cluster.AWSIntegrationID)
  40. if err != nil {
  41. if errors.Is(err, gorm.ErrRecordNotFound) {
  42. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("no AWS integration found with project ID: %d and "+
  43. "integration ID: %d", proj.ID, cluster.AWSIntegrationID)))
  44. return
  45. }
  46. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error fetching AWS integration with project ID: %d and "+
  47. "integration ID: %d. Error: %w", proj.ID, cluster.AWSIntegrationID, err)))
  48. return
  49. }
  50. // clusterName := cluster.Name
  51. if strings.HasPrefix(cluster.Name, "arn:aws:eks:") {
  52. parts := strings.Split(cluster.Name, "/")
  53. cluster.Name = parts[len(parts)-1]
  54. }
  55. awsConf := awsInt.Config()
  56. eksSvc := eks.NewFromConfig(awsConf)
  57. clusterInfo, err := eksSvc.DescribeCluster(ctx, &eks.DescribeClusterInput{
  58. Name: &cluster.Name,
  59. })
  60. if err != nil || clusterInfo.Cluster == nil {
  61. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  62. return
  63. }
  64. ec2Svc := ec2.NewFromConfig(awsConf)
  65. res := &types.GetAWSClusterInfoResponse{
  66. Name: cluster.Name,
  67. ARN: *clusterInfo.Cluster.Arn,
  68. Status: string(clusterInfo.Cluster.Status),
  69. K8sVersion: *clusterInfo.Cluster.Version,
  70. EKSVersion: *clusterInfo.Cluster.PlatformVersion,
  71. }
  72. subnetPaginate := ec2.NewDescribeSubnetsPaginator(ec2Svc, &ec2.DescribeSubnetsInput{
  73. Filters: []ec2Types.Filter{
  74. {
  75. Name: aws.String("vpc-id"),
  76. Values: []string{
  77. *clusterInfo.Cluster.ResourcesVpcConfig.VpcId,
  78. },
  79. },
  80. },
  81. })
  82. for subnetPaginate.HasMorePages() {
  83. page, err := subnetPaginate.NextPage(ctx)
  84. if err != nil {
  85. continue
  86. }
  87. for _, subnet := range page.Subnets {
  88. res.Subnets = append(res.Subnets, &types.AWSSubnet{
  89. SubnetID: *subnet.SubnetId,
  90. AvailabilityZone: *subnet.AvailabilityZone,
  91. AvailableIPAddressCount: int64(*subnet.AvailableIpAddressCount),
  92. })
  93. }
  94. }
  95. if err != nil {
  96. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  97. return
  98. }
  99. c.WriteResult(w, r, res)
  100. }