forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete sidecar pod when task completes. (#26)
* Delete sidecar pod when task completes. * missing comment * Update plugin_executor.go * Update plugin_executor.go * in two steps * fix * Update plugin_executor.go * test fixes * tests * bogus comment * fixing k8s object state * debug logs * Update go/tasks/v1/flytek8s/k8splugin_state_test.go Co-Authored-By: Haytham AbuelFutuh <[email protected]> * cr feedback * cr feedback * cr feedback * removing djo
- Loading branch information
1 parent
a483b2e
commit 3013158
Showing
4 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package flytek8s | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/lyft/flyteplugins/go/tasks/v1/types" | ||
) | ||
|
||
const stateKey = "os" | ||
|
||
// This status internal state of the object not read/updated by upstream components (eg. Node manager) | ||
type K8sObjectStatus int | ||
|
||
const ( | ||
k8sObjectUnknown K8sObjectStatus = iota | ||
k8sObjectExists | ||
k8sObjectDeleted | ||
) | ||
|
||
func (q K8sObjectStatus) String() string { | ||
switch q { | ||
case k8sObjectUnknown: | ||
return "NotStarted" | ||
case k8sObjectExists: | ||
return "Running" | ||
case k8sObjectDeleted: | ||
return "Deleted" | ||
} | ||
return "IllegalK8sObjectStatus" | ||
} | ||
|
||
// This status internal state of the object not read/updated by upstream components (eg. Node manager) | ||
type K8sObjectState struct { | ||
Status K8sObjectStatus `json:"s"` | ||
TerminalPhase types.TaskPhase `json:"tp"` | ||
} | ||
|
||
func retrieveK8sObjectState(customState map[string]interface{}) (K8sObjectStatus, types.TaskPhase, error) { | ||
v, found := customState[stateKey] | ||
if !found { | ||
return k8sObjectUnknown, types.TaskPhaseUnknown, nil | ||
} | ||
|
||
state, err := convertToState(v) | ||
if err != nil { | ||
return k8sObjectUnknown, types.TaskPhaseUnknown, err | ||
} | ||
return state.Status, state.TerminalPhase, nil | ||
} | ||
|
||
func storeK8sObjectState(status K8sObjectStatus, phase types.TaskPhase) map[string]interface{} { | ||
customState := make(map[string]interface{}) | ||
customState[stateKey] = K8sObjectState{Status: status, TerminalPhase: phase} | ||
return customState | ||
} | ||
|
||
func convertToState(iface interface{}) (K8sObjectState, error) { | ||
raw, err := json.Marshal(iface) | ||
if err != nil { | ||
return K8sObjectState{}, err | ||
} | ||
|
||
item := &K8sObjectState{} | ||
err = json.Unmarshal(raw, item) | ||
if err != nil { | ||
return K8sObjectState{}, fmt.Errorf("failed to unmarshal state into K8sObjectState") | ||
} | ||
|
||
return *item, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package flytek8s | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/lyft/flyteplugins/go/tasks/v1/types" | ||
"encoding/json" | ||
) | ||
|
||
func TestRetrieveK8sObjectStatus(t *testing.T) { | ||
status := k8sObjectExists | ||
phase := types.TaskPhaseRunning | ||
customState := storeK8sObjectState(status, phase) | ||
|
||
raw, err := json.Marshal(customState) | ||
assert.NoError(t, err) | ||
|
||
unmarshalledCustomState := make(map[string]interface{}) | ||
err = json.Unmarshal(raw, &unmarshalledCustomState) | ||
assert.NoError(t, err) | ||
|
||
retrievedStatus, retrievedPhase, err := retrieveK8sObjectState(unmarshalledCustomState) | ||
assert.NoError(t, err) | ||
assert.Equal(t, status, retrievedStatus) | ||
assert.Equal(t, phase, retrievedPhase) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters