Skip to content

Commit a45937f

Browse files
authored
Set analytics clientID (#246)
1 parent ea53cea commit a45937f

File tree

6 files changed

+81
-42
lines changed

6 files changed

+81
-42
lines changed

backup.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cmds
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67
"time"
78

89
"github.com/appscode/go/log"
910
"github.com/appscode/kutil/meta"
11+
"github.com/appscode/kutil/tools/analytics"
1012
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
1113
"github.com/appscode/stash/pkg/backup"
1214
"github.com/appscode/stash/pkg/util"
@@ -22,12 +24,12 @@ func NewCmdBackup() *cobra.Command {
2224
opt = backup.Options{
2325
Namespace: meta.Namespace(),
2426
ScratchDir: "/tmp",
25-
PushgatewayURL: "http://stash-operator.kube-system.svc:56789",
2627
PodLabelsPath: "/etc/stash/labels",
2728
ResyncPeriod: 5 * time.Minute,
2829
MaxNumRequeues: 5,
2930
}
3031
)
32+
opt.PushgatewayURL = fmt.Sprintf("http://stash-operator.%s.svc:56789", opt.Namespace)
3133

3234
cmd := &cobra.Command{
3335
Use: "backup",
@@ -38,8 +40,12 @@ func NewCmdBackup() *cobra.Command {
3840
if err != nil {
3941
log.Fatalf("Could not get Kubernetes config: %s", err)
4042
}
41-
kubeClient = kubernetes.NewForConfigOrDie(config)
42-
stashClient = cs.NewForConfigOrDie(config)
43+
kubeClient := kubernetes.NewForConfigOrDie(config)
44+
stashClient := cs.NewForConfigOrDie(config)
45+
46+
if meta.PossiblyInCluster() {
47+
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
48+
}
4349

4450
opt.NodeName = os.Getenv("NODE_NAME")
4551
if opt.NodeName == "" {

check.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package cmds
33
import (
44
"github.com/appscode/go/log"
55
"github.com/appscode/kutil/meta"
6-
"github.com/appscode/stash/client/typed/stash/v1alpha1"
6+
"github.com/appscode/kutil/tools/analytics"
7+
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
78
"github.com/appscode/stash/pkg/check"
89
"github.com/spf13/cobra"
910
"k8s.io/client-go/kubernetes"
@@ -28,11 +29,14 @@ func NewCmdCheck() *cobra.Command {
2829
if err != nil {
2930
log.Fatalln(err)
3031
}
31-
c := check.New(
32-
kubernetes.NewForConfigOrDie(config),
33-
v1alpha1.NewForConfigOrDie(config),
34-
opt,
35-
)
32+
kubeClient := kubernetes.NewForConfigOrDie(config)
33+
stashClient := cs.NewForConfigOrDie(config)
34+
35+
if meta.PossiblyInCluster() {
36+
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
37+
}
38+
39+
c := check.New(kubeClient, stashClient, opt)
3640
if err = c.Run(); err != nil {
3741
log.Fatal(err)
3842
}

recover.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package cmds
33
import (
44
"github.com/appscode/go/log"
55
"github.com/appscode/kutil/meta"
6-
"github.com/appscode/stash/client/typed/stash/v1alpha1"
6+
"github.com/appscode/kutil/tools/analytics"
7+
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
78
"github.com/appscode/stash/pkg/recovery"
89
"github.com/spf13/cobra"
910
"k8s.io/client-go/kubernetes"
@@ -26,12 +27,14 @@ func NewCmdRecover() *cobra.Command {
2627
if err != nil {
2728
log.Fatalln(err)
2829
}
29-
c := recovery.New(
30-
kubernetes.NewForConfigOrDie(config),
31-
v1alpha1.NewForConfigOrDie(config),
32-
meta.Namespace(),
33-
recoveryName,
34-
)
30+
kubeClient := kubernetes.NewForConfigOrDie(config)
31+
stashClient := cs.NewForConfigOrDie(config)
32+
33+
if meta.PossiblyInCluster() {
34+
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
35+
}
36+
37+
c := recovery.New(kubeClient, stashClient, meta.Namespace(), recoveryName)
3538
c.Run()
3639
},
3740
}

root.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"log"
66
"strings"
77

8+
"github.com/appscode/go/analytics"
89
v "github.com/appscode/go/version"
10+
"github.com/appscode/kutil/meta"
911
"github.com/appscode/stash/client/scheme"
1012
"github.com/jpillora/go-ogle-analytics"
1113
"github.com/spf13/cobra"
@@ -17,10 +19,7 @@ const (
1719
gaTrackingCode = "UA-62096468-20"
1820
)
1921

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

4743
rootCmd.AddCommand(v.NewCmdVersion())
48-
rootCmd.AddCommand(NewCmdRun(version))
44+
rootCmd.AddCommand(NewCmdRun())
4945
rootCmd.AddCommand(NewCmdBackup())
5046
rootCmd.AddCommand(NewCmdRecover())
5147
rootCmd.AddCommand(NewCmdCheck())
5248
return rootCmd
5349
}
50+
51+
var (
52+
enableAnalytics = true
53+
)
54+
55+
func sendAnalytics(c *cobra.Command, clientID string) {
56+
if enableAnalytics && gaTrackingCode != "" {
57+
if client, err := ga.NewClient(gaTrackingCode); err == nil {
58+
client.ClientID(clientID)
59+
parts := strings.Split(c.CommandPath(), " ")
60+
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(v.Version.Version))
61+
}
62+
}
63+
}

run.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77

88
"github.com/appscode/go/log"
99
stringz "github.com/appscode/go/strings"
10+
v "github.com/appscode/go/version"
11+
"github.com/appscode/kutil/meta"
12+
"github.com/appscode/kutil/tools/analytics"
1013
"github.com/appscode/pat"
1114
api "github.com/appscode/stash/apis/stash"
1215
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
@@ -20,23 +23,17 @@ import (
2023
"k8s.io/client-go/tools/clientcmd"
2124
)
2225

23-
var (
24-
kubeClient kubernetes.Interface
25-
stashClient cs.StashV1alpha1Interface
26-
27-
scratchDir string = "/tmp"
28-
)
29-
30-
func NewCmdRun(version string) *cobra.Command {
26+
func NewCmdRun() *cobra.Command {
3127
var (
3228
masterURL string
3329
kubeconfigPath string
3430
address string = ":56790"
3531
opts = controller.Options{
36-
SidecarImageTag: stringz.Val(version, "canary"),
32+
SidecarImageTag: stringz.Val(v.Version.Version, "canary"),
3733
ResyncPeriod: 5 * time.Minute,
3834
MaxNumRequeues: 5,
3935
}
36+
scratchDir = "/tmp"
4037
)
4138

4239
cmd := &cobra.Command{
@@ -52,10 +49,14 @@ func NewCmdRun(version string) *cobra.Command {
5249
if err != nil {
5350
log.Fatalln(err)
5451
}
55-
kubeClient = kubernetes.NewForConfigOrDie(config)
56-
stashClient = cs.NewForConfigOrDie(config)
52+
kubeClient := kubernetes.NewForConfigOrDie(config)
53+
stashClient := cs.NewForConfigOrDie(config)
5754
crdClient := crd_cs.NewForConfigOrDie(config)
5855

56+
if meta.PossiblyInCluster() {
57+
sendAnalytics(cmd, analytics.ClientID(kubeClient.CoreV1().Nodes()))
58+
}
59+
5960
// get kube api server version
6061
version, err := kubeClient.Discovery().ServerVersion()
6162
if err != nil {
@@ -89,7 +90,12 @@ func NewCmdRun(version string) *cobra.Command {
8990

9091
pattern := fmt.Sprintf("/%s/v1beta1/namespaces/%s/restics/%s/metrics", api.GroupName, PathParamNamespace, PathParamName)
9192
log.Infof("URL pattern: %s", pattern)
92-
m.Get(pattern, http.HandlerFunc(ExportSnapshots))
93+
exporter := &PrometheusExporter{
94+
kubeClient: kubeClient,
95+
stashClient: stashClient,
96+
scratchDir: scratchDir,
97+
}
98+
m.Get(pattern, exporter)
9399

94100
http.Handle("/", m)
95101
log.Infoln("Listening on", address)

snapshot_handler.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77

88
"github.com/appscode/pat"
99
api "github.com/appscode/stash/apis/stash/v1alpha1"
10+
cs "github.com/appscode/stash/client/typed/stash/v1alpha1"
1011
"github.com/appscode/stash/pkg/cli"
1112
core "k8s.io/api/core/v1"
1213
kerr "k8s.io/apimachinery/pkg/api/errors"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/client-go/kubernetes"
1416
)
1517

1618
const (
@@ -19,7 +21,15 @@ const (
1921
QueryParamAutoPrefix = "autoPrefix"
2022
)
2123

22-
func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
24+
type PrometheusExporter struct {
25+
kubeClient kubernetes.Interface
26+
stashClient cs.StashV1alpha1Interface
27+
scratchDir string
28+
}
29+
30+
var _ http.Handler = &PrometheusExporter{}
31+
32+
func (e PrometheusExporter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2333
params, found := pat.FromContext(r.Context())
2434
if !found {
2535
http.Error(w, "Missing parameters", http.StatusBadRequest)
@@ -35,10 +45,10 @@ func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
3545
http.Error(w, "Missing parameter:"+PathParamName, http.StatusBadRequest)
3646
return
3747
}
38-
resticCLI := cli.New(scratchDir, true, "")
48+
resticCLI := cli.New(e.scratchDir, true, "")
3949

4050
var resource *api.Restic
41-
resource, err := stashClient.Restics(namespace).Get(name, metav1.GetOptions{})
51+
resource, err := e.stashClient.Restics(namespace).Get(name, metav1.GetOptions{})
4252
if kerr.IsNotFound(err) {
4353
http.Error(w, err.Error(), http.StatusNotFound)
4454
return
@@ -52,7 +62,7 @@ func ExportSnapshots(w http.ResponseWriter, r *http.Request) {
5262
return
5363
}
5464
var secret *core.Secret
55-
secret, err = kubeClient.CoreV1().Secrets(resource.Namespace).Get(resource.Spec.Backend.StorageSecretName, metav1.GetOptions{})
65+
secret, err = e.kubeClient.CoreV1().Secrets(resource.Namespace).Get(resource.Spec.Backend.StorageSecretName, metav1.GetOptions{})
5666
if kerr.IsNotFound(err) {
5767
http.Error(w, err.Error(), http.StatusNotFound)
5868
return

0 commit comments

Comments
 (0)