-
Notifications
You must be signed in to change notification settings - Fork 93
Merge main into epic/issue-373 #376
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
21 changes: 21 additions & 0 deletions
21
pkg/task/inspection/googlecloudcommon/contract/tasklabel.go
This file contains hidden or 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,21 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package googlecloudcommon_contract | ||
|
|
||
| import "github.com/GoogleCloudPlatform/khi/pkg/common/typedmap" | ||
|
|
||
| // RequestOptionalInputResourceNameTaskLabel is a label assigned to a task that requests the Cloud Logging resource name optionally. | ||
| // The value is the query ID. | ||
| var RequestOptionalInputResourceNameTaskLabel = typedmap.NewTypedKey[string]("request-optional-input-resource-name") |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -15,28 +15,68 @@ | |
| package googlecloudcommon_impl | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/GoogleCloudPlatform/khi/pkg/common/khictx" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/common/typedmap" | ||
| inspectionmetadata "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/metadata" | ||
| inspectiontest "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/test" | ||
| coretask "github.com/GoogleCloudPlatform/khi/pkg/core/task" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/task/taskid" | ||
| googlecloudcommon_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/googlecloudcommon/contract" | ||
| inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract" | ||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| type mockTaskRunner struct { | ||
| tasks []coretask.UntypedTask | ||
| } | ||
|
|
||
| // AddInterceptor implements coretask.TaskRunner. | ||
| func (m *mockTaskRunner) AddInterceptor(interceptor coretask.Interceptor) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| // Result implements coretask.TaskRunner. | ||
| func (m *mockTaskRunner) Result() (*typedmap.ReadonlyTypedMap, error) { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| // Run implements coretask.TaskRunner. | ||
| func (m *mockTaskRunner) Run(ctx context.Context) error { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| // Wait implements coretask.TaskRunner. | ||
| func (m *mockTaskRunner) Wait() <-chan interface{} { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (m *mockTaskRunner) Tasks() []coretask.UntypedTask { | ||
| return m.tasks | ||
| } | ||
|
|
||
| var _ coretask.TaskRunner = (*mockTaskRunner)(nil) | ||
|
|
||
| func TestInputLoggingFilterResourceNameTask(t *testing.T) { | ||
| defaultNames := []string{"projects/foo"} | ||
| t1 := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("t1"), nil, nil, coretask.WithLabelValue( | ||
| googlecloudcommon_contract.RequestOptionalInputResourceNameTaskLabel, "test", | ||
| )) | ||
| nonRelatedTask := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("not-related"), nil, nil) | ||
| testCases := []struct { | ||
| desc string | ||
| taskMode inspectioncore_contract.InspectionTaskModeType | ||
| inputValue string | ||
| tasks []coretask.UntypedTask | ||
| wantForm inspectionmetadata.GroupParameterFormField | ||
| }{ | ||
| { | ||
| desc: "basic input", | ||
| taskMode: inspectioncore_contract.TaskModeDryRun, | ||
| inputValue: "projects/foo", | ||
| tasks: []coretask.UntypedTask{t1, nonRelatedTask}, | ||
| wantForm: inspectionmetadata.GroupParameterFormField{ | ||
| ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{ | ||
| Priority: -1000000, | ||
|
|
@@ -68,6 +108,7 @@ func TestInputLoggingFilterResourceNameTask(t *testing.T) { | |
| desc: "invalid input", | ||
| taskMode: inspectioncore_contract.TaskModeDryRun, | ||
| inputValue: "invalid-resource-name", | ||
| tasks: []coretask.UntypedTask{t1, nonRelatedTask}, | ||
| wantForm: inspectionmetadata.GroupParameterFormField{ | ||
| ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{ | ||
| Priority: -1000000, | ||
|
|
@@ -100,6 +141,7 @@ func TestInputLoggingFilterResourceNameTask(t *testing.T) { | |
| desc: "basic input for run mode", | ||
| taskMode: inspectioncore_contract.TaskModeRun, | ||
| inputValue: "projects/foo", | ||
| tasks: []coretask.UntypedTask{t1, nonRelatedTask}, | ||
| wantForm: inspectionmetadata.GroupParameterFormField{ | ||
| ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{ | ||
| Priority: -1000000, | ||
|
|
@@ -127,10 +169,31 @@ func TestInputLoggingFilterResourceNameTask(t *testing.T) { | |
| CollapsedByDefault: true, | ||
| }, | ||
| }, | ||
| { | ||
| desc: "shouldn't populate inputs when the task requesting the resource name wasn't included in the graph even it already has the default value updated", | ||
| taskMode: inspectioncore_contract.TaskModeRun, | ||
| inputValue: "projects/foo", | ||
| tasks: []coretask.UntypedTask{nonRelatedTask}, | ||
| wantForm: inspectionmetadata.GroupParameterFormField{ | ||
| ParameterFormFieldBase: inspectionmetadata.ParameterFormFieldBase{ | ||
| Priority: -1000000, | ||
| ID: googlecloudcommon_contract.InputLoggingFilterResourceNameTaskID.ReferenceIDString(), | ||
| Type: inspectionmetadata.Group, | ||
| Label: "Logging filter resource names (advanced)", | ||
| Description: "Override these parameters when your logs are not on the same project of the cluster, or customize the log filter target resources.", | ||
| HintType: inspectionmetadata.None, | ||
| Hint: "", | ||
| }, | ||
| Children: []inspectionmetadata.ParameterFormField{}, | ||
| Collapsible: true, | ||
| CollapsedByDefault: true, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.desc, func(t *testing.T) { | ||
| ctx := inspectiontest.WithDefaultTestInspectionTaskContext(t.Context()) | ||
| ctx = khictx.WithValue[coretask.TaskRunner](ctx, inspectioncore_contract.TaskRunner, &mockTaskRunner{tasks: tc.tasks}) | ||
| resourceNames, _, err := inspectiontest.RunInspectionTask(ctx, InputLoggingFilterResourceNameTask, inspectioncore_contract.TaskModeDryRun, map[string]any{}) | ||
| if err != nil { | ||
| t.Fatalf("Failed to call InputLoggingFilterResourceNameTask at 1st time:%v", err) | ||
|
|
@@ -139,6 +202,7 @@ func TestInputLoggingFilterResourceNameTask(t *testing.T) { | |
| QueryID: "test", | ||
| } | ||
| newCtx := inspectiontest.NextRunTaskContext(t.Context(), ctx) | ||
| newCtx = khictx.WithValue[coretask.TaskRunner](newCtx, inspectioncore_contract.TaskRunner, &mockTaskRunner{tasks: tc.tasks}) | ||
| resourceNames.UpdateDefaultResourceNamesForQuery("test", defaultNames) | ||
| _, metadata, err := inspectiontest.RunInspectionTask(newCtx, InputLoggingFilterResourceNameTask, tc.taskMode, map[string]any{ | ||
| resourceName.GetInputID(): tc.inputValue, | ||
|
|
@@ -157,3 +221,40 @@ func TestInputLoggingFilterResourceNameTask(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetCurrentActiveQueryIDsForResourceName(t *testing.T) { | ||
| t1 := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("t1"), nil, nil, coretask.WithLabelValue( | ||
| googlecloudcommon_contract.RequestOptionalInputResourceNameTaskLabel, "test1", | ||
| )) | ||
| t2 := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("t2"), nil, nil, coretask.WithLabelValue( | ||
| googlecloudcommon_contract.RequestOptionalInputResourceNameTaskLabel, "test2", | ||
| )) | ||
| nonRelatedTask := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("not-related"), nil, nil) | ||
| testCases := []struct { | ||
| desc string | ||
| tasks []coretask.UntypedTask | ||
| want []string | ||
| }{ | ||
| { | ||
| desc: "with mixed tasks", | ||
| tasks: []coretask.UntypedTask{t1, t2, nonRelatedTask}, | ||
| want: []string{ | ||
| "test1", | ||
| "test2", | ||
| }, | ||
| }, | ||
| { | ||
| desc: "with no tasks", | ||
| tasks: []coretask.UntypedTask{}, | ||
| want: []string{}, | ||
| }, | ||
| } | ||
|
Comment on lines
+226
to
+251
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good practice to also test the case where multiple tasks might have the same query ID. This would highlight the need for deduplication in t1 := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("t1"), nil, nil, coretask.WithLabelValue(
googlecloudcommon_contract.RequestOptionalInputResourceNameTaskLabel, "test1",
))
t2 := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("t2"), nil, nil, coretask.WithLabelValue(
googlecloudcommon_contract.RequestOptionalInputResourceNameTaskLabel, "test2",
))
t1Dup := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("t1-dup"), nil, nil, coretask.WithLabelValue(
googlecloudcommon_contract.RequestOptionalInputResourceNameTaskLabel, "test1",
))
nonRelatedTask := coretask.NewTask(taskid.NewDefaultImplementationID[struct{}]("not-related"), nil, nil)
testCases := []struct {
desc string
tasks []coretask.UntypedTask
want []string
}{
{
desc: "with mixed tasks",
tasks: []coretask.UntypedTask{t1, t2, nonRelatedTask},
want: []string{
"test1",
"test2",
},
},
{
desc: "with duplicate query IDs",
tasks: []coretask.UntypedTask{t1, t2, t1Dup, nonRelatedTask},
want: []string{
"test1",
"test2",
},
},
{
desc: "with no tasks",
tasks: []coretask.UntypedTask{},
want: []string{},
},
} |
||
| for _, tc := range testCases { | ||
| t.Run(tc.desc, func(t *testing.T) { | ||
| got := getCurrentActiveQueryIDsForResourceName(&mockTaskRunner{tasks: tc.tasks}) | ||
| if diff := cmp.Diff(tc.want, got); diff != "" { | ||
| t.Errorf("getResourceNameInputRequests mismatch (-want,+got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation may return duplicate query IDs if multiple active tasks use the same ID. This could lead to duplicate form fields in the UI, which is confusing for the user.
To prevent this, it's better to use a map to collect unique query IDs. Sorting the result also ensures a deterministic order, which improves UI consistency and testability.
Please also add
import "sort"to the file.