Skip to content

Commit

Permalink
refactoring:- removing unwanted code
Browse files Browse the repository at this point in the history
  • Loading branch information
prakash100198 committed Jul 2, 2024
1 parent 231f17e commit 03e32dc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 112 deletions.
19 changes: 7 additions & 12 deletions pkg/service/CommonHelperService/CommonServices.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (impl *CommonUtils) getManifestData(restConfig *rest.Config, releaseNamespa
}
return desiredOrLiveManifest
}

func (impl *CommonUtils) BuildNodes(restConfig *rest.Config, desiredOrLiveManifests []*bean.DesiredOrLiveManifest, releaseNamespace string, parentResourceRef *bean.ResourceRef) ([]*bean.ResourceNode, []*bean.HealthStatus, error) {
var nodes []*bean.ResourceNode
var healthStatusArray []*bean.HealthStatus
Expand Down Expand Up @@ -287,6 +288,7 @@ func (impl *CommonUtils) BuildNodes(restConfig *rest.Config, desiredOrLiveManife

return nodes, healthStatusArray, nil
}

func (impl *CommonUtils) filterNodes(resourceTreeFilter *client.ResourceTreeFilter, nodes []*bean.ResourceNode) []*bean.ResourceNode {
resourceFilters := resourceTreeFilter.ResourceFilters
globalFilter := resourceTreeFilter.GlobalFilter
Expand Down Expand Up @@ -331,6 +333,7 @@ func (impl *CommonUtils) filterNodes(resourceTreeFilter *client.ResourceTreeFilt

return filteredNodes
}

func (impl *CommonUtils) buildPodMetadata(nodes []*bean.ResourceNode, restConfig *rest.Config) ([]*bean.PodMetadata, error) {
deploymentPodHashMap, rolloutMap, uidVsExtraNodeInfoMap := getExtraNodeInfoMappings(nodes)
podsMetadata := make([]*bean.PodMetadata, 0, len(nodes))
Expand Down Expand Up @@ -404,6 +407,7 @@ func (impl *CommonUtils) buildPodMetadata(nodes []*bean.ResourceNode, restConfig
}
return podsMetadata, nil
}

func (impl *CommonUtils) GetResourceTreeForExternalResources(req *client.ExternalResourceTreeRequest) (*bean.ResourceTreeResponse, error) {
k8sClusterConfig := impl.converter.GetClusterConfigFromClientBean(req.ClusterConfig)
restConfig, err := impl.k8sUtil.GetRestConfigByCluster(k8sClusterConfig)
Expand Down Expand Up @@ -465,6 +469,7 @@ func (impl *CommonUtils) getManifestsForExternalResources(restConfig *rest.Confi
}
return manifests
}

func (impl *CommonUtils) isPodNew(nodes []*bean.ResourceNode, node *bean.ResourceNode, deploymentPodHashMap map[string]string, rolloutMap map[string]*util.ExtraNodeInfo,
uidVsExtraNodeInfoMap map[string]*util.ExtraNodeInfo, restConfig *rest.Config) (bool, error) {

Expand Down Expand Up @@ -533,6 +538,7 @@ func (impl *CommonUtils) isPodNew(nodes []*bean.ResourceNode, node *bean.Resourc
}
return isNew, nil
}

func (impl *CommonUtils) getReplicaSetObject(restConfig *rest.Config, replicaSetNode *bean.ResourceNode) (*v1beta1.ReplicaSet, error) {
gvk := &schema.GroupVersionKind{
Group: replicaSetNode.Group,
Expand Down Expand Up @@ -561,6 +567,7 @@ func (impl *CommonUtils) getReplicaSetObject(restConfig *rest.Config, replicaSet
}
return replicaSetObj, nil
}

func (impl *CommonUtils) getDeploymentCollisionCount(restConfig *rest.Config, deploymentInfo *bean.ResourceRef) (*int32, error) {
parentGvk := &schema.GroupVersionKind{
Group: deploymentInfo.Group,
Expand Down Expand Up @@ -590,18 +597,6 @@ func (impl *CommonUtils) getDeploymentCollisionCount(restConfig *rest.Config, de
return deploymentObj.Status.CollisionCount, nil
}

// func buildResourceRef(gvk schema.GroupVersionKind, manifest unstructured.Unstructured, namespace string) *bean.ResourceRef {
// resourceRef := &bean.ResourceRef{
// Group: gvk.Group,
// Version: gvk.Version,
// Kind: gvk.Kind,
// Namespace: namespace,
// Name: manifest.GetName(),
// UID: string(manifest.GetUID()),
// Manifest: manifest,
// }
// return resourceRef
// }
func updateHookInfoForChildNodes(nodes []*bean.ResourceNode) {
hookUidToHookTypeMap := make(map[string]string)
for _, node := range nodes {
Expand Down
101 changes: 1 addition & 100 deletions pkg/service/HelmApplicationService/HelmAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@ import (
"errors"
"fmt"
registry2 "github.com/devtron-labs/common-lib/helmLib/registry"
//k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
//k8sObjectUtils "github.com/devtron-labs/common-lib/utils/k8sObjectsUtil"
"github.com/devtron-labs/kubelink/converter"
error2 "github.com/devtron-labs/kubelink/error"
"github.com/devtron-labs/kubelink/pkg/cache"
repository "github.com/devtron-labs/kubelink/pkg/cluster"
"github.com/devtron-labs/kubelink/pkg/service/CommonHelperService"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/storage/driver"
//"k8s.io/api/extensions/v1beta1"
//"k8s.io/apimachinery/pkg/util/yaml"
"net/url"
"path"
"strings"
Expand All @@ -50,7 +45,6 @@ import (
"github.com/devtron-labs/kubelink/pkg/helmClient"
"github.com/devtron-labs/kubelink/pkg/k8sInformer"
"github.com/devtron-labs/kubelink/pkg/util"
"github.com/devtron-labs/kubelink/pkg/util/argo"
jsonpatch "github.com/evanphx/json-patch"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -64,7 +58,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"math/rand"
"net/http"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -1252,7 +1245,7 @@ func (impl *HelmAppServiceImpl) getNodes(appDetailRequest *client.AppDetailReque
return nil, nil, err
}
// build resource nodes
nodes, healthStatusArray, err := impl.buildNodes(conf, desiredOrLiveManifests, appDetailRequest.Namespace, nil)
nodes, healthStatusArray, err := impl.common.BuildNodes(conf, desiredOrLiveManifests, appDetailRequest.Namespace, nil)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1314,98 +1307,6 @@ func (impl HelmAppServiceImpl) getManifestData(restConfig *rest.Config, releaseN
return desiredOrLiveManifest
}

func (impl HelmAppServiceImpl) buildNodes(restConfig *rest.Config, desiredOrLiveManifests []*bean.DesiredOrLiveManifest, releaseNamespace string, parentResourceRef *bean.ResourceRef) ([]*bean.ResourceNode, []*bean.HealthStatus, error) {
var nodes []*bean.ResourceNode
var healthStatusArray []*bean.HealthStatus
for _, desiredOrLiveManifest := range desiredOrLiveManifests {
manifest := desiredOrLiveManifest.Manifest
gvk := manifest.GroupVersionKind()
_namespace := manifest.GetNamespace()
if _namespace == "" {
_namespace = releaseNamespace
}
ports := util.GetPorts(manifest, gvk)
resourceRef := CommonHelperService.BuildResourceRef(gvk, *manifest, _namespace)

if impl.k8sService.CanHaveChild(gvk) {
children, err := impl.k8sService.GetChildObjects(restConfig, _namespace, gvk, manifest.GetName(), manifest.GetAPIVersion())
if err != nil {
return nil, nil, err
}
desiredOrLiveManifestsChildren := make([]*bean.DesiredOrLiveManifest, 0, len(children))
for _, child := range children {
desiredOrLiveManifestsChildren = append(desiredOrLiveManifestsChildren, &bean.DesiredOrLiveManifest{
Manifest: child,
})
}
childNodes, _, err := impl.buildNodes(restConfig, desiredOrLiveManifestsChildren, releaseNamespace, resourceRef)
if err != nil {
return nil, nil, err
}

for _, childNode := range childNodes {
nodes = append(nodes, childNode)
healthStatusArray = append(healthStatusArray, childNode.Health)
}
}

creationTimeStamp := ""
val, found, err := unstructured.NestedString(manifest.Object, "metadata", "creationTimestamp")
if found && err == nil {
creationTimeStamp = val
}
node := &bean.ResourceNode{
ResourceRef: resourceRef,
ResourceVersion: manifest.GetResourceVersion(),
NetworkingInfo: &bean.ResourceNetworkingInfo{
Labels: manifest.GetLabels(),
},
CreatedAt: creationTimeStamp,
Port: ports,
}
node.IsHook, node.HookType = util.GetHookMetadata(manifest)

if parentResourceRef != nil {
node.ParentRefs = append(make([]*bean.ResourceRef, 0), parentResourceRef)
}

// set health of node
if desiredOrLiveManifest.IsLiveManifestFetchError {
if desiredOrLiveManifest.LiveManifestFetchErrorCode == http.StatusNotFound {
node.Health = &bean.HealthStatus{
Status: bean.HealthStatusMissing,
Message: "Resource missing as live manifest not found",
}
} else {
node.Health = &bean.HealthStatus{
Status: bean.HealthStatusUnknown,
Message: "Resource state unknown as error while fetching live manifest",
}
}
} else {
cache.SetHealthStatusForNode(node, manifest, gvk)
}

// hibernate set starts
if parentResourceRef == nil {

// set CanBeHibernated
cache.SetHibernationRules(node, &node.Manifest)
}
// hibernate set ends
if k8sUtils.IsPod(gvk) {
infoItems, _ := argo.PopulatePodInfo(manifest)
node.Info = infoItems
}
util.AddSelectiveInfoInResourceNode(node, gvk, manifest.Object)

nodes = append(nodes, node)
healthStatusArray = append(healthStatusArray, node.Health)
}

return nodes, healthStatusArray, nil
}

func (impl HelmAppServiceImpl) getHelmClient(clusterConfig *client.ClusterConfig, releaseNamespace string) (helmClient.Client, error) {
k8sClusterConfig := impl.converter.GetClusterConfigFromClientBean(clusterConfig)
conf, err := impl.k8sUtil.GetRestConfigByCluster(k8sClusterConfig)
Expand Down

0 comments on commit 03e32dc

Please sign in to comment.