Skip to content

Commit

Permalink
Display summary data per deployment
Browse files Browse the repository at this point in the history
- Modified the observability to include Summary data per deployment
- Added deployment name to struct PodInfo
- Modified codebase to get details of replicasets and statefulset along with deployments
- Modified codebase to include deployment name as part of PodInfo

Signed-off-by: Vishnu Soman <vishnu@accuknox.com>
  • Loading branch information
vishnusomank committed Apr 10, 2023
1 parent ce80984 commit 4ca51fc
Show file tree
Hide file tree
Showing 13 changed files with 642 additions and 168 deletions.
116 changes: 105 additions & 11 deletions src/cluster/k8sClientHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"flag"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand All @@ -21,7 +20,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)

var parsed bool = false
var parsed = false
var kubeconfig *string

func isInCluster() bool {
Expand Down Expand Up @@ -98,7 +97,7 @@ func ConnectInClusterAPIClient() *kubernetes.Clientset {
port = "6443"
}

read, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
read, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
if err != nil {
log.Error().Msg(err.Error())
return nil
Expand Down Expand Up @@ -446,17 +445,112 @@ func GetDeploymentsFromK8sClient() []types.Deployment {
continue
}

var label string
if d.Spec.Selector.MatchLabels != nil {
var labels []string

for k, v := range d.Spec.Selector.MatchLabels {
label = k + "=" + v
for k, v := range d.Spec.Selector.MatchLabels {
label := k + "=" + v
labels = append(labels, label)
}

results = append(results, types.Deployment{
Name: d.Name,
Namespace: d.Namespace,
Labels: strings.Join(labels, ","),
})
}
}

results = append(results, GetReplicaSetsFromK8sClient()...)
results = append(results, GetStatefulSetsFromK8sClient()...)

return results
}

// ================= //
// == ReplicaSet == //
// ================= //

func GetReplicaSetsFromK8sClient() []types.Deployment {
results := []types.Deployment{}

client := ConnectK8sClient()
if client == nil {
return results
}

// get namespaces from k8s api client
replicasets, err := client.AppsV1().ReplicaSets("").List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error().Msg(err.Error())
return results
}

results = append(results, types.Deployment{
Name: d.Name,
Namespace: d.Namespace,
Labels: label,
})
for _, rs := range replicasets.Items {
if rs.OwnerReferences == nil {
if rs.Namespace == "kube-system" {
continue
}

if rs.Spec.Selector.MatchLabels != nil {
var labels []string

for k, v := range rs.Spec.Selector.MatchLabels {
label := k + "=" + v
labels = append(labels, label)
}

results = append(results, types.Deployment{
Name: rs.Name,
Namespace: rs.Namespace,
Labels: strings.Join(labels, ","),
})
}
}
}
return results
}

// ================= //
// == StatefulSet == //
// ================= //

func GetStatefulSetsFromK8sClient() []types.Deployment {
results := []types.Deployment{}

client := ConnectK8sClient()
if client == nil {
return results
}

// get namespaces from k8s api client
statefulset, err := client.AppsV1().StatefulSets("").List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error().Msg(err.Error())
return results
}

for _, sts := range statefulset.Items {
if sts.OwnerReferences == nil {
if sts.Namespace == "kube-system" {
continue
}

if sts.Spec.Selector.MatchLabels != nil {
var labels []string

for k, v := range sts.Spec.Selector.MatchLabels {
label := k + "=" + v
labels = append(labels, label)
}

results = append(results, types.Deployment{
Name: sts.Name,
Namespace: sts.Namespace,
Labels: strings.Join(labels, ","),
})
}
}
}
return results
}
Expand Down
11 changes: 11 additions & 0 deletions src/libs/dbHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ func GetPodNames(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string, error
return res, err
}

func GetDeployNames(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string, error) {
res := []string{}
var err = errors.New("unknown db driver")
if cfg.DBDriver == "mysql" {
res, err = GetDeployNamesMySQL(cfg, filter)
} else if cfg.DBDriver == "sqlite3" {
res, err = GetDeployNamesSQLite(cfg, filter)
}
return res, err
}

// =============== //
// == Policy DB == //
// =============== //
Expand Down
56 changes: 56 additions & 0 deletions src/libs/mysqlHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,10 @@ func GetPodNamesMySQL(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
concatWhereClause(&whereClause, "container_name")
sysargs = append(sysargs, filter.ContainerName)
}
if filter.DeployName != "" {
concatWhereClause(&whereClause, "deployment_name")
sysargs = append(sysargs, filter.DeployName)
}

results, err = db.Query(query+whereClause, sysargs...)
if err != nil {
Expand All @@ -1593,6 +1597,58 @@ func GetPodNamesMySQL(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
return resPodNames, err
}

func GetDeployNamesMySQL(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string, error) {
db := connectMySQL(cfg)
defer db.Close()

resDeployNames := []string{}

var results *sql.Rows
var err error

// Get podnames from system table
query := "SELECT deployment_name FROM " + TableSystemSummarySQLite + " "

var whereClause string
var sysargs []interface{}

if filter.ClusterName != "" {
concatWhereClause(&whereClause, "cluster_name")
sysargs = append(sysargs, filter.ClusterName)
}
if filter.Namespace != "" {
concatWhereClause(&whereClause, "namespace_name")
sysargs = append(sysargs, filter.Namespace)
}
if filter.DeployName != "" {
concatWhereClause(&whereClause, "deployment_name")
sysargs = append(sysargs, filter.DeployName)
}
if filter.Labels != "" {
concatWhereClause(&whereClause, "labels")
sysargs = append(sysargs, filter.Labels)
}

results, err = db.Query(query+whereClause, sysargs...)
if err != nil {
log.Error().Msg(err.Error())
return nil, err
}
defer results.Close()

for results.Next() {
var locDeployName string
if err := results.Scan(
&locDeployName,
); err != nil {
return nil, err
}
resDeployNames = append(resDeployNames, locDeployName)
}

return resDeployNames, err
}

// =============== //
// == Policy DB == //
// =============== //
Expand Down
56 changes: 56 additions & 0 deletions src/libs/sqliteHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,10 @@ func GetPodNamesSQLite(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
concatWhereClause(&whereClause, "container_name")
sysargs = append(sysargs, filter.ContainerName)
}
if filter.DeployName != "" {
concatWhereClause(&whereClause, "deployment_name")
sysargs = append(sysargs, filter.DeployName)
}

results, err = db.Query(query+whereClause, sysargs...)
if err != nil {
Expand All @@ -1600,6 +1604,58 @@ func GetPodNamesSQLite(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string,
return resPodNames, err
}

func GetDeployNamesSQLite(cfg types.ConfigDB, filter types.ObsPodDetail) ([]string, error) {
db := connectSQLite(cfg, config.GetCfgObservabilityDBName())
defer db.Close()

resDeployNames := []string{}

var results *sql.Rows
var err error

// Get podnames from system table
query := "SELECT deployment_name FROM " + TableSystemSummarySQLite + " "

var whereClause string
var sysargs []interface{}

if filter.ClusterName != "" {
concatWhereClause(&whereClause, "cluster_name")
sysargs = append(sysargs, filter.ClusterName)
}
if filter.Namespace != "" {
concatWhereClause(&whereClause, "namespace_name")
sysargs = append(sysargs, filter.Namespace)
}
if filter.DeployName != "" {
concatWhereClause(&whereClause, "deployment_name")
sysargs = append(sysargs, filter.DeployName)
}
if filter.Labels != "" {
concatWhereClause(&whereClause, "labels")
sysargs = append(sysargs, filter.Labels)
}

results, err = db.Query(query+whereClause, sysargs...)
if err != nil {
log.Error().Msg(err.Error())
return nil, err
}
defer results.Close()

for results.Next() {
var locDeployName string
if err := results.Scan(
&locDeployName,
); err != nil {
return nil, err
}
resDeployNames = append(resDeployNames, locDeployName)
}

return resDeployNames, err
}

// =============== //
// == Policy DB == //
// =============== //
Expand Down
2 changes: 2 additions & 0 deletions src/observability/kubearmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func GetKubearmorSummaryData(req *opb.Request) ([]types.SysObsProcFileData, []ty
ContainerName: req.ContainerName,
ClusterName: req.ClusterName,
Labels: req.Label,
Deployment: req.DeployName,
})
if err != nil {
return nil, nil, nil, types.ObsPodDetail{}
Expand All @@ -241,6 +242,7 @@ func GetKubearmorSummaryData(req *opb.Request) ([]types.SysObsProcFileData, []ty
podInfo.ContainerName = ss.ContainerName
podInfo.Labels = ss.Labels
podInfo.Namespace = ss.NamespaceName
podInfo.DeployName = ss.Deployment
}

t := time.Unix(ss.UpdatedTime, 0)
Expand Down
24 changes: 24 additions & 0 deletions src/observability/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func GetPodNames(request *opb.Request) (opb.PodNameResponse, error) {
ClusterName: request.ClusterName,
ContainerName: request.ContainerName,
Labels: request.Label,
DeployName: request.DeployName,
})
if err != nil {
return opb.PodNameResponse{}, err
Expand All @@ -128,3 +129,26 @@ func GetPodNames(request *opb.Request) (opb.PodNameResponse, error) {

return opb.PodNameResponse{PodName: result}, nil
}

func GetDeployNames(request *opb.Request) (opb.DeployNameResponse, error) {

result, err := libs.GetDeployNames(CfgDB, types.ObsPodDetail{
PodName: request.PodName,
Namespace: request.NameSpace,
ClusterName: request.ClusterName,
ContainerName: request.ContainerName,
Labels: request.Label,
DeployName: request.DeployName,
})
if err != nil {
return opb.DeployNameResponse{}, err
}

result = common.StringDeDuplication(result)

if len(result) <= 0 {
return opb.DeployNameResponse{}, errors.New("no pods matching the input request")
}

return opb.DeployNameResponse{DeployName: result}, nil
}
2 changes: 1 addition & 1 deletion src/observability/summarizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func convertSysLogToSysSummaryMap(syslogs []*pb.Alert) {
sysSummary.Deployment = ""

for _, d := range deployments {
if d.Labels == syslog.Labels && d.Namespace == syslog.NamespaceName {
if strings.Contains(syslog.Labels, d.Labels) && d.Namespace == syslog.NamespaceName {
sysSummary.Deployment = d.Name
break
}
Expand Down
Loading

0 comments on commit 4ca51fc

Please sign in to comment.