From 78f08bf5ad64a89b18158f17237ab18810c6eb02 Mon Sep 17 00:00:00 2001 From: David Gamero Date: Mon, 28 Oct 2024 16:20:51 +0000 Subject: [PATCH] remove workflow package --- cmd/generate-workflow.go | 4 +- pkg/workflows/charts/production.yaml | 8 - pkg/workflows/github_workflow.go | 25 --- pkg/workflows/manifests/deployment.yaml | 22 --- .../overlays/production/deployment.yaml | 12 -- pkg/workflows/service_types.go | 113 ----------- pkg/workflows/workflows.go | 176 ------------------ pkg/workflows/workflows_test.go | 119 ------------ 8 files changed, 2 insertions(+), 477 deletions(-) delete mode 100644 pkg/workflows/charts/production.yaml delete mode 100644 pkg/workflows/github_workflow.go delete mode 100644 pkg/workflows/manifests/deployment.yaml delete mode 100644 pkg/workflows/overlays/production/deployment.yaml delete mode 100644 pkg/workflows/service_types.go delete mode 100644 pkg/workflows/workflows.go delete mode 100644 pkg/workflows/workflows_test.go diff --git a/cmd/generate-workflow.go b/cmd/generate-workflow.go index 8ee4550d..c47bb0b4 100644 --- a/cmd/generate-workflow.go +++ b/cmd/generate-workflow.go @@ -11,8 +11,8 @@ import ( "github.com/Azure/draft/pkg/handlers" "github.com/Azure/draft/pkg/prompts" "github.com/Azure/draft/pkg/templatewriter" + "github.com/Azure/draft/pkg/cmdhelpers" "github.com/Azure/draft/pkg/templatewriter/writers" - "github.com/Azure/draft/pkg/workflows" ) type generateWorkflowCmd struct { @@ -87,7 +87,7 @@ func (gwc *generateWorkflowCmd) generateWorkflows() error { return err } - if err := workflows.UpdateProductionDeployments(gwc.deployType, gwc.dest, t.Config, gwc.templateWriter); err != nil { + if err := cmdhelpers.UpdateProductionDeployments(gwc.deployType, gwc.dest, t.Config, gwc.templateWriter); err != nil { return fmt.Errorf("update production deployments: %w", err) } diff --git a/pkg/workflows/charts/production.yaml b/pkg/workflows/charts/production.yaml deleted file mode 100644 index dc887970..00000000 --- a/pkg/workflows/charts/production.yaml +++ /dev/null @@ -1,8 +0,0 @@ -image: - repository: "test" - pullPolicy: Always - tag: "latest" -service: - annotations: {} - type: LoadBalancer - port: 80 diff --git a/pkg/workflows/github_workflow.go b/pkg/workflows/github_workflow.go deleted file mode 100644 index ec5f94f7..00000000 --- a/pkg/workflows/github_workflow.go +++ /dev/null @@ -1,25 +0,0 @@ -package workflows - -//GitHubWorkflow is a rough struct to allow for yaml editing including deletion of Job steps -type GitHubWorkflow struct { - Name string - On on `yaml:"on"` - Env map[string]string - Jobs map[string]job -} - -type on struct { - Push push - WorkflowDispatch interface{} `yaml:"workflow_dispatch"` -} - -type push struct { - Branches []string -} - -type job struct { - Permissions map[string]string - RunsOn string `yaml:"runs-on"` - Needs []string `yaml:"needs,omitempty"` - Steps []map[string]interface{} -} 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 diff --git a/pkg/workflows/service_types.go b/pkg/workflows/service_types.go deleted file mode 100644 index c88ab7eb..00000000 --- a/pkg/workflows/service_types.go +++ /dev/null @@ -1,113 +0,0 @@ -package workflows - -import ( - "errors" - "io/ioutil" - "os" - - "gopkg.in/yaml.v3" - corev1 "k8s.io/api/core/v1" - "k8s.io/cli-runtime/pkg/printers" - "k8s.io/client-go/kubernetes/scheme" -) - -type ServiceManifest interface { - LoadFromFile(string) error - WriteToFile(string) error - SetAnnotations(map[string]string) - SetServiceType(string) - GetServiceName() string -} - -type HelmProductionYaml struct { - Image image `yaml:"image"` - Service service `yaml:"service"` -} - -type service struct { - Annotations map[string]string `yaml:"annotations"` - ServiceType string `yaml:"type"` - Port string `yaml:"port"` -} - -type image struct { - Repository string `yaml:"repository"` - PullPolicy string `yaml:"pullPolicy"` - Tag string `yaml:"tag"` -} - -func (hpy *HelmProductionYaml) SetAnnotations(annotations map[string]string) { - hpy.Service.Annotations = annotations -} - -func (hpy *HelmProductionYaml) SetServiceType(serviceType string) { - hpy.Service.ServiceType = serviceType -} - -func (hpy *HelmProductionYaml) GetServiceName() string { - return `{{ include "{{APPNAME}}.fullname" . }}` -} - -func (hpy *HelmProductionYaml) LoadFromFile(filePath string) error { - file, err := ioutil.ReadFile(filePath) - if err != nil { - return err - } - - return yaml.Unmarshal(file, &hpy) -} - -func (hpy *HelmProductionYaml) WriteToFile(filePath string) error { - currYaml, err := yaml.Marshal(hpy) - if err != nil { - return err - } - - return ioutil.WriteFile(filePath, currYaml, 0755) -} - -type ServiceYaml struct { - *corev1.Service -} - -func (sy *ServiceYaml) SetAnnotations(annotations map[string]string) { - sy.Annotations = annotations -} - -func (sy *ServiceYaml) SetServiceType(serviceType string) { - sy.Spec.Type = corev1.ServiceType(serviceType) -} - -func (sy *ServiceYaml) GetServiceName() string { - return sy.Name -} - -func (sy *ServiceYaml) LoadFromFile(filePath string) error { - decode := scheme.Codecs.UniversalDeserializer().Decode - file, err := ioutil.ReadFile(filePath) - k8sObj, _, err := decode(file, nil, nil) - if err != nil { - return err - } - - svc, ok := k8sObj.(*corev1.Service) - if !ok { - return errors.New("could not load file into ServiceYaml") - } - - sy.Service = svc - - return nil -} - -func (hpy *ServiceYaml) WriteToFile(filePath string) error { - printer := printers.YAMLPrinter{} - - out, err := os.OpenFile(filePath, os.O_RDWR, 0755) - if err != nil { - return nil - } - defer out.Close() - - return printer.PrintObj(hpy.Service, out) -} diff --git a/pkg/workflows/workflows.go b/pkg/workflows/workflows.go deleted file mode 100644 index a1553fb8..00000000 --- a/pkg/workflows/workflows.go +++ /dev/null @@ -1,176 +0,0 @@ -package workflows - -import ( - "errors" - "fmt" - "io/fs" - "io/ioutil" - "os" - "path" - - "gopkg.in/yaml.v3" - appsv1 "k8s.io/api/apps/v1" - "k8s.io/cli-runtime/pkg/printers" - "k8s.io/client-go/kubernetes/scheme" - - log "github.com/sirupsen/logrus" - - "github.com/Azure/draft/pkg/config" - "github.com/Azure/draft/pkg/osutil" - "github.com/Azure/draft/pkg/templatewriter" -) - -type Workflows struct { - workflows map[string]fs.DirEntry - configs map[string]*config.DraftConfig - Dest string - workflowTemplates fs.FS -} - -const ( - parentDirName = "workflows" - configFileName = "draft.yaml" - emptyDefaultFlagValue = "" -) - -func UpdateProductionDeployments(deployType, dest string, draftConfig *config.DraftConfig, templateWriter templatewriter.TemplateWriter) error { - acr, err := draftConfig.GetVariable("AZURECONTAINERREGISTRY") - if err != nil { - return fmt.Errorf("get variable: %w", err) - } - - containerName, err := draftConfig.GetVariable("CONTAINERNAME") - if err != nil { - return fmt.Errorf("get variable: %w", err) - } - - productionImage := fmt.Sprintf("%s.azurecr.io/%s", acr.Value, containerName.Value) - switch deployType { - case "helm": - return setHelmContainerImage(dest+"/charts/production.yaml", productionImage, templateWriter) - case "kustomize": - return setDeploymentContainerImage(dest+"/overlays/production/deployment.yaml", productionImage) - case "manifests": - return setDeploymentContainerImage(dest+"/manifests/deployment.yaml", productionImage) - } - return nil -} - -func setDeploymentContainerImage(filePath, productionImage string) error { - - decode := scheme.Codecs.UniversalDeserializer().Decode - file, err := ioutil.ReadFile(filePath) - if err != nil { - return err - } - - k8sObj, _, err := decode(file, nil, nil) - if err != nil { - return err - } - deploy, ok := k8sObj.(*appsv1.Deployment) - if !ok { - return errors.New("could not decode kubernetes deployment") - } - - if len(deploy.Spec.Template.Spec.Containers) != 1 { - return errors.New("unsupported number of containers defined in the deployment spec") - } - - deploy.Spec.Template.Spec.Containers[0].Image = productionImage - - printer := printers.YAMLPrinter{} - - out, err := os.OpenFile(filePath, os.O_RDWR, 0755) - if err != nil { - return nil - } - defer func() { - if err := out.Close(); err != nil { - log.Errorf("error closing file: %v", err) - } - }() - - return printer.PrintObj(deploy, out) -} - -func setHelmContainerImage(filePath, productionImage string, templateWriter templatewriter.TemplateWriter) error { - file, err := ioutil.ReadFile(filePath) - if err != nil { - return err - } - - var deploy HelmProductionYaml - err = yaml.Unmarshal(file, &deploy) - if err != nil { - return err - } - - deploy.Image.Repository = productionImage - - out, err := yaml.Marshal(deploy) - if err != nil { - return err - } - - return templateWriter.WriteFile(filePath, out) -} - -func (w *Workflows) loadConfig(deployType string) (*config.DraftConfig, error) { - val, ok := w.workflows[deployType] - if !ok { - return nil, fmt.Errorf("deploy type %s unsupported", deployType) - } - - configPath := path.Join(parentDirName, val.Name(), configFileName) - configBytes, err := fs.ReadFile(w.workflowTemplates, configPath) - if err != nil { - return nil, err - } - - var draftConfig config.DraftConfig - if err = yaml.Unmarshal(configBytes, &draftConfig); err != nil { - return nil, err - } - - return &draftConfig, nil -} - -func (w *Workflows) GetConfig(deployType string) (*config.DraftConfig, error) { - val, ok := w.configs[deployType] - if !ok { - return nil, fmt.Errorf("deploy type %s unsupported", deployType) - } - return val, nil -} - -func (w *Workflows) populateConfigs() { - for deployType := range w.workflows { - draftConfig, err := w.loadConfig(deployType) - if err != nil { - log.Debugf("no draftConfig found for workflow of deploy type %s", deployType) - draftConfig = &config.DraftConfig{} - } - w.configs[deployType] = draftConfig - } -} - -func (w *Workflows) CreateWorkflowFiles(deployType string, draftConfig *config.DraftConfig, templateWriter templatewriter.TemplateWriter) error { - val, ok := w.workflows[deployType] - if !ok { - return fmt.Errorf("deployment type: %s is not currently supported", deployType) - } - - srcDir := path.Join(parentDirName, val.Name()) - log.Debugf("source directory for workflow template: %s", srcDir) - - if err := draftConfig.ApplyDefaultVariables(); err != nil { - return fmt.Errorf("create workflow files: %w", err) - } - - if err := osutil.CopyDirWithTemplates(w.workflowTemplates, srcDir, w.Dest, draftConfig, templateWriter); err != nil { - return err - } - - return nil -} diff --git a/pkg/workflows/workflows_test.go b/pkg/workflows/workflows_test.go deleted file mode 100644 index dca4f62f..00000000 --- a/pkg/workflows/workflows_test.go +++ /dev/null @@ -1,119 +0,0 @@ -package workflows - -import ( - "errors" - "io" - "io/ioutil" - "os" - "testing" - - "github.com/Azure/draft/pkg/config" - "github.com/Azure/draft/pkg/templatewriter/writers" - - "github.com/stretchr/testify/assert" - - appsv1 "k8s.io/api/apps/v1" - "k8s.io/client-go/kubernetes/scheme" -) - -func TestUpdateProductionDeploymentsValid(t *testing.T) { - testTemplateWriter := &writers.LocalFSWriter{} - - //test for valid helm deployment file - helmFileName, _ := createTempManifest("../../test/templates/helm_prod_values.yaml") - defer os.Remove(helmFileName) - - assert.Nil(t, setHelmContainerImage(helmFileName, "testImage", testTemplateWriter)) - - helmDeploy := &HelmProductionYaml{} - assert.Nil(t, helmDeploy.LoadFromFile(helmFileName)) - assert.Equal(t, "testImage", helmDeploy.Image.Repository) - - //test for valid deployment file - deploymentFileName, _ := createTempManifest("../../test/templates/deployment.yaml") - defer os.Remove(deploymentFileName) - - assert.Nil(t, setDeploymentContainerImage(deploymentFileName, "testImage")) - decode := scheme.Codecs.UniversalDeserializer().Decode - file, err := ioutil.ReadFile(deploymentFileName) - assert.Nil(t, err) - - k8sObj, _, err := decode(file, nil, nil) - assert.Nil(t, err) - - deploy, ok := k8sObj.(*appsv1.Deployment) - assert.True(t, ok) - assert.Equal(t, "testImage", deploy.Spec.Template.Spec.Containers[0].Image) -} - -func TestUpdateProductionDeploymentsInvalid(t *testing.T) { - testTemplateWriter := &writers.LocalFSWriter{} - - //test for invalid helm deployment file - tempFile, err := ioutil.TempFile("", "*.yaml") - assert.Nil(t, err) - defer os.Remove(tempFile.Name()) - yamlData := []byte(`not a valid yaml`) - _, err = tempFile.Write(yamlData) - assert.Nil(t, err) - err = tempFile.Close() - assert.Nil(t, err) - assert.NotNil(t, setHelmContainerImage(tempFile.Name(), "testImage", testTemplateWriter)) - - //test for invalid deployment file - assert.NotNil(t, setDeploymentContainerImage(tempFile.Name(), "testImage")) - - //test for invalid k8sObj - invalidDeploymentFile, _ := createTempManifest("../../test/templates/invalid_deployment.yaml") - assert.Equal(t, errors.New("could not decode kubernetes deployment"), setDeploymentContainerImage(invalidDeploymentFile, "testImage")) - - //test for unsupported number of containers in the deployment spec - invalidDeploymentFile, _ = createTempManifest("../../test/templates/unsupported_no_of_containers.yaml") - defer os.Remove(invalidDeploymentFile) - assert.Equal(t, errors.New("unsupported number of containers defined in the deployment spec"), setDeploymentContainerImage(invalidDeploymentFile, "testImage")) -} - -func TestUpdateProductionDeploymentsMissing(t *testing.T) { - draftConfig := &config.DraftConfig{ - Variables: []*config.BuilderVar{ - { - Name: "AZURECONTAINERREGISTRY", - Value: "testRegistry", - }, - { - Name: "CONTAINERNAME", - Value: "testContainer", - }, - }, - } - testTemplateWriter := &writers.LocalFSWriter{} - //test for missing deploy type - assert.Nil(t, UpdateProductionDeployments("", ".", draftConfig, testTemplateWriter)) - - //test for missing helm deployment file - assert.NotNil(t, setHelmContainerImage("", "testImage", testTemplateWriter)) - - //test for missing deployment file - assert.NotNil(t, setDeploymentContainerImage("", "testImage")) -} - -func createTempManifest(path string) (string, error) { - file, err := ioutil.TempFile("", "*.yaml") - if err != nil { - return "", err - } - defer file.Close() - - var source *os.File - source, err = os.Open(path) - if err != nil { - return "", err - } - defer source.Close() - - _, err = io.Copy(file, source) - if err != nil { - return "", err - } - return file.Name(), nil -}