Skip to content

Commit

Permalink
migrate from maorfr to nuvo
Browse files Browse the repository at this point in the history
  • Loading branch information
maorfr committed Dec 18, 2018
1 parent b009a5e commit f0b4fd1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
29 changes: 26 additions & 3 deletions cmd/kube-tasks.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"fmt"
"io"
"log"
"os"

"github.com/maorfr/kube-tasks/pkg/kubetasks"
"github.com/maorfr/kube-tasks/pkg/utils"
"github.com/nuvo/kube-tasks/pkg/kubetasks"
"github.com/nuvo/kube-tasks/pkg/utils"
"github.com/spf13/cobra"
)

Expand All @@ -30,6 +31,7 @@ func NewRootCmd(args []string) *cobra.Command {
cmd.AddCommand(NewSimpleBackupCmd(out))
cmd.AddCommand(NewWaitForPodsCmd(out))
cmd.AddCommand(NewExecuteCmd(out))
cmd.AddCommand(NewVersionCmd(out))

return cmd
}
Expand All @@ -53,7 +55,7 @@ func NewSimpleBackupCmd(out io.Writer) *cobra.Command {

cmd := &cobra.Command{
Use: "simple-backup",
Short: "backup files to S3",
Short: "Backup files to cloud storage",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if _, err := kubetasks.SimpleBackup(b.namespace, b.selector, b.container, b.path, b.dst, b.parallel, b.tag, b.bufferSize); err != nil {
Expand Down Expand Up @@ -139,3 +141,24 @@ func NewExecuteCmd(out io.Writer) *cobra.Command {

return cmd
}

var (
// GitTag stands for a git tag
GitTag string
// GitCommit stands for a git commit hash
GitCommit string
)

// NewVersionCmd prints version information
func NewVersionCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print version information",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Version %s (git-%s)\n", GitTag, GitCommit)
},
}

return cmd
}
4 changes: 2 additions & 2 deletions pkg/kubetasks/kubetasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"time"

"github.com/maorfr/kube-tasks/pkg/utils"
"github.com/maorfr/skbn/pkg/skbn"
"github.com/nuvo/kube-tasks/pkg/utils"
"github.com/nuvo/skbn/pkg/skbn"
)

// SimpleBackup performs backup
Expand Down
42 changes: 39 additions & 3 deletions pkg/utils/kube.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package utils

import (
"github.com/maorfr/skbn/pkg/skbn"
"log"
"os"
"path/filepath"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

// GetReadyPods gets a list of all ready ports according to defined namespace and selector
func GetReadyPods(iClient interface{}, namespace, selector string) ([]string, error) {

k8sClient := *iClient.(*skbn.K8sClient)
pods, err := k8sClient.ClientSet.CoreV1().Pods(namespace).List(metav1.ListOptions{
clientSet := GetClientSet()
pods, err := clientSet.CoreV1().Pods(namespace).List(metav1.ListOptions{
LabelSelector: selector,
})
if err != nil {
Expand All @@ -32,3 +38,33 @@ func GetReadyPods(iClient interface{}, namespace, selector string) ([]string, er

return podList, nil
}

// GetClientSet returns a kubernetes ClientSet
func GetClientSet() *kubernetes.Clientset {
var kubeconfig string
if kubeConfigPath := os.Getenv("KUBECONFIG"); kubeConfigPath != "" {
kubeconfig = kubeConfigPath
} else {
kubeconfig = filepath.Join(os.Getenv("HOME"), ".kube", "config")
}

config, err := buildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err.Error())
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err.Error())
}

return clientset
}

func buildConfigFromFlags(context, kubeconfigPath string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
}
2 changes: 1 addition & 1 deletion pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package utils
import (
"path/filepath"

"github.com/maorfr/skbn/pkg/skbn"
"github.com/nuvo/skbn/pkg/skbn"
)

// GetFromAndToPathsFromK8s aggregates paths from all pods
Expand Down

0 comments on commit f0b4fd1

Please sign in to comment.