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 20, 2023
1 parent 03f25f1 commit a7e59de
Show file tree
Hide file tree
Showing 13 changed files with 641 additions and 167 deletions.
131 changes: 115 additions & 16 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

type Config struct {
Expand Down Expand Up @@ -102,7 +101,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 All @@ -118,13 +117,13 @@ func ConnectInClusterAPIClient() *kubernetes.Clientset {
Insecure: true,
},
}

if client, err := kubernetes.NewForConfig(kubeConfig); err != nil {
client, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
log.Error().Msg(err.Error())
return nil
} else {
return client
}
return client

}

// =============== //
Expand All @@ -136,6 +135,7 @@ func GetNamespacesFromK8sClient() []string {

client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
return results
}

Expand All @@ -161,7 +161,7 @@ func GetNamespacesFromK8sClient() []string {
// == Pod == //
// ========= //

var skipLabelKey []string = []string{
var skipLabelKey = []string{
"pod-template-hash", // common k8s hash label
"controller-revision-hash", // from istana robot-shop
"statefulset.kubernetes.io/pod-name"} // from istana robot-shop
Expand All @@ -171,6 +171,7 @@ func GetPodsFromK8sClient() []types.Pod {

client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
return nil
}

Expand Down Expand Up @@ -278,6 +279,7 @@ func GetServicesFromK8sClient() []types.Service {

client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
return results
}

Expand Down Expand Up @@ -330,6 +332,7 @@ func GetEndpointsFromK8sClient() []types.Endpoint {

client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
return results
}

Expand Down Expand Up @@ -398,6 +401,7 @@ func GetEndpointsFromK8sClient() []types.Endpoint {
func GetClusterNameFromK8sClient() string {
client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
return "default"
}

Expand Down Expand Up @@ -435,6 +439,7 @@ func GetDeploymentsFromK8sClient() []types.Deployment {

client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
return results
}

Expand All @@ -450,17 +455,111 @@ 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 {
labels = append(labels, k+"="+v)
}

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{}

results = append(results, types.Deployment{
Name: d.Name,
Namespace: d.Namespace,
Labels: label,
})
client := ConnectK8sClient()
if client == nil {
log.Error().Msg("failed to create k8s client")
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
}

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 {
labels = append(labels, k+"="+v)
}

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 {
log.Error().Msg("failed to create k8s client")
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 {
labels = append(labels, k+"="+v)
}

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
Loading

0 comments on commit a7e59de

Please sign in to comment.