Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert and fix the deployertoken injection using a custom unmarshal #231

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 51 additions & 10 deletions apis/lagoon/v1beta1/lagoontask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.
package v1beta1

import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
16 changes: 0 additions & 16 deletions config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 0 additions & 20 deletions config/crd/bases/crd.lagoon.sh_lagoontasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions internal/messenger/tasks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down