Skip to content

Commit

Permalink
refactore more code
Browse files Browse the repository at this point in the history
  • Loading branch information
Shambugouda annigeri committed Jul 28, 2022
1 parent 872d311 commit af7e7cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 94 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/fatih/color v1.10.0
github.com/go-logr/logr v0.4.0
github.com/google/go-cmp v0.5.5
github.com/google/uuid v1.1.2
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/onsi/ginkgo/v2 v2.1.3
github.com/onsi/gomega v1.17.0
Expand Down Expand Up @@ -55,7 +56,6 @@ require (
github.com/google/btree v1.0.0 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
Expand Down
144 changes: 51 additions & 93 deletions kubectl-fdb/cmd/profile_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package cmd

import (
"context"
"fmt"
"github.com/FoundationDB/fdb-kubernetes-operator/internal"
"github.com/google/uuid"
"github.com/spf13/cobra"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,8 +32,6 @@ import (
"k8s.io/utils/pointer"
"strconv"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"log"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -47,18 +45,15 @@ func newProfileAnalyzerCmd(streams genericclioptions.IOStreams) *cobra.Command {
Short: "Analyze FDB shards to find the busiest team",
Long: "Analyze FDB shards to find the busiest team",
RunE: func(cmd *cobra.Command, args []string) error {
startTime, err := cmd.Flags().GetString("start-time")

clusterName, err := cmd.Flags().GetString("fdb-cluster")
if err != nil {
return err
}

endTime, err := cmd.Flags().GetString("end-time")

startTime, err := cmd.Flags().GetString("start-time")
if err != nil {
return err
}
filterRange, err := cmd.Flags().GetBool("filter-get-range")
endTime, err := cmd.Flags().GetString("end-time")
if err != nil {
return err
}
Expand All @@ -67,32 +62,28 @@ func newProfileAnalyzerCmd(streams genericclioptions.IOStreams) *cobra.Command {
return err
}
kubeClient, err := getKubeClient(o)

if err != nil {
return err
}

namespace, err := getNamespace(*o.configFlags.Namespace)
if err != nil {
return err
}

return runProfileAnalyzer(kubeClient, namespace, *o.configFlags.ClusterName, startTime, endTime, filterRange, topRequests, args)
return runProfileAnalyzer(kubeClient, namespace, clusterName, startTime, endTime, topRequests)
},
Example: `
# Run the profiler for cluster-1. We require --cluster option explicitly because analyze commands take lot many arguments.
kubectl fdb analyze-profile --cluster cluster-1 --star-time "01:01 20/07/2022 BST" --end-time "01:30 20/07/2022 BST "
kubectl fdb analyze-profile -c cluster-1 --star-time "01:01 20/07/2022 BST" --end-time "01:30 20/07/2022 BST" --top-requests 100
`,
}
cmd.SetOut(o.Out)
cmd.SetErr(o.ErrOut)
cmd.SetIn(o.In)

cmd.Flags().StringP("fdb-cluster", "c", "", "updates the crash-loop list in the provided cluster.")
cmd.Flags().Bool("filter-get-range", false, "defines if the analyze tasks should try to fix the found issues (e.g. replace Pods).")
cmd.Flags().String("start-time", "", "defines all clusters in the given namespace should be analyzed.")
// We might want to move this into the root cmd if we need this in multiple places
cmd.Flags().String("end-time", "", "Disable color output.")
cmd.Flags().StringP("fdb-cluster", "c", "", "cluster name for running hot shard tool.")
cmd.Flags().String("start-time", "", "start time for the analyzing transaction '01:30 30/07/2022 BST'")
cmd.Flags().String("end-time", "", "end time for analyzing the transaction '02:30 30/07/2022 BST'")
cmd.Flags().Int("top-requests", 100, "")
err := cmd.MarkFlagRequired("fdb-cluster")
if err != nil {
Expand All @@ -103,88 +94,55 @@ kubectl fdb analyze-profile --cluster cluster-1 --star-time "01:01 20/07/2022 BS
return cmd
}

func runProfileAnalyzer(kubeClient client.Client, namespace string, clusterName string, startTime string, endTime string, filterRange bool, topRequests int, flags []string) error {
needsCreation := false
log.Println(filterRange)
log.Println(flags)

job := &batchv1.Job{}
err := kubeClient.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: "fdb-profile-analyzer"}, job)
if err != nil {
if k8serrors.IsNotFound(err) {
needsCreation = true
} else {
return fmt.Errorf("unable to get job from kubeclient %s", err)
}
}
if needsCreation {
job = &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName + "profile-analyzer",
Namespace: namespace,
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(4059),
RunAsGroup: pointer.Int64(4059),
FSGroup: pointer.Int64(4059),
},
func runProfileAnalyzer(kubeClient client.Client, namespace string, clusterName string, startTime string, endTime string, topRequests int) error {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName + "-hot-shard-" + uuid.New().String(),
Namespace: namespace,
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(4059),
RunAsGroup: pointer.Int64(4059),
FSGroup: pointer.Int64(4059),
},

Containers: []corev1.Container{
{
Name: "profile-analyzer",
Image: "fdb-profile-analyzer",
Command: []string{"python3", "./transaction_profiling_analyzer.py"},
Args: []string{"-C", "$FDB_CLUSTER_FILE", "--start-time", startTime, "--end-time", endTime,
"--filter-get-range", "--top-requests", strconv.Itoa(topRequests),
},
Env: []corev1.EnvVar{
{Name: "FDB_CLUSTER_FILE", Value: "/var/dynamic-conf/fdb.cluster"},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("1"),
"memory": resource.MustParse("1Gi"),
},
},
Containers: []corev1.Container{
{
Name: "profile-analyzer",
Image: "fdb-profile-analyzer",
Command: []string{"python3", "./transaction_profiling_analyzer.py"},
Args: []string{"-C", "$FDB_CLUSTER_FILE", "--start-time", startTime, "--end-time", endTime,
"--filter-get-range", "--top-requests", strconv.Itoa(topRequests),
},
},
RestartPolicy: corev1.RestartPolicyNever,
Volumes: []corev1.Volume{{
Name: "config-map",
VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: "fdb-profile-analyzer"},
Items: []corev1.KeyToPath{
{Key: internal.ClusterFileKey, Path: "fdb.cluster"},
},
}},
}},
},
},
TTLSecondsAfterFinished: pointer.Int32(10800),
},
}
return kubeClient.Create(context.TODO(), job)
} else {
job = &batchv1.Job{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Command: []string{"python3", "./transaction_profiling_analyzer.py"},
Args: []string{"-C", "$FDB_CLUSTER_FILE", "--start-time", startTime, "--end-time", endTime,
"--filter-get-range", "--top-requests", strconv.Itoa(topRequests),
Env: []corev1.EnvVar{
{Name: "FDB_CLUSTER_FILE", Value: "/var/dynamic-conf/fdb.cluster"},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("1"),
"memory": resource.MustParse("1Gi"),
},
},
},
},
RestartPolicy: corev1.RestartPolicyNever,
Volumes: []corev1.Volume{{
Name: "config-map",
VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: clusterName + "-config"},
Items: []corev1.KeyToPath{
{Key: internal.ClusterFileKey, Path: "fdb.cluster"},
},
}},
}},
},
},
}
return kubeClient.Update(context.TODO(), job)
TTLSecondsAfterFinished: pointer.Int32(7200),
},
}
return nil
log.Printf("Creating job %s", job.Name)
return kubeClient.Create(context.TODO(), job)
}

0 comments on commit af7e7cc

Please sign in to comment.