Skip to content

Commit

Permalink
feat: add very basic component log display
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
metacosm committed May 26, 2020
1 parent 9d6bad6 commit 94da338
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/hal/cli/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewCmdComponent(parent string) *cobra.Command {
push,
mode,
bind,
NewCmdLog(fullName),
)

return hal
Expand Down
56 changes: 56 additions & 0 deletions pkg/hal/cli/component/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package component

import (
"fmt"
"github.com/spf13/cobra"
"halkyon.io/api/component/v1beta1"
"halkyon.io/hal/pkg/cmdutil"
"halkyon.io/hal/pkg/k8s"
)

const logCommandName = "log"

type logOptions struct {
component *v1beta1.Component
*cmdutil.ComponentTargetingOptions
}

func (o *logOptions) SetTargetingOptions(options *cmdutil.ComponentTargetingOptions) {
o.ComponentTargetingOptions = options
}

func (o *logOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
// get the targeted component
o.component, err = Entity.GetTyped(o.GetTargetedComponentName())
if err != nil {
return err
}

return nil
}

func (o *logOptions) Validate() error {
return nil
}

func (o *logOptions) Run() error {
podName := o.component.Status.GetAssociatedPodName()
if err := k8s.Logs(podName); err != nil {
return err
}

return nil
}

func NewCmdLog(fullParentName string) *cobra.Command {
o := &logOptions{}
bind := &cobra.Command{
Use: fmt.Sprintf("%s [flags]", logCommandName),
Short: "Retrieve the logs for the component",
Long: `Retrieve the logs for the component.`,
Example: fmt.Sprintf(modeExample, cmdutil.CommandName(logCommandName, fullParentName)),
Args: cobra.NoArgs,
}
cmdutil.ConfigureRunnableAndCommandWithTargeting(o, bind)
return bind
}
21 changes: 18 additions & 3 deletions pkg/k8s/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ func IsJarPresent(podName string) bool {
return runKubectl([]string{"exec", podName, "--", "ls", JarPathInContainer}...) == nil
}

func Logs(podName string) error {
command, interceptor := configureKubectlCmd("logs", "--since=10s", podName)
command.Stdout = os.Stdout // so that we can print the output of the command
return runKubectlCmd(command, interceptor)
}

func Apply(path, namespace string) error {
return runKubectl([]string{"apply", "-f", path, "-n", namespace}...)
}

func runKubectl(args ...string) error {
command := exec.Command(kubectl, args...)
interceptor := log.GetErrorInterceptor()
command.Stderr = interceptor
command, interceptor := configureKubectlCmd(args...)
return runKubectlCmd(command, interceptor)
}

func runKubectlCmd(command *exec.Cmd, interceptor *log.ErrorInterceptor) error {
err := command.Run()
if err != nil {
if len(interceptor.ErrorMsg) > 0 {
Expand All @@ -48,6 +56,13 @@ func runKubectl(args ...string) error {
return nil
}

func configureKubectlCmd(args ...string) (*exec.Cmd, *log.ErrorInterceptor) {
command := exec.Command(kubectl, args...)
interceptor := log.GetErrorInterceptor()
command.Stderr = interceptor
return command, interceptor
}

func GetK8SClientFlavor() string {
return kubectl
}
Expand Down

0 comments on commit 94da338

Please sign in to comment.