From 768949fc91784f456ccb9250b218f6946f1ccaba Mon Sep 17 00:00:00 2001 From: manasachi Date: Thu, 1 Aug 2024 09:05:13 -0400 Subject: [PATCH 1/3] cleanup and adding comments --- pkg/safeguards/preprocessing/preprocessing.go | 11 ----------- pkg/safeguards/preprocessing/preprocessing_helpers.go | 1 + pkg/safeguards/preprocessing/preprocessing_test.go | 3 ++- .../preprocessing/preprocessing_test_helpers.go | 2 -- pkg/safeguards/types/constants.go | 8 +++----- 5 files changed, 6 insertions(+), 19 deletions(-) diff --git a/pkg/safeguards/preprocessing/preprocessing.go b/pkg/safeguards/preprocessing/preprocessing.go index 37d5bad4..53568d8f 100644 --- a/pkg/safeguards/preprocessing/preprocessing.go +++ b/pkg/safeguards/preprocessing/preprocessing.go @@ -2,7 +2,6 @@ package preprocessing import ( "fmt" - "os" "path/filepath" sgTypes "github.com/Azure/draft/pkg/safeguards/types" @@ -67,16 +66,6 @@ func RenderHelmChart(isFile bool, mainChartPath string, opt chartutil.ReleaseOpt return manifestFiles, nil } -// CreateTempDir creates a temporary directory on the user's file system for rendering templates -func CreateTempDir(p string) error { - err := os.MkdirAll(p, 0755) - if err != nil { - log.Fatal(err) - } - - return err -} - // Given a kustomization manifest file within kustomizationPath, RenderKustomizeManifest will return render templates func RenderKustomizeManifest(kustomizationPath string) ([]sgTypes.ManifestFile, error) { log.Debugf("Rendering kustomization.yaml...") diff --git a/pkg/safeguards/preprocessing/preprocessing_helpers.go b/pkg/safeguards/preprocessing/preprocessing_helpers.go index 0ae79946..9f65799f 100644 --- a/pkg/safeguards/preprocessing/preprocessing_helpers.go +++ b/pkg/safeguards/preprocessing/preprocessing_helpers.go @@ -27,6 +27,7 @@ func getValues(chart *chart.Chart, valuesPath string, opt chartutil.ReleaseOptio return mergedValues, err } +// Extracts release options from either CLI flags, values.yaml, or name of containing directory func getReleaseOptions(chart *chart.Chart, vals map[string]interface{}, opt chartutil.ReleaseOptions, dirName string) (chartutil.Values, error) { // Extract release options from values var options chartutil.ReleaseOptions diff --git a/pkg/safeguards/preprocessing/preprocessing_test.go b/pkg/safeguards/preprocessing/preprocessing_test.go index c0ba8943..fded63f1a 100644 --- a/pkg/safeguards/preprocessing/preprocessing_test.go +++ b/pkg/safeguards/preprocessing/preprocessing_test.go @@ -96,7 +96,7 @@ func TestSubCharts(t *testing.T) { assert.Nil(t, err) // Given a Chart.yaml in the main directory, main chart and subcharts should be evaluated - manifestFiles, err = RenderHelmChart(true, consts.DirectPath_ToSubchartYaml, opt) + _, err = RenderHelmChart(true, consts.SubchartDir, opt) assert.Nil(t, err) // Given path to a sub-Chart.yaml with a dependency on another subchart, should render both subcharts, but not the main chart @@ -132,6 +132,7 @@ func TestInvalidChartAndValues(t *testing.T) { assert.NotNil(t, err) } +// Testing with malformed Deployment.yaml func TestInvalidDeployments(t *testing.T) { var opt chartutil.ReleaseOptions diff --git a/pkg/safeguards/preprocessing/preprocessing_test_helpers.go b/pkg/safeguards/preprocessing/preprocessing_test_helpers.go index ad81425c..5078e7aa 100644 --- a/pkg/safeguards/preprocessing/preprocessing_test_helpers.go +++ b/pkg/safeguards/preprocessing/preprocessing_test_helpers.go @@ -7,8 +7,6 @@ import ( "testing" ) -const () - // Returns the content of a manifest file as bytes func getManifestAsBytes(t *testing.T, filePath string) []byte { yamlFileContent, err := os.ReadFile(filePath) diff --git a/pkg/safeguards/types/constants.go b/pkg/safeguards/types/constants.go index c9f35398..175b00e5 100644 --- a/pkg/safeguards/types/constants.go +++ b/pkg/safeguards/types/constants.go @@ -33,17 +33,15 @@ const ( DirectPath_ToSubchartYaml = "../tests/testmanifests/multiple-charts/charts/subchart1/Chart.yaml" irectPath_ToMainChartYaml = "../tests/testmanifests/multiple-charts/Chart.yaml" directPath_ToInvalidChart = "../tests/testmanifests/invalidchart/Chart.yaml" + + TemplateFileName = "template.yaml" + ConstraintFileName = "constraint.yaml" ) var SelectedVersion = "v1.0.0" var SupportedVersions = []string{SelectedVersion} -const ( - TemplateFileName = "template.yaml" - ConstraintFileName = "constraint.yaml" -) - var Safeguard_CRIP = Safeguard{ Name: Constraint_CRIP, TemplatePath: fmt.Sprintf("lib/%s/%s/%s", SelectedVersion, Constraint_CRIP, TemplateFileName), From 9bfa05140b61cb038f638bb5108b25890c33ccd7 Mon Sep 17 00:00:00 2001 From: manasachi Date: Thu, 1 Aug 2024 09:37:15 -0400 Subject: [PATCH 2/3] more minor updates with consts --- .../preprocessing/preprocessing_test.go | 5 ++++- pkg/safeguards/types/constants.go | 12 +++++----- pkg/workflows/manifests/deployment.yaml | 22 +++++++++++++++++++ .../overlays/production/deployment.yaml | 12 ++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 pkg/workflows/manifests/deployment.yaml create mode 100644 pkg/workflows/overlays/production/deployment.yaml diff --git a/pkg/safeguards/preprocessing/preprocessing_test.go b/pkg/safeguards/preprocessing/preprocessing_test.go index fded63f1a..5da6cd6e 100644 --- a/pkg/safeguards/preprocessing/preprocessing_test.go +++ b/pkg/safeguards/preprocessing/preprocessing_test.go @@ -96,7 +96,7 @@ func TestSubCharts(t *testing.T) { assert.Nil(t, err) // Given a Chart.yaml in the main directory, main chart and subcharts should be evaluated - _, err = RenderHelmChart(true, consts.SubchartDir, opt) + _, err = RenderHelmChart(true, consts.DirectPath_ToMainChartYaml, opt) assert.Nil(t, err) // Given path to a sub-Chart.yaml with a dependency on another subchart, should render both subcharts, but not the main chart @@ -130,6 +130,9 @@ func TestInvalidChartAndValues(t *testing.T) { _, err = RenderHelmChart(false, consts.InvalidValuesChart, opt) assert.NotNil(t, err) + + _, err = RenderHelmChart(false, consts.DirectPath_ToInvalidChart, opt) + assert.NotNil(t, err) } // Testing with malformed Deployment.yaml diff --git a/pkg/safeguards/types/constants.go b/pkg/safeguards/types/constants.go index 175b00e5..a2a7d0bb 100644 --- a/pkg/safeguards/types/constants.go +++ b/pkg/safeguards/types/constants.go @@ -16,23 +16,21 @@ const ( Constraint_all = "all" KustomizationPath = "../tests/kustomize/overlays/production" - KustomizationFilePath = "../tests/kustomize/overlays/production/kustomization.yaml" DirectPath_ToValidChart = "../tests/testmanifests/validchart/Chart.yaml" ChartPath = "../tests/testmanifests/validchart" InvalidChartPath = "../tests/testmanifests/invalidchart" InvalidValuesChart = "../tests/testmanifests/invalidvalues" - InvalidDeploymentsChart = "../tests/testmanifests/invaliddeployment" InvalidDeploymentSyntax = "../tests/testmanifests/invaliddeployment-syntax" InvalidDeploymentValues = "../tests/testmanifests/invaliddeployment-values" FolderwithHelpersTmpl = "../tests/testmanifests/different-structure" MultipleTemplateDirs = "../tests/testmanifests/multiple-templates" MultipleValuesFile = "../tests/testmanifests/multiple-values-files" - Subcharts = "../tests/testmanifests/multiple-charts" - SubchartDir = "../tests/testmanifests/multiple-charts/charts/subchart2" - DirectPath_ToSubchartYaml = "../tests/testmanifests/multiple-charts/charts/subchart1/Chart.yaml" - irectPath_ToMainChartYaml = "../tests/testmanifests/multiple-charts/Chart.yaml" - directPath_ToInvalidChart = "../tests/testmanifests/invalidchart/Chart.yaml" + Subcharts = "../tests/testmanifests/multiple-charts" + SubchartDir = "../tests/testmanifests/multiple-charts/charts/subchart2" + DirectPath_ToSubchartYaml = "../tests/testmanifests/multiple-charts/charts/subchart1/Chart.yaml" + DirectPath_ToMainChartYaml = "../tests/testmanifests/multiple-charts/Chart.yaml" + DirectPath_ToInvalidChart = "../tests/testmanifests/invalidchart/Chart.yaml" TemplateFileName = "template.yaml" ConstraintFileName = "constraint.yaml" diff --git a/pkg/workflows/manifests/deployment.yaml b/pkg/workflows/manifests/deployment.yaml new file mode 100644 index 00000000..d339c904 --- /dev/null +++ b/pkg/workflows/manifests/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test + labels: + app: test + kubernetes.azure.com/generator: draft +spec: + replicas: 1 + selector: + matchLabels: + app: test + template: + metadata: + labels: + app: test + spec: + containers: + - name: test + image: test + ports: + - containerPort: 8000 \ No newline at end of file diff --git a/pkg/workflows/overlays/production/deployment.yaml b/pkg/workflows/overlays/production/deployment.yaml new file mode 100644 index 00000000..11621fba --- /dev/null +++ b/pkg/workflows/overlays/production/deployment.yaml @@ -0,0 +1,12 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test + labels: + kubernetes.azure.com/generator: draft +spec: + template: + spec: + containers: + - name: test + image: acr.test:latest \ No newline at end of file From 2f3f3363d6a859882db155f301e2ddaf1cf38e0e Mon Sep 17 00:00:00 2001 From: manasachi Date: Fri, 2 Aug 2024 10:54:26 -0400 Subject: [PATCH 3/3] removing generated workflow files --- pkg/workflows/manifests/deployment.yaml | 22 ------------------- .../overlays/production/deployment.yaml | 12 ---------- 2 files changed, 34 deletions(-) delete mode 100644 pkg/workflows/manifests/deployment.yaml delete mode 100644 pkg/workflows/overlays/production/deployment.yaml diff --git a/pkg/workflows/manifests/deployment.yaml b/pkg/workflows/manifests/deployment.yaml deleted file mode 100644 index d339c904..00000000 --- a/pkg/workflows/manifests/deployment.yaml +++ /dev/null @@ -1,22 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: test - labels: - app: test - kubernetes.azure.com/generator: draft -spec: - replicas: 1 - selector: - matchLabels: - app: test - template: - metadata: - labels: - app: test - spec: - containers: - - name: test - image: test - ports: - - containerPort: 8000 \ No newline at end of file diff --git a/pkg/workflows/overlays/production/deployment.yaml b/pkg/workflows/overlays/production/deployment.yaml deleted file mode 100644 index 11621fba..00000000 --- a/pkg/workflows/overlays/production/deployment.yaml +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: test - labels: - kubernetes.azure.com/generator: draft -spec: - template: - spec: - containers: - - name: test - image: acr.test:latest \ No newline at end of file