Skip to content

Commit

Permalink
Merge pull request #3702 from uselagoon/actions-handler-environment-fix
Browse files Browse the repository at this point in the history
fix: get environment by namespacename before getting deployment
  • Loading branch information
tobybellwood authored Apr 4, 2024
2 parents 5585b7e + 5fbca51 commit 5c1fa7d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 55 deletions.
70 changes: 17 additions & 53 deletions services/actions-handler/handler/controller_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ func (m *Messenger) handleBuild(ctx context.Context, messageQueue *mq.MessageQue

// set up a lagoon client for use in the following process
l := lclient.New(m.LagoonAPI.Endpoint, "actions-handler", &token, false)
deployment, err := lagoon.GetDeploymentByName(ctx, message.Meta.Project, message.Meta.Environment, message.Meta.BuildName, false, l)
environment, err := lagoon.GetEnvironmentByNamespace(ctx, message.Namespace, l)
if err != nil || environment == nil {
if err != nil {
m.toLagoonLogs(messageQueue, map[string]interface{}{
"severity": "error",
"event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"),
"meta": environment,
"message": err.Error(),
})
if m.EnableDebug {
log.Println(fmt.Sprintf("%sERROR:unable to get environment by namespace - %v", prefix, err))
}
return err
}
}
environmentID := environment.ID
deployment, err := lagoon.GetDeploymentByName(ctx, message.Meta.Project, environment.Name, message.Meta.BuildName, false, l)
if err != nil {
m.toLagoonLogs(messageQueue, map[string]interface{}{
"severity": "error",
Expand All @@ -60,58 +76,6 @@ func (m *Messenger) handleBuild(ctx context.Context, messageQueue *mq.MessageQue
}
return nil
}
var environmentID uint
environment := &schema.Environment{}
// determine the environment id from the message
if message.Meta.ProjectID == nil && message.Meta.EnvironmentID == nil {
project, err := lagoon.GetMinimalProjectByName(ctx, message.Meta.Project, l)
if err != nil {
// send the log to the lagoon-logs exchange to be processed
m.toLagoonLogs(messageQueue, map[string]interface{}{
"severity": "error",
"event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"),
"meta": project,
"message": err.Error(),
})
if m.EnableDebug {
log.Println(fmt.Sprintf("%sERROR: unable to get project - %v", prefix, err))
}
return err
}
environment, err = lagoon.GetEnvironmentByName(ctx, message.Meta.Environment, project.ID, l)
if err != nil {
// send the log to the lagoon-logs exchange to be processed
m.toLagoonLogs(messageQueue, map[string]interface{}{
"severity": "error",
"event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"),
"meta": environment,
"message": err.Error(),
})
if m.EnableDebug {
log.Println(fmt.Sprintf("%sERROR: unable to get environment - %v", prefix, err))
}
return err
}
environmentID = environment.ID
} else {
// pull the id from the message
environmentID = *message.Meta.EnvironmentID
environment, err = lagoon.GetEnvironmentByID(ctx, environmentID, l)
if err != nil {
// send the log to the lagoon-logs exchange to be processed
m.toLagoonLogs(messageQueue, map[string]interface{}{
"severity": "error",
"event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"),
"meta": environment,
"message": err.Error(),
})
if m.EnableDebug {
log.Println(fmt.Sprintf("%sERROR: unable to get environment - %v", prefix, err))
}
return err
}
}

// prepare the deployment patch for later step
statusType := schema.StatusTypes(strings.ToUpper(buildStatus))
updateDeploymentPatch := schema.UpdateDeploymentPatchInput{
Expand Down
4 changes: 2 additions & 2 deletions tests/tasks/api/get-latest-deployment-source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
query: '{{ lookup("template", "./get-latest-deployment-source.gql") }}'
register: apiresponse

- name: "{{ testname }} - POST api trigger bulk deployment for environments to {{ graphql_url }}"
- name: "{{ testname }} - POST api check response {{ graphql_url }}"
debug:
msg: "api response: {{ apiresponse.json }}"

Expand All @@ -49,6 +49,6 @@
retries: 60
delay: 10

- name: "{{ testname }} - POST api trigger bulk deployment for environments to {{ graphql_url }}"
- name: "{{ testname }} - POST api check response {{ graphql_url }}"
debug:
msg: "api response: {{ apiresponse.json }}"
19 changes: 19 additions & 0 deletions tests/tasks/api/get-latest-deployment-status.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
query environmentByKubernetesNamespaceName {
environmentByKubernetesNamespaceName(
kubernetesNamespaceName: "{{ namespace }}"
){
deployments(limit: 1){
id
name
status
bulkId
bulkName
priority
started
created
completed
sourceUser
sourceType
}
}
}
53 changes: 53 additions & 0 deletions tests/tasks/api/get-latest-deployment-status.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
- name: "{{ testname }} - Verify that status are correct"
block:
- ansible.builtin.include_tasks: admin-token.yaml
- name: "{{ testname }} - POST api check latest deployment {{ graphql_url }}"
uri:
url: "{{ graphql_url }}"
method: POST
headers:
Authorization: "Bearer {{ admin_token }}"
body_format: json
body:
query: '{{ lookup("template", "./get-latest-deployment-status.gql") }}'
register: apiresponse
until:
- apiresponse.json.data.environmentByKubernetesNamespaceName.deployments[0].status is defined
retries: 30
delay: 10

- ansible.builtin.include_tasks: admin-token.yaml
- name: "{{ testname }} - POST api check latest deployment {{ graphql_url }}"
uri:
url: "{{ graphql_url }}"
method: POST
headers:
Authorization: "Bearer {{ admin_token }}"
body_format: json
body:
query: '{{ lookup("template", "./get-latest-deployment-status.gql") }}'
register: apiresponse

- name: "{{ testname }} - POST api check response {{ graphql_url }}"
debug:
msg: "api response: {{ apiresponse.json }}"

- ansible.builtin.include_tasks: admin-token.yaml
- name: "{{ testname }} - POST api check latest deployment status are correct {{ graphql_url }}"
uri:
url: "{{ graphql_url }}"
method: POST
headers:
Authorization: "Bearer {{ admin_token }}"
body_format: json
body:
query: '{{ lookup("template", "./get-latest-deployment-status.gql") }}'
register: apiresponse
until:
- apiresponse.json.data.environmentByKubernetesNamespaceName.deployments[0].status == buildStatus
retries: 60
delay: 10

- name: "{{ testname }} - POST api check response {{ graphql_url }}"
debug:
msg: "api response: {{ apiresponse.json }}"
9 changes: 9 additions & 0 deletions tests/tests/github/branch-picky.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
project: "{{ project }}"
url: "{{ check_url }}"

- name: "{{ testname }} - api check deployment status"
hosts: localhost
serial: 1
vars:
namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}"
buildStatus: "complete"
tasks:
- ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml

- name: "{{ testname }} - webhook github delete push"
hosts: localhost
serial: 1
Expand Down
9 changes: 9 additions & 0 deletions tests/tests/github/branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@
project: "{{ project }}"
url: "{{ check_url }}"

- name: "{{ testname }} - api check deployment status"
hosts: localhost
serial: 1
vars:
namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}"
buildStatus: "complete"
tasks:
- ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml

- name: "{{ testname }} - webhook github delete push"
hosts: localhost
serial: 1
Expand Down
9 changes: 9 additions & 0 deletions tests/tests/gitlab/branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@
project: "{{ project }}"
url: "{{ check_url }}"

- name: "{{ testname }} - api check deployment status"
hosts: localhost
serial: 1
vars:
namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}"
buildStatus: "complete"
tasks:
- ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml

- name: "{{ testname }} - webhook gitlab delete push"
hosts: localhost
serial: 1
Expand Down
9 changes: 9 additions & 0 deletions tests/tests/nginx/nginx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@
tasks:
- ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-source.yaml

- name: "{{ testname }} - api check deployment status"
hosts: localhost
serial: 1
vars:
namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}"
buildStatus: "complete"
tasks:
- ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml

- name: "{{ testname }} - api deleteEnvironment on {{ project }}, which should remove all resources"
hosts: localhost
serial: 1
Expand Down

0 comments on commit 5c1fa7d

Please sign in to comment.