Skip to content

Commit b290e5d

Browse files
committed
Feat: Added excludeRules for exclude resources only specified and also apply rules to specific resources only
1 parent 28b5edb commit b290e5d

File tree

11 files changed

+153
-95
lines changed

11 files changed

+153
-95
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This tool is **HIGHLY DESTRUCTIVE** and can deletes all resources! This should b
2626
- [x] [Persist state to remote sources](https://github.com/MeNsaaH/reka/issues/4)
2727
- [ ] [Add More AWS Resources](https://github.com/MeNsaaH/reka/issues/1)
2828
- [ ] [Add More GCP Resources](https://github.com/MeNsaaH/reka/issues/2)
29-
- [ ] [Add More Azure Resources](https://github.com/MeNsaaH/reka/issues/6)
29+
- [ ] [Add MoreAzure Resources](https://github.com/MeNsaaH/reka/issues/6)
3030
- [ ] [Create Web Dashboard](https://github.com/MeNsaaH/reka/issues/3)
3131

3232
#### Supported Resources

cmd/root.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cmd
1818
import (
1919
"fmt"
2020
"os"
21-
"unsafe"
2221

2322
"github.com/spf13/cobra"
2423

@@ -36,12 +35,15 @@ import (
3635
)
3736

3837
var (
39-
cfgFile string
40-
cfg *config.Config
41-
providers []*types.Provider
42-
backend state.Backender
43-
activeState *state.State
44-
verbose bool
38+
cfgFile string
39+
cfg *config.Config
40+
providers []*types.Provider
41+
backend state.Backender
42+
activeState *state.State
43+
verbose bool
44+
disableStop bool
45+
disableResume bool
46+
disableDestroy bool
4547
)
4648

4749
// rootCmd represents the base command when called without any subcommands
@@ -56,12 +58,9 @@ var rootCmd = &cobra.Command{
5658
config.LoadConfig()
5759

5860
cfg = config.GetConfig()
59-
60-
for _, rule := range cfg.Rules {
61-
// Convert Rule in config to rules.Rule type
62-
r := *((*rules.Rule)(unsafe.Pointer(&rule)))
63-
r.Tags = resource.Tags(rule.Tags)
64-
rules.ParseRule(r)
61+
err := rules.LoadRules()
62+
if err != nil {
63+
log.Fatal(err)
6564
}
6665

6766
// Initialize Provider objects
@@ -72,20 +71,26 @@ var rootCmd = &cobra.Command{
7271
for _, p := range providers {
7372
res := activeState.Current[p.Name]
7473

75-
stoppableResources := p.GetStoppableResources(res)
76-
fmt.Println("Stoppable Resources: ", stoppableResources)
77-
errs := p.StopResources(stoppableResources)
78-
fmt.Println("Errors Stopping Resources: ", errs)
74+
if !disableStop {
75+
stoppableResources := p.GetStoppableResources(res)
76+
fmt.Println("Stoppable Resources: ", stoppableResources)
77+
errs := p.StopResources(stoppableResources)
78+
logErrors(errs)
79+
}
7980

80-
resumableResources := p.GetResumableResources(res)
81-
fmt.Println("Resumable Resources: ", resumableResources)
82-
errs = p.ResumeResources(resumableResources)
83-
fmt.Println("Errors Resuming Resources: ", errs)
81+
if !disableResume {
82+
resumableResources := p.GetResumableResources(res)
83+
fmt.Println("Resumable Resources: ", resumableResources)
84+
errs := p.ResumeResources(resumableResources)
85+
logErrors(errs)
86+
}
8487

85-
destroyableResources := p.GetDestroyableResources(res)
86-
fmt.Println("Destroyable Resources: ", destroyableResources)
87-
errs = p.DestroyResources(destroyableResources)
88-
fmt.Println("Errors Destroying Resources: ", errs)
88+
if !disableDestroy {
89+
destroyableResources := p.GetDestroyableResources(res)
90+
fmt.Println("Destroyable Resources: ", destroyableResources)
91+
errs := p.DestroyResources(destroyableResources)
92+
logErrors(errs)
93+
}
8994
}
9095
},
9196
}
@@ -104,6 +109,9 @@ func init() {
104109
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.reka.yaml)")
105110
rootCmd.Flags().BoolP("unused-only", "t", false, "Reap only unused resources")
106111
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Output verbose logs (DEBUG)")
112+
rootCmd.Flags().BoolVar(&disableStop, "disable-stop", false, "Disable stopping of resources")
113+
rootCmd.Flags().BoolVar(&disableResume, "disable-resume", false, "Disable resuming of resources")
114+
rootCmd.Flags().BoolVar(&disableDestroy, "disable-destroy", false, "Disable destruction of resources")
107115
}
108116

109117
// initConfig reads in config file and ENV variables if set.
@@ -201,3 +209,9 @@ func containsResource(res []*resource.Resource, r *resource.Resource) bool {
201209
}
202210
return false
203211
}
212+
213+
func logErrors(errs map[string]error) {
214+
for k, v := range errs {
215+
log.Errorf("%s: %s", k, v.Error())
216+
}
217+
}

cmd/version.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,4 @@ var versionCmd = &cobra.Command{
3535

3636
func init() {
3737
rootCmd.AddCommand(versionCmd)
38-
39-
// Here you will define your flags and configuration settings.
40-
41-
// Cobra supports Persistent Flags which will work for this command
42-
// and all subcommands, e.g.:
43-
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")
44-
45-
// Cobra supports local flags which will only run when this command
46-
// is called directly, e.g.:
47-
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4838
}

config/config.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@ const (
2121
appName = "REKA"
2222
)
2323

24+
type ExcludeRule struct {
25+
Name string
26+
Region string
27+
Tags map[string]string
28+
Resources []string
29+
}
30+
31+
type Rule struct {
32+
Name string
33+
Condition struct {
34+
ActiveDuration struct {
35+
StartTime string
36+
StopTime string
37+
StartDay string
38+
StopDay string
39+
}
40+
TerminationPolicy string
41+
TerminationDate string
42+
}
43+
Resources []string
44+
Region string
45+
Tags map[string]string
46+
}
47+
48+
func (r Rule) String() string {
49+
return r.Name
50+
}
51+
52+
type Backend struct {
53+
Type string
54+
Path string
55+
Bucket string
56+
Region string
57+
}
58+
2459
// Config : The Config values passed to application
2560
type Config struct {
2661
Name string
@@ -43,41 +78,15 @@ type Config struct {
4378
staticPath string // Path to Static File
4479

4580
// Exclude block prevents certain resources from been tracked or affected by reka.
46-
Exclude []struct {
47-
Name string
48-
Region string
49-
Tags map[string]string
50-
Resources []string
51-
}
81+
Exclude []*ExcludeRule
5282

5383
// StateBackend is how state is stored (read & write)
5484
// State files contain details used for infrastructure resumption and history of
5585
// infrastructural management
56-
StateBackend struct {
57-
Type string
58-
Path string
59-
Bucket string
60-
Region string
61-
}
62-
86+
StateBackend *Backend
6387
// Rules block define how reka should behave given certain resources. These rules
6488
// usually target resources based on tags/labels which are attached to the resources
65-
Rules []struct {
66-
Name string
67-
Condition struct {
68-
ActiveDuration struct {
69-
StartTime string
70-
StopTime string
71-
StartDay string
72-
StopDay string
73-
}
74-
TerminationPolicy string
75-
TerminationDate string
76-
}
77-
Region string
78-
Tags map[string]string
79-
}
80-
89+
Rules []*Rule
8190
// AWS Config
8291
Aws *aws.Config
8392
// Gcp configuration

config/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ func GetLogger(mgrName, logPath string) *log.Entry {
2222
}
2323
return logger.WithFields(log.Fields{
2424
"resource": mgrName,
25+
"provider": mgrName,
2526
})
2627
}

provider/aws/aws.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewResource(id, manager string) *resource.Resource {
2323
resource := resource.Resource{}
2424
resource.UUID = id
2525
resource.Manager = resourceManagers[manager]
26+
resource.ProviderName = providerName
2627

2728
return &resource
2829
}

provider/gcp/gcp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewResource(id, manager string) *resource.Resource {
2323
resource := resource.Resource{}
2424
resource.UUID = id
2525
resource.Manager = resourceManagers[manager]
26+
resource.ProviderName = providerName
2627

2728
return &resource
2829
}

provider/types/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (p *Provider) GetUnusedResources(resources Resources) Resources {
147147
// DestroyResources : Return the resources which can be destroyed
148148
func (p *Provider) DestroyResources(resources Resources) map[string]error {
149149
errs := make(map[string]error)
150-
p.Logger.Debugf("Destroying Resources...")
150+
p.Logger.Info("Destroying Resources...")
151151
var wg sync.WaitGroup
152152

153153
for mgrName, res := range resources {

resource/resource.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resource
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

78
log "github.com/sirupsen/logrus"
@@ -45,9 +46,9 @@ type Resource struct {
4546
gorm.Model `json:"-"`
4647
// UUID defines any unique field use to identify a resource. For some resources its their IDs, some their names.
4748
// Not using ID because gorm.Model defines an ID field already
48-
UUID string `gorm:"unique;not null"`
49-
ManagerName string `json:"-"`
50-
Manager *Manager `gorm:"foreignKey:ManagerName;references:Name" json:"-"`
49+
UUID string `gorm:"unique;not null"`
50+
Manager *Manager `gorm:"foreignKey:ManagerName;references:Name" json:"-"`
51+
ProviderName string
5152

5253
Region string // Region of Resource
5354

@@ -85,3 +86,10 @@ func (r Resource) IsStopped() bool {
8586
func (r Resource) IsUnused() bool {
8687
return r.Status == Unused
8788
}
89+
90+
// Uri a simple uri of the resource in the form provider.resource_type for example ec2 instances will have the
91+
// url aws.ec2
92+
func (r Resource) Uri() string {
93+
return fmt.Sprintf("%s.%s", strings.ToLower(r.ProviderName), strings.ToLower(r.Manager.Name))
94+
95+
}

0 commit comments

Comments
 (0)