Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shambugouda annigeri committed Jul 21, 2022
1 parent 43d418c commit 39302a4
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 0 deletions.
56 changes: 56 additions & 0 deletions config/deployment/profile_analyzer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: fdb-profile-analyzer
labels:
app: fdb-profile-analyzer
spec:
selector:
matchLabels:
app: fdb-profile-analyzer
replicas: 1
template:
metadata:
labels:
app: fdb-profile-analyzer
spec:
securityContext:
runAsUser: 4059
runAsGroup: 4059
fsGroup: 4059
serviceAccountName: fdb-profile-analyzer
containers:
- command:
- ./transaction_profiling_analyzer.py
image: foundationdb/fdb-profile-analyzer
name: fdb-profile-analyzer
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
ports:
- name: metrics
containerPort: 8080
resources:
limits:
cpu: 500m
memory: 256Mi
requests:
cpu: 500m
memory: 256Mi
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
privileged: false
volumeMounts:
- name: tmp
mountPath: /tmp
- name: logs
mountPath: /var/log/fdb
terminationGracePeriodSeconds: 10
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: fdb-profile-analyzer
90 changes: 90 additions & 0 deletions internal/pod_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,93 @@ func GetObjectMetadata(cluster *fdbv1beta2.FoundationDBCluster, base *metav1.Obj
func GetPodDNSName(cluster *fdbv1beta2.FoundationDBCluster, podName string) string {
return fmt.Sprintf("%s.%s.%s.svc.%s", podName, cluster.Name, cluster.Namespace, cluster.GetDNSDomain())
}

// GetProfileAnalyzer builds a deployment for profile analyzing.
func GetProfileAnalyzer(namespace string, cmd []string, args []string) (*appsv1.Deployment, error) {
deploymentName := "fdb-profile-analyzer"
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: deploymentName,
Annotations: map[string]string{},
Labels: map[string]string{},
},
}

podTemplate := &corev1.PodTemplateSpec{}

var mainContainer *corev1.Container
var initContainer *corev1.Container

if mainContainer == nil {
containers := []corev1.Container{
{Name: "fdb-profile-analyzer"},
}
containers = append(containers, podTemplate.Spec.Containers...)
mainContainer = &containers[0]
podTemplate.Spec.Containers = containers
}

mainContainer.Image = "fdb-profile-analyzer"
mainContainer.Command = cmd
mainContainer.Args = args
if mainContainer.Env == nil {
mainContainer.Env = make([]corev1.EnvVar, 0, 1)
}

extendEnv(mainContainer, corev1.EnvVar{Name: "FDB_CLUSTER_FILE", Value: "/var/dynamic-conf/fdb.cluster"})

mainContainer.VolumeMounts = append(mainContainer.VolumeMounts,
corev1.VolumeMount{Name: "logs", MountPath: "/var/log/fdb-trace-logs"},
corev1.VolumeMount{Name: "dynamic-conf", MountPath: "/var/dynamic-conf"},
)

if mainContainer.Resources.Requests == nil {
mainContainer.Resources.Requests = corev1.ResourceList{
"cpu": resource.MustParse("1"),
"memory": resource.MustParse("1Gi"),
}
}

if mainContainer.Resources.Limits == nil {
mainContainer.Resources.Limits = mainContainer.Resources.Requests
}

if initContainer == nil {
podTemplate.Spec.InitContainers = append(podTemplate.Spec.InitContainers, corev1.Container{Name: "foundationdb-kubernetes-init"})
initContainer = &podTemplate.Spec.InitContainers[0]
}

if podTemplate.ObjectMeta.Labels == nil {
podTemplate.ObjectMeta.Labels = make(map[string]string, 1)
}
podTemplate.ObjectMeta.Labels["foundationdb.org/deployment-name"] = deployment.ObjectMeta.Name
deployment.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{
"foundationdb.org/deployment-name": deployment.ObjectMeta.Name,
}}

podTemplate.Spec.Volumes = append(podTemplate.Spec.Volumes,
corev1.Volume{Name: "logs", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
corev1.Volume{Name: "dynamic-conf", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
corev1.Volume{
Name: "config-map",
VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: "fdb-profile-analyzer"},
Items: []corev1.KeyToPath{
{Key: ClusterFileKey, Path: "fdb.cluster"},
},
}},
},
)

deployment.Spec.Template = *podTemplate

specHash, err := GetJSONHash(deployment.Spec)
if err != nil {
return nil, err
}

deployment.ObjectMeta.Annotations[fdbv1beta2.LastSpecKey] = specHash

return deployment, nil
}
111 changes: 111 additions & 0 deletions kubectl-fdb/cmd/profile_analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* profile_analyzer.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cmd

import (
"context"
"fmt"
"github.com/FoundationDB/fdb-kubernetes-operator/internal"
"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func newProfileAnalyzerCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := newFDBOptions(streams)

cmd := &cobra.Command{
Use: "analyze-profile",
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 {
clusterName, err := cmd.Root().Flags().GetString("cluster")
if err != nil {
return err
}
flags := cmd.Root().Flags().Args()
kubeClient, err := getKubeClient(o)
if err != nil {
return err
}
namespace, err := getNamespace(*o.configFlags.Namespace)
if err != nil {
return err
}

return runProfileAnalyzer(kubeClient, namespace, clusterName, flags)
},
Example: `
# Run the profiler for cluster-1
kubectl fdb analyze-profile cluster-1 --cluster cluster-1 --star-time "01:01 20/07/2022 BST" --end-time "01:30 20/07/2022 BST"
`,
}
cmd.SetOut(o.Out)
cmd.SetErr(o.ErrOut)
cmd.SetIn(o.In)

return cmd
}

func runProfileAnalyzer(kubeClient client.Client, namespace string, clusterName string, flags []string) error {
cluster, err := loadCluster(kubeClient, namespace, clusterName)
if err != nil {
return err
}
cmd := []string{"python" , "./transaction_profiling_analyzer.py"}
args := []string{"--cluster-file", "/var/fdb/fdb.cluster"}
args = append(args, flags...)
needsCreation := false
deployment := &appsv1.Deployment{}
if deployment == nil {
return fmt.Errorf("could not get deployment")
}
err = kubeClient.Get(context.TODO(), types.NamespacedName{Namespace: cluster.Namespace, Name: "fdb-profile-analyzer"}, deployment)
if err != nil {
if k8serrors.IsNotFound(err) {
needsCreation = true
} else {
return err
}
}
deployment.Spec.Template.ClusterName = clusterName
deployment.Spec.Template.Spec.Containers[0].Command = cmd
deployment.Spec.Template.Spec.Containers[0].Args = args
if needsCreation {
deployment, err = internal.GetProfileAnalyzer(namespace, cmd, args)
if err != nil {
return err
}
err = kubeClient.Create(context.TODO(), deployment)
if err != nil {
return err
}
} else {
err = kubeClient.Update(context.TODO(), deployment)
if err != nil {
return err
}
}
return nil
}
1 change: 1 addition & 0 deletions kubectl-fdb/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewRootCmd(streams genericclioptions.IOStreams) *cobra.Command {
newFixCoordinatorIPsCmd(streams),
newGetCmd(streams),
newBuggifyCmd(streams),
newProfileAnalyzerCmd(streams),
)

return cmd
Expand Down
23 changes: 23 additions & 0 deletions sample-apps/fdb-profile-analyzer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM python:3.9-slim
USER root

RUN microdnf --enablerepo=ubi* --disablerepo=rhel* install -y wget \
bind-utils \
lsof \
nmap-ncat \
less \
vim-minimal \
net-tools \
jq \
openssl \
pkg-config \
procps-ng\
iputils && \
microdnf --enablerepo=ubi* --disablerepo=rhel* clean all \

RUN groupadd -g 4059 foundationdb
RUN useradd -m -d / -s /bin/bash -u 4059 -o foundationdb -g 4059 -G 266

pip install foundationdb -i https://pypi.org/project/foundationdb


0 comments on commit 39302a4

Please sign in to comment.