Skip to content

Commit

Permalink
Snpashot cmds filtering consistency (#350)
Browse files Browse the repository at this point in the history
* add function-names-regex flag to snapshot lambda

* refactor snapshot k8s

* bump deps

* fix linting errors
  • Loading branch information
sami-alajrami authored Oct 9, 2024
1 parent 601278e commit 82993c9
Show file tree
Hide file tree
Showing 14 changed files with 789 additions and 599 deletions.
9 changes: 6 additions & 3 deletions cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,13 @@ The ^.kosli_ignore^ will be treated as part of the artifact like any other file,
ecsExcludeClustersRegexFlag = "[optional] The comma-separated list of ECS cluster name regex patterns to exclude. Can't be used together with --clusters or --clusters-regex."
ecsServiceFlag = "[optional] The name of the ECS service."
kubeconfigFlag = "[defaulted] The kubeconfig path for the target cluster."
namespaceFlag = "[conditional] The comma separated list of namespaces regex patterns to report artifacts info from. Can't be used together with --exclude-namespace."
excludeNamespaceFlag = "[conditional] The comma separated list of namespaces regex patterns NOT to report artifacts info from. Can't be used together with --namespace."
namespacesFlag = "[optional] The comma separated list of namespaces names to report artifacts info from. Can't be used together with --exclude-namespaces or --exclude-namespaces-regex."
excludeNamespacesFlag = "[optional] The comma separated list of namespaces names to exclude from reporting artifacts info from. Can't be used together with --namespaces or --namespaces-regex."
namespacesRegexFlag = "[optional] The comma separated list of namespaces regex patterns to report artifacts info from. Can't be used together with --exclude-namespaces --exclude-namespaces-regex."
excludeNamespacesRegexFlag = "[optional] The comma separated list of namespaces regex patterns to exclude from reporting artifacts info from. Can't be used together with --namespaces or --namespaces-regex."
functionNameFlag = "[optional] The name of the AWS Lambda function."
functionNamesFlag = "[optional] The comma-separated list of AWS Lambda function names to be reported."
functionNamesFlag = "[optional] The comma-separated list of AWS Lambda function names to be reported. Cannot be used together with --exclude or --exclude-regex."
functionNamesRegexFlag = "[optional] The comma-separated list of AWS Lambda function names regex patterns to be reported. Cannot be used together with --exclude or --exclude-regex."
excludeFlag = "[optional] The comma-separated list of AWS Lambda function names to be excluded. Cannot be used together with --function-names"
excludeRegexFlag = "[optional] The comma-separated list of name regex patterns for AWS Lambda functions to be excluded. Cannot be used together with --function-names. Allowed regex patterns are described in https://github.com/google/re2/wiki/Syntax"
functionVersionFlag = "[optional] The version of the AWS Lambda function."
Expand Down
4 changes: 2 additions & 2 deletions cmd/kosli/snapshotDocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/kosli-dev/cli/internal/digest"
"github.com/kosli-dev/cli/internal/requests"
Expand Down Expand Up @@ -90,7 +90,7 @@ func CreateDockerArtifactsData() ([]*server.ServerData, error) {
return result, err
}

containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
containers, err := cli.ContainerList(context.Background(), container.ListOptions{})
if err != nil {
return result, err
}
Expand Down
24 changes: 9 additions & 15 deletions cmd/kosli/snapshotECS.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/kosli-dev/cli/internal/aws"
"github.com/kosli-dev/cli/internal/filters"
"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -56,18 +57,15 @@ kosli snapshot ecs yourEnvironmentName \
`

type snapshotECSOptions struct {
clusters []string
clustersRegex []string
exclude []string
excludeRegex []string
filter *filters.ResourceFilterOptions
serviceName string
cluster string
awsStaticCreds *aws.AWSStaticCreds
}

func newSnapshotECSCmd(out io.Writer) *cobra.Command {
o := new(snapshotECSOptions)
o.awsStaticCreds = new(aws.AWSStaticCreds)
o.filter = new(filters.ResourceFilterOptions)
cmd := &cobra.Command{
Use: "ecs ENVIRONMENT-NAME",
Short: snapshotECSShortDesc,
Expand Down Expand Up @@ -103,12 +101,12 @@ func newSnapshotECSCmd(out io.Writer) *cobra.Command {
},
}

cmd.Flags().StringSliceVar(&o.clusters, "clusters", []string{}, ecsClustersFlag)
cmd.Flags().StringSliceVar(&o.clustersRegex, "clusters-regex", []string{}, ecsClustersRegexFlag)
cmd.Flags().StringSliceVar(&o.exclude, "exclude", []string{}, ecsExcludeClustersFlag)
cmd.Flags().StringSliceVar(&o.excludeRegex, "exclude-regex", []string{}, ecsExcludeClustersRegexFlag)
cmd.Flags().StringSliceVar(&o.filter.IncludeNames, "clusters", []string{}, ecsClustersFlag)
cmd.Flags().StringSliceVar(&o.filter.IncludeNamesRegex, "clusters-regex", []string{}, ecsClustersRegexFlag)
cmd.Flags().StringSliceVar(&o.filter.ExcludeNames, "exclude", []string{}, ecsExcludeClustersFlag)
cmd.Flags().StringSliceVar(&o.filter.ExcludeNamesRegex, "exclude-regex", []string{}, ecsExcludeClustersRegexFlag)

cmd.Flags().StringVarP(&o.cluster, "cluster", "C", "", ecsClusterFlag)
cmd.Flags().StringSliceVarP(&o.filter.IncludeNames, "cluster", "C", []string{}, ecsClusterFlag)
cmd.Flags().StringVarP(&o.serviceName, "service-name", "s", "", ecsServiceFlag)
addAWSAuthFlags(cmd, o.awsStaticCreds)
addDryRunFlag(cmd)
Expand All @@ -128,11 +126,7 @@ func (o *snapshotECSOptions) run(args []string) error {
envName := args[0]
url := fmt.Sprintf("%s/api/v2/environments/%s/%s/report/ECS", global.Host, global.Org, envName)

if o.cluster != "" {
o.clusters = append(o.clusters, o.cluster)
}

tasksData, err := o.awsStaticCreds.GetEcsTasksData(o.clusters, o.clustersRegex, o.exclude, o.excludeRegex)
tasksData, err := o.awsStaticCreds.GetEcsTasksData(o.filter)
if err != nil {
return err
}
Expand Down
18 changes: 12 additions & 6 deletions cmd/kosli/snapshotK8S.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/kosli-dev/cli/internal/filters"
"github.com/kosli-dev/cli/internal/kube"
"github.com/kosli-dev/cli/internal/requests"
homedir "github.com/mitchellh/go-homedir"
Expand All @@ -16,6 +17,7 @@ import (
const snapshotK8SShortDesc = `Report a snapshot of running pods in a K8S cluster or namespace(s) to Kosli. `

const snapshotK8SLongDesc = snapshotK8SShortDesc + `
Skip ^--namespaces^ and ^--namespaces-regex^ to report all pods in all namespaces in a cluster.
The reported data includes pod container images digests and creation timestamps. You can customize the scope of reporting
to include or exclude namespaces.`

Expand Down Expand Up @@ -52,13 +54,15 @@ kosli snapshot k8s yourEnvironmentName \
`

type snapshotK8SOptions struct {
kubeconfig string
namespaces []string
excludeNamespaces []string
kubeconfig string
// namespaces []string
// excludeNamespaces []string
filter *filters.ResourceFilterOptions
}

func newSnapshotK8SCmd(out io.Writer) *cobra.Command {
o := new(snapshotK8SOptions)
o.filter = new(filters.ResourceFilterOptions)
cmd := &cobra.Command{
Use: "k8s ENVIRONMENT-NAME",
Aliases: []string{"kubernetes"},
Expand All @@ -79,8 +83,10 @@ func newSnapshotK8SCmd(out io.Writer) *cobra.Command {
}

cmd.Flags().StringVarP(&o.kubeconfig, "kubeconfig", "k", defaultKubeConfigPath(), kubeconfigFlag)
cmd.Flags().StringSliceVarP(&o.namespaces, "namespaces", "n", []string{}, namespaceFlag)
cmd.Flags().StringSliceVarP(&o.excludeNamespaces, "exclude-namespaces", "x", []string{}, excludeNamespaceFlag)
cmd.Flags().StringSliceVarP(&o.filter.IncludeNames, "namespaces", "n", []string{}, namespacesFlag)
cmd.Flags().StringSliceVar(&o.filter.IncludeNamesRegex, "namespaces-regex", []string{}, namespacesRegexFlag)
cmd.Flags().StringSliceVarP(&o.filter.ExcludeNames, "exclude-namespaces", "x", []string{}, excludeNamespacesFlag)
cmd.Flags().StringSliceVar(&o.filter.ExcludeNamesRegex, "exclude-namespaces-regex", []string{}, excludeNamespacesRegexFlag)
addDryRunFlag(cmd)
return cmd
}
Expand All @@ -92,7 +98,7 @@ func (o *snapshotK8SOptions) run(args []string) error {
if err != nil {
return err
}
podsData, err := kube.GetPodsData(o.namespaces, o.excludeNamespaces, clientset, logger)
podsData, err := clientset.GetPodsData(o.filter, logger)
if err != nil {
return err
}
Expand Down
43 changes: 32 additions & 11 deletions cmd/kosli/snapshotLambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"net/http"

"github.com/kosli-dev/cli/internal/aws"
"github.com/kosli-dev/cli/internal/filters"
"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
)

const snapshotLambdaShortDesc = `Report a snapshot of artifacts deployed as one or more AWS Lambda functions and their digests to Kosli.`

const snapshotLambdaLongDesc = snapshotLambdaShortDesc + `
Skip ^--function-names^ to report all functions in a given AWS account. Or use ^--exclude^ and/or ^--exclude-regex^ to report all functions excluding some.` + awsAuthDesc
Skip ^--function-names^ and ^--function-names-regex^ to report all functions in a given AWS account. Or use ^--exclude^ and/or ^--exclude-regex^ to report all functions excluding some.` + awsAuthDesc

const snapshotLambdaExample = `
# report all Lambda functions running in an AWS account (AWS auth provided in env variables):
Expand Down Expand Up @@ -46,6 +47,16 @@ kosli snapshot lambda yourEnvironmentName \
--api-token yourAPIToken \
--org yourOrgName
# report what is running in the latest version of AWS Lambda functions that match a name regex:
export AWS_REGION=yourAWSRegion
export AWS_ACCESS_KEY_ID=yourAWSAccessKeyID
export AWS_SECRET_ACCESS_KEY=yourAWSSecretAccessKey
kosli snapshot lambda yourEnvironmentName \
--function-names-regex yourFunctionNameRegexPattern \
--api-token yourAPIToken \
--org yourOrgName
# report what is running in the latest version of multiple AWS Lambda functions (AWS auth provided in env variables):
export AWS_REGION=yourAWSRegion
export AWS_ACCESS_KEY_ID=yourAWSAccessKeyID
Expand All @@ -67,16 +78,15 @@ kosli snapshot lambda yourEnvironmentName \
`

type snapshotLambdaOptions struct {
functionNames []string
functionVersion string
excludeNames []string
excludeNamesRegex []string
awsStaticCreds *aws.AWSStaticCreds
functionVersion string
filter *filters.ResourceFilterOptions
awsStaticCreds *aws.AWSStaticCreds
}

func newSnapshotLambdaCmd(out io.Writer) *cobra.Command {
o := new(snapshotLambdaOptions)
o.awsStaticCreds = new(aws.AWSStaticCreds)
o.filter = new(filters.ResourceFilterOptions)
cmd := &cobra.Command{
Use: "lambda ENVIRONMENT-NAME",
Short: snapshotLambdaShortDesc,
Expand All @@ -94,23 +104,34 @@ func newSnapshotLambdaCmd(out io.Writer) *cobra.Command {
return err
}

err = MuXRequiredFlags(cmd, []string{"function-names-regex", "exclude"}, false)
if err != nil {
return err
}

err = MuXRequiredFlags(cmd, []string{"function-name", "function-names", "exclude-regex"}, false)
if err != nil {
return err
}

err = MuXRequiredFlags(cmd, []string{"function-names-regex", "exclude-regex"}, false)
if err != nil {
return err
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(args)
},
}

cmd.Flags().StringSliceVar(&o.functionNames, "function-name", []string{}, functionNameFlag)
cmd.Flags().StringSliceVar(&o.functionNames, "function-names", []string{}, functionNamesFlag)
cmd.Flags().StringSliceVar(&o.filter.IncludeNames, "function-name", []string{}, functionNameFlag)
cmd.Flags().StringSliceVar(&o.filter.IncludeNames, "function-names", []string{}, functionNamesFlag)
cmd.Flags().StringSliceVar(&o.filter.IncludeNamesRegex, "function-names-regex", []string{}, functionNamesRegexFlag)
cmd.Flags().StringVar(&o.functionVersion, "function-version", "", functionVersionFlag)
cmd.Flags().StringSliceVar(&o.excludeNames, "exclude", []string{}, excludeFlag)
cmd.Flags().StringSliceVar(&o.excludeNamesRegex, "exclude-regex", []string{}, excludeRegexFlag)
cmd.Flags().StringSliceVar(&o.filter.ExcludeNames, "exclude", []string{}, excludeFlag)
cmd.Flags().StringSliceVar(&o.filter.ExcludeNamesRegex, "exclude-regex", []string{}, excludeRegexFlag)
addAWSAuthFlags(cmd, o.awsStaticCreds)
addDryRunFlag(cmd)

Expand All @@ -129,7 +150,7 @@ func (o *snapshotLambdaOptions) run(args []string) error {
envName := args[0]

url := fmt.Sprintf("%s/api/v2/environments/%s/%s/report/lambda", global.Host, global.Org, envName)
lambdaData, err := o.awsStaticCreds.GetLambdaPackageData(o.functionNames, o.excludeNames, o.excludeNamesRegex)
lambdaData, err := o.awsStaticCreds.GetLambdaPackageData(o.filter)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 82993c9

Please sign in to comment.