Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
  • Loading branch information
juev committed Sep 19, 2024
1 parent e51bef2 commit 0798bf9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 105 deletions.
33 changes: 0 additions & 33 deletions pkg/helm/log.go

This file was deleted.

34 changes: 0 additions & 34 deletions pkg/helm/log_test.go

This file was deleted.

16 changes: 5 additions & 11 deletions pkg/helm/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@ package helm

import (
"fmt"
"log"
"os"

"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
)

func init() {
log.SetOutput(&FilteredHelmWriter{Writer: os.Stderr})
}

type Renderer struct {
Name string
Namespace string
LintMode bool
}

func (r Renderer) RenderChartFromDir(dir string, values string) (files map[string]string, err error) {
func (r Renderer) RenderChartFromDir(dir, values string) (files map[string]string, err error) {
c, err := loader.Load(dir)
if err != nil {
panic(fmt.Errorf("chart load from '%s': %v", dir, err))
panic(fmt.Errorf("chart load from '%s': %w", dir, err))
}
return r.RenderChart(c, values)
}

func (r Renderer) RenderChart(c *chart.Chart, values string) (files map[string]string, err error) {
vals, err := chartutil.ReadValues([]byte(values))
if err != nil {
return nil, fmt.Errorf("helm chart read raw values: %v", err)
return nil, fmt.Errorf("helm chart read raw values: %w", err)
}

releaseName := "release"
Expand Down Expand Up @@ -65,7 +59,7 @@ func (r Renderer) RenderChart(c *chart.Chart, values string) (files map[string]s

valuesToRender, err := chartutil.ToRenderValues(c, vals, releaseOptions, nil)
if err != nil {
return nil, fmt.Errorf("helm chart prepare render values: %v", err)
return nil, fmt.Errorf("helm chart prepare render values: %w", err)
}

return r.RenderChartFromRawValues(c, valuesToRender)
Expand All @@ -78,7 +72,7 @@ func (r Renderer) RenderChartFromRawValues(c *chart.Chart, values chartutil.Valu

out, err := e.Render(c, values)
if err != nil {
return nil, fmt.Errorf("helm chart render: %v", err)
return nil, fmt.Errorf("helm chart render: %w", err)
}

return out, nil
Expand Down
62 changes: 36 additions & 26 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

const (
deploymentString = "Deployment"
daemonSetString = "DaemonSet"
statefulSetString = "StatefulSet"
podString = "Pod"
jobString = "Job"
cronJobString = "CronJob"
replicaSetString = "ReplicaSet"
)

type ResourceIndex struct {
Kind string
Name string
Expand Down Expand Up @@ -46,7 +56,7 @@ func (s *StoreObject) GetInitContainers() ([]v1.Container, error) {
converter := runtime.DefaultUnstructuredConverter

switch s.Unstructured.GetKind() {
case "Deployment":
case deploymentString:
deployment := new(appsv1.Deployment)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), deployment)
Expand All @@ -55,7 +65,7 @@ func (s *StoreObject) GetInitContainers() ([]v1.Container, error) {
}

containers = deployment.Spec.Template.Spec.InitContainers
case "DaemonSet":
case daemonSetString:
daemonSet := new(appsv1.DaemonSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), daemonSet)
Expand All @@ -64,7 +74,7 @@ func (s *StoreObject) GetInitContainers() ([]v1.Container, error) {
}

containers = daemonSet.Spec.Template.Spec.InitContainers
case "StatefulSet":
case statefulSetString:
statefulSet := new(appsv1.StatefulSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), statefulSet)
Expand All @@ -73,7 +83,7 @@ func (s *StoreObject) GetInitContainers() ([]v1.Container, error) {
}

containers = statefulSet.Spec.Template.Spec.InitContainers
case "Pod":
case podString:
pod := new(v1.Pod)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), pod)
Expand All @@ -82,7 +92,7 @@ func (s *StoreObject) GetInitContainers() ([]v1.Container, error) {
}

containers = pod.Spec.InitContainers
case "Job":
case jobString:
job := new(batchv1.Job)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), job)
Expand All @@ -91,7 +101,7 @@ func (s *StoreObject) GetInitContainers() ([]v1.Container, error) {
}

containers = job.Spec.Template.Spec.InitContainers
case "CronJob":
case cronJobString:
cronJob := new(batchv1.CronJob)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), cronJob)
Expand All @@ -109,7 +119,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
converter := runtime.DefaultUnstructuredConverter

switch s.Unstructured.GetKind() {
case "Deployment":
case deploymentString:
deployment := new(appsv1.Deployment)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), deployment)
Expand All @@ -118,7 +128,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
}

containers = deployment.Spec.Template.Spec.Containers
case "DaemonSet":
case daemonSetString:
daemonSet := new(appsv1.DaemonSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), daemonSet)
Expand All @@ -127,7 +137,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
}

containers = daemonSet.Spec.Template.Spec.Containers
case "StatefulSet":
case statefulSetString:
statefulSet := new(appsv1.StatefulSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), statefulSet)
Expand All @@ -136,7 +146,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
}

containers = statefulSet.Spec.Template.Spec.Containers
case "Pod":
case podString:
pod := new(v1.Pod)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), pod)
Expand All @@ -145,7 +155,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
}

containers = pod.Spec.Containers
case "Job":
case jobString:
job := new(batchv1.Job)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), job)
Expand All @@ -154,7 +164,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
}

containers = job.Spec.Template.Spec.Containers
case "CronJob":
case cronJobString:
cronJob := new(batchv1.CronJob)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), cronJob)
Expand All @@ -163,7 +173,7 @@ func (s *StoreObject) GetContainers() ([]v1.Container, error) {
}

containers = cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers
case "ReplicaSet":
case replicaSetString:
replicaSet := new(appsv1.ReplicaSet)
err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), replicaSet)
if err != nil {
Expand All @@ -179,7 +189,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
converter := runtime.DefaultUnstructuredConverter

switch s.Unstructured.GetKind() {
case "Deployment":
case deploymentString:
deployment := new(appsv1.Deployment)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), deployment)
Expand All @@ -188,7 +198,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
}

return deployment.Spec.Template.Spec.SecurityContext, nil
case "DaemonSet":
case daemonSetString:
daemonSet := new(appsv1.DaemonSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), daemonSet)
Expand All @@ -197,7 +207,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
}

return daemonSet.Spec.Template.Spec.SecurityContext, nil
case "StatefulSet":
case statefulSetString:
statefulSet := new(appsv1.StatefulSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), statefulSet)
Expand All @@ -206,7 +216,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
}

return statefulSet.Spec.Template.Spec.SecurityContext, nil
case "Pod":
case podString:
pod := new(v1.Pod)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), pod)
Expand All @@ -215,7 +225,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
}

return pod.Spec.SecurityContext, nil
case "Job":
case jobString:
job := new(batchv1.Job)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), job)
Expand All @@ -224,7 +234,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
}

return job.Spec.Template.Spec.SecurityContext, nil
case "CronJob":
case cronJobString:
cronJob := new(batchv1.CronJob)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), cronJob)
Expand All @@ -233,7 +243,7 @@ func (s *StoreObject) GetPodSecurityContext() (*v1.PodSecurityContext, error) {
}

return cronJob.Spec.JobTemplate.Spec.Template.Spec.SecurityContext, nil
case "ReplicaSet":
case replicaSetString:
replicaSet := new(appsv1.ReplicaSet)
err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), replicaSet)
if err != nil {
Expand All @@ -249,7 +259,7 @@ func (s *StoreObject) IsHostNetwork() (bool, error) {
converter := runtime.DefaultUnstructuredConverter

switch s.Unstructured.GetKind() {
case "Deployment":
case deploymentString:
deployment := new(appsv1.Deployment)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), deployment)
Expand All @@ -258,7 +268,7 @@ func (s *StoreObject) IsHostNetwork() (bool, error) {
}

return deployment.Spec.Template.Spec.HostNetwork, nil
case "DaemonSet":
case daemonSetString:
daemonSet := new(appsv1.DaemonSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), daemonSet)
Expand All @@ -267,7 +277,7 @@ func (s *StoreObject) IsHostNetwork() (bool, error) {
}

return daemonSet.Spec.Template.Spec.HostNetwork, nil
case "StatefulSet":
case statefulSetString:
statefulSet := new(appsv1.StatefulSet)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), statefulSet)
Expand All @@ -276,7 +286,7 @@ func (s *StoreObject) IsHostNetwork() (bool, error) {
}

return statefulSet.Spec.Template.Spec.HostNetwork, nil
case "Pod":
case podString:
pod := new(v1.Pod)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), pod)
Expand All @@ -285,7 +295,7 @@ func (s *StoreObject) IsHostNetwork() (bool, error) {
}

return pod.Spec.HostNetwork, nil
case "Job":
case jobString:
job := new(batchv1.Job)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), job)
Expand All @@ -294,7 +304,7 @@ func (s *StoreObject) IsHostNetwork() (bool, error) {
}

return job.Spec.Template.Spec.HostNetwork, nil
case "CronJob":
case cronJobString:
cronJob := new(batchv1.CronJob)

err := converter.FromUnstructured(s.Unstructured.UnstructuredContent(), cronJob)
Expand Down
2 changes: 1 addition & 1 deletion pkg/values_validation/values_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ValuesValidator struct {
}

func NewValuesValidator(moduleName, modulePath string) (*ValuesValidator, error) {
openAPIDir := filepath.Join(modulePath, "..", "..", "global-hooks", "openapi")
openAPIDir := filepath.Join("/deckhouse", "global-hooks", "openapi")

Check failure on line 21 in pkg/values_validation/values_validation.go

View workflow job for this annotation

GitHub Actions / golangci-lint

filepathJoin: "/deckhouse" contains a path separator (gocritic)
configBytes, valuesBytes, err := utils.ReadOpenAPIFiles(openAPIDir)
if err != nil {
return nil, fmt.Errorf("read global openAPI schemas: %v", err)

Check failure on line 24 in pkg/values_validation/values_validation.go

View workflow job for this annotation

GitHub Actions / golangci-lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
Expand Down

0 comments on commit 0798bf9

Please sign in to comment.