Skip to content

Commit

Permalink
Set analytics clientID (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Dec 18, 2017
1 parent ea53cea commit a45937f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 42 deletions.
12 changes: 9 additions & 3 deletions backup.go
@@ -1,12 +1,14 @@
package cmds

import (
"fmt"
"os"
"strings"
"time"

"github.com/appscode/go/log"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/backup"
"github.com/appscode/stash/pkg/util"
Expand All @@ -22,12 +24,12 @@ func NewCmdBackup() *cobra.Command {
opt = backup.Options{
Namespace: meta.Namespace(),
ScratchDir: "/tmp",
PushgatewayURL: "http://stash-operator.kube-system.svc:56789",
PodLabelsPath: "/etc/stash/labels",
ResyncPeriod: 5 * time.Minute,
MaxNumRequeues: 5,
}
)
opt.PushgatewayURL = fmt.Sprintf("http://stash-operator.%s.svc:56789", opt.Namespace)

cmd := &cobra.Command{
Use: "backup",
Expand All @@ -38,8 +40,12 @@ func NewCmdBackup() *cobra.Command {
if err != nil {
log.Fatalf("Could not get Kubernetes config: %s", err)
}
kubeClient = kubernetes.NewForConfigOrDie(config)
stashClient = cs.NewForConfigOrDie(config)
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

opt.NodeName = os.Getenv("NODE_NAME")
if opt.NodeName == "" {
Expand Down
16 changes: 10 additions & 6 deletions check.go
Expand Up @@ -3,7 +3,8 @@ package cmds
import (
"github.com/appscode/go/log"
"github.com/appscode/kutil/meta"
"github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/kutil/tools/analytics"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/check"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
Expand All @@ -28,11 +29,14 @@ func NewCmdCheck() *cobra.Command {
if err != nil {
log.Fatalln(err)
}
c := check.New(
kubernetes.NewForConfigOrDie(config),
v1alpha1.NewForConfigOrDie(config),
opt,
)
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

c := check.New(kubeClient, stashClient, opt)
if err = c.Run(); err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 10 additions & 7 deletions recover.go
Expand Up @@ -3,7 +3,8 @@ package cmds
import (
"github.com/appscode/go/log"
"github.com/appscode/kutil/meta"
"github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/kutil/tools/analytics"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/recovery"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
Expand All @@ -26,12 +27,14 @@ func NewCmdRecover() *cobra.Command {
if err != nil {
log.Fatalln(err)
}
c := recovery.New(
kubernetes.NewForConfigOrDie(config),
v1alpha1.NewForConfigOrDie(config),
meta.Namespace(),
recoveryName,
)
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

c := recovery.New(kubeClient, stashClient, meta.Namespace(), recoveryName)
c.Run()
},
}
Expand Down
30 changes: 20 additions & 10 deletions root.go
Expand Up @@ -5,7 +5,9 @@ import (
"log"
"strings"

"github.com/appscode/go/analytics"
v "github.com/appscode/go/version"
"github.com/appscode/kutil/meta"
"github.com/appscode/stash/client/scheme"
"github.com/jpillora/go-ogle-analytics"
"github.com/spf13/cobra"
Expand All @@ -17,10 +19,7 @@ const (
gaTrackingCode = "UA-62096468-20"
)

func NewCmdStash(version string) *cobra.Command {
var (
enableAnalytics = true
)
func NewRootCmd() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "stash",
Short: `Stash by AppsCode - Backup your Kubernetes Volumes`,
Expand All @@ -30,11 +29,8 @@ func NewCmdStash(version string) *cobra.Command {
c.Flags().VisitAll(func(flag *pflag.Flag) {
log.Printf("FLAG: --%s=%q", flag.Name, flag.Value)
})
if enableAnalytics && gaTrackingCode != "" {
if client, err := ga.NewClient(gaTrackingCode); err == nil {
parts := strings.Split(c.CommandPath(), " ")
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(version))
}
if !meta.PossiblyInCluster() {
sendAnalytics(c, analytics.ClientID())
}
scheme.AddToScheme(clientsetscheme.Scheme)
},
Expand All @@ -45,9 +41,23 @@ func NewCmdStash(version string) *cobra.Command {
rootCmd.PersistentFlags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical events to Google Analytics")

rootCmd.AddCommand(v.NewCmdVersion())
rootCmd.AddCommand(NewCmdRun(version))
rootCmd.AddCommand(NewCmdRun())
rootCmd.AddCommand(NewCmdBackup())
rootCmd.AddCommand(NewCmdRecover())
rootCmd.AddCommand(NewCmdCheck())
return rootCmd
}

var (
enableAnalytics = true
)

func sendAnalytics(c *cobra.Command, clientID string) {
if enableAnalytics && gaTrackingCode != "" {
if client, err := ga.NewClient(gaTrackingCode); err == nil {
client.ClientID(clientID)
parts := strings.Split(c.CommandPath(), " ")
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(v.Version.Version))
}
}
}
30 changes: 18 additions & 12 deletions run.go
Expand Up @@ -7,6 +7,9 @@ import (

"github.com/appscode/go/log"
stringz "github.com/appscode/go/strings"
v "github.com/appscode/go/version"
"github.com/appscode/kutil/meta"
"github.com/appscode/kutil/tools/analytics"
"github.com/appscode/pat"
api "github.com/appscode/stash/apis/stash"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
Expand All @@ -20,23 +23,17 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

var (
kubeClient kubernetes.Interface
stashClient cs.StashV1alpha1Interface

scratchDir string = "/tmp"
)

func NewCmdRun(version string) *cobra.Command {
func NewCmdRun() *cobra.Command {
var (
masterURL string
kubeconfigPath string
address string = ":56790"
opts = controller.Options{
SidecarImageTag: stringz.Val(version, "canary"),
SidecarImageTag: stringz.Val(v.Version.Version, "canary"),
ResyncPeriod: 5 * time.Minute,
MaxNumRequeues: 5,
}
scratchDir = "/tmp"
)

cmd := &cobra.Command{
Expand All @@ -52,10 +49,14 @@ func NewCmdRun(version string) *cobra.Command {
if err != nil {
log.Fatalln(err)
}
kubeClient = kubernetes.NewForConfigOrDie(config)
stashClient = cs.NewForConfigOrDie(config)
kubeClient := kubernetes.NewForConfigOrDie(config)
stashClient := cs.NewForConfigOrDie(config)
crdClient := crd_cs.NewForConfigOrDie(config)

if meta.PossiblyInCluster() {
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
}

// get kube api server version
version, err := kubeClient.Discovery().ServerVersion()
if err != nil {
Expand Down Expand Up @@ -89,7 +90,12 @@ func NewCmdRun(version string) *cobra.Command {

pattern := fmt.Sprintf("/%s/v1beta1/namespaces/%s/restics/%s/metrics", api.GroupName, PathParamNamespace, PathParamName)
log.Infof("URL pattern: %s", pattern)
m.Get(pattern, http.HandlerFunc(ExportSnapshots))
exporter := &PrometheusExporter{
kubeClient: kubeClient,
stashClient: stashClient,
scratchDir: scratchDir,
}
m.Get(pattern, exporter)

http.Handle("/", m)
log.Infoln("Listening on", address)
Expand Down
18 changes: 14 additions & 4 deletions snapshot_handler.go
Expand Up @@ -7,10 +7,12 @@ import (

"github.com/appscode/pat"
api "github.com/appscode/stash/apis/stash/v1alpha1"
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/cli"
core "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

const (
Expand All @@ -19,7 +21,15 @@ const (
QueryParamAutoPrefix = "autoPrefix"
)

func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
type PrometheusExporter struct {
kubeClient kubernetes.Interface
stashClient cs.StashV1alpha1Interface
scratchDir string
}

var _ http.Handler = &PrometheusExporter{}

func (e PrometheusExporter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
params, found := pat.FromContext(r.Context())
if !found {
http.Error(w, "Missing parameters", http.StatusBadRequest)
Expand All @@ -35,10 +45,10 @@ func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Missing parameter:"+PathParamName, http.StatusBadRequest)
return
}
resticCLI := cli.New(scratchDir, true, "")
resticCLI := cli.New(e.scratchDir, true, "")

var resource *api.Restic
resource, err := stashClient.Restics(namespace).Get(name, metav1.GetOptions{})
resource, err := e.stashClient.Restics(namespace).Get(name, metav1.GetOptions{})
if kerr.IsNotFound(err) {
http.Error(w, err.Error(), http.StatusNotFound)
return
Expand All @@ -52,7 +62,7 @@ func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
return
}
var secret *core.Secret
secret, err = kubeClient.CoreV1().Secrets(resource.Namespace).Get(resource.Spec.Backend.StorageSecretName, metav1.GetOptions{})
secret, err = e.kubeClient.CoreV1().Secrets(resource.Namespace).Get(resource.Spec.Backend.StorageSecretName, metav1.GetOptions{})
if kerr.IsNotFound(err) {
http.Error(w, err.Error(), http.StatusNotFound)
return
Expand Down

0 comments on commit a45937f

Please sign in to comment.