From 73901b2edb82ce07893633c1d0ac39f45964b659 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Wed, 4 Oct 2023 09:29:19 +1100 Subject: [PATCH] fix: revert and fix the deployertoken injection using a custom unmarshal --- apis/lagoon/v1beta1/lagoontask_types.go | 61 ++++++++++++++++--- .../crd/bases/crd.lagoon.sh_lagoonbuilds.yaml | 16 ----- .../crd/bases/crd.lagoon.sh_lagoontasks.yaml | 20 ------ internal/messenger/tasks_handler.go | 6 -- 4 files changed, 51 insertions(+), 52 deletions(-) diff --git a/apis/lagoon/v1beta1/lagoontask_types.go b/apis/lagoon/v1beta1/lagoontask_types.go index d7739fb5..1708f0ba 100644 --- a/apis/lagoon/v1beta1/lagoontask_types.go +++ b/apis/lagoon/v1beta1/lagoontask_types.go @@ -16,6 +16,10 @@ limitations under the License. package v1beta1 import ( + "encoding/json" + "fmt" + "reflect" + "strconv" "strings" corev1 "k8s.io/api/core/v1" @@ -81,16 +85,14 @@ type LagoonTaskSpec struct { // LagoonTaskInfo defines what a task can use to communicate with Lagoon via SSH/API. type LagoonTaskInfo struct { - ID string `json:"id"` // should be int, but the api sends it as a string :\ - Name string `json:"name,omitempty"` - TaskName string `json:"taskName,omitempty"` - Service string `json:"service,omitempty"` - Command string `json:"command,omitempty"` - SSHHost string `json:"sshHost,omitempty"` - SSHPort string `json:"sshPort,omitempty"` - APIHost string `json:"apiHost,omitempty"` - DeployTokenInjection bool `json:"deployTokenInjection,omitempty"` - ProjectKeyInjection bool `json:"projectKeyInjection,omitempty"` + ID string `json:"id"` // should be int, but the api sends it as a string :\ + Name string `json:"name,omitempty"` + TaskName string `json:"taskName,omitempty"` + Service string `json:"service,omitempty"` + Command string `json:"command,omitempty"` + SSHHost string `json:"sshHost,omitempty"` + SSHPort string `json:"sshPort,omitempty"` + APIHost string `json:"apiHost,omitempty"` } // LagoonAdvancedTaskInfo defines what an advanced task can use for the creation of the pod. @@ -172,3 +174,42 @@ type LagoonTaskList struct { func init() { SchemeBuilder.Register(&LagoonTask{}, &LagoonTaskList{}) } + +// this is a custom unmarshal function that will check deployerToken and sshKey which come from Lagoon as `1|0` booleans because javascript +// this converts them from floats to bools +func (a *LagoonAdvancedTaskInfo) UnmarshalJSON(data []byte) error { + tmpMap := map[string]interface{}{} + json.Unmarshal(data, &tmpMap) + if value, ok := tmpMap["deployerToken"]; ok { + if reflect.TypeOf(value).Kind() == reflect.Float64 { + vBool, err := strconv.ParseBool(fmt.Sprintf("%v", value)) + if err == nil { + a.DeployerToken = vBool + } + } + if reflect.TypeOf(value).Kind() == reflect.Bool { + a.DeployerToken = value.(bool) + } + } + if value, ok := tmpMap["sshKey"]; ok { + if reflect.TypeOf(value).Kind() == reflect.Float64 { + vBool, err := strconv.ParseBool(fmt.Sprintf("%v", value)) + if err == nil { + a.SSHKey = vBool + } + } + if reflect.TypeOf(value).Kind() == reflect.Bool { + a.SSHKey = value.(bool) + } + } + if value, ok := tmpMap["RunnerImage"]; ok { + a.RunnerImage = value.(string) + } + if value, ok := tmpMap["runnerImage"]; ok { + a.RunnerImage = value.(string) + } + if value, ok := tmpMap["JSONPayload"]; ok { + a.JSONPayload = value.(string) + } + return nil +} diff --git a/config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml b/config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml index 7ca5f781..e3b5946d 100644 --- a/config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml +++ b/config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml @@ -268,14 +268,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -361,14 +357,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -456,14 +448,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -553,14 +541,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: diff --git a/config/crd/bases/crd.lagoon.sh_lagoontasks.yaml b/config/crd/bases/crd.lagoon.sh_lagoontasks.yaml index c4149f44..180051c3 100644 --- a/config/crd/bases/crd.lagoon.sh_lagoontasks.yaml +++ b/config/crd/bases/crd.lagoon.sh_lagoontasks.yaml @@ -132,14 +132,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -254,14 +250,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -347,14 +339,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -442,14 +430,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: @@ -539,14 +523,10 @@ spec: type: string command: type: string - deployTokenInjection: - type: boolean id: type: string name: type: string - projectKeyInjection: - type: boolean service: type: string sshHost: diff --git a/internal/messenger/tasks_handler.go b/internal/messenger/tasks_handler.go index eed0c437..c0937d44 100644 --- a/internal/messenger/tasks_handler.go +++ b/internal/messenger/tasks_handler.go @@ -264,12 +264,6 @@ func (m *Messenger) ActiveStandbySwitch(namespace string, jobSpec *lagoonv1beta1 // AdvancedTask handles running the ingress migrations. func (m *Messenger) AdvancedTask(namespace string, jobSpec *lagoonv1beta1.LagoonTaskSpec) error { - if jobSpec.Task.DeployTokenInjection { - jobSpec.AdvancedTask.DeployerToken = jobSpec.Task.DeployTokenInjection - } - if jobSpec.Task.ProjectKeyInjection { - jobSpec.AdvancedTask.SSHKey = jobSpec.Task.ProjectKeyInjection - } return m.createAdvancedTask(namespace, jobSpec, nil) }