-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds
agent config
command that returns the agent configuration (
#4671) - closes #4113 Example: ```shell $ bacalhau agent config ``` ```yaml API: Host: 0.0.0.0 Port: 1234 Auth: Methods: ClientKey: Type: challenge NameProvider: puuid DataDir: /home/frrist/.bacalhau Orchestrator: Enabled: true Host: 0.0.0.0 Port: 4222 NodeManager: DisconnectTimeout: 1m0s Scheduler: WorkerCount: 32 QueueBackoff: 1m0s HousekeepingInterval: 30s HousekeepingTimeout: 2m0s EvaluationBroker: VisibilityTimeout: 1m0s MaxRetryCount: 10 Compute: Orchestrators: - nats://127.0.0.1:4222 Heartbeat: InfoUpdateInterval: 1m0s ResourceUpdateInterval: 30s Interval: 15s AllocatedCapacity: CPU: 70% Memory: 70% Disk: 70% GPU: 100% AllowListedLocalPaths: [] WebUI: Listen: 0.0.0.0:8438 InputSources: ReadTimeout: 5m0s MaxRetryCount: 3 Publishers: Types: Local: Address: 127.0.0.1 Port: 6001 Engines: Types: Docker: ManifestCache: Size: 1000 TTL: 1h0m0s Refresh: 1h0m0s JobDefaults: Batch: Task: Resources: CPU: 500m Memory: 1Gb Ops: Task: Resources: CPU: 500m Memory: 1Gb Daemon: Task: Resources: CPU: 500m Memory: 1Gb Service: Task: Resources: CPU: 500m Memory: 1Gb JobAdmissionControl: Locality: anywhere Logging: Level: info Mode: default LogDebugInfoInterval: 30s UpdateConfig: Interval: 24h0m0s ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a command to retrieve the agent's configuration, allowing users to access configuration details easily. - Added support for customizable output formats (JSON and YAML) for the configuration command. - **Bug Fixes** - Enhanced error handling and sensitive data redaction in the configuration retrieval process. - **Tests** - Implemented tests to ensure the correct functionality and output of the new configuration command, including validation of sensitive data redaction. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: frrist <[email protected]>
- Loading branch information
Showing
14 changed files
with
201 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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,48 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/bacalhau-project/bacalhau/cmd/util" | ||
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags" | ||
"github.com/bacalhau-project/bacalhau/cmd/util/output" | ||
"github.com/bacalhau-project/bacalhau/pkg/config/types" | ||
"github.com/bacalhau-project/bacalhau/pkg/publicapi/client/v2" | ||
) | ||
|
||
func NewConfigCmd() *cobra.Command { | ||
o := output.NonTabularOutputOptions{ | ||
Format: output.YAMLFormat, | ||
Pretty: true, | ||
} | ||
configCmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "Get the agent's configuration.", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, err := util.SetupRepoConfig(cmd) | ||
if err != nil { | ||
return fmt.Errorf("failed to setup repo: %w", err) | ||
} | ||
api, err := util.GetAPIClientV2(cmd, cfg) | ||
if err != nil { | ||
return fmt.Errorf("failed to create api client: %w", err) | ||
} | ||
return run(cmd, api, o) | ||
}, | ||
} | ||
configCmd.Flags().AddFlagSet(cliflags.OutputNonTabularFormatFlags(&o)) | ||
return configCmd | ||
} | ||
|
||
func run(cmd *cobra.Command, api client.API, o output.NonTabularOutputOptions) error { | ||
ctx := cmd.Context() | ||
response, err := api.Agent().Config(ctx) | ||
if err != nil { | ||
return fmt.Errorf("cannot get agent config: %w", err) | ||
} | ||
|
||
return output.OutputOneNonTabular[types.Bacalhau](cmd, o, response.Config) | ||
} |
This file contains 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,46 @@ | ||
//go:build unit || !integration | ||
|
||
package agent_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"gopkg.in/yaml.v3" | ||
|
||
cmdtesting "github.com/bacalhau-project/bacalhau/cmd/testing" | ||
"github.com/bacalhau-project/bacalhau/cmd/util/output" | ||
"github.com/bacalhau-project/bacalhau/pkg/config/types" | ||
) | ||
|
||
func TestConfigSuite(t *testing.T) { | ||
suite.Run(t, new(ConfigSuite)) | ||
} | ||
|
||
type ConfigSuite struct { | ||
cmdtesting.BaseSuite | ||
} | ||
|
||
func (s *ConfigSuite) TestConfigJSONOutput() { | ||
_, out, err := s.ExecuteTestCobraCommand( | ||
"agent", "config", "--output", string(output.JSONFormat), "--pretty=false", | ||
) | ||
s.Require().NoError(err, "Could not request config with json output.") | ||
|
||
var cfg types.Bacalhau | ||
err = json.Unmarshal([]byte(out), &cfg) | ||
s.Require().NoError(err, "Could not unmarshal the output into json - %+v", err) | ||
s.Require().True(cfg.Orchestrator.Enabled) | ||
} | ||
|
||
func (s *ConfigSuite) TestConfigYAMLOutput() { | ||
// NB: the default output is yaml, thus we don't specify it here. | ||
_, out, err := s.ExecuteTestCobraCommand("agent", "config") | ||
s.Require().NoError(err, "Could not request config with yaml output.") | ||
|
||
var cfg types.Bacalhau | ||
err = yaml.Unmarshal([]byte(out), &cfg) | ||
s.Require().NoError(err, "Could not unmarshal the output into yaml - %+v", out) | ||
s.Require().True(cfg.Orchestrator.Enabled) | ||
} |
This file contains 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 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 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 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 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 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,52 @@ | ||
//go:build unit || !integration | ||
|
||
package agent | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/bacalhau-project/bacalhau/pkg/config/types" | ||
"github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels" | ||
) | ||
|
||
// TestEndpointConfigRedactFields asserts that auth tokens in the config are redacted. | ||
func TestEndpointConfigRedactFields(t *testing.T) { | ||
router := echo.New() | ||
|
||
// populate the fields that should be redacted with "secret" values. | ||
NewEndpoint(EndpointParams{ | ||
Router: router, | ||
BacalhauConfig: types.Bacalhau{ | ||
Orchestrator: types.Orchestrator{ | ||
Auth: types.OrchestratorAuth{ | ||
Token: "super-secret-orchestrator-token", | ||
}, | ||
}, | ||
Compute: types.Compute{ | ||
Auth: types.ComputeAuth{ | ||
Token: "super-secret-orchestrator-token", | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/api/v1/agent/config", nil) | ||
rr := httptest.NewRecorder() | ||
router.ServeHTTP(rr, req) | ||
|
||
require.Equal(t, http.StatusOK, rr.Code) | ||
|
||
// assert the secret values are not present. | ||
var payload apimodels.GetAgentConfigResponse | ||
err := json.NewDecoder(rr.Body).Decode(&payload) | ||
require.NoError(t, err) | ||
assert.Equal(t, payload.Config.Orchestrator.Auth.Token, "<redacted>") | ||
assert.Equal(t, payload.Config.Compute.Auth.Token, "<redacted>") | ||
} |
This file contains 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 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 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 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 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 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