Skip to content

Commit 8170c60

Browse files
committed
[Rules] Feat: Added Termination Policy for unused resources
1 parent 9df6bea commit 8170c60

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

cmd/root.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func refreshResources(providers []*provider.Provider) {
150150
// errs = provider.ResumeResources(resumableResources)
151151
// fmt.Println("Errors Resuming Resources: ", errs)
152152

153-
// destroyableResources := provider.GetDestroyableResources(allResources)
154-
// fmt.Println("Destroyable Resources: ", destroyableResources)
153+
destroyableResources := provider.GetDestroyableResources(allResources)
154+
fmt.Println("Destroyable Resources: ", destroyableResources)
155155
// errs = provider.DestroyResources(destroyableResources)
156156
// fmt.Println("Errors Destroying Resources: ", errs)
157157
}

config/config.example.yaml

+2-6
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,18 @@ rules:
6262
env: test
6363
ci: true
6464
condition:
65-
terminationPolicy:
66-
- older than 24hrs
65+
terminationPolicy: older than 24hrs
6766
- name: Nuke all project A instances after Demo october 9th (staging)
6867
tags:
6968
env: staging
7069
project: A
7170
condition:
7271
terminationDate: october 9
73-
action: terminate
7472
- name: Delete all unused instances older than 48hrs (staging)
7573
tags:
7674
env: staging
7775
condition:
78-
terminationPolicy:
79-
- unused
80-
action: terminate
76+
terminationPolicy: unused
8177

8278

8379
timezone: Africa/Lagos

rules/rule.go

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func ParseRule(rule Rule) Ruler {
7575
activeRule = &ActiveDurationRule{Rule: &rule}
7676
} else if rule.Condition.TerminationDate != "" {
7777
activeRule = &TerminationDateRule{Rule: &rule}
78+
} else if rule.Condition.TerminationPolicy != "" {
79+
activeRule = &TerminationPolicyRule{Rule: &rule}
7880
} else {
7981
log.Fatalf("No Conditions specified for rule: `%s`", rule.Name)
8082
}

rules/rules.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"github.com/mensaah/reka/resource"
99
)
1010

11-
// TerminationDateRule defines rule that sets when a resource should be terminated
11+
// TerminationDateRule defines rule that sets when a resource should be terminated based on termination-date rule
12+
// set in config file. This rule checks if the set termination-date is past and activates destroy on the instance
1213
type TerminationDateRule struct {
1314
*Rule
1415
Date time.Time
@@ -72,3 +73,28 @@ func (r ActiveDurationRule) CheckResource(res *resource.Resource) Action {
7273
}
7374
return DoNothing
7475
}
76+
77+
// TerminationPolicyRule defines rule that sets when a resource should be terminated.
78+
type TerminationPolicyRule struct {
79+
*Rule
80+
Policy string
81+
}
82+
83+
func (r *TerminationPolicyRule) validate() error {
84+
validPolicies := []string{"unused"}
85+
for _, p := range validPolicies {
86+
if p == r.Condition.TerminationPolicy {
87+
return nil
88+
}
89+
}
90+
r.Policy = r.Condition.TerminationPolicy
91+
return fmt.Errorf("Error parsing condition.terminationPolicy: Invalid Policy %s", r.Policy)
92+
}
93+
94+
// CheckResource Returns a list of resources whose termination Date is exceeed
95+
func (r TerminationPolicyRule) CheckResource(res *resource.Resource) Action {
96+
if res.IsUnused() {
97+
return Destroy
98+
}
99+
return DoNothing
100+
}

0 commit comments

Comments
 (0)