Skip to content

Commit

Permalink
Fixed issue with iterating over rules
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Nov 28, 2020
1 parent 82e68f9 commit ba9b37c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 125 deletions.
31 changes: 13 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 3,20 @@ module github.com/mensaah/reka
go 1.14

require (
github.com/aws/aws-sdk-go-v2 v0.28.0
github.com/aws/aws-sdk-go-v2/config v0.2.1
github.com/aws/aws-sdk-go-v2/credentials v0.1.3
github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.1
github.com/aws/aws-sdk-go-v2/service/ec2 v0.28.0
github.com/aws/aws-sdk-go-v2/service/s3 v0.28.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/aws/aws-sdk-go-v2 v0.29.0
github.com/aws/aws-sdk-go-v2/config v0.2.2
github.com/aws/aws-sdk-go-v2/credentials v0.1.4
github.com/aws/aws-sdk-go-v2/feature/s3/manager v0.1.2
github.com/aws/aws-sdk-go-v2/service/ec2 v0.29.0
github.com/aws/aws-sdk-go-v2/service/s3 v0.29.0
github.com/gin-gonic/gin v1.6.3
github.com/go-co-op/gocron v0.3.1
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/go-co-op/gocron v0.3.2
github.com/jinzhu/now v1.1.1
github.com/labstack/gommon v0.3.0
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.7.0
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gorm.io/driver/postgres v1.0.0
gorm.io/driver/sqlite v1.1.2
gorm.io/gorm v1.20.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
gorm.io/driver/postgres v1.0.5
gorm.io/driver/sqlite v1.1.3
gorm.io/gorm v1.20.5
)
140 changes: 54 additions & 86 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion provider/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,7 @@ func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesOutput, re
for _, reservation := range output.Reservations {
for _, instance := range reservation.Instances {
// https://stackoverflow.com/a/48554123/7167357
tags := utils.ParseTags(*(*[]utils.AWSTag)(unsafe.Pointer(&instance.Tags)))
tags := utils.ParseTags(*(*[]*utils.AWSTag)(unsafe.Pointer(&instance.Tags)))

// We need the creation-date when parsing Tags for relative defintions
// EC2 Instances Launch Time is not the creation date. It's the time it was last launched.
Expand Down
2 changes: 1 addition & 1 deletion provider/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 43,7 @@ func getS3BucketTags(svc *s3.Client, bucketName string, logger *log.Entry) (reso
return resource.Tags{}, err
}
// https://stackoverflow.com/a/48554123/7167357
tags := utils.ParseTags(*(*[]utils.AWSTag)(unsafe.Pointer(&result.TagSet)))
tags := utils.ParseTags(*((*[]*utils.AWSTag)(unsafe.Pointer(&result.TagSet))))
return tags, nil
}

Expand Down
2 changes: 1 addition & 1 deletion provider/aws/utils/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 11,7 @@ type AWSTag struct {
}

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

for _, tag := range tags {
Expand Down
63 changes: 47 additions & 16 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,12 @@ import (
// resources managed by the manager
type Resources map[string][]*resource.Resource

type SafeResources struct {
mu sync.Mutex
v Resources
err map[string]error
}

// Provider : Provider definition
// Implements all logic for controlling Resource Managers
type Provider struct {
Expand All @@ -25,67 31,86 @@ type Provider struct {
func (p *Provider) GetAllResources() Resources {
p.Logger.Info("Fetching All Resources")
var wg sync.WaitGroup
resources := make(Resources)
resources := SafeResources{v: make(Resources)}
for _, resMgr := range p.Managers {
wg.Add(1)
go func(res Resources, resMgr *resource.Manager) {
go func(res *SafeResources, resMgr *resource.Manager) {
defer wg.Done()

resMgrResources, err := resMgr.GetAll()
if err != nil {
resMgr.Logger.Error(err)
}
res[resMgr.Name] = resMgrResources
}(resources, resMgr)
res.mu.Lock()
defer res.mu.Unlock()
res.v[resMgr.Name] = resMgrResources
}(&resources, resMgr)
}
wg.Wait()
return resources
return resources.v
}

// GetDestroyableResources : Return the resources which can be destroyed
func (p *Provider) GetDestroyableResources(resources Resources) Resources {
p.Logger.Debug("Getting Destroyable Resources")
count := 0
destroyableResources := make(Resources)
for mgrName, resList := range resources {
var destroyableResList []*resource.Resource
for _, r := range resList {
if rules.GetResourceAction(r) == rules.Destroy {
destroyableResList = append(destroyableResList, r)
// Returns the first Matching Rule Action for a resource
for _, rule := range rules.GetRules() {
if action := rule.CheckResource(r); action == rules.Destroy {
destroyableResList = append(destroyableResList, r)
}
}
}
count = len(destroyableResList)
destroyableResources[mgrName] = destroyableResList
}
p.Logger.Infof("Found %d resources to be destroyed", count)
return destroyableResources
}

// GetStoppableResources : Return the resources which can be stopped
func (p *Provider) GetStoppableResources(resources Resources) Resources {
p.Logger.Debug("Getting Stoppable Resources")
stoppableResources := make(Resources)
count := 0
for mgrName, resList := range resources {
var stoppableResList []*resource.Resource
for _, r := range resList {
if r.IsActive() && rules.GetResourceAction(r) == rules.Stop {
stoppableResList = append(stoppableResList, r)
for _, rule := range rules.GetRules() {
if action := rule.CheckResource(r); r.IsActive() && action == rules.Stop {
stoppableResList = append(stoppableResList, r)
}
}
}
count = len(stoppableResList)
stoppableResources[mgrName] = stoppableResList
}
p.Logger.Infof("Found %d resources to be stopped", count)
return stoppableResources
}

// GetResumableResources : Return the resources which can be Resumed
func (p *Provider) GetResumableResources(resources Resources) Resources {
p.Logger.Debug("Getting resumable Resources")
resumableResource := make(Resources)
count := 0
for mgrName, resList := range resources {
var resumableResList []*resource.Resource
for _, r := range resList {
if r.IsStopped() && rules.GetResourceAction(r) == rules.Resume {
resumableResList = append(resumableResList, r)
for _, rule := range rules.GetRules() {
if action := rule.CheckResource(r); r.IsStopped() && action == rules.Resume {
resumableResList = append(resumableResList, r)
}
}
}
resumableResource[mgrName] = resumableResList
}
count = len(resumableResource)
p.Logger.Infof("Found %d resources to be resumed", count)
return resumableResource
}

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

for mgrName, res := range resources {
mgr := p.getManager(mgrName)
mgr.Logger.Debugf("Destroying %s ", mgrName)
if err := mgr.Destroy(res); err != nil {
errs[mgrName] = err.Error()
}
wg.Add(1)
go func(mgrName string, res []*resource.Resource) {
defer wg.Done()
mgr := p.getManager(mgrName)
mgr.Logger.Debugf("Destroying %s ", mgrName)
if err := mgr.Destroy(res); err != nil {
errs[mgrName] = err.Error()
}
}(mgrName, res)
}
wg.Wait()
return errs
}

Expand Down
52 changes: 52 additions & 0 deletions rules/action_enumer.go

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

4 changes: 2 additions & 2 deletions rules/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 114,8 @@ func GetRules() []Ruler {
func GetResourceAction(res *resource.Resource) Action {
// Returns the first Matching Rule Action for a resource
for _, r := range rules {
if r.CheckResource(res) != DoNothing {
return r.CheckResource(res)
if action := r.CheckResource(res); action != DoNothing {
return action
}
}
return DoNothing
Expand Down

0 comments on commit ba9b37c

Please sign in to comment.