Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Transition to failed state if the k8s resource cannot be created #519

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions pkg/controller/nodes/task/k8s/plugin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,19 @@ func (e *PluginManager) LaunchResource(ctx context.Context, tCtx pluginsCore.Tas

tmpl, err := tCtx.TaskReader().Read(ctx)
if err != nil {
return pluginsCore.Transition{}, err
return pluginsCore.DoTransition(pluginsCore.PhaseInfoFailure("Failed to read task template", err.Error(), nil)), nil
}

k8sTaskCtxMetadata, err := newTaskExecutionMetadata(tCtx.TaskExecutionMetadata(), tmpl)
if err != nil {
return pluginsCore.Transition{}, err
return pluginsCore.DoTransition(pluginsCore.PhaseInfoFailure("Failed to create task context", err.Error(), nil)), nil
}

k8sTaskCtx := newTaskExecutionContext(tCtx, k8sTaskCtxMetadata)

o, err := e.plugin.BuildResource(ctx, k8sTaskCtx)
if err != nil {
return pluginsCore.UnknownTransition, err
return pluginsCore.DoTransition(pluginsCore.PhaseInfoFailure("Failed to build k8s resource", err.Error(), nil)), nil
}

e.AddObjectMetadata(k8sTaskCtxMetadata, o, config.GetK8sPluginConfig())
Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/nodes/task/k8s/plugin_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,28 @@ func TestK8sTaskExecutor_Handle_LaunchResource(t *testing.T) {
assert.NoError(t, fakeClient.Delete(ctx, createdPod))
})

t.Run("failed to build k8s resource", func(t *testing.T) {
tCtx := getMockTaskContext(PluginPhaseNotStarted, PluginPhaseStarted)
// common setup code
mockResourceHandler := &pluginsk8sMock.Plugin{}
mockResourceHandler.OnGetProperties().Return(k8s.PluginProperties{})
mockResourceHandler.OnBuildResourceMatch(mock.Anything, mock.Anything).Return(nil, errors.New("failed"))
fakeClient := fake.NewClientBuilder().WithRuntimeObjects().Build()
pluginManager, err := NewPluginManager(ctx, dummySetupContext(fakeClient), k8s.PluginEntry{
ID: "x",
ResourceToWatch: &v1.Pod{},
Plugin: mockResourceHandler,
}, NewResourceMonitorIndex())
assert.NoError(t, err)

transition, err := pluginManager.Handle(ctx, tCtx)
assert.NoError(t, err)
assert.NotNil(t, transition)
transitionInfo := transition.Info()
assert.NotNil(t, transitionInfo)
assert.Equal(t, pluginsCore.PhasePermanentFailure, transitionInfo.Phase())
})

t.Run("jobAlreadyExists", func(t *testing.T) {
tctx := getMockTaskContext(PluginPhaseNotStarted, PluginPhaseStarted)
// common setup code
Expand Down