Skip to content

Commit e6a0a0e

Browse files
committed
*: add "pkg/aws/ssm"
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
1 parent 5229df8 commit e6a0a0e

File tree

10 files changed

+105
-30
lines changed

10 files changed

+105
-30
lines changed

CHANGELOG/CHANGELOG-1.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ See [code changes](https://github.com/aws/aws-k8s-tester/compare/v1.1.6...v1.1.7
6666
- Reorder [`AddOnAppMesh` to the front](https://github.com/aws/aws-k8s-tester/commit/843e1c297ef0ce0a093267dd9fa2c1ffefdecba7).
6767
- Fix [`ec2config.ASG.SSMDocumentCreate` `false` for node groups](https://github.com/aws/aws-k8s-tester/commit/cb47ca22ca042be3b6f8464680ec7463e2e8f01f).
6868

69+
### `pkg/aws/ssm`
70+
71+
- Initial [commit](https://github.com/aws/aws-k8s-tester/commit/).
72+
6973
### Dependency
7074

7175
- Upgrade [`github.com/aws/aws-sdk-go`](https://github.com/aws/aws-sdk-go/releases) from [`v1.30.21`](https://github.com/aws/aws-sdk-go/releases/tag/v1.30.21) to [`v1.30.22`](https://github.com/aws/aws-sdk-go/releases/tag/v1.30.22).

ec2/asgs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,15 @@ type templateASG struct {
367367
UserData string
368368
}
369369

370+
const rfc3339Micro = "2006-01-02T15:04:05.999Z07:00"
371+
370372
func (ts *Tester) createASGs() (err error) {
371373
createStart := time.Now()
372374
defer func() {
373375
ts.cfg.CreateTook += time.Since(createStart)
374376
ts.cfg.CreateTookString = ts.cfg.CreateTook.String()
375377
ts.cfg.TimeUTCCreateComplete = time.Now().UTC()
376-
ts.cfg.TimeUTCCreateCompleteRFC3339Micro = ts.cfg.TimeUTCCreateComplete.Format(ec2config.RFC3339Micro)
378+
ts.cfg.TimeUTCCreateCompleteRFC3339Micro = ts.cfg.TimeUTCCreateComplete.Format(rfc3339Micro)
377379
ts.cfg.Sync()
378380
}()
379381

@@ -626,7 +628,7 @@ func (ts *Tester) deleteASGs() (err error) {
626628
var errs []string
627629

628630
ts.cfg.TimeUTCDeleteStart = time.Now().UTC()
629-
ts.cfg.TimeUTCDeleteStartRFC3339Micro = ts.cfg.TimeUTCDeleteStart.Format(ec2config.RFC3339Micro)
631+
ts.cfg.TimeUTCDeleteStartRFC3339Micro = ts.cfg.TimeUTCDeleteStart.Format(rfc3339Micro)
630632
ts.cfg.Sync()
631633

632634
ts.lg.Info("deleting ASGs using CFN", zap.String("name", ts.cfg.Name))

ec2config/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ const (
4040
ASGMaxLimit = 100
4141
)
4242

43-
// RFC3339Micro is the RFC3339 time format.
44-
const RFC3339Micro = "2006-01-02T15:04:05.999Z07:00"
45-
4643
// Config defines EC2 configuration.
4744
type Config struct {
4845
mu *sync.RWMutex

ec2config/default.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
asgs:
2-
ec2-2020050616-awesomegue4u-asg:
2+
ec2-2020050700-accountable3-asg:
33
ami-type: AL2_x86_64
44
asg-cfn-stack-id: ""
55
asg-desired-capacity: 1
@@ -11,7 +11,7 @@ asgs:
1111
instance-types:
1212
- c5.xlarge
1313
logs: null
14-
name: ec2-2020050616-awesomegue4u-asg
14+
name: ec2-2020050700-accountable3-asg
1515
remote-access-user-name: ec2-user
1616
ssm-document-cfn-stack-id: ""
1717
ssm-document-cfn-stack-name: ""
@@ -22,7 +22,7 @@ asgs:
2222
ssm-document-name: ""
2323
volume-size: 40
2424
asgs-fetch-logs: true
25-
asgs-logs-dir: /home/ANT.AMAZON.COM/leegyuho/go/src/github.com/aws/aws-k8s-tester/ec2config/ec2-2020050616-awesomegue4u-logs-remote
25+
asgs-logs-dir: /home/ANT.AMAZON.COM/leegyuho/go/src/github.com/aws/aws-k8s-tester/ec2config/ec2-2020050700-accountable3-logs-remote
2626
aws-account-id: ""
2727
aws-credential-path: ""
2828
aws-iam-role-arn: ""
@@ -34,7 +34,7 @@ log-level: info
3434
log-outputs:
3535
- stderr
3636
- /home/ANT.AMAZON.COM/leegyuho/go/src/github.com/aws/aws-k8s-tester/ec2config/default.yaml.log
37-
name: ec2-2020050616-awesomegue4u
37+
name: ec2-2020050700-accountable3
3838
on-failure-delete: true
3939
on-failure-delete-wait-seconds: 120
4040
partition: aws
@@ -43,13 +43,13 @@ public-subnet-ids: null
4343
region: us-west-2
4444
remote-access-commands-output-path: /home/ANT.AMAZON.COM/leegyuho/go/src/github.com/aws/aws-k8s-tester/ec2config/default.ssh.sh
4545
remote-access-key-create: true
46-
remote-access-key-name: ec2-2020050616-awesomegue4u-key
47-
remote-access-private-key-path: /tmp/reinventhn.insecure.key
46+
remote-access-key-name: ec2-2020050700-accountable3-key
47+
remote-access-private-key-path: /tmp/hikingk8lg.insecure.key
4848
role-arn: ""
4949
role-cfn-stack-id: ""
5050
role-create: true
5151
role-managed-policy-arns: null
52-
role-name: ec2-2020050616-awesomegue4u-role
52+
role-name: ec2-2020050700-accountable3-role
5353
role-service-principals: null
5454
s3-bucket-create: false
5555
s3-bucket-create-keep: false

eks/eks-cluster.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ func (ts *Tester) createCluster() (err error) {
134134
return ts.createSubTesters()
135135
}
136136

137+
const rfc3339Micro = "2006-01-02T15:04:05.999Z07:00"
138+
137139
func (ts *Tester) createEKS() (err error) {
138140
createStart := time.Now()
139141
defer func() {
140142
ts.cfg.Status.CreateTook = time.Since(createStart)
141143
ts.cfg.Status.CreateTookString = ts.cfg.Status.CreateTook.String()
142144
ts.cfg.Status.TimeUTCCreateComplete = time.Now().UTC()
143-
ts.cfg.Status.TimeUTCCreateCompleteRFC3339Micro = ts.cfg.Status.TimeUTCCreateComplete.Format(eksconfig.RFC3339Micro)
145+
ts.cfg.Status.TimeUTCCreateCompleteRFC3339Micro = ts.cfg.Status.TimeUTCCreateComplete.Format(rfc3339Micro)
144146
ts.cfg.Sync()
145147
}()
146148

eks/eks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ func (ts *Tester) down() (err error) {
15291529
}()
15301530

15311531
ts.cfg.Status.TimeUTCDeleteStart = time.Now().UTC()
1532-
ts.cfg.Status.TimeUTCDeleteStartRFC3339Micro = ts.cfg.Status.TimeUTCDeleteStart.Format(eksconfig.RFC3339Micro)
1532+
ts.cfg.Status.TimeUTCDeleteStartRFC3339Micro = ts.cfg.Status.TimeUTCDeleteStart.Format(rfc3339Micro)
15331533
ts.cfg.Sync()
15341534

15351535
var errs []string

eksconfig/default.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ log-level: info
2121
log-outputs:
2222
- stderr
2323
- /home/ANT.AMAZON.COM/leegyuho/go/src/github.com/aws/aws-k8s-tester/eksconfig/default.log
24-
name: eks-2020050616-splendid46e5
24+
name: eks-2020050700-hawaii0cwxb6
2525
on-failure-delete: true
2626
on-failure-delete-wait-seconds: 120
2727
parameters:
@@ -38,7 +38,7 @@ parameters:
3838
role-cfn-stack-id: ""
3939
role-create: true
4040
role-managed-policy-arns: null
41-
role-name: eks-2020050616-splendid46e5-role
41+
role-name: eks-2020050700-hawaii0cwxb6-role
4242
role-service-principals: null
4343
signing-name: eks
4444
tags: null
@@ -51,7 +51,7 @@ partition: aws
5151
region: us-west-2
5252
remote-access-commands-output-path: /home/ANT.AMAZON.COM/leegyuho/go/src/github.com/aws/aws-k8s-tester/eksconfig/default.ssh.sh
5353
remote-access-key-create: true
54-
remote-access-key-name: eks-2020050616-splendid46e5-key-nodes
54+
remote-access-key-name: eks-2020050700-hawaii0cwxb6-key-nodes
5555
remote-access-private-key-path: /home/ANT.AMAZON.COM/leegyuho/.ssh/kube_aws_rsa
5656
s3-bucket-create: false
5757
s3-bucket-create-keep: false

eksconfig/status.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
aws_eks "github.com/aws/aws-sdk-go/service/eks"
88
)
99

10-
// RFC3339Micro is the RFC3339 time format.
11-
const RFC3339Micro = "2006-01-02T15:04:05.999Z07:00"
12-
1310
// Status represents the current status of AWS resources.
1411
// Read-only. Cannot be configured via environmental variables.
1512
type Status struct {

pkg/aws/ssm/ssm.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Package ssm implements common SSM utilities.
2+
package ssm
3+
4+
import (
5+
"time"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/service/ssm"
9+
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
10+
"go.uber.org/zap"
11+
)
12+
13+
// FetchConfig is the SSM fetch configuration.
14+
type FetchConfig struct {
15+
Logger *zap.Logger
16+
SSMAPI ssmiface.SSMAPI
17+
DocumentName string
18+
InvokedAfter time.Time
19+
BatchLimit int64
20+
BatchInterval time.Duration
21+
}
22+
23+
const rfc3339Micro = "2006-01-02T15:04:05.999Z07:00"
24+
25+
// FetchOutputs downloads SSM doc run outputs.
26+
// It only returns the invocation whose status is "Success".
27+
// e.g. aws ssm list-command-invocations --details --filter key=DocumentName,value=ResourceCounterSSMDocDevStack
28+
func FetchOutputs(cfg FetchConfig) (ivs []*ssm.CommandInvocation, err error) {
29+
cfg.Logger.Info("fetching SSM doc outputs", zap.String("document-name", cfg.DocumentName), zap.Int64("batch-limit", cfg.BatchLimit))
30+
input := &ssm.ListCommandInvocationsInput{
31+
Details: aws.Bool(true),
32+
MaxResults: aws.Int64(cfg.BatchLimit),
33+
Filters: []*ssm.CommandFilter{
34+
{
35+
Key: aws.String("DocumentName"),
36+
Value: aws.String(cfg.DocumentName),
37+
},
38+
{
39+
Key: aws.String("InvokedAfter"),
40+
Value: aws.String(cfg.InvokedAfter.Format(rfc3339Micro)),
41+
},
42+
},
43+
}
44+
var output *ssm.ListCommandInvocationsOutput
45+
for {
46+
output, err = cfg.SSMAPI.ListCommandInvocations(input)
47+
if err != nil {
48+
cfg.Logger.Warn("failed to fetch SSM doc outputs", zap.Error(err))
49+
return nil, err
50+
}
51+
rs := output.CommandInvocations
52+
n := len(rs)
53+
if n == 0 {
54+
break
55+
}
56+
for _, rv := range rs {
57+
if aws.StringValue(rv.Status) == "Success" {
58+
ivs = append(ivs, rv)
59+
}
60+
}
61+
token := aws.StringValue(output.NextToken)
62+
input.NextToken = aws.String(token)
63+
cfg.Logger.Info("received SSM command invocation outputs", zap.Int("received", n), zap.Int("total", len(ivs)))
64+
if token == "" {
65+
break
66+
}
67+
time.Sleep(cfg.BatchInterval)
68+
}
69+
cfg.Logger.Info("fetching SSM doc outputs", zap.Int("total", len(ivs)))
70+
return ivs, nil
71+
}

pkg/k8s-object/node.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ type NodeInfo struct {
1717
// only numbers and dots
1818
var regexVer = regexp.MustCompile("[^.0-9]+")
1919

20+
// ParseVersion parses the version string.
21+
// Returns 0.0 if not valid.
22+
func ParseVersion(s string) (v float64) {
23+
vs := strings.Split(regexVer.ReplaceAllString(s, ""), ".")
24+
if len(vs) > 2 {
25+
v, _ = strconv.ParseFloat(strings.Join(vs[:2], "."), 64)
26+
}
27+
return v
28+
}
29+
2030
// ParseNodeInfo parses the node info.
2131
// e.g. {"machineID":"","systemUUID":"","bootID":"","kernelVersion":"4.14.173-137.229.amzn2.x86_64","osImage":"Amazon Linux 2","containerRuntimeVersion":"docker://19.3.6","kubeletVersion":"v1.16.8-eks-e16311","kubeProxyVersion":"v1.16.8-eks-e16311","operatingSystem":"linux","architecture":"amd64"}
2232
func ParseNodeInfo(info v1.NodeSystemInfo) (nodeInfo NodeInfo) {
2333
nodeInfo = NodeInfo{NodeSystemInfo: info}
24-
25-
kss := strings.Split(regexVer.ReplaceAllString(nodeInfo.KubeletVersion, ""), ".")
26-
if len(kss) > 2 {
27-
nodeInfo.KubeletMinorVersionValue, _ = strconv.ParseFloat(strings.Join(kss[:2], "."), 64)
28-
}
29-
pss := strings.Split(regexVer.ReplaceAllString(nodeInfo.KubeProxyVersion, ""), ".")
30-
if len(kss) > 2 {
31-
nodeInfo.KubeProxyMinorVersionValue, _ = strconv.ParseFloat(strings.Join(pss[:2], "."), 64)
32-
}
33-
34+
nodeInfo.KubeletMinorVersionValue = ParseVersion(nodeInfo.KubeletVersion)
35+
nodeInfo.KubeProxyMinorVersionValue = ParseVersion(nodeInfo.KubeProxyVersion)
3436
return nodeInfo
3537
}

0 commit comments

Comments
 (0)