Skip to content

Commit

Permalink
Implement org deletion job
Browse files Browse the repository at this point in the history
[#2605]

- Add check for org deletion timeout with message
- Calculate status for deletion based on presence of CRD and its timestamp

Co-authored-by: Dave Walter <[email protected]>
  • Loading branch information
acosta11 and davewalter committed Jun 27, 2023
1 parent 96a33a6 commit ef34e0f
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 22 deletions.
38 changes: 35 additions & 3 deletions api/handlers/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"net/http"
"net/url"
"regexp"
"time"

"code.cloudfoundry.org/korifi/api/authorization"

apierrors "code.cloudfoundry.org/korifi/api/errors"
"code.cloudfoundry.org/korifi/api/presenter"
Expand All @@ -22,21 +25,26 @@ const (
spaceDeletePrefix = "space.delete"
domainDeletePrefix = "domain.delete"
roleDeletePrefix = "role.delete"

JobTimeoutDuration = 120.0
)

const JobResourceType = "Job"

type Job struct {
serverURL url.URL
orgRepo CFOrgRepository
}

func NewJob(serverURL url.URL) *Job {
func NewJob(serverURL url.URL, orgRepo CFOrgRepository) *Job {
return &Job{
serverURL: serverURL,
orgRepo: orgRepo,
}
}

func (h *Job) get(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.job.get")

jobGUID := routing.URLParam(r, "guid")
Expand All @@ -56,8 +64,32 @@ func (h *Job) get(r *http.Request) (*routing.Response, error) {
switch jobType {
case syncSpacePrefix:
jobResponse = presenter.ForManifestApplyJob(jobGUID, resourceGUID, h.serverURL)
case appDeletePrefix, orgDeletePrefix, spaceDeletePrefix, routeDeletePrefix, domainDeletePrefix, roleDeletePrefix:
jobResponse = presenter.ForJob(jobGUID, jobType, h.serverURL)
case appDeletePrefix, spaceDeletePrefix, routeDeletePrefix, domainDeletePrefix, roleDeletePrefix:
jobResponse = presenter.ForJob(jobGUID, []presenter.JobResponseError{}, presenter.StateComplete, jobType, h.serverURL)
case orgDeletePrefix:
org, err := h.orgRepo.GetOrg(r.Context(), authInfo, resourceGUID)
if err != nil {
if _, ok := err.(apierrors.NotFoundError); !ok {
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "Failed to fetch org from Kubernetes", "OrgGUID", resourceGUID)
}
jobResponse = presenter.ForJob(jobGUID, []presenter.JobResponseError{}, presenter.StateComplete, jobType, h.serverURL)
break
}

deletionTime, err := time.Parse(time.RFC3339Nano, org.DeletedAt)
if err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to parse org deletion time", "name", org.Name, "timestamp", org.DeletedAt)
}

if time.Since(deletionTime).Seconds() < JobTimeoutDuration {
jobResponse = presenter.ForJob(jobGUID, []presenter.JobResponseError{}, presenter.StateProcessing, jobType, h.serverURL)
} else {
jobResponse = presenter.ForJob(jobGUID, []presenter.JobResponseError{{
Detail: fmt.Sprintf("CFOrg deletion timed out. Check for lingering resources in the %q namespace", org.GUID),
Title: "CF-UnprocessableEntity",
Code: 10008,
}}, presenter.StateFailed, jobType, h.serverURL)
}
default:
return nil, apierrors.LogAndReturn(
logger,
Expand Down
106 changes: 103 additions & 3 deletions api/handlers/job_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package handlers_test

import (
"fmt"
"net/http"
"net/http/httptest"
"time"

apierrors "code.cloudfoundry.org/korifi/api/errors"

"code.cloudfoundry.org/korifi/api/repositories"

"code.cloudfoundry.org/korifi/api/handlers/fake"

"code.cloudfoundry.org/korifi/api/handlers"
. "code.cloudfoundry.org/korifi/tests/matchers"
Expand All @@ -17,11 +25,14 @@ var _ = Describe("Job", func() {
spaceGUID string
jobGUID string
req *http.Request
orgRepo *fake.OrgRepository
)

BeforeEach(func() {
spaceGUID = uuid.NewString()
apiHandler := handlers.NewJob(*serverURL)

orgRepo = new(fake.OrgRepository)
apiHandler := handlers.NewJob(*serverURL, orgRepo)
routerBuilder.LoadRoutes(apiHandler)
})

Expand Down Expand Up @@ -63,9 +74,7 @@ var _ = Describe("Job", func() {
MatchJSONPath("$.operation", operation),
)))
},

Entry("app delete", "app.delete", "cf-app-guid"),
Entry("org delete", "org.delete", "cf-org-guid"),
Entry("space delete", "space.delete", "cf-space-guid"),
Entry("route delete", "route.delete", "cf-route-guid"),
Entry("domain delete", "domain.delete", "cf-domain-guid"),
Expand All @@ -81,5 +90,96 @@ var _ = Describe("Job", func() {
expectNotFoundError("Job")
})
})

Describe("org delete", func() {
var (
operation string
resourceGUID string
)

BeforeEach(func() {
operation = "org.delete"
resourceGUID = "cf-org-guid"
})

When("org delete is in progress", func() {
BeforeEach(func() {
orgRepo.GetOrgReturns(repositories.OrgRecord{
GUID: "cf-org-guid",
DeletedAt: time.Now().Format(time.RFC3339Nano),
}, nil)
})

It("returns a processing status", func() {
guid := operation + "~" + resourceGUID
req, err := http.NewRequestWithContext(ctx, "GET", "/v3/jobs/"+guid, nil)
Expect(err).NotTo(HaveOccurred())

rr = httptest.NewRecorder()
routerBuilder.Build().ServeHTTP(rr, req)

Expect(rr).To(HaveHTTPBody(SatisfyAll(
MatchJSONPath("$.guid", guid),
MatchJSONPath("$.links.self.href", defaultServerURL+"/v3/jobs/"+guid),
MatchJSONPath("$.operation", operation),
MatchJSONPath("$.state", "PROCESSING"),
MatchJSONPath("$.errors", BeEmpty()),
)))
})
})

When("org delete completed", func() {
BeforeEach(func() {
orgRepo.GetOrgReturns(repositories.OrgRecord{}, apierrors.NewNotFoundError(nil, repositories.OrgResourceType))
})

It("returns a complete status", func() {
guid := operation + "~" + resourceGUID
req, err := http.NewRequestWithContext(ctx, "GET", "/v3/jobs/"+guid, nil)
Expect(err).NotTo(HaveOccurred())

rr = httptest.NewRecorder()
routerBuilder.Build().ServeHTTP(rr, req)

Expect(rr).To(HaveHTTPBody(SatisfyAll(
MatchJSONPath("$.guid", guid),
MatchJSONPath("$.links.self.href", defaultServerURL+"/v3/jobs/"+guid),
MatchJSONPath("$.operation", operation),
MatchJSONPath("$.state", "COMPLETE"),
MatchJSONPath("$.errors", BeEmpty()),
)))
})
})

When("org delete times out", func() {
BeforeEach(func() {
orgRepo.GetOrgReturns(repositories.OrgRecord{
GUID: "cf-org-guid",
DeletedAt: (time.Now().Add(-180 * time.Second)).Format(time.RFC3339Nano),
}, nil)
})

It("returns a failed status", func() {
guid := operation + "~" + resourceGUID
req, err := http.NewRequestWithContext(ctx, "GET", "/v3/jobs/"+guid, nil)
Expect(err).NotTo(HaveOccurred())

rr = httptest.NewRecorder()
routerBuilder.Build().ServeHTTP(rr, req)

Expect(rr).To(HaveHTTPBody(SatisfyAll(
MatchJSONPath("$.guid", guid),
MatchJSONPath("$.links.self.href", defaultServerURL+"/v3/jobs/"+guid),
MatchJSONPath("$.operation", operation),
MatchJSONPath("$.state", "FAILED"),
MatchJSONPath("$.errors", ConsistOf(map[string]interface{}{
"code": float64(10008),
"detail": fmt.Sprintf("CFOrg deletion timed out. Check for lingering resources in the %q namespace", resourceGUID),
"title": "CF-UnprocessableEntity",
})),
)))
})
})
})
})
})
1 change: 1 addition & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func main() {
),
handlers.NewJob(
*serverURL,
orgRepo,
),
handlers.NewLogCache(
appRepo,
Expand Down
34 changes: 22 additions & 12 deletions api/presenter/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
const (
JobGUIDDelimiter = "~"

StateComplete = "COMPLETE"
StateFailed = "FAILED"
StateProcessing = "PROCESSING"

AppDeleteOperation = "app.delete"
OrgDeleteOperation = "org.delete"
RouteDeleteOperation = "route.delete"
Expand All @@ -17,15 +21,21 @@ const (
RoleDeleteOperation = "role.delete"
)

type JobResponseError struct {
Detail string `json:"detail"`
Title string `json:"title"`
Code int `json:"code"`
}

type JobResponse struct {
GUID string `json:"guid"`
Errors *string `json:"errors"`
Warnings *string `json:"warnings"`
Operation string `json:"operation"`
State string `json:"state"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Links JobLinks `json:"links"`
GUID string `json:"guid"`
Errors []JobResponseError `json:"errors"`
Warnings *string `json:"warnings"`
Operation string `json:"operation"`
State string `json:"state"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Links JobLinks `json:"links"`
}

type JobLinks struct {
Expand All @@ -34,20 +44,20 @@ type JobLinks struct {
}

func ForManifestApplyJob(jobGUID string, spaceGUID string, baseURL url.URL) JobResponse {
response := ForJob(jobGUID, SpaceApplyManifestOperation, baseURL)
response := ForJob(jobGUID, []JobResponseError{}, StateComplete, SpaceApplyManifestOperation, baseURL)
response.Links.Space = &Link{
HRef: buildURL(baseURL).appendPath("/v3/spaces", spaceGUID).build(),
}
return response
}

func ForJob(jobGUID string, operation string, baseURL url.URL) JobResponse {
func ForJob(jobGUID string, errors []JobResponseError, state string, operation string, baseURL url.URL) JobResponse {
return JobResponse{
GUID: jobGUID,
Errors: nil,
Errors: errors,
Warnings: nil,
Operation: operation,
State: "COMPLETE",
State: state,
CreatedAt: "",
UpdatedAt: "",
Links: JobLinks{
Expand Down
16 changes: 13 additions & 3 deletions api/presenter/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var _ = Describe("", func() {
It("renders the job", func() {
Expect(output).To(MatchJSON(`{
"created_at": "",
"errors": null,
"errors": [],
"guid": "the-job-guid",
"links": {
"self": {
Expand All @@ -52,7 +52,11 @@ var _ = Describe("", func() {

Describe("ForDeleteJob", func() {
JustBeforeEach(func() {
response := presenter.ForJob("the-job-guid", "the.operation", *baseURL)
response := presenter.ForJob("the-job-guid", []presenter.JobResponseError{{
Detail: "error detail",
Title: "CF-JobErrorTitle",
Code: 12345,
}}, "COMPLETE", "the.operation", *baseURL)
var err error
output, err = json.Marshal(response)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -61,7 +65,13 @@ var _ = Describe("", func() {
It("renders the job", func() {
Expect(output).To(MatchJSON(`{
"created_at": "",
"errors": null,
"errors": [
{
"code": 12345,
"detail": "error detail",
"title": "CF-JobErrorTitle"
}
],
"guid": "the-job-guid",
"links": {
"self": {
Expand Down
1 change: 1 addition & 0 deletions api/repositories/org_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type OrgRecord struct {
Annotations map[string]string
CreatedAt string
UpdatedAt string
DeletedAt string
}

type OrgRepo struct {
Expand Down
2 changes: 1 addition & 1 deletion api/repositories/task_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"sync"
"time"

"code.cloudfoundry.org/korifi/tools/k8s"
"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/api/repositories/conditions"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools/k8s"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down

0 comments on commit ef34e0f

Please sign in to comment.