Skip to content

Commit

Permalink
Merge branch 'more-testing' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jan 13, 2022
2 parents eeb79fa + 258d887 commit e510741
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 55 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Test

on:
push:
branches: [ main ]
pull_request:

jobs:
Expand Down Expand Up @@ -54,4 +53,4 @@ jobs:
run: make build

- name: Test
run: go test -v ./...
run: make test
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ LDFLAGS += -X github.com/AliyunContainerService/ack-ram-tool/pkg/version.GitComm
build:
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags "$(LDFLAGS)" -a -o ack-ram-tool \
cmd/ack-ram-tool/main.go

.PHONY: test
test:
go test -v ./...

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ack-ram-tool

A command line utility and library for using RAM in ACK.
A command line utility and library for using RAM in Alibaba Cloud Container Service For Kubernetes (ACK).

## Installation

Expand Down Expand Up @@ -61,7 +61,7 @@ Diable RRSA feature:
$ ack-ram-tool rrsa disable -c <clusterId>
? Are you sure you want to disable RRSA feature? Yes
Disable RRSA feature for cluster c86fdd096c5ab482982cf33302f914af4 successfully
Disable RRSA feature for cluster c86fdd*** successfully
```

Expand Down
7 changes: 4 additions & 3 deletions cmd/ack-ram-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import (
)

const (
envAccessKeyId = "ALIBABA_CLOUD_ACCESS_KEY_ID"
envAccessKeyId = "ALIBABA_CLOUD_ACCESS_KEY_ID"
envAccessKeySecret = "ALIBABA_CLOUD_ACCESS_KEY_SECRET"
)

var defaultProfilePath = filepath.Join("~", ".alibabacloud", "credentials")
var profilePath = ""

var (
rootCmd = &cobra.Command{
Use: "ack-ram-tool",
Short: "A command line utility for using RAM in ACK.",
Long: `A command line utility for using RAM in ACK.
Short: "A command line utility for using RAM in Alibaba Cloud Container Service For Kubernetes (ACK).",
Long: `A command line utility for using RAM in Alibaba Cloud Container Service For Kubernetes (ACK).
More info: https://github.com/AliyunContainerService/ack-ram-tool`,
PersistentPreRun: func(_ *cobra.Command, _ []string) {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.16

require (
github.com/AlecAivazis/survey/v2 v2.3.2
github.com/alibabacloud-go/cs-20151215/v3 v3.0.2
github.com/alibabacloud-go/darabonba-openapi v0.1.7
github.com/alibabacloud-go/cs-20151215/v3 v3.0.3
github.com/alibabacloud-go/darabonba-openapi v0.1.11
github.com/alibabacloud-go/openapi-util v0.0.9
github.com/alibabacloud-go/ram-20150501 v1.0.1
github.com/alibabacloud-go/sts-20150401 v1.1.0
Expand All @@ -17,4 +17,5 @@ require (
github.com/fatih/color v1.13.0
github.com/sergi/go-diff v1.2.0
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
)
10 changes: 7 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/alibabacloud-go/cs-20151215/v3 v3.0.2 h1:sWCtbdz7rMQwZ+mM7Uphv58Wjv1WO5kSIPdBpTDHXUk=
github.com/alibabacloud-go/cs-20151215/v3 v3.0.2/go.mod h1:oWOvuDqXXjNkCr4d/78iNBdYY16U5TrEfZ+qTt3T160=
github.com/alibabacloud-go/cs-20151215/v3 v3.0.3 h1:cs2L7Cw3UQOGQR8kUz0V6/e7a75GGMNrVafheytyJGA=
github.com/alibabacloud-go/cs-20151215/v3 v3.0.3/go.mod h1:Q/ZLsgxaeobHYc2VHgUd4bc67MWPO7Bmq6ZPa1QjqmU=
github.com/alibabacloud-go/darabonba-openapi v0.0.9/go.mod h1:YGpp3JTSkPwUK5vH/qFWbxTpfgWp44GkN45QcPAaOyc=
github.com/alibabacloud-go/darabonba-openapi v0.1.7 h1:W0uSIzejswpz02ILRgEMFFkMZGAnfpB6BjrGvbOjCK0=
github.com/alibabacloud-go/darabonba-openapi v0.1.7/go.mod h1:6FV1Bt1AItYIlC2rVopPTumrRNtkfPBmrPVAZ8v2bLk=
github.com/alibabacloud-go/darabonba-openapi v0.1.11 h1:w59gtSA0s87p0U5NNG/N/PIHsRP3rtj7qCP9hx9+GL8=
github.com/alibabacloud-go/darabonba-openapi v0.1.11/go.mod h1:MPJMxv7HYrFm5m9uOZWkDYsAWyZztEgnBRfk9Fg0eIU=
github.com/alibabacloud-go/darabonba-string v1.0.0/go.mod h1:93cTfV3vuPhhEwGGpKKqhVW4jLe7tDpo3LUM0i0g6mA=
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68 h1:NqugFkGxx1TXSh/pBcU00Y6bljgDPaFdh5MUSeJ7e50=
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68/go.mod h1:6pb/Qy8c+lqua8cFpEy7g39NRRqOWc3rOwAy8m5Y2BY=
Expand Down Expand Up @@ -227,6 +228,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down Expand Up @@ -255,6 +257,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
Expand Down Expand Up @@ -643,6 +646,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/rrsa/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ func getRRSAFailMessage(ctx context.Context, clusterId string, client openapi.CS
// TODO: xxx
return ""
}
max := 20
n := 0
for _, log := range logs {
n++
if n >= max {
break
}
if !strings.Contains(log.Log, "Failed") {
continue
}
Expand Down
30 changes: 2 additions & 28 deletions pkg/openapi/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (

"github.com/AliyunContainerService/ack-ram-tool/pkg/types"
cs "github.com/alibabacloud-go/cs-20151215/v3/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
openapiutil "github.com/alibabacloud-go/openapi-util/service"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
)

Expand Down Expand Up @@ -86,34 +83,11 @@ func convertDescribeClusterDetailResponse(c *types.Cluster, resp *cs.DescribeClu

func (c *Client) GetRecentClusterLogs(ctx context.Context, clusterId string) ([]types.ClusterLog, error) {
client := c.csClient
runtime := &util.RuntimeOptions{}
headers := make(map[string]*string)
_result := &cs.DescribeClusterLogsResponse{}
clusterId = *(openapiutil.GetEncodeParam(&clusterId))
req := &openapi.OpenApiRequest{
Headers: headers,
Query: map[string]*string{"limit": tea.String("100")},
}
params := &openapi.Params{
Action: tea.String("DescribeClusterLogs"),
Version: tea.String("2015-12-15"),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/clusters/" + clusterId + "/logs"),
Method: tea.String("GET"),
AuthType: tea.String("AK"),
Style: tea.String("ROA"),
ReqBodyType: tea.String("json"),
BodyType: tea.String("array"),
}
_body, err := client.CallApi(params, req, runtime)
if err != nil {
return nil, err
}
err = tea.Convert(_body, &_result)
ret, err := client.DescribeClusterLogs(&clusterId)
if err != nil {
return nil, err
}
return convertDescribeClusterLogsResponse(_result), nil
return convertDescribeClusterLogsResponse(ret), nil
}

func convertDescribeClusterLogsResponse(resp *cs.DescribeClusterLogsResponse) []types.ClusterLog {
Expand Down
38 changes: 23 additions & 15 deletions pkg/types/ram.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,33 @@ func MakeAssumeRolePolicyStatementWithServiceAccount(oidcIssuer, oidcArn, namesp
}
}

func (p AssumeRolePolicyDocument) AppendPolicy(policy AssumeRolePolicyStatement) error {
if len(p) == 0 {
v := &p
*v = MakeAssumeRolePolicyDocument([]AssumeRolePolicyStatement{})
func (p *AssumeRolePolicyDocument) AppendPolicyIfNotExist(policy AssumeRolePolicyStatement) error {
if exist, err := p.IncludePolicy(policy); err != nil {
return err
} else if exist {
return nil
}
return p.AppendPolicy(policy)
}

func (p *AssumeRolePolicyDocument) AppendPolicy(policy AssumeRolePolicyStatement) error {
if p == nil || len(*p) == 0 {
*p = MakeAssumeRolePolicyDocument([]AssumeRolePolicyStatement{})
}
if _, ok := p[AssumeRolePolicyKeyStatement]; !ok {
p[AssumeRolePolicyKeyStatement] = []AssumeRolePolicyStatement{}
if _, ok := (*p)[AssumeRolePolicyKeyStatement]; !ok {
(*p)[AssumeRolePolicyKeyStatement] = []AssumeRolePolicyStatement{}
}
policies, err := p.policies()
if err != nil {
return err
}

policies = append(policies, policy)
p[AssumeRolePolicyKeyStatement] = policies
(*p)[AssumeRolePolicyKeyStatement] = policies
return nil
}

func (p AssumeRolePolicyDocument) IncludePolicy(policy AssumeRolePolicyStatement) (bool, error) {
func (p *AssumeRolePolicyDocument) IncludePolicy(policy AssumeRolePolicyStatement) (bool, error) {
policies, err := p.policies()
if err != nil {
log.Printf("parse statements failed: %+v", err)
Expand All @@ -88,19 +96,19 @@ func (p AssumeRolePolicyDocument) IncludePolicy(policy AssumeRolePolicyStatement
return false, nil
}

func (p AssumeRolePolicyDocument) JSON() string {
func (p *AssumeRolePolicyDocument) JSON() string {
data, err := json.MarshalIndent(p, " ", " ")
if err != nil {
log.Printf("errro: %+v\n", err)
}
return string(data)
}

func (p AssumeRolePolicyDocument) policies() ([]AssumeRolePolicyStatement, error) {
if len(p) == 0 {
func (p *AssumeRolePolicyDocument) policies() ([]AssumeRolePolicyStatement, error) {
if p == nil || len(*p) == 0 {
return nil, nil
}
statements, ok := p[AssumeRolePolicyKeyStatement]
statements, ok := (*p)[AssumeRolePolicyKeyStatement]
if !ok {
return nil, nil
}
Expand All @@ -117,20 +125,20 @@ func (p AssumeRolePolicyDocument) policies() ([]AssumeRolePolicyStatement, error
return policies, nil
}

func (s AssumeRolePolicyStatement) Equal(another AssumeRolePolicyStatement) bool {
func (s *AssumeRolePolicyStatement) Equal(another AssumeRolePolicyStatement) bool {
if reflect.DeepEqual(s, another) {
return true
}
if utils.JSONEqual(s, another) {
return true
}
if utils.StringInterfaceMapEqual(s, another) {
if utils.StringInterfaceMapEqual(*s, another) {
return true
}
return false
}

func (s AssumeRolePolicyStatement) JSON() string {
func (s *AssumeRolePolicyStatement) JSON() string {
data, _ := json.Marshal(s)
return string(data)
}
Loading

0 comments on commit e510741

Please sign in to comment.