Skip to content

Commit

Permalink
refactor: use interface to avoid dupplication
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
Signed-off-by: Rakshit Gondwal <rakshitgondwal3@gmail.com>
  • Loading branch information
matthisholleville authored and rakshitgondwal committed Apr 17, 2023
1 parent fc62ac6 commit af06ef1
Showing 1 changed file with 72 additions and 62 deletions.
134 changes: 72 additions & 62 deletions pkg/analyzer/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -30,80 +32,28 @@ func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {

// check ScaleTargetRef exist
scaleTargetRef := hpa.Spec.ScaleTargetRef
scaleTargetRefNotFound := false
var podInfo PodInfo

switch scaleTargetRef.Kind {
case "Deployment":
deployment, err := a.Client.GetClient().AppsV1().Deployments(hpa.Namespace).Get(a.Context, scaleTargetRef.Name, metav1.GetOptions{})
if err != nil {
scaleTargetRefNotFound = true
} else {
// check if the deployment has resource configured
if (deployment.Spec.Template.Spec.Containers[0].Resources.Requests == nil) || (deployment.Spec.Template.Spec.Containers[0].Resources.Limits == nil) {
failures = append(failures, common.Failure{
Text: fmt.Sprintf("Deployment %s/%s does not have resource configured.", deployment.Namespace, deployment.Name),
Sensitive: []common.Sensitive{
{
Unmasked: deployment.Name,
Masked: util.MaskString(deployment.Name),
},
},
})
}
if err == nil {
podInfo = DeploymentInfo{deployment}
}
case "ReplicationController":
rc, err := a.Client.GetClient().CoreV1().ReplicationControllers(hpa.Namespace).Get(a.Context, scaleTargetRef.Name, metav1.GetOptions{})
if err != nil {
scaleTargetRefNotFound = true
} else {
// check if the replication controller has resource configured
if (rc.Spec.Template.Spec.Containers[0].Resources.Requests == nil) || (rc.Spec.Template.Spec.Containers[0].Resources.Limits == nil) {
failures = append(failures, common.Failure{
Text: fmt.Sprintf("ReplicationController %s/%s does not have resource configured.", rc.Namespace, rc.Name),
Sensitive: []common.Sensitive{
{
Unmasked: rc.Name,
Masked: util.MaskString(rc.Name),
},
},
})
}
if err == nil {
podInfo = ReplicationControllerInfo{rc}
}
case "ReplicaSet":
rs, err := a.Client.GetClient().AppsV1().ReplicaSets(hpa.Namespace).Get(a.Context, scaleTargetRef.Name, metav1.GetOptions{})
if err != nil {
scaleTargetRefNotFound = true
} else {
// check if the replica set has resource configured
if (rs.Spec.Template.Spec.Containers[0].Resources.Requests == nil) || (rs.Spec.Template.Spec.Containers[0].Resources.Limits == nil) {
failures = append(failures, common.Failure{
Text: fmt.Sprintf("ReplicaSet %s/%s does not have resource configured.", rs.Namespace, rs.Name),
Sensitive: []common.Sensitive{
{
Unmasked: rs.Name,
Masked: util.MaskString(rs.Name),
},
},
})
}
if err == nil {
podInfo = ReplicaSetInfo{rs}
}
case "StatefulSet":
ss, err := a.Client.GetClient().AppsV1().StatefulSets(hpa.Namespace).Get(a.Context, scaleTargetRef.Name, metav1.GetOptions{})
if err != nil {
scaleTargetRefNotFound = true
} else {
// check if the stateful set has resource configured
if (ss.Spec.Template.Spec.Containers[0].Resources.Requests == nil) || (ss.Spec.Template.Spec.Containers[0].Resources.Limits == nil) {
failures = append(failures, common.Failure{
Text: fmt.Sprintf("StatefulSet %s/%s does not have resource configured.", ss.Namespace, ss.Name),
Sensitive: []common.Sensitive{
{
Unmasked: ss.Name,
Masked: util.MaskString(ss.Name),
},
},
})
}
if err == nil {
podInfo = StatefulSetInfo{ss}
}
default:
failures = append(failures, common.Failure{
Expand All @@ -112,7 +62,7 @@ func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
})
}

if scaleTargetRefNotFound {
if podInfo == nil {
failures = append(failures, common.Failure{
Text: fmt.Sprintf("HorizontalPodAutoscaler uses %s/%s as ScaleTargetRef which does not exist.", scaleTargetRef.Kind, scaleTargetRef.Name),
Sensitive: []common.Sensitive{
Expand All @@ -122,6 +72,26 @@ func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
},
},
})
} else {
containers := len(podInfo.GetPodSpec().Containers)
for _, container := range podInfo.GetPodSpec().Containers {
if container.Resources.Requests == nil || container.Resources.Limits == nil {
containers--
}
}

if containers <= 0 {
failures = append(failures, common.Failure{
Text: fmt.Sprintf("%s %s/%s does not have resource configured.", scaleTargetRef.Kind, a.Namespace, scaleTargetRef.Name),
Sensitive: []common.Sensitive{
{
Unmasked: scaleTargetRef.Name,
Masked: util.MaskString(scaleTargetRef.Name),
},
},
})
}

}

if len(failures) > 0 {
Expand All @@ -148,3 +118,43 @@ func (HpaAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {

return a.Results, nil
}

type PodInfo interface {
GetPodSpec() corev1.PodSpec
}

type DeploymentInfo struct {
*appsv1.Deployment
}

func (d DeploymentInfo) GetPodSpec() corev1.PodSpec {
return d.Spec.Template.Spec
}

// define a structure for ReplicationController
type ReplicationControllerInfo struct {
*corev1.ReplicationController
}

func (rc ReplicationControllerInfo) GetPodSpec() corev1.PodSpec {
return rc.Spec.Template.Spec
}

// define a structure for ReplicaSet
type ReplicaSetInfo struct {
*appsv1.ReplicaSet
}

func (rs ReplicaSetInfo) GetPodSpec() corev1.PodSpec {
return rs.Spec.Template.Spec
}

// define a structure for StatefulSet
type StatefulSetInfo struct {
*appsv1.StatefulSet
}

// implement PodInfo for StatefulSetInfo
func (ss StatefulSetInfo) GetPodSpec() corev1.PodSpec {
return ss.Spec.Template.Spec
}

0 comments on commit af06ef1

Please sign in to comment.