-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: corvofeng <corvofeng@gmail.com>
- Loading branch information
Showing
8 changed files
with
785 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/eks" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func awsCmd(rootCmd *rootCmd) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "aws", | ||
Short: "Display one or many resources", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
awsCMDExec() | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func awsCMDExec() { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String("eu-north-1"), | ||
}) | ||
if err != nil { | ||
fmt.Println("Error creating session:", err) | ||
return | ||
} | ||
|
||
input := &eks.ListClustersInput{} | ||
svc := eks.New(sess) | ||
svc.ListClustersPages(input, func(page *eks.ListClustersOutput, lastPage bool) bool { | ||
for _, cluster := range page.Clusters { | ||
fmt.Println(*cluster) | ||
} | ||
return !lastPage | ||
}) | ||
|
||
// input = &eks.DescribeClusterInput{} | ||
// regions, err := svc.DescribeCluster(input) | ||
// // regions, err := svc.DescribeClusters(&eks.DescribeClustersInput{}) | ||
// if err != nil { | ||
// fmt.Println("Error describing EKS clusters:", err) | ||
// return | ||
// } | ||
|
||
// fmt.Println(regions) | ||
// 遍历每个区域 | ||
// for regions.Next() { | ||
// // 遍历每个集群 | ||
// for _, cluster := range regions.ClusterList { | ||
// fmt.Printf("Cluster: %s\n", *cluster.Name) | ||
// fmt.Printf("ARN: %s\n", *cluster.Arn) | ||
// fmt.Printf("Status: %s\n", *cluster.Status) | ||
// fmt.Println("----") | ||
// } | ||
// } | ||
|
||
// if err := regions.Err(); err != nil { | ||
// fmt.Println("Error paging through EKS clusters:", err) | ||
// return | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
module kubemux | ||
|
||
go 1.20 | ||
go 1.22.0 | ||
|
||
toolchain go1.22.2 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.52.5 | ||
github.com/sirupsen/logrus v1.9.3 | ||
github.com/spf13/cobra v1.8.0 | ||
github.com/stretchr/testify v1.8.4 | ||
github.com/tj/assert v0.0.3 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
k8s.io/client-go v0.30.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-logr/logr v1.4.1 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/jmespath/go-jmespath v0.4.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/stretchr/testify v1.7.0 // indirect | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect | ||
golang.org/x/sys v0.18.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/apimachinery v0.30.0 // indirect | ||
k8s.io/klog/v2 v2.120.1 // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Package internal provides wrapper for creating aws sessions | ||
package aws | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
|
||
cluster "kubemux/lib/kubernetes" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/eks" | ||
"github.com/aws/aws-sdk-go/service/eks/eksiface" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type EKSClient struct { | ||
EKS eksiface.EKSAPI | ||
Region string | ||
} | ||
|
||
func (c *EKSClient) String() string { | ||
return fmt.Sprintf("EKS Client for region %v", c.Region) | ||
} | ||
|
||
// TODO(mmicu): | ||
// - test GetClusters function | ||
// - use assert library in others tests also | ||
func (c *EKSClient) GetClusters(ch chan<- *cluster.Cluster) { | ||
input := &eks.ListClustersInput{} | ||
|
||
err := c.EKS.ListClustersPages(input, | ||
func(page *eks.ListClustersOutput, lastPage bool) bool { | ||
log.WithFields(log.Fields{ | ||
"svc": c.String(), | ||
"page": page.GoString(), | ||
}).Debug("Parse page") | ||
for _, cluster := range page.Clusters { | ||
log.WithFields(log.Fields{ | ||
"svc": c.String(), | ||
"cluster": *cluster, | ||
}).Debug("Found cluster") | ||
if cls, err := c.detailCluster(*cluster); err == nil { | ||
ch <- cls | ||
} else { | ||
log.WithFields(log.Fields{ | ||
"svc": c.String(), | ||
"cluster": *cluster, | ||
"err": err, | ||
}).Warn("Can't get details on the cluster") | ||
} | ||
} | ||
|
||
if lastPage { | ||
log.WithFields(log.Fields{ | ||
"svc": c.String(), | ||
}).Debug("hit last page") | ||
return false | ||
} | ||
return true | ||
}) | ||
|
||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"err": err, | ||
"svc": c.String(), | ||
}).Warn("Can't list clusters") | ||
} | ||
|
||
close(ch) | ||
} | ||
|
||
func (c *EKSClient) detailCluster(cName string) (*cluster.Cluster, error) { | ||
input := &eks.DescribeClusterInput{ | ||
Name: aws.String(cName), | ||
} | ||
|
||
result, err := c.EKS.(input) | ||
if err != nil { | ||
// TODO(mmicu): handle errors better here | ||
if aerr, ok := err.(awserr.Error); ok { | ||
log.Warn(aerr.Error()) | ||
} else { | ||
log.Warn(err.Error()) | ||
} | ||
msg := fmt.Sprintf("Can't fetch more details for the cluster %v", cName) | ||
log.WithFields(log.Fields{ | ||
"cluster-name": cName, | ||
"svc": c.String(), | ||
}).Warn(msg) | ||
return nil, errors.New(msg) | ||
} | ||
|
||
certificatAuthorityData, err := base64.StdEncoding.DecodeString(*result.Cluster.CertificateAuthority.Data) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"cluster-name": *result.Cluster.Name, | ||
"arn": *result.Cluster.Arn, | ||
"certificate-authority-data": *result.Cluster.CertificateAuthority.Data, | ||
"svc": c.String(), | ||
}).Error("Can't decode the Certificate Authority Data") | ||
return nil, err | ||
} | ||
|
||
cls := cluster.NewCluster() | ||
cls.Name = *result.Cluster.Name | ||
cls.ID = *result.Cluster.Arn | ||
cls.Endpoint = *result.Cluster.Endpoint | ||
cls.CertificateAuthorityData = string(certificatAuthorityData) | ||
// cls.Status = *result.Cluster.Status | ||
cls.Region = c.Region | ||
|
||
return cls, nil | ||
} | ||
|
||
func NewEKS(region string) (*EKSClient, error) { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String(region), | ||
}) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"region": region, | ||
"error": err.Error(), | ||
}).Error("Failed to create AWS SDK session") | ||
return nil, err | ||
} | ||
return &EKSClient{ | ||
EKS: eks.New(sess), | ||
Region: region, | ||
}, nil | ||
} |
Oops, something went wrong.