Skip to content

Commit

Permalink
removed suspended field from crd
Browse files Browse the repository at this point in the history
  • Loading branch information
asiyani committed Sep 21, 2023
1 parent d3b55b1 commit 48ca17f
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ terraform-applier
bin
# test state files
*.tfstate
webserver/index.html
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ spec:
repoName: terraform-applier
path: dev/hello
schedule: "00 */1 * * *"
suspend: false
planOnly: false
pollInterval: 60
runTimeout: 900
Expand Down
10 changes: 0 additions & 10 deletions api/v1beta1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ type ModuleSpec struct {
// +optional
Schedule string `json:"schedule,omitempty"`

// This flag tells the controller to suspend all subsequent runs, it does
// not apply to already started run. Defaults to false.
// +optional
Suspend *bool `json:"suspend,omitempty"`

// +optional
PlanOnly *bool `json:"planOnly,omitempty"`

Expand Down Expand Up @@ -195,7 +190,6 @@ type ModuleStatus struct {
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Schedule",type="string",JSONPath=".spec.schedule",description=""
//+kubebuilder:printcolumn:name="Suspend",type="string",JSONPath=".spec.suspend",description=""
//+kubebuilder:printcolumn:name="PlanOnly",type="string",JSONPath=".spec.planOnly",description=""
//+kubebuilder:printcolumn:name="State",type="string",JSONPath=".status.currentState",description=""
//+kubebuilder:printcolumn:name="Started At",type="string",JSONPath=`.status.runStartedAt`,description=""
Expand Down Expand Up @@ -289,10 +283,6 @@ type Subject struct {
Name string `json:"name,omitempty"`
}

func (m *Module) IsSuspended() bool {
return m.Spec.Suspend != nil && *m.Spec.Suspend
}

func (m *Module) IsPlanOnly() bool {
return m.Spec.PlanOnly != nil && *m.Spec.PlanOnly
}
Expand Down
5 changes: 0 additions & 5 deletions api/v1beta1/zz_generated.deepcopy.go

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

7 changes: 0 additions & 7 deletions config/crd/bases/terraform-applier.uw.systems_modules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ spec:
- jsonPath: .spec.schedule
name: Schedule
type: string
- jsonPath: .spec.suspend
name: Suspend
type: string
- jsonPath: .spec.planOnly
name: PlanOnly
type: string
Expand Down Expand Up @@ -357,10 +354,6 @@ spec:
run for a given schedule if no schedule provided then module will
only run if new PRs are added to given module path
type: string
suspend:
description: This flag tells the controller to suspend all subsequent
runs, it does not apply to already started run. Defaults to false.
type: boolean
var:
description: List of input variables passed to the Terraform execution.
items:
Expand Down
6 changes: 0 additions & 6 deletions controllers/module_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ func (r *ModuleReconciler) Reconcile(ctx context.Context, req reconcile.Request)
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// Do not requeue if suspended
if module.Spec.Suspend != nil && *module.Spec.Suspend {
log.Debug("module suspended, skipping")
return ctrl.Result{}, nil
}

// Do not requeue if module is being deleted
if !module.ObjectMeta.DeletionTimestamp.IsZero() {
// TODO: what if module is in running state?
Expand Down
82 changes: 0 additions & 82 deletions integration_test/module_controller_no_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,87 +317,5 @@ var _ = Describe("Module controller without runner", func() {
// delete module to stopping requeue
Expect(k8sClient.Delete(ctx, module)).Should(Succeed())
})

It("Should not trigger run for suspended module", func() {
const (
moduleName = "test-module5"
repo = "modules"
path = "dev/" + moduleName
)
var boolTrue = true
// testGitSyncPool.EXPECT().HasChangesForPath(gomock.Any(), repo, path, "CommitAbc123").Return(true, nil)

By("By creating a new Module")
ctx := context.Background()
module := &tfaplv1beta1.Module{
TypeMeta: metav1.TypeMeta{
APIVersion: "terraform-applier.uw.systems/v1beta1",
Kind: "Module",
},
ObjectMeta: metav1.ObjectMeta{
Name: moduleName,
Namespace: moduleNamespace,
},
Spec: tfaplv1beta1.ModuleSpec{
Schedule: "1 * * * *",
RepoName: repo,
Path: path,
},
}
Expect(k8sClient.Create(ctx, module)).Should(Succeed())

// After creating this Module, let's check that the Module's Spec fields match what we passed in.
moduleLookupKey := types.NamespacedName{Name: moduleName, Namespace: moduleNamespace}
fetchedModule := &tfaplv1beta1.Module{}

Eventually(func() bool {
err := k8sClient.Get(ctx, moduleLookupKey, fetchedModule)
return err == nil
}, time.Second*10, interval).Should(BeTrue())

By("By absorbing initial run due to no run commit history and updating status with commit hash")
Eventually(func() types.NamespacedName {
timer := time.NewTimer(time.Second)
for {
select {
case req := <-testControllerQueue:
return req.NamespacedName
case <-timer.C:
return types.NamespacedName{}
}
}
}, time.Second*60, interval).Should(Equal(moduleLookupKey))
// trick controller to accept mocked test time as earliestTime as we cannot control created time
// also add commit of initial run
fetchedModule.Status.RunCommitHash = "CommitAbc123"
fetchedModule.Status.RunStartedAt = &metav1.Time{Time: time.Date(2022, 02, 01, 01, 00, 30, 0000, time.UTC)}
Expect(k8sClient.Status().Update(ctx, fetchedModule)).Should(Succeed())

// let controller reschedule
time.Sleep(10 * time.Second)

By("By suspending Module")
// moduleLookupKey := types.NamespacedName{Name: moduleName, Namespace: moduleNamespace}
fetchedModule.Spec.Suspend = &boolTrue
Expect(k8sClient.Update(ctx, fetchedModule)).Should(Succeed())

// advance time
By("By making sure job was never sent to jobQueue after advancing time")
fakeClock.T = time.Date(2022, 02, 01, 01, 01, 00, 0000, time.UTC)
Consistently(func() types.NamespacedName {
timer := time.NewTimer(time.Second)
for {
select {
case req := <-testControllerQueue:
return req.NamespacedName
case <-timer.C:
return types.NamespacedName{}
}
}
}, time.Second*20, interval).Should(Equal(types.NamespacedName{}))
// delete module to stopping requeue
Expect(k8sClient.Delete(ctx, module)).Should(Succeed())
})

})
})
19 changes: 5 additions & 14 deletions webserver/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ func Test_ExecuteTemplate(t *testing.T) {
StateMessage: `some very long error message with \n Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future`,
},
},
{
TypeMeta: metav1.TypeMeta{APIVersion: "terraform-applier.uw.systems/v1beta1", Kind: "Module"},
ObjectMeta: metav1.ObjectMeta{Name: "application", Namespace: "bar"},
Spec: tfaplv1beta1.ModuleSpec{
Schedule: "00 06 * * *",
Path: "dev/application",
Suspend: &boolTrue,
},
},
{
TypeMeta: metav1.TypeMeta{APIVersion: "terraform-applier.uw.systems/v1beta1", Kind: "Module"},
ObjectMeta: metav1.ObjectMeta{Name: "groups", Namespace: "bar"},
Expand Down Expand Up @@ -160,9 +151,9 @@ Apply complete! Resources: 7 added, 0 changed, 0 destroyed.`,
return
}

// // uncomment to load index.html file locally after running test
// if err := os.WriteFile("index.html", rendered.Bytes(), 0666); err != nil {
// t.Errorf("error reading test file: %v\n", err)
// return
// }
// open index.html in browser to view test output
if err := os.WriteFile("index.html", rendered.Bytes(), 0666); err != nil {
t.Errorf("error reading test file: %v\n", err)
return
}
}
7 changes: 3 additions & 4 deletions webserver/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ <h5 class="fw-normal">{{.Name}} {{ if .IsPlanOnly}}(plan only){{end}}</h5>
class='badge rounded-pill bg-primary'
{{else if eq .Status.CurrentState "Errored"}}
class='badge rounded-pill bg-danger'
{{else if .IsSuspended}}
class='badge rounded-pill bg-warning text-dark'
{{else}}
class='badge rounded-pill bg-light text-dark'
{{end}}>
{{ if .IsSuspended}}Suspended{{else}}{{.Status.CurrentState}} ({{.Status.StateReason}}){{end}}
{{.Status.CurrentState}} ({{.Status.StateReason}})
</p>

</div>
Expand Down Expand Up @@ -132,7 +130,8 @@ <h5 class="fw-normal">{{.Name}} {{ if .IsPlanOnly}}(plan only){{end}}</h5>
<div>At: <small>{{ formattedTime .Status.LastApplyInfo.Timestamp }}</small></div>
</div>
<pre class="file-output font-monospace p-3" style="max-height: 40em; background-color: #f5f5f5;">
<small>{{.Status.LastApplyInfo.Output}}</small>
<!-- to remove left margin on first line -->
<small>{{.Status.LastApplyInfo.Output}}</small>
</pre>
</div>
</div>
Expand Down

0 comments on commit 48ca17f

Please sign in to comment.