-
Notifications
You must be signed in to change notification settings - Fork 181
feat: added jobservice pools, workers, jobs subcommand #796
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
Open
NishchayRajput
wants to merge
4
commits into
goharbor:main
Choose a base branch
from
NishchayRajput:feat/jobservice-workers-pools-jobs-subcommand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 jobservice | ||
|
|
||
| import ( | ||
| "github.com/goharbor/harbor-cli/cmd/harbor/root/jobservice/jobs" | ||
| "github.com/goharbor/harbor-cli/cmd/harbor/root/jobservice/pools" | ||
| "github.com/goharbor/harbor-cli/cmd/harbor/root/jobservice/workers" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // JobService creates the jobservice command | ||
| func JobService() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "jobservice", | ||
| Short: "Manage Harbor job service (admin only)", | ||
| Long: `Manage Harbor job service components including worker pools, job queues, schedules, and job logs. | ||
| This requires system admin privileges. | ||
|
|
||
| Use "harbor jobservice [command] --help" for detailed examples and flags per subcommand. | ||
|
|
||
| Examples: | ||
| harbor jobservice pools list | ||
| harbor jobservice workers list | ||
| harbor jobservice jobs list | ||
| harbor jobservice jobs list --page 1 --page-size 10`, | ||
| } | ||
|
|
||
| cmd.AddCommand( | ||
| pools.PoolsCommand(), | ||
| workers.WorkersCommand(), | ||
| jobs.JobsCommand(), | ||
| ) | ||
|
|
||
| return cmd | ||
| } | ||
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,31 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 jobs | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // JobsCommand creates the jobs subcommand | ||
| func JobsCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "jobs", | ||
| Short: "Manage job logs (view by job ID)", | ||
| Long: "View logs for specific jobs.", | ||
| } | ||
|
|
||
| cmd.AddCommand(LogCommand()) | ||
|
|
||
| return cmd | ||
| } |
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,73 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 jobs | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| jobserviceutils "github.com/goharbor/harbor-cli/pkg/utils/jobservice" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // LogCommand retrieves and displays job logs | ||
| func LogCommand() *cobra.Command { | ||
| var jobID string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "log", | ||
| Short: "View a job log (--job-id required)", | ||
| Long: `Display the log for a specific job by job ID. | ||
|
|
||
| Job logs contain detailed execution information including status updates, error messages, and processing details. | ||
| The --job-id flag is required to specify which job's log to retrieve. | ||
|
|
||
| Job IDs can be obtained from the 'harbor jobservice jobs list' command.`, | ||
| Example: `View a specific job log: | ||
| harbor jobservice jobs log --job-id abc123def456 | ||
|
|
||
| View log with verbose output: | ||
| harbor jobservice jobs log --job-id abc123def456 -v`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if jobID == "" { | ||
| return fmt.Errorf("--job-id must be specified") | ||
| } | ||
|
|
||
| fmt.Printf("Retrieving log for job %s...\n\n", jobID) | ||
|
|
||
| log, err := api.GetJobLog(jobID) | ||
| if err != nil { | ||
| return jobserviceutils.FormatScheduleError("failed to retrieve job log", err, "authenticated") | ||
| } | ||
|
|
||
| if log == "" { | ||
| fmt.Println("No log content available for this job.") | ||
| return nil | ||
| } | ||
|
|
||
| fmt.Println("=== Job Log ===") | ||
| fmt.Println(log) | ||
|
NishchayRajput marked this conversation as resolved.
|
||
| fmt.Println("=== End of Log ===") | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVar(&jobID, "job-id", "", "Job ID to fetch log for (required)") | ||
| if err := cmd.MarkFlagRequired("job-id"); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
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,44 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 jobs | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestJobsCommandIncludesLogSubcommand(t *testing.T) { | ||
| cmd := JobsCommand() | ||
|
|
||
| if cmd.Use != "jobs" { | ||
| t.Fatalf("expected command use to be jobs, got %s", cmd.Use) | ||
| } | ||
|
|
||
| if len(cmd.Commands()) != 1 { | ||
| t.Fatalf("expected one subcommand, got %d", len(cmd.Commands())) | ||
| } | ||
|
|
||
| if got := cmd.Commands()[0].Name(); got != "log" { | ||
| t.Fatalf("expected log subcommand, got %s", got) | ||
| } | ||
| } | ||
|
|
||
| func TestLogCommandRequiresJobID(t *testing.T) { | ||
| cmd := LogCommand() | ||
| cmd.SetArgs([]string{}) | ||
| if err := cmd.Execute(); err == nil || !strings.Contains(err.Error(), "required flag(s) \"job-id\" not set") { | ||
| t.Fatalf("expected job-id validation error, got %v", err) | ||
| } | ||
| } |
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,37 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 pools | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // PoolsCommand creates the pools subcommand. | ||
| func PoolsCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "pools", | ||
| Aliases: []string{"pool"}, | ||
| Short: "Manage worker pools (list available pools)", | ||
| Long: `List and manage worker pools for the Harbor job service. | ||
|
|
||
| Use 'list' to view all worker pools. | ||
|
|
||
| Examples: | ||
| harbor jobservice pools list | ||
| harbor jobservice pool list`, | ||
| } | ||
|
|
||
| cmd.AddCommand(ListCommand()) | ||
| return cmd | ||
| } |
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,56 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 pools | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/utils" | ||
| jobserviceutils "github.com/goharbor/harbor-cli/pkg/utils/jobservice" | ||
| poolviews "github.com/goharbor/harbor-cli/pkg/views/jobservice/pools" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| // ListCommand lists all worker pools. | ||
| func ListCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List all worker pools", | ||
| Long: "Display all worker pools with their details.", | ||
| Example: "harbor jobservice pools list\nharbor jobservice pool list", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| response, err := api.GetWorkerPools() | ||
| if err != nil { | ||
| return jobserviceutils.FormatScheduleError("failed to retrieve worker pools", err, "ActionList") | ||
| } | ||
|
|
||
| if response == nil || response.Payload == nil || len(response.Payload) == 0 { | ||
| fmt.Println("No worker pools found.") | ||
| return nil | ||
| } | ||
|
|
||
| formatFlag := viper.GetString("output-format") | ||
| if formatFlag != "" { | ||
| return utils.PrintFormat(response.Payload, formatFlag) | ||
| } | ||
|
|
||
| poolviews.ListPools(response.Payload) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } |
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,45 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // 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 pools | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestPoolsCommandIncludesListSubcommand(t *testing.T) { | ||
| cmd := PoolsCommand() | ||
|
|
||
| if cmd.Use != "pools" { | ||
| t.Fatalf("expected command use to be pools, got %s", cmd.Use) | ||
| } | ||
|
|
||
| if len(cmd.Commands()) != 1 { | ||
| t.Fatalf("expected one subcommand, got %d", len(cmd.Commands())) | ||
| } | ||
|
|
||
| if got := cmd.Commands()[0].Name(); got != "list" { | ||
| t.Fatalf("expected list subcommand, got %s", got) | ||
| } | ||
| } | ||
|
|
||
| func TestListCommandName(t *testing.T) { | ||
| cmd := ListCommand() | ||
|
|
||
| if cmd.Name() != "list" { | ||
| t.Fatalf("expected list command, got %s", cmd.Name()) | ||
| } | ||
|
|
||
| if cmd.Short == "" { | ||
| t.Fatal("expected short description to be populated") | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.