Skip to content

Commit

Permalink
Remove kubectl template dependency with simplified implementation (#4807
Browse files Browse the repository at this point in the history
)

This PR removes the dependency on kubectl's template package by
providing a simplified implementation for CLI help text formatting.
Binary size reduced from `92M` to `81M`. The new implementation:

- Removes i18n support to reduce complexity
- Drops markdown processing (blackfriday) dependency  
- Keeps the core formatting functionality for command help text and
examples
- Maintains heredoc support for clean multiline strings
- Includes comprehensive tests

The result is a lighter, more focused package that handles just what we
need for CLI help formatting while removing a heavy dependency.

Original inspiration from kubectl is credited in the package
documentation.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
	- Removed internationalization (i18n) support from CLI commands
	- Updated template handling to use a custom local package
	- Simplified string definitions for command descriptions and examples
	- Reduced external dependencies related to Kubernetes libraries

- **Chores**
	- Cleaned up module dependencies in `go.mod`
	- Removed unused Kubernetes-related packages

- **New Features**
	- Introduced a new `templates` utility package for CLI text formatting

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
wdbaruni authored Jan 14, 2025
1 parent 5003f7d commit 478e3f9
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 145 deletions.
7 changes: 3 additions & 4 deletions cmd/cli/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
Expand All @@ -19,11 +18,11 @@ import (
"github.com/bacalhau-project/bacalhau/pkg/config/types"
)

var setExample = templates.Examples(i18n.T(`
var setExample = templates.Examples(`
bacalhau config set api.host=127.0.0.1
bacalhau config set compute.orchestrators=http://127.0.0.1:1234,http://1.1.1.1:1234
bacalhau config set labels=foo=bar,baz=buz
`))
`)

func newSetCmd() *cobra.Command {
setCmd := &cobra.Command{
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/devstack/devstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"k8s.io/kubectl/pkg/util/i18n"

"github.com/bacalhau-project/bacalhau/cmd/util/flags/configflags"
"github.com/bacalhau-project/bacalhau/pkg/config"
"github.com/bacalhau-project/bacalhau/pkg/config_legacy"
"github.com/bacalhau-project/bacalhau/pkg/logger"
"github.com/bacalhau-project/bacalhau/webui"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/pkg/devstack"
Expand All @@ -27,12 +26,12 @@ import (
)

var (
devStackLong = templates.LongDesc(i18n.T(`
devStackLong = templates.LongDesc(`
Start a cluster of nodes and run a job on them.
`))
`)

//nolint:lll // Documentation
devstackExample = templates.Examples(i18n.T(`
devstackExample = templates.Examples(`
# Create a devstack cluster with a single requester node and 3 compute nodes (Default values)
bacalhau devstack
Expand All @@ -44,7 +43,7 @@ var (
# Run a devstack and create (or use) the config repo in a specific folder
bacalhau devstack --stack-repo ./my-devstack-configuration
`))
`)
)

type options struct {
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/docker/docker_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/cli/helpers"
"github.com/bacalhau-project/bacalhau/cmd/util"
Expand All @@ -24,12 +23,12 @@ import (
)

var (
runLong = templates.LongDesc(i18n.T(`
runLong = templates.LongDesc(`
Runs a job using the Docker executor on the node.
`))
`)

//nolint:lll // Documentation
runExample = templates.Examples(i18n.T(`
runExample = templates.Examples(`
# Run a Docker job, using the image 'dpokidov/imagemagick', with a CID mounted at /input_images and an output volume mounted at /outputs in the container. All flags after the '--' are passed directly into the container for execution.
bacalhau docker run \
-i src=ipfs://QmeZRGhe4PmjctYVSVHuEiA9oSXnqmYa4kQubSHgWbjv72,dst=/input_images \
Expand All @@ -47,7 +46,7 @@ var (
# Specify an image digest
bacalhau docker run ubuntu@sha256:35b4f89ec2ee42e7e12db3d107fe6a487137650a2af379bbd49165a1494246ea echo hello
`))
`)
)

// DockerRunOptions declares the arguments accepted by the `docker run` command
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/job/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (

"github.com/samber/lo"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"

"github.com/bacalhau-project/bacalhau/cmd/util/cols"
"github.com/bacalhau-project/bacalhau/pkg/lib/collections"
"github.com/bacalhau-project/bacalhau/pkg/models"
"github.com/bacalhau-project/bacalhau/pkg/publicapi/client/v2"
"github.com/bacalhau-project/bacalhau/pkg/util/idgen"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
Expand All @@ -25,11 +24,11 @@ import (
)

var (
describeLong = templates.LongDesc(i18n.T(`
describeLong = templates.LongDesc(`
Full description of a job, in yaml format.
Use 'bacalhau job list' to get a list of jobs.
`))
describeExample = templates.Examples(i18n.T(`
`)
describeExample = templates.Examples(`
# Describe a job with the full ID
bacalhau job describe j-e3f8c209-d683-4a41-b840-f09b88d087b9
Expand All @@ -38,7 +37,7 @@ var (
# Describe a job with json output
bacalhau job describe --output json --pretty j-b6ad164a
`))
`)
)

// DescribeOptions is a struct to support job command
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/job/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
Expand All @@ -27,14 +26,14 @@ var executionsOrderByFields = []string{"modified_at", "created_at"}
var (
executionShort = `List executions for a job by id.`

executionLong = templates.LongDesc(i18n.T(`
executionLong = templates.LongDesc(`
List executions for a job by id.
`))
`)

executionExample = templates.Examples(i18n.T(`
executionExample = templates.Examples(`
# All executions for a given job.
bacalhau job executions j-e3f8c209-d683-4a41-b840-f09b88d087b9
`))
`)
)

// ExecutionOptions is a struct to support node command
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/job/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
Expand All @@ -19,18 +18,18 @@ import (
)

var (
getLong = templates.LongDesc(i18n.T(`
getLong = templates.LongDesc(`
Get the results of the job, including stdout and stderr.
`))
`)

//nolint:lll // Documentation
getExample = templates.Examples(i18n.T(`
getExample = templates.Examples(`
# Get the results of a job.
bacalhau job get j-51225160-807e-48b8-88c9-28311c7899e1
# Get the results of a job, with a short ID.
bacalhau job get ebd9bf2f
`))
`)
)

type GetOptions struct {
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/job/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/cols"
Expand All @@ -21,11 +20,11 @@ import (
var (
historyShort = `List history events for a job by id.`

historyLong = templates.LongDesc(i18n.T(`
historyLong = templates.LongDesc(`
List job history events for a job by id.
`))
`)

historyExample = templates.Examples(i18n.T(`
historyExample = templates.Examples(`
# All events for a given job.
bacalhau job history e3f8c209-d683-4a41-b840-f09b88d087b9
Expand All @@ -34,7 +33,7 @@ var (
# Execution level events
bacalhau job history --type execution e3f8c209
`))
`)
)

// HistoryOptions is a struct to support node command
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
Expand All @@ -28,16 +27,16 @@ var orderByFields = []string{"id", "created_at"}
var (
listShort = `List submitted jobs.`

listLong = templates.LongDesc(i18n.T(`
listLong = templates.LongDesc(`
List submitted jobs.
`))
`)

listExample = templates.Examples(i18n.T(`
listExample = templates.Examples(`
# List submitted jobs.
bacalhau job list
# List jobs and output as json
bacalhau job list --output json --pretty`))
bacalhau job list --output json --pretty`)

// defaultLabelFilter is the default label filter for the list command when
// no other labels are specified.
Expand Down
11 changes: 5 additions & 6 deletions cmd/cli/job/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
)

var (
logsShortDesc = templates.LongDesc(i18n.T(`
logsShortDesc = templates.LongDesc(`
Follow logs from a currently executing job
`))
`)

logsExample = templates.Examples(i18n.T(`
logsExample = templates.Examples(`
# Read logs for a previously submitted job
bacalhau job logs j-51225160-807e-48b8-88c9-28311c7899e1
Expand All @@ -25,7 +24,7 @@ var (
# Tail logs for a previously submitted job
bacalhau job logs j-51225160-807e-48b8-88c9-28311c7899e1 --tail
`))
`)
)

type LogCommandOptions struct {
Expand Down
12 changes: 6 additions & 6 deletions cmd/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"

"github.com/bacalhau-project/bacalhau/cmd/util/output"
"github.com/bacalhau-project/bacalhau/pkg/lib/marshaller"
"github.com/bacalhau-project/bacalhau/pkg/lib/template"
"github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels"
"github.com/bacalhau-project/bacalhau/pkg/publicapi/client/v2"

"k8s.io/kubectl/pkg/util/templates"
"github.com/bacalhau-project/bacalhau/cmd/util/templates"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
Expand All @@ -21,21 +20,22 @@ import (
)

var (
runLong = templates.LongDesc(i18n.T(`
runLong = templates.LongDesc(`
Run a job from a file or from stdin.
JSON and YAML formats are accepted.
`))
`)

//nolint:lll // Documentation
runExample = templates.Examples(i18n.T(`
runExample = templates.Examples(`
# Run a job using the data in job.yaml
bacalhau job run ./job.yaml
# Run a new job from an already executed job
bacalhau job describe 6e51df50 | bacalhau job run
# Download the
`))
`)
)

type RunOptions struct {
Expand Down
Loading

0 comments on commit 478e3f9

Please sign in to comment.