Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 31 additions & 28 deletions pkg/core/inspection/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,41 @@ var DefaultFeatureTaskOrder = 1000000
// InspectionTaskRunner manages the lifecycle of a single inspection instance.
// It handles task graph resolution, execution, and result retrieval for a given inspection type and feature set.
type InspectionTaskRunner struct {
inspectionServer *InspectionTaskServer
ID string
runIDGenerator idgenerator.IDGenerator
enabledFeatures map[string]bool
availableTasks *coretask.TaskSet
featureTasks *coretask.TaskSet
runner coretask.TaskRunner
runnerLock sync.Mutex
metadata *typedmap.ReadonlyTypedMap
cancel context.CancelFunc
inspectionSharedMap *typedmap.TypedMap
currentInspectionType string
ioconfig *inspectioncore_contract.IOConfig
runContextOptions []RunContextOption
inspectionServer *InspectionTaskServer
ID string
runIDGenerator idgenerator.IDGenerator
enabledFeatures map[string]bool
availableTasks *coretask.TaskSet
featureTasks *coretask.TaskSet
runner coretask.TaskRunner
runnerLock sync.Mutex
metadata *typedmap.ReadonlyTypedMap
cancel context.CancelFunc
inspectionSharedMap *typedmap.TypedMap
currentInspectionType string
ioconfig *inspectioncore_contract.IOConfig
runContextOptions []RunContextOption
inspectionCreationTime time.Time
}

// NewInspectionRunner creates a new InspectionTaskRunner.
func NewInspectionRunner(server *InspectionTaskServer, ioConfig *inspectioncore_contract.IOConfig, id string, options ...RunContextOption) *InspectionTaskRunner {
runner := &InspectionTaskRunner{
inspectionServer: server,
ID: id,
runIDGenerator: idgenerator.NewPrefixIDGenerator("run-"),
enabledFeatures: map[string]bool{},
availableTasks: nil,
featureTasks: nil,
runner: nil,
runnerLock: sync.Mutex{},
metadata: nil,
inspectionSharedMap: typedmap.NewTypedMap(),
cancel: nil,
currentInspectionType: "N/A",
ioconfig: ioConfig,
runContextOptions: options,
inspectionCreationTime: time.Now(),
inspectionServer: server,
ID: id,
runIDGenerator: idgenerator.NewPrefixIDGenerator("run-"),
enabledFeatures: map[string]bool{},
availableTasks: nil,
featureTasks: nil,
runner: nil,
runnerLock: sync.Mutex{},
metadata: nil,
inspectionSharedMap: typedmap.NewTypedMap(),
cancel: nil,
currentInspectionType: "N/A",
ioconfig: ioConfig,
runContextOptions: options,
}
runner.addDefaultRunContextOptions()
return runner
Expand All @@ -87,6 +89,7 @@ func NewInspectionRunner(server *InspectionTaskServer, ioConfig *inspectioncore_
func (i *InspectionTaskRunner) addDefaultRunContextOptions() {
// Options common for any run from this runner.
defaultRunContextOptions := []RunContextOption{
RunContextOptionFromValue(inspectioncore_contract.InspectionCreationTime, i.inspectionCreationTime),
RunContextOptionFromFunc(inspectioncore_contract.InspectionTaskRunID, func(ctx context.Context, mode inspectioncore_contract.InspectionTaskModeType) (string, error) {
return i.runIDGenerator.Generate(), nil
}),
Expand Down
6 changes: 5 additions & 1 deletion pkg/core/inspection/test/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package inspectiontest

import (
"context"
"time"

"github.com/GoogleCloudPlatform/khi/pkg/common/khictx"
"github.com/GoogleCloudPlatform/khi/pkg/common/typedmap"
Expand All @@ -26,9 +27,12 @@ import (
inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract"
)

var TestTime = time.Date(2025, time.January, 1, 1, 1, 1, 1, time.UTC)
Comment thread
kyasbal marked this conversation as resolved.
Outdated

// WithDefaultTestInspectionTaskContext returns a new context used for running inspection task.
func WithDefaultTestInspectionTaskContext(baseContext context.Context) context.Context {
taskCtx := khictx.WithValue(baseContext, inspectioncore_contract.InspectionTaskInspectionID, "fake-inspection-id")
taskCtx := khictx.WithValue(baseContext, inspectioncore_contract.InspectionCreationTime, TestTime)
taskCtx = khictx.WithValue(taskCtx, inspectioncore_contract.InspectionTaskInspectionID, "fake-inspection-id")
taskCtx = khictx.WithValue(taskCtx, inspectioncore_contract.InspectionTaskRunID, "fake-run-id")

taskCtx = khictx.WithValue(taskCtx, inspectioncore_contract.GlobalSharedMap, typedmap.NewTypedMap())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/GoogleCloudPlatform/khi/pkg/common"
"github.com/GoogleCloudPlatform/khi/pkg/common/khictx"
"github.com/GoogleCloudPlatform/khi/pkg/core/inspection/formtask"
inspectionmetadata "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/metadata"
coretask "github.com/GoogleCloudPlatform/khi/pkg/core/task"
Expand All @@ -31,7 +32,6 @@ import (
// InputEndTimeTask defines a form task to input the end time for log queries.
var InputEndTimeTask = formtask.NewTextFormTaskBuilder(googlecloudcommon_contract.InputEndTimeTaskID, googlecloudcommon_contract.PriorityForQueryTimeGroup+5000, "End time").
WithDependencies([]taskid.UntypedTaskReference{
inspectioncore_contract.InspectionTimeTaskID.Ref(),
inspectioncore_contract.TimeZoneShiftInputTaskID.Ref(),
}).
WithDescription(`The endtime of query. Please input it in the format of RFC3339
Expand All @@ -43,16 +43,16 @@ var InputEndTimeTask = formtask.NewTextFormTaskBuilder(googlecloudcommon_contrac
if len(previousValues) > 0 {
return previousValues[0], nil
}
inspectionTime := coretask.GetTaskResult(ctx, inspectioncore_contract.InspectionTimeTaskID.Ref())
creationTime := khictx.MustGetValue(ctx, inspectioncore_contract.InspectionCreationTime)
timezoneShift := coretask.GetTaskResult(ctx, inspectioncore_contract.TimeZoneShiftInputTaskID.Ref())

return inspectionTime.In(timezoneShift).Format(time.RFC3339), nil
return creationTime.In(timezoneShift).Format(time.RFC3339), nil
}).
WithHintFunc(func(ctx context.Context, value string, convertedValue any) (string, inspectionmetadata.ParameterHintType, error) {
inspectionTime := coretask.GetTaskResult(ctx, inspectioncore_contract.InspectionTimeTaskID.Ref())
creationTime := khictx.MustGetValue(ctx, inspectioncore_contract.InspectionCreationTime)

specifiedTime := convertedValue.(time.Time)
if inspectionTime.Sub(specifiedTime) < 0 {
if creationTime.Sub(specifiedTime) < 0 {
return fmt.Sprintf("Specified time `%s` is pointing the future. Please make sure if you specified the right value", value), inspectionmetadata.Warning, nil
}
return "", inspectionmetadata.Info, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func TestInputEndtime(t *testing.T) {
expectedDescription := "The endtime of query. Please input it in the format of RFC3339\n(example: 2006-01-02T15:04:05-07:00)"
expectedLabel := "End time"
expectedValue1, err := time.Parse(time.RFC3339, "2020-01-02T03:04:05Z")
expectedValue1, err := time.Parse(time.RFC3339, "2025-01-01T01:01:01Z")
if err != nil {
t.Errorf("unexpected error\n%s", err)
}
Expand All @@ -44,15 +44,15 @@ func TestInputEndtime(t *testing.T) {
Name: "with empty",
Input: "",
ExpectedValue: expectedValue1,
Dependencies: []coretask.UntypedTask{inspectioncore_impl.TestInspectionTimeTaskProducer("2020-01-02T03:04:05Z"), timezoneTaskUTC},
Dependencies: []coretask.UntypedTask{timezoneTaskUTC},
ExpectedFormField: inspectionmetadata.TextParameterFormField{
ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{
Label: expectedLabel,
Description: expectedDescription,
Hint: "invalid time format. Please specify in the format of `2006-01-02T15:04:05-07:00`(RFC3339)",
HintType: inspectionmetadata.Error,
},
Default: "2020-01-02T03:04:05Z",
Default: "2025-01-01T01:01:01Z",
Suggestions: []string{},
ValidationTiming: inspectionmetadata.Change,
},
Expand All @@ -61,31 +61,31 @@ func TestInputEndtime(t *testing.T) {
Name: "with valid timestamp and UTC timezone",
Input: "2020-01-02T00:00:00Z",
ExpectedValue: expectedValue2,
Dependencies: []coretask.UntypedTask{inspectioncore_impl.TestInspectionTimeTaskProducer("2020-01-02T03:04:05Z"), timezoneTaskUTC},
Dependencies: []coretask.UntypedTask{timezoneTaskUTC},
ExpectedFormField: inspectionmetadata.TextParameterFormField{
ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{
Label: expectedLabel,
Description: expectedDescription,
HintType: inspectionmetadata.None,
},
Suggestions: []string{},
Default: "2020-01-02T03:04:05Z",
Default: "2025-01-01T01:01:01Z",
ValidationTiming: inspectionmetadata.Change,
},
},
{
Name: "with valid timestamp and non UTC timezone",
Input: "2020-01-02T00:00:00Z",
ExpectedValue: expectedValue2,
Dependencies: []coretask.UntypedTask{inspectioncore_impl.TestInspectionTimeTaskProducer("2020-01-02T03:04:05Z"), timezoneTaskJST},
Dependencies: []coretask.UntypedTask{timezoneTaskJST},
ExpectedFormField: inspectionmetadata.TextParameterFormField{
ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{
Label: expectedLabel,
Description: expectedDescription,
HintType: inspectionmetadata.None,
},
Suggestions: []string{},
Default: "2020-01-02T12:04:05+09:00",
Default: "2025-01-01T10:01:01+09:00",
ValidationTiming: inspectionmetadata.Change,
},
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/task/inspection/inspectioncore/contract/contextkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package inspectioncore_contract

import (
"time"

"github.com/GoogleCloudPlatform/khi/pkg/common/typedmap"
"github.com/GoogleCloudPlatform/khi/pkg/model/history"
)
Expand All @@ -26,6 +28,9 @@ var InspectionTaskMode = typedmap.NewTypedKey[InspectionTaskModeType]("khi.googl
// It contains a map of parameter names to their values.
var InspectionTaskInput = typedmap.NewTypedKey[map[string]any]("khi.google.com/inspection/task-input")

// InspectionCreationTime is the context key to access the time when user created the inspection.
var InspectionCreationTime = typedmap.NewTypedKey[time.Time]("khi.google.com/inspection/creation-time")

// InspectionSharedMap is the context key to access a shared typed map
// that persists across multiple executions of an inspection task.
// This map maintains its values between task executions and does not reset,
Expand Down
Loading