Skip to content

Commit

Permalink
Add get command for the kubectl (#3606)
Browse files Browse the repository at this point in the history
  • Loading branch information
knanao authored May 10, 2022
1 parent 17deb61 commit 768c960
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,40 @@ func (c *Kubectl) Delete(ctx context.Context, kubeconfig, namespace string, r Re
}
return nil
}

func (c *Kubectl) Get(ctx context.Context, kubeconfig, namespace string, r ResourceKey) (m Manifest, err error) {
defer func() {
kubernetesmetrics.IncKubectlCallsCounter(
c.version,
kubernetesmetrics.LabelGetCommand,
err == nil,
)
}()

args := make([]string, 0, 7)
if kubeconfig != "" {
args = append(args, "--kubeconfig", kubeconfig)
}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
args = append(args, "get", r.Kind, r.Name, "-o", "yaml")

cmd := exec.CommandContext(ctx, c.execPath, args...)
out, err := cmd.CombinedOutput()

if strings.Contains(string(out), "(NotFound)") {
return Manifest{}, fmt.Errorf("not found manifest %v, (%w), %v", r, ErrNotFound, err)
}
if err != nil {
return Manifest{}, fmt.Errorf("failed to get: %s, %v", string(out), err)
}
ms, err := ParseManifests(string(out))
if err != nil {
return Manifest{}, fmt.Errorf("failed to parse manifests %v: %v", r, err)
}
if len(ms) == 0 {
return Manifest{}, fmt.Errorf("not found manifest %v, (%w)", r, ErrNotFound)
}
return ms[0], nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
LabelCreateCommand ToolCommand = "create"
LabelReplaceCommand ToolCommand = "replace"
LabelDeleteCommand ToolCommand = "delete"
LabelGetCommand ToolCommand = "get"
)

type CommandOutput string
Expand Down

0 comments on commit 768c960

Please sign in to comment.