-
Notifications
You must be signed in to change notification settings - Fork 14
feat(cli): complete CLI with full CRUD operations (#563) #621
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
ArangoGutierrez
merged 23 commits into
NVIDIA:main
from
ArangoGutierrez:feat/563-cli-crud-operations
Feb 6, 2026
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0833884
docs: add CLI CRUD and verbosity design document
ArangoGutierrez 72aacd7
docs: add CLI CRUD implementation plan
ArangoGutierrez 7fad32a
feat(logger): add verbosity levels with Debug and Trace methods
ArangoGutierrez 6d90bb2
feat(cli): add global verbosity flags (-q, --verbose, -d)
ArangoGutierrez 9cd1b6b
refactor(cli): rename list --quiet to --ids-only
ArangoGutierrez 3b6a39d
test(output): add unit tests for output formatting package
ArangoGutierrez 4670134
feat(output): add output formatting package with table/JSON/YAML support
ArangoGutierrez 54f1c58
feat(cli): add output formatting to status command
ArangoGutierrez dda3b16
feat(cli): add describe, get, ssh, scp, update commands with tests
ArangoGutierrez 57d6d80
test(list): update tests for --ids-only flag rename
ArangoGutierrez ec65767
fix(cli): fix nil map panic and SSH command quoting
ArangoGutierrez fb19ac0
refactor(cli): extract shared utilities and fix review issues
ArangoGutierrez 8440213
fix(ci): resolve lint issues, update go.mod, and remove plan docs
ArangoGutierrez 80e2802
fix(security): correct misleading host key comments (CWE-322)
ArangoGutierrez 8a168c8
refactor(cli): use common.GetHostURL in update runProvision (S4)
ArangoGutierrez 2867418
fix(cli): consolidate update command cache file writes (C2)
ArangoGutierrez ec2770c
feat(cli): add -q as alias for --ids-only for backwards compatibility…
ArangoGutierrez 1c0c325
refactor(cli): extract SSH retry constants in ConnectSSH (S2)
ArangoGutierrez 6b12fbb
docs(cli): document SSH remote command quoting behavior (S3)
ArangoGutierrez 4cc2ec7
fix(logger): warnings always print regardless of verbosity (M1)
ArangoGutierrez 7ee9120
refactor(cli): consolidate GetHostURL tests into common package (M3)
ArangoGutierrez ce7aef7
fix(logger): make Verbosity thread-safe with atomic.Int32 (M6)
ArangoGutierrez 607a329
fix(logger): add nolint directive for gosec G115 in SetVerbosity
ArangoGutierrez 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * 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 common | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| "golang.org/x/crypto/ssh" | ||
|
|
||
| "github.com/NVIDIA/holodeck/api/holodeck/v1alpha1" | ||
| "github.com/NVIDIA/holodeck/internal/logger" | ||
| "github.com/NVIDIA/holodeck/pkg/provider/aws" | ||
| ) | ||
|
|
||
| // GetHostURL resolves the SSH-reachable host URL for an environment. | ||
| // If nodeName is set, it looks for that specific node. | ||
| // If preferControlPlane is true and no nodeName is set, it prefers a control-plane node. | ||
| // Falls back to the first available node, then single-node properties. | ||
| func GetHostURL(env *v1alpha1.Environment, nodeName string, preferControlPlane bool) (string, error) { | ||
| // For multinode clusters, find the appropriate node | ||
| if env.Spec.Cluster != nil && env.Status.Cluster != nil && len(env.Status.Cluster.Nodes) > 0 { | ||
| if nodeName != "" { | ||
| for _, node := range env.Status.Cluster.Nodes { | ||
| if node.Name == nodeName { | ||
| return node.PublicIP, nil | ||
| } | ||
| } | ||
| return "", fmt.Errorf("node %q not found in cluster", nodeName) | ||
| } | ||
|
|
||
| if preferControlPlane { | ||
| for _, node := range env.Status.Cluster.Nodes { | ||
| if node.Role == "control-plane" { | ||
| return node.PublicIP, nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Fallback to first node | ||
| return env.Status.Cluster.Nodes[0].PublicIP, nil | ||
| } | ||
|
|
||
| // Single node - get from properties | ||
| switch env.Spec.Provider { | ||
| case v1alpha1.ProviderAWS: | ||
| for _, p := range env.Status.Properties { | ||
| if p.Name == aws.PublicDnsName { | ||
| return p.Value, nil | ||
| } | ||
| } | ||
| case v1alpha1.ProviderSSH: | ||
| return env.Spec.HostUrl, nil | ||
| } | ||
|
|
||
| return "", fmt.Errorf("unable to determine host URL") | ||
| } | ||
|
|
||
| const ( | ||
| // sshMaxRetries is the number of SSH connection attempts before giving up. | ||
| sshMaxRetries = 3 | ||
| // sshRetryDelay is the delay between SSH connection retry attempts. | ||
| sshRetryDelay = 2 * time.Second | ||
| ) | ||
|
|
||
| // ConnectSSH establishes an SSH connection with retries. | ||
| // | ||
| // Host key verification is disabled because server host keys are generated | ||
| // at instance boot time and there is no trusted channel to distribute them | ||
| // to the client beforehand. The env file's privateKey/publicKey fields are | ||
| // SSH *authentication* keys (client-to-server), not server host keys. | ||
| func ConnectSSH(log *logger.FunLogger, keyPath, userName, hostUrl string) (*ssh.Client, error) { | ||
| key, err := os.ReadFile(keyPath) //nolint:gosec // keyPath is from trusted env config | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read key file %s: %v", keyPath, err) | ||
| } | ||
|
|
||
| signer, err := ssh.ParsePrivateKey(key) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse private key: %v", err) | ||
| } | ||
|
|
||
| config := &ssh.ClientConfig{ | ||
| User: userName, | ||
| Auth: []ssh.AuthMethod{ | ||
| ssh.PublicKeys(signer), | ||
| }, | ||
| // Server host keys are generated at instance boot with no trusted | ||
| // distribution channel; disable verification (CWE-322 accepted risk). | ||
| HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec | ||
|
||
| Timeout: 30 * time.Second, | ||
| } | ||
|
|
||
| var client *ssh.Client | ||
| for i := 0; i < sshMaxRetries; i++ { | ||
| client, err = ssh.Dial("tcp", hostUrl+":22", config) | ||
| if err == nil { | ||
| return client, nil | ||
| } | ||
| log.Warning("Connection attempt %d failed: %v", i+1, err) | ||
| time.Sleep(sshRetryDelay) | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("failed to connect after %d attempts: %v", sshMaxRetries, 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,176 @@ | ||
| /* | ||
| * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * 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 common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/NVIDIA/holodeck/api/holodeck/v1alpha1" | ||
| "github.com/NVIDIA/holodeck/pkg/provider/aws" | ||
| ) | ||
|
|
||
| func TestGetHostURL_AWS_SingleNode(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderAWS, | ||
| }, | ||
| Status: v1alpha1.EnvironmentStatus{ | ||
| Properties: []v1alpha1.Properties{ | ||
| {Name: aws.PublicDnsName, Value: "ec2-1-2-3-4.compute.amazonaws.com"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| url, err := GetHostURL(env, "", false) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if url != "ec2-1-2-3-4.compute.amazonaws.com" { | ||
| t.Errorf("expected DNS name, got %s", url) | ||
| } | ||
| } | ||
|
|
||
| func TestGetHostURL_SSH(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderSSH, | ||
| Instance: v1alpha1.Instance{ | ||
| HostUrl: "192.168.1.100", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| url, err := GetHostURL(env, "", false) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if url != "192.168.1.100" { | ||
| t.Errorf("expected 192.168.1.100, got %s", url) | ||
| } | ||
| } | ||
|
|
||
| func TestGetHostURL_NoProperties(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderAWS, | ||
| }, | ||
| Status: v1alpha1.EnvironmentStatus{ | ||
| Properties: []v1alpha1.Properties{}, | ||
| }, | ||
| } | ||
|
|
||
| _, err := GetHostURL(env, "", false) | ||
| if err == nil { | ||
| t.Error("expected error for missing properties") | ||
| } | ||
| } | ||
|
|
||
| func TestGetHostURL_Cluster_PreferControlPlane(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderAWS, | ||
| Cluster: &v1alpha1.ClusterSpec{}, | ||
| }, | ||
| Status: v1alpha1.EnvironmentStatus{ | ||
| Cluster: &v1alpha1.ClusterStatus{ | ||
| Nodes: []v1alpha1.NodeStatus{ | ||
| {Name: "worker-0", Role: "worker", PublicIP: "10.0.0.1"}, | ||
| {Name: "cp-0", Role: "control-plane", PublicIP: "10.0.0.2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| url, err := GetHostURL(env, "", true) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if url != "10.0.0.2" { | ||
| t.Errorf("expected control-plane IP 10.0.0.2, got %s", url) | ||
| } | ||
| } | ||
|
|
||
| func TestGetHostURL_Cluster_FallbackToFirstNode(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderAWS, | ||
| Cluster: &v1alpha1.ClusterSpec{}, | ||
| }, | ||
| Status: v1alpha1.EnvironmentStatus{ | ||
| Cluster: &v1alpha1.ClusterStatus{ | ||
| Nodes: []v1alpha1.NodeStatus{ | ||
| {Name: "worker-0", Role: "worker", PublicIP: "10.0.0.1"}, | ||
| {Name: "worker-1", Role: "worker", PublicIP: "10.0.0.2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| url, err := GetHostURL(env, "", true) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if url != "10.0.0.1" { | ||
| t.Errorf("expected first node IP 10.0.0.1, got %s", url) | ||
| } | ||
| } | ||
|
|
||
| func TestGetHostURL_Cluster_SpecificNode(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderAWS, | ||
| Cluster: &v1alpha1.ClusterSpec{}, | ||
| }, | ||
| Status: v1alpha1.EnvironmentStatus{ | ||
| Cluster: &v1alpha1.ClusterStatus{ | ||
| Nodes: []v1alpha1.NodeStatus{ | ||
| {Name: "cp-0", Role: "control-plane", PublicIP: "10.0.0.1"}, | ||
| {Name: "worker-0", Role: "worker", PublicIP: "10.0.0.2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| url, err := GetHostURL(env, "worker-0", false) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if url != "10.0.0.2" { | ||
| t.Errorf("expected worker IP 10.0.0.2, got %s", url) | ||
| } | ||
| } | ||
|
|
||
| func TestGetHostURL_Cluster_NodeNotFound(t *testing.T) { | ||
| env := &v1alpha1.Environment{ | ||
| Spec: v1alpha1.EnvironmentSpec{ | ||
| Provider: v1alpha1.ProviderAWS, | ||
| Cluster: &v1alpha1.ClusterSpec{}, | ||
| }, | ||
| Status: v1alpha1.EnvironmentStatus{ | ||
| Cluster: &v1alpha1.ClusterStatus{ | ||
| Nodes: []v1alpha1.NodeStatus{ | ||
| {Name: "cp-0", Role: "control-plane", PublicIP: "10.0.0.1"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| _, err := GetHostURL(env, "nonexistent", false) | ||
| if err == nil { | ||
| t.Error("expected error for nonexistent node") | ||
| } | ||
| } |
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.
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 field path should be
env.Spec.Instance.HostUrlnotenv.Spec.HostUrl. The HostUrl field is defined on the Instance struct, not directly on EnvironmentSpec. This will cause a compilation error or runtime panic.