Skip to content

Commit

Permalink
[Rules] Feat: Added Termination Policy for unused resources
Browse files Browse the repository at this point in the history
Former-commit-id: 8170c60
  • Loading branch information
MeNsaaH committed Jan 4, 2021
1 parent dd4a7c0 commit fe61f6a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 150,8 @@ func refreshResources(providers []*provider.Provider) {
// errs = provider.ResumeResources(resumableResources)
// fmt.Println("Errors Resuming Resources: ", errs)

// destroyableResources := provider.GetDestroyableResources(allResources)
// fmt.Println("Destroyable Resources: ", destroyableResources)
destroyableResources := provider.GetDestroyableResources(allResources)
fmt.Println("Destroyable Resources: ", destroyableResources)
// errs = provider.DestroyResources(destroyableResources)
// fmt.Println("Errors Destroying Resources: ", errs)
}
Expand Down
8 changes: 2 additions & 6 deletions config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 62,18 @@ rules:
env: test
ci: true
condition:
terminationPolicy:
- older than 24hrs
terminationPolicy: older than 24hrs
- name: Nuke all project A instances after Demo october 9th (staging)
tags:
env: staging
project: A
condition:
terminationDate: october 9
action: terminate
- name: Delete all unused instances older than 48hrs (staging)
tags:
env: staging
condition:
terminationPolicy:
- unused
action: terminate
terminationPolicy: unused


timezone: Africa/Lagos
Expand Down
2 changes: 2 additions & 0 deletions rules/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 75,8 @@ func ParseRule(rule Rule) Ruler {
activeRule = &ActiveDurationRule{Rule: &rule}
} else if rule.Condition.TerminationDate != "" {
activeRule = &TerminationDateRule{Rule: &rule}
} else if rule.Condition.TerminationPolicy != "" {
activeRule = &TerminationPolicyRule{Rule: &rule}
} else {
log.Fatalf("No Conditions specified for rule: `%s`", rule.Name)
}
Expand Down
28 changes: 27 additions & 1 deletion rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,8 @@ import (
"github.com/mensaah/reka/resource"
)

// TerminationDateRule defines rule that sets when a resource should be terminated
// TerminationDateRule defines rule that sets when a resource should be terminated based on termination-date rule
// set in config file. This rule checks if the set termination-date is past and activates destroy on the instance
type TerminationDateRule struct {
*Rule
Date time.Time
Expand Down Expand Up @@ -72,3 73,28 @@ func (r ActiveDurationRule) CheckResource(res *resource.Resource) Action {
}
return DoNothing
}

// TerminationPolicyRule defines rule that sets when a resource should be terminated.
type TerminationPolicyRule struct {
*Rule
Policy string
}

func (r *TerminationPolicyRule) validate() error {
validPolicies := []string{"unused"}
for _, p := range validPolicies {
if p == r.Condition.TerminationPolicy {
return nil
}
}
r.Policy = r.Condition.TerminationPolicy
return fmt.Errorf("Error parsing condition.terminationPolicy: Invalid Policy %s", r.Policy)
}

// CheckResource Returns a list of resources whose termination Date is exceeed
func (r TerminationPolicyRule) CheckResource(res *resource.Resource) Action {
if res.IsUnused() {
return Destroy
}
return DoNothing
}

0 comments on commit fe61f6a

Please sign in to comment.