Skip to content
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

feat(cli): Add support for klog levels #291

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion experiments/swdt/cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package cmd

import (
"github.com/spf13/cobra"
"swdt/apis/config/v1alpha1"
"swdt/pkg/pwsh/executor"
"swdt/pkg/pwsh/kubernetes"

"github.com/spf13/cobra"
)

func init() {
Expand Down
4 changes: 4 additions & 0 deletions experiments/swdt/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ limitations under the License.
package cmd

import (
"flag"
"os"
"swdt/apis/config/v1alpha1"
"swdt/pkg/config"

"github.com/spf13/cobra"
"k8s.io/klog"
)

func init() {
klog.InitFlags(nil)
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
rootCmd.PersistentFlags().StringP("config", "c", "samples/config.yaml", "Configuration file path.")
}

Expand Down
2 changes: 2 additions & 0 deletions experiments/swdt/pkg/config/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog"
)

var (
Expand All @@ -38,6 +39,7 @@ func init() {

// LoadConfigNodeFromFile LoadConfigFromFile returns the marshalled Node configuration object
func LoadConfigNodeFromFile(file string) (*v1alpha1.Node, error) {
klog.V(1).Infof("Loading Node configuration from %s", file)
data, err := os.ReadFile(file)
if err != nil {
return nil, err
Expand Down
15 changes: 12 additions & 3 deletions experiments/swdt/pkg/connections/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import (
"context"
"errors"
"fmt"
scp "github.com/bramvdbogaerde/go-scp"
"golang.org/x/crypto/ssh"
"io"
"os"
"path"
"regexp"
"swdt/apis/config/v1alpha1"
"sync"
"time"

scp "github.com/bramvdbogaerde/go-scp"
"golang.org/x/crypto/ssh"
"k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -70,15 +72,19 @@ func (c *SSHConnection) fetchAuthMethod() (authMethod []ssh.AuthMethod, err erro
if password != "" {
authMethod = append(authMethod, ssh.Password(password))
}

return
}

// Connect creates the client connection object
func (c *SSHConnection) Connect() error {
klog.V(1).Infof("SSH connecting to %s as %s", c.credentials.Hostname, c.credentials.Username)

authMethod, err := c.fetchAuthMethod()
if err != nil {
return err
}

client, err := ssh.Dial(TCP_TYPE, c.credentials.Hostname, &ssh.ClientConfig{
User: c.credentials.Username,
Auth: authMethod,
Expand Down Expand Up @@ -111,7 +117,10 @@ func (c *SSHConnection) Run(args string) (string, error) {
// Multiline PowerShell commands over SSH trip over newlines - only first one is executed
args = regexp.MustCompile(`\r?\n`).ReplaceAllLiteralString(args, ";")
cmd := fmt.Sprintf("powershell -nologo -noprofile -c { %s }", args)
// TODO(mloskot): Log executed commands in --verbose mode

// TODO(mloskot): Why this is not printed?
klog.V(1).Infof("SSH executing command: %s", cmd)
knabben marked this conversation as resolved.
Show resolved Hide resolved

if err := session.Run(cmd); err != nil {
return "", err
}
Expand Down
Loading