Skip to content

Commit

Permalink
tidy up (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
surajnarwade authored Jun 4, 2023
1 parent a73c5ff commit 9c58fb3
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 116 deletions.
21 changes: 3 additions & 18 deletions cmd/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ import (
// addonsCmd represents the addons command
var addonsCmd = &cobra.Command{
Use: "addons",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: addons,
Short: "List Addons with current and recommended versions",
Long: "List Addons with current and recommended versions",
RunE: addons,
}

type Addons struct {
Expand Down Expand Up @@ -103,14 +98,4 @@ func init() {
rootCmd.AddCommand(addonsCmd)
addonsCmd.PersistentFlags().String("cluster-name", "", "Cluster name")
addonsCmd.PersistentFlags().String("region", "", "region")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addonsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addonsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
11 changes: 3 additions & 8 deletions cmd/ami-suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ import (
// suggestionCmd represents the suggestion command
var suggestionCmd = &cobra.Command{
Use: "suggest-ami",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: suggestion,
Short: "Suggest recommended version of AMI",
Long: "Suggest recommended version of AMI",
RunE: suggestion,
}

func suggestion(cmd *cobra.Command, args []string) error {
Expand Down
11 changes: 3 additions & 8 deletions cmd/fargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ import (
// fargateCmd represents the fargate command
var fargateCmd = &cobra.Command{
Use: "fargate",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: fargate,
Short: "List fargate profiles",
Long: "List fargate profiles",
RunE: fargate,
}

func fargate(cmd *cobra.Command, args []string) error {
Expand Down
11 changes: 3 additions & 8 deletions cmd/irsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ import (
// irsaCmd represents the irsa command
var irsaCmd = &cobra.Command{
Use: "irsa",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: irsa,
Short: "List Serviceaccounts with their IRSA information",
Long: "List Serviceaccounts with their IRSA information",
RunE: irsa,
}

func irsa(cmd *cobra.Command, args []string) error {
Expand Down
11 changes: 3 additions & 8 deletions cmd/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ import (
// kubeconfigCmd represents the kubeconfig command
var kubeconfigCmd = &cobra.Command{
Use: "kubeconfig",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: kubeconfigCommand,
Short: "Get Kubeconfig for given cluster",
Long: "Get Kubeconfig for given cluster",
RunE: kubeconfigCommand,
}

// Constants for kubeconfig
Expand Down
11 changes: 3 additions & 8 deletions cmd/nodegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ import (
// nodegroupsCmd represents the nodegroups command
var nodegroupsCmd = &cobra.Command{
Use: "nodegroups",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: nodegroups,
Short: "List EKS Nodegroups",
Long: "List EKS Nodegroups",
RunE: nodegroups,
}

func nodegroups(cmd *cobra.Command, args []string) error {
Expand Down
30 changes: 5 additions & 25 deletions cmd/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,23 @@ import (
"log"
"os"
"text/tabwriter"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/spf13/cobra"
"github.com/surajincloud/kubectl-eks/pkg/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
)

// nodesCmd represents the nodes command
var nodesCmd = &cobra.Command{
Use: "nodes",
Short: "List all EKS Nodes",
Short: "List EKS Nodes",
Long: `A better way to list EKS nodes`,

RunE: nodes,
}

func init() {
rootCmd.AddCommand(nodesCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// nodesCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// nodesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func nodes(cmd *cobra.Command, args []string) error {
nodeList, err := kube.GetNodes(KubernetesConfigFlags)
if err != nil {
Expand All @@ -52,7 +35,7 @@ func nodes(cmd *cobra.Command, args []string) error {
region, _ := cmd.Flags().GetString("region")

// get Clustername
clusterName, err := kube.GetClusterName(clusterName)
clusterName, err = kube.GetClusterName(clusterName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -71,7 +54,7 @@ func nodes(cmd *cobra.Command, args []string) error {
defer w.Flush()
fmt.Fprintln(w, "NAME", "\t", "INSTANCE-TYPE", "\t", "OS", "\t", "CAPACITY-TYPE", "\t", "REGION", "\t", "AMI-ID", "\t", "AMI-NAME", "\t", "AGE")
for _, i := range nodeList {
age := getAge(i.CreationTimestamp)
age := kube.GetAge(i.CreationTimestamp)
img := i.Labels[kube.NodeGroupImage]
dis, _ := ec2Client.DescribeImages(ctx, &ec2.DescribeImagesInput{ImageIds: []string{img}})

Expand All @@ -81,9 +64,6 @@ func nodes(cmd *cobra.Command, args []string) error {
return nil
}

func getAge(creationStamp metav1.Time) string {

currentTime := time.Now()
diff := currentTime.Sub(creationStamp.Time)
return duration.HumanDuration(diff)
func init() {
rootCmd.AddCommand(nodesCmd)
}
24 changes: 8 additions & 16 deletions cmd/ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,13 @@ import (
var ssmCmd = &cobra.Command{
Use: "ssm",
Short: "Access given EKS node via SSM",
Long: `SSM Access to given EKS Node
IAM Roles needs to be attached to given EKS Node`,
Long: `
SSM Access to given EKS Node
IAM Roles needs to be attached to given EKS Node
Check docs: https://surajincloud.github.io/kubectl-eks/usage/#access-to-eks-node-via-ssm`,
RunE: performSSM,
}

func init() {
rootCmd.AddCommand(ssmCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// ssmCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// ssmCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func performSSM(cmd *cobra.Command, args []string) error {
nodeList, err := kube.GetNodes(KubernetesConfigFlags)
if err != nil {
Expand Down Expand Up @@ -64,3 +52,7 @@ func performSSM(cmd *cobra.Command, args []string) error {
return nil

}

func init() {
rootCmd.AddCommand(ssmCmd)
}
19 changes: 2 additions & 17 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ import (
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of kubectl-eks",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: version,
Long: "Print the version of kubectl-eks",
RunE: version,
}

func version(cmd *cobra.Command, args []string) error {
Expand All @@ -26,14 +21,4 @@ func version(cmd *cobra.Command, args []string) error {

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
12 changes: 12 additions & 0 deletions pkg/kube/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package kube
import (
"fmt"
"os"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/util/duration"
)

func GetClusterName(clusterName string) (string, error) {
Expand All @@ -26,3 +31,10 @@ func GetRegion(region string) (string, error) {
}
return region, nil
}

func GetAge(creationStamp metav1.Time) string {

currentTime := time.Now()
diff := currentTime.Sub(creationStamp.Time)
return duration.HumanDuration(diff)
}

0 comments on commit 9c58fb3

Please sign in to comment.