Skip to content

Commit aaff4e8

Browse files
committed
Fixed issue with iterating over rules
1 parent 48df31d commit aaff4e8

File tree

9 files changed

+181
-131
lines changed

9 files changed

+181
-131
lines changed

go.mod

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@ module github.com/mensaah/reka
33
go 1.14
44

55
require (
6-
github.com/aws/aws-sdk-go-v2 v0.28.0
7-
github.com/aws/aws-sdk-go-v2/config v0.2.1
8-
github.com/aws/aws-sdk-go-v2/credentials v0.1.3
9-
github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.1
10-
github.com/aws/aws-sdk-go-v2/service/ec2 v0.28.0
11-
github.com/aws/aws-sdk-go-v2/service/s3 v0.28.0
12-
github.com/fsnotify/fsnotify v1.4.9 // indirect
6+
github.com/aws/aws-sdk-go-v2 v0.29.0
7+
github.com/aws/aws-sdk-go-v2/config v0.2.2
8+
github.com/aws/aws-sdk-go-v2/credentials v0.1.4
9+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.2
10+
github.com/aws/aws-sdk-go-v2/service/ec2 v0.29.0
11+
github.com/aws/aws-sdk-go-v2/service/s3 v0.29.0
1312
github.com/gin-gonic/gin v1.6.3
14-
github.com/go-co-op/gocron v0.3.1
15-
github.com/go-sql-driver/mysql v1.5.0 // indirect
13+
github.com/go-co-op/gocron v0.3.2
1614
github.com/jinzhu/now v1.1.1
1715
github.com/labstack/gommon v0.3.0
18-
github.com/pelletier/go-toml v1.8.1 // indirect
19-
github.com/sirupsen/logrus v1.4.2
20-
github.com/spf13/pflag v1.0.3
21-
github.com/spf13/viper v1.7.0
22-
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff // indirect
23-
gopkg.in/yaml.v2 v2.3.0 // indirect
24-
gorm.io/driver/postgres v1.0.0
25-
gorm.io/driver/sqlite v1.1.2
26-
gorm.io/gorm v1.20.0
16+
github.com/sirupsen/logrus v1.7.0
17+
github.com/spf13/pflag v1.0.5
18+
github.com/spf13/viper v1.7.1
19+
gorm.io/driver/postgres v1.0.5
20+
gorm.io/driver/sqlite v1.1.3
21+
gorm.io/gorm v1.20.5
2722
)

go.sum

Lines changed: 54 additions & 86 deletions
Large diffs are not rendered by default.

provider/aws/ec2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesOutput, re
2020
for _, reservation := range output.Reservations {
2121
for _, instance := range reservation.Instances {
2222
// https://stackoverflow.com/a/48554123/7167357
23-
tags := utils.ParseTags(*(*[]utils.AWSTag)(unsafe.Pointer(&instance.Tags)))
23+
tags := utils.ParseTags(*(*[]*utils.AWSTag)(unsafe.Pointer(&instance.Tags)))
2424

2525
// We need the creation-date when parsing Tags for relative defintions
2626
// EC2 Instances Launch Time is not the creation date. It's the time it was last launched.

provider/aws/s3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func getS3BucketTags(svc *s3.Client, bucketName string, logger *log.Entry) (reso
4343
return resource.Tags{}, err
4444
}
4545
// https://stackoverflow.com/a/48554123/7167357
46-
tags := utils.ParseTags(*(*[]utils.AWSTag)(unsafe.Pointer(&result.TagSet)))
46+
tags := utils.ParseTags(*((*[]*utils.AWSTag)(unsafe.Pointer(&result.TagSet))))
4747
return tags, nil
4848
}
4949

provider/aws/utils/tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type AWSTag struct {
1111
}
1212

1313
// ParseTags Returns a valid Tags type from AWS Instance Tags
14-
func ParseTags(tags []AWSTag) resource.Tags {
14+
func ParseTags(tags []*AWSTag) resource.Tags {
1515
t := make(resource.Tags)
1616

1717
for _, tag := range tags {

provider/provider.go

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
// resources managed by the manager
1414
type Resources map[string][]*resource.Resource
1515

16+
type SafeResources struct {
17+
mu sync.Mutex
18+
v Resources
19+
err map[string]error
20+
}
21+
1622
// Provider : Provider definition
1723
// Implements all logic for controlling Resource Managers
1824
type Provider struct {
@@ -25,67 +31,86 @@ type Provider struct {
2531
func (p *Provider) GetAllResources() Resources {
2632
p.Logger.Info("Fetching All Resources")
2733
var wg sync.WaitGroup
28-
resources := make(Resources)
34+
resources := SafeResources{v: make(Resources)}
2935
for _, resMgr := range p.Managers {
3036
wg.Add(1)
31-
go func(res Resources, resMgr *resource.Manager) {
37+
go func(res *SafeResources, resMgr *resource.Manager) {
3238
defer wg.Done()
39+
3340
resMgrResources, err := resMgr.GetAll()
3441
if err != nil {
3542
resMgr.Logger.Error(err)
3643
}
37-
res[resMgr.Name] = resMgrResources
38-
}(resources, resMgr)
44+
res.mu.Lock()
45+
defer res.mu.Unlock()
46+
res.v[resMgr.Name] = resMgrResources
47+
}(&resources, resMgr)
3948
}
4049
wg.Wait()
41-
return resources
50+
return resources.v
4251
}
4352

4453
// GetDestroyableResources : Return the resources which can be destroyed
4554
func (p *Provider) GetDestroyableResources(resources Resources) Resources {
4655
p.Logger.Debug("Getting Destroyable Resources")
56+
count := 0
4757
destroyableResources := make(Resources)
4858
for mgrName, resList := range resources {
4959
var destroyableResList []*resource.Resource
5060
for _, r := range resList {
51-
if rules.GetResourceAction(r) == rules.Destroy {
52-
destroyableResList = append(destroyableResList, r)
61+
// Returns the first Matching Rule Action for a resource
62+
for _, rule := range rules.GetRules() {
63+
if action := rule.CheckResource(r); action == rules.Destroy {
64+
destroyableResList = append(destroyableResList, r)
65+
}
5366
}
5467
}
68+
count += len(destroyableResList)
5569
destroyableResources[mgrName] = destroyableResList
5670
}
71+
p.Logger.Infof("Found %d resources to be destroyed", count)
5772
return destroyableResources
5873
}
5974

6075
// GetStoppableResources : Return the resources which can be stopped
6176
func (p *Provider) GetStoppableResources(resources Resources) Resources {
6277
p.Logger.Debug("Getting Stoppable Resources")
6378
stoppableResources := make(Resources)
79+
count := 0
6480
for mgrName, resList := range resources {
6581
var stoppableResList []*resource.Resource
6682
for _, r := range resList {
67-
if r.IsActive() && rules.GetResourceAction(r) == rules.Stop {
68-
stoppableResList = append(stoppableResList, r)
83+
for _, rule := range rules.GetRules() {
84+
if action := rule.CheckResource(r); r.IsActive() && action == rules.Stop {
85+
stoppableResList = append(stoppableResList, r)
86+
}
6987
}
7088
}
89+
count += len(stoppableResList)
7190
stoppableResources[mgrName] = stoppableResList
7291
}
92+
p.Logger.Infof("Found %d resources to be stopped", count)
7393
return stoppableResources
7494
}
7595

7696
// GetResumableResources : Return the resources which can be Resumed
7797
func (p *Provider) GetResumableResources(resources Resources) Resources {
7898
p.Logger.Debug("Getting resumable Resources")
7999
resumableResource := make(Resources)
100+
count := 0
80101
for mgrName, resList := range resources {
81102
var resumableResList []*resource.Resource
82103
for _, r := range resList {
83-
if r.IsStopped() && rules.GetResourceAction(r) == rules.Resume {
84-
resumableResList = append(resumableResList, r)
104+
for _, rule := range rules.GetRules() {
105+
if action := rule.CheckResource(r); r.IsStopped() && action == rules.Resume {
106+
resumableResList = append(resumableResList, r)
107+
}
85108
}
86109
}
87110
resumableResource[mgrName] = resumableResList
88111
}
112+
count += len(resumableResource)
113+
p.Logger.Infof("Found %d resources to be resumed", count)
89114
return resumableResource
90115
}
91116

@@ -98,14 +123,20 @@ func (p *Provider) GetUnusedResources(resources Resources) Resources {
98123
func (p *Provider) DestroyResources(resources Resources) map[string]string {
99124
errs := make(map[string]string)
100125
p.Logger.Debugf("Destroying Resources...")
126+
var wg sync.WaitGroup
101127

102128
for mgrName, res := range resources {
103-
mgr := p.getManager(mgrName)
104-
mgr.Logger.Debugf("Destroying %s ", mgrName)
105-
if err := mgr.Destroy(res); err != nil {
106-
errs[mgrName] = err.Error()
107-
}
129+
wg.Add(1)
130+
go func(mgrName string, res []*resource.Resource) {
131+
defer wg.Done()
132+
mgr := p.getManager(mgrName)
133+
mgr.Logger.Debugf("Destroying %s ", mgrName)
134+
if err := mgr.Destroy(res); err != nil {
135+
errs[mgrName] = err.Error()
136+
}
137+
}(mgrName, res)
108138
}
139+
wg.Wait()
109140
return errs
110141
}
111142

rules/action_enumer.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rules/rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ func GetRules() []Ruler {
114114
func GetResourceAction(res *resource.Resource) Action {
115115
// Returns the first Matching Rule Action for a resource
116116
for _, r := range rules {
117-
if r.CheckResource(res) != DoNothing {
118-
return r.CheckResource(res)
117+
if action := r.CheckResource(res); action != DoNothing {
118+
return action
119119
}
120120
}
121121
return DoNothing

web/main.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/mensaah/reka/config"
3131
"github.com/mensaah/reka/provider"
3232
"github.com/mensaah/reka/provider/aws"
33+
"github.com/mensaah/reka/resource"
3334
"github.com/mensaah/reka/rules"
3435
"github.com/mensaah/reka/web/controllers"
3536
"github.com/mensaah/reka/web/models"
@@ -53,7 +54,10 @@ func main() {
5354
cfg := config.GetConfig()
5455

5556
for _, rule := range cfg.Rules {
56-
rules.ParseRule(*((*rules.Rule)(unsafe.Pointer(&rule))))
57+
// Convert Rule in config to rules.Rule type
58+
r := *((*rules.Rule)(unsafe.Pointer(&rule)))
59+
r.Tags = resource.Tags(rule.Tags)
60+
rules.ParseRule(r)
5761
}
5862

5963
// Initialize Provider objects
@@ -138,10 +142,10 @@ func refreshResources(providers []*provider.Provider) {
138142
}
139143
}
140144
destroyableResources := provider.GetDestroyableResources(allResources)
141-
fmt.Println(destroyableResources)
142-
// stoppableResources := provider.GetStoppableResources(allResources)
143-
// save(stoppableResources)
144-
// resumableResources := provider.GetResumableResources(allResources)
145-
// save(resumableResources)
145+
fmt.Println("Destroyable Resources: ", destroyableResources)
146+
stoppableResources := provider.GetStoppableResources(allResources)
147+
fmt.Println("Stoppable Resources: ", stoppableResources)
148+
resumableResources := provider.GetResumableResources(allResources)
149+
fmt.Println("Resumable Resources: ", resumableResources)
146150
}
147151
}

0 commit comments

Comments
 (0)