Skip to content

Commit

Permalink
add otterize export clientintents cli command; Bump golang to v1.23…
Browse files Browse the repository at this point in the history
….1; bump actions versions (#164)
  • Loading branch information
amitlicht authored Oct 21, 2024
1 parent 2189e19 commit 076f9f1
Show file tree
Hide file tree
Showing 28 changed files with 10,466 additions and 5,873 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-fork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Check out the code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.22
go-version: 1.23.1

- name: Install dependencies
if: matrix.os == 'macos-latest'
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Check out the code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.22
go-version: 1.23.1

- name: Install dependencies
if: matrix.os == 'macos-latest'
Expand Down Expand Up @@ -66,17 +66,17 @@ jobs:
- shell: bash
run: |
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: matrix.os == 'ubuntu-latest'
with:
path: dist/linux
name: linux-${{ env.sha_short }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: matrix.os == 'macos-latest'
with:
path: dist/darwin
name: darwin-${{ env.sha_short }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: matrix.os == 'windows-latest'
with:
path: dist/windows
Expand Down
28 changes: 15 additions & 13 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: golangci-lint
name: lint
on:
pull_request:
types:
Expand All @@ -13,32 +13,34 @@ permissions:
# pull-requests: read

jobs:
golangci:
name: golangci
go-lint:
name: go-lint
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.22

go-version: 1.23.1
- name: go vet
run: go vet ./...
working-directory: .

- name: staticcheck
uses: dominikh/staticcheck-action@v1.3.1
with:
version: "2024.1.1"
install-go: false
working-directory: .
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.55.2
version: v1.61.0

# Optional: working directory, useful for monorepos
working-directory: .

# Optional: golangci-lint command line arguments.
args: --timeout 5m
args: --timeout 2m --out-format=colored-line-number

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
Expand Down
32 changes: 16 additions & 16 deletions .github/workflows/release-tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.22
go-version: 1.23.1
cache: true


Expand Down Expand Up @@ -60,17 +60,17 @@ jobs:
- shell: bash
run: |
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: matrix.os == 'ubuntu-latest'
with:
path: dist/linux
name: linux-${{ env.sha_short }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: matrix.os == 'macos-latest'
with:
path: dist/darwin
name: darwin-${{ env.sha_short }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: matrix.os == 'windows-latest'
with:
path: dist/windows
Expand All @@ -80,12 +80,12 @@ jobs:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.22
go-version: 1.23.1
cache: true

- name: GCP auth
Expand All @@ -96,15 +96,15 @@ jobs:
# copy the caches from prepare
- shell: bash
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
path: dist/linux
name: linux-${{ env.sha_short }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
path: dist/darwin
name: darwin-${{ env.sha_short }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
path: dist/windows
name: windows-${{ env.sha_short }}
Expand Down Expand Up @@ -137,7 +137,7 @@ jobs:
# For macOS, we need to modify the hashes for the zips ot match
# those of the notarized ZIPs, rather than the build output zips.
- name: Checkout homebrew-otterize
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
token: ${{ secrets.OTTERIZEBOT_GITHUB_TOKEN }}
repository: otterize/homebrew-otterize
Expand All @@ -149,7 +149,7 @@ jobs:
steps:

- name: Checkout docs
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
token: ${{ secrets.OTTERIZEBOT_GITHUB_TOKEN }}
repository: otterize/docs
Expand All @@ -172,7 +172,7 @@ jobs:
needs: release
steps:
- name: Checkout docs
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
token: ${{ secrets.OTTERIZEBOT_GITHUB_TOKEN }}
repository: otterize/network-mapper
Expand All @@ -195,7 +195,7 @@ jobs:
needs: release
steps:
- name: Checkout CLI
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
token: ${{ secrets.OTTERIZEBOT_GITHUB_TOKEN }}
path: ./cli
Expand Down
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 32 additions & 12 deletions src/cmd/accessgraph/get/get-accessgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ const (
servicesIdsKey = "services-ids"
)

func getInputIncludeFilterFromViper(key string) *map[string]any {
if viper.IsSet(key) {
return &map[string]any{
"include": lo.ToPtr(viper.GetStringSlice(key)),
}
}
return nil
}

func getInputTimeFilterValueFromViper(key string) (*map[string]any, error) {
if viper.IsSet(key) {
lastSeenAfterStr := viper.GetString(key)
lastSeenAfter, err := time.Parse(time.RFC3339, lastSeenAfterStr)
if err != nil {
return nil, fmt.Errorf("invalid last seen after format: %s", err)
}
return &map[string]any{
"operator": lo.ToPtr(cloudapi.AFTER),
"value": lo.ToPtr(lastSeenAfter),
}, nil
}
return nil, nil
}

var GetAccessGraph = &cobra.Command{
Use: "get",
Short: "Get access graph",
Expand All @@ -35,21 +59,17 @@ var GetAccessGraph = &cobra.Command{
}

filter := cloudapi.InputAccessGraphFilter{
IncludeServicesWithNoEdges: lo.ToPtr(true),
ClusterIds: lo.Ternary(viper.IsSet(clustersIdsKey), lo.ToPtr(viper.GetStringSlice(clustersIdsKey)), nil),
EnvironmentIds: lo.Ternary(viper.IsSet(envIdsKey), lo.ToPtr(viper.GetStringSlice(envIdsKey)), nil),
NamespaceIds: lo.Ternary(viper.IsSet(namespacesIdsKey), lo.ToPtr(viper.GetStringSlice(namespacesIdsKey)), nil),
ServiceIds: lo.Ternary(viper.IsSet(servicesIdsKey), lo.ToPtr(viper.GetStringSlice(servicesIdsKey)), nil),
ClusterIds: getInputIncludeFilterFromViper(clustersIdsKey),
EnvironmentIds: getInputIncludeFilterFromViper(envIdsKey),
NamespaceIds: getInputIncludeFilterFromViper(namespacesIdsKey),
ServiceIds: getInputIncludeFilterFromViper(servicesIdsKey),
}

if viper.IsSet(lastSeenAfterKey) {
lastSeenAfterStr := viper.GetString(lastSeenAfterKey)
lastSeenAfter, err := time.Parse(time.RFC3339, lastSeenAfterStr)
if err != nil {
return fmt.Errorf("invalid last seen after format: %s", err)
}
filter.LastSeenAfter = &lastSeenAfter
lastSeenFilter, err := getInputTimeFilterValueFromViper(lastSeenAfterKey)
if err != nil {
return err
}
filter.LastSeen = lastSeenFilter

r, err := c.AccessGraphQueryWithResponse(ctxTimeout, cloudapi.AccessGraphQueryJSONRequestBody{Filter: &filter})
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions src/cmd/clientintents/clientintents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package clientintents

import (
"github.com/otterize/otterize-cli/src/cmd/clientintents/export"
"github.com/otterize/otterize-cli/src/cmd/groups"
"github.com/otterize/otterize-cli/src/pkg/cloudclient"
"github.com/spf13/cobra"
)

var ClientIntentsCmd = &cobra.Command{
Use: "clientintents",
GroupID: groups.ResourcesGroup.ID,
Short: "Get service client intents information",
}

func init() {
cloudclient.RegisterAPIFlags(ClientIntentsCmd)
ClientIntentsCmd.AddCommand(export.ExportClientIntentsCmd)
}
80 changes: 80 additions & 0 deletions src/cmd/clientintents/export/export-clientintents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package export

import (
"context"
cloudclient "github.com/otterize/otterize-cli/src/pkg/cloudclient/restapi"
"github.com/otterize/otterize-cli/src/pkg/cloudclient/restapi/cloudapi"
"github.com/otterize/otterize-cli/src/pkg/config"
"github.com/otterize/otterize-cli/src/pkg/output"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

const (
outputLocationKey = "output"
outputLocationShorthand = "o"
)

var ExportClientIntentsCmd = &cobra.Command{
Use: "export <service-id>",
Short: "Export client intents for a service",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(_ *cobra.Command, args []string) error {
ctxTimeout, cancel := context.WithTimeout(context.Background(), config.DefaultTimeout)
defer cancel()

c, err := cloudclient.NewClient(ctxTimeout)
if err != nil {
return err
}

id := args[0]
r, err := c.ServiceClientIntentsQueryWithResponse(ctxTimeout, id, cloudapi.ServiceClientIntentsQueryJSONRequestBody{
AsServiceId: nil,
ClusterIds: nil,
EnableInternetIntents: lo.ToPtr(true),
FeatureFlags: nil,
LastSeenAfter: nil,
})
if err != nil {
return err
}

serviceClientIntents := r.JSON200.AsClient
s := output.FormatClientIntents(serviceClientIntents)

outputLocation := viper.GetString(outputLocationKey)
if outputLocation == "" {
output.PrintStdout(s)
} else {
err := writeIntentsFile(outputLocation, s)
if err != nil {
return err
}
output.PrintStderr("Successfully wrote intents into %s", outputLocation)
}
return nil
},
}

func writeIntentsFile(filePath string, content string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()

_, err = f.WriteString(content)
if err != nil {
return err
}
return nil
}

func init() {
ExportClientIntentsCmd.Flags().StringP(outputLocationKey, outputLocationShorthand, "", "file path to write the output into")

}
Loading

0 comments on commit 076f9f1

Please sign in to comment.