Skip to content

Commit

Permalink
Merge pull request #283 from leanix/feature/HEL-2723
Browse files Browse the repository at this point in the history
feature/HEL-2723 modify workloads structure and add cluster info
  • Loading branch information
daniellesarmiento authored Jun 1, 2023
2 parents 883b3e0 + 5a8801e commit 93ddaa6
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 279 deletions.
17 changes: 14 additions & 3 deletions pkg/iris/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,18 @@ func (s *scanner) ScanNamespaces(kubernetesConfig models.KubernetesConfig, kuber

func (s *scanner) ScanWorkloads(kubernetesAPI *kubernetes.API, kubernetesConfig models.KubernetesConfig) error {
mapper := workloadMap.NewMapper(kubernetesAPI, kubernetesConfig.Cluster, s.workspaceId, s.runId)
discoveredWorkloads, err := s.ProcessWorkloads(mapper, kubernetesConfig.Cluster)

nodes, err := kubernetesAPI.Nodes()
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s cluster nodes. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

clusterInfo, err := mapper.MapCluster(kubernetesConfig.Cluster, nodes)
if err != nil {
return s.LogAndShareError("Scan failed while aggregating cluster information. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

discoveredWorkloads, err := s.ProcessWorkloads(mapper, clusterInfo)
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s workload. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}
Expand Down Expand Up @@ -193,8 +204,8 @@ func (s *scanner) ProcessNamespace(k8sApi *kubernetes.API, mapper namespaceMap.M
return ecstData, nil
}

func (s *scanner) ProcessWorkloads(mapper workloadMap.MapperWorkload, clusterName string) ([]workload.Workload, error) {
return mapper.MapWorkloads(clusterName)
func (s *scanner) ProcessWorkloads(mapper workloadMap.WorkloadMapper, clusterInfo workload.Cluster) ([]workload.Data, error) {
return mapper.MapWorkloads(clusterInfo)
}

func (s *scanner) LogAndShareError(message string, loglevel string, err error, id string) error {
Expand Down
40 changes: 22 additions & 18 deletions pkg/iris/workloads/models/discoveryItemEcst.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package models

type Data struct {
Workload Workload `json:"workload"`
}

type Service struct {
Name string `json:"name"`
Workload Workload `json:"workload"`
NamespaceName string `json:"namespaceName"`
ServiceName string `json:"serviceName"`
Cluster Cluster `json:"cluster"`
Timestamp string `json:"timestamp"`
}

type Workload struct {
ClusterName string `json:"clusterName"`
WorkloadType string `json:"workloadType"`
WorkloadName string `json:"workloadName"`
Containers Containers `json:"containers"`
ServiceName string `json:"serviceName"`
Labels interface{} `json:"labels"`
Timestamp string `json:"timestamp"`
WorkloadProperties Properties `json:"WorkloadProperties"`
Name string `json:"name"`
WorkloadType string `json:"type"`
Labels map[string]string `json:"labels"`
WorkloadProperties WorkloadProperties `json:"workloadProperties"`
}

type Properties struct {
Schedule string `json:"schedule"`
Replicas string `json:"replicas"`
UpdateStrategy string `json:"updateStrategy"`
type WorkloadProperties struct {
Schedule string `json:"schedule"`
Replicas string `json:"replicas"`
UpdateStrategy string `json:"updateStrategy"`
Containers Containers `json:"containers"`
}

type Containers struct {
Name string `json:"containerName"`
Name string `json:"name"`
Image string `json:"image"`
Port interface{} `json:"port"`
K8sLimits K8sResources `json:"k8sLimits"`
Expand All @@ -37,3 +34,10 @@ type K8sResources struct {
Cpu string `json:"cpu"`
Memory string `json:"memory"`
}

type Cluster struct {
Name string `json:"name"`
OsImage string `json:"os"`
K8sVersion string `json:"k8sVersion"`
NoOfNodes int `json:"noOfNodes"`
}
6 changes: 3 additions & 3 deletions pkg/iris/workloads/models/eventsEcst.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ func CreateEcstDiscoveryEvent(eventType string, changeAction string, data Data,

body := models.DiscoveryBody{
State: models.State{
Name: fmt.Sprintf("%s/%s", data.Workload.WorkloadType, data.Workload.WorkloadName),
Name: fmt.Sprintf("%s/%s", data.Workload.WorkloadType, data.Workload.Name),
SourceType: "kubernetes",
SourceInstance: fmt.Sprintf("cluster/%s", data.Workload.ClusterName),
SourceInstance: fmt.Sprintf("cluster/%s", data.Cluster.Name),
Time: time,
Data: data,
},
Expand All @@ -147,7 +147,7 @@ func CreateEcstDiscoveryEvent(eventType string, changeAction string, data Data,
func GenerateId(workspaceId string, configId string, data Data) string {
scope := fmt.Sprintf(models.EventScopeFormat, workspaceId, configId)
// workspace/{workspaceId}/configuration/{configurationId}/discoveryItem/service/kubernetes/workload/{clusterName}/{workloadType}/{workloadName}
idString := fmt.Sprintf("%s/%s/%s/%s/%s", scope, models.EventClassWorkload, data.Workload.ClusterName, data.Workload.WorkloadType, data.Workload.WorkloadName)
idString := fmt.Sprintf("%s/%s/%s/%s/%s", scope, models.EventClassWorkload, data.Cluster.Name, data.Workload.WorkloadType, data.Workload.Name)
sum := sha256.Sum256([]byte(idString))
id := hex.EncodeToString(sum[:])
return id
Expand Down
15 changes: 7 additions & 8 deletions pkg/iris/workloads/services/events/eventProducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type WorkloadEventProducer interface {
ProcessWorkloads(data []workload.Workload, oldData []models.DiscoveryEvent, configId string) error
ProcessWorkloads(data []workload.Data, oldData []models.DiscoveryEvent, configId string) error
PostStatus(status []byte) error
FilterForChangedItems(newData map[string]workload.Data, oldData map[string]models.DiscoveryEvent, configId string) ([]models.DiscoveryEvent, []models.DiscoveryEvent, map[string]models.DiscoveryEvent, error)
}
Expand All @@ -30,7 +30,7 @@ func NewEventWorkloadProducer(irisApi common.IrisApi, runId string, workspaceId
}
}

func (p *workloadEventProducer) ProcessWorkloads(data []workload.Workload, oldData []models.DiscoveryEvent, configId string) error {
func (p *workloadEventProducer) ProcessWorkloads(data []workload.Data, oldData []models.DiscoveryEvent, configId string) error {
created, updated, deleted, err := p.CreateECSTWorkloadEvents(data, oldData, configId)
if err != nil {
return err
Expand All @@ -52,7 +52,7 @@ func (p *workloadEventProducer) PostStatus(status []byte) error {
return p.irisApi.PostStatus(status)
}

func (p *workloadEventProducer) CreateECSTWorkloadEvents(data []workload.Workload, oldData []models.DiscoveryEvent, configId string) ([]models.DiscoveryEvent, []models.DiscoveryEvent, []models.DiscoveryEvent, error) {
func (p *workloadEventProducer) CreateECSTWorkloadEvents(data []workload.Data, oldData []models.DiscoveryEvent, configId string) ([]models.DiscoveryEvent, []models.DiscoveryEvent, []models.DiscoveryEvent, error) {
resultMap := p.createItemMap(data, configId)
oldResultMap := p.createOldItemMap(oldData)

Expand All @@ -70,13 +70,12 @@ func (p *workloadEventProducer) CreateECSTWorkloadEvents(data []workload.Workloa

}

func (p *workloadEventProducer) createItemMap(workflows []workload.Workload, configId string) map[string]workload.Data {
func (p *workloadEventProducer) createItemMap(workloads []workload.Data, configId string) map[string]workload.Data {
resultMap := map[string]workload.Data{}
for _, item := range workflows {
data := workload.Data{Workload: item}
for _, item := range workloads {
// Build unique string hash for discoveryItem
id := workload.GenerateId(p.workspaceId, configId, data)
resultMap[id] = data
id := workload.GenerateId(p.workspaceId, configId, item)
resultMap[id] = item
}
return resultMap
}
Expand Down
Loading

0 comments on commit 93ddaa6

Please sign in to comment.