From c80c97aeec2b38b5c6adf9efb492053e5359b38c Mon Sep 17 00:00:00 2001 From: eborders Date: Wed, 4 May 2022 11:42:00 -0400 Subject: [PATCH 01/23] test create cmd - detecting languages --- cmd/create_test.go | 97 ++++++++++++++++++++++++++++++++++ pkg/deployments/deployments.go | 4 +- pkg/languages/languages.go | 2 +- 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 cmd/create_test.go diff --git a/cmd/create_test.go b/cmd/create_test.go new file mode 100644 index 00000000..8869eded --- /dev/null +++ b/cmd/create_test.go @@ -0,0 +1,97 @@ +package cmd + +import ( + "fmt" + "strings" + "testing" + + "github.com/Azure/draft/pkg/config" + "github.com/Azure/draft/pkg/filematches" + "github.com/Azure/draft/pkg/languages" + "github.com/Azure/draft/pkg/linguist" + "github.com/stretchr/testify/assert" + log "github.com/sirupsen/logrus" +) + +type mockCreateCmd struct { + appName string + lang string + dest string + + dockerfileOnly bool + deploymentOnly bool + + createConfigPath string + createConfig *config.CreateConfig + + supportedLangs *languages.Languages + fileMatches *filematches.FileMatches +} + +func TestRun(t *testing.T) { + mockCC := &mockCreateCmd{} + mockCC.createConfig = &config.CreateConfig{} + mockCC.dest = "./.." + + detectedLang, lowerLang, err := mockCC.mockDetectLanguage() + + assert.False(t, detectedLang == nil) + assert.False(t, lowerLang == "") + assert.True(t, err == nil) +} + +func (mcc *mockCreateCmd) mockDetectLanguage() (*config.DraftConfig, string, error) { + hasGo := false + hasGoMod := false + var langs []*linguist.Language + var err error + + if mcc.createConfig.LanguageType == "" { + langs, err = linguist.ProcessDir(mcc.dest) + log.Debugf("linguist.ProcessDir(%v) result:\n\nError: %v", mcc.dest, err) + if err != nil { + return nil, "", fmt.Errorf("there was an error detecting the language: %s", err) + } + + for _, lang := range langs { + log.Debugf("%s:\t%f (%s)", lang.Language, lang.Percent, lang.Color) + } + + log.Debugf("detected %d langs", len(langs)) + + if len(langs) == 0 { + return nil, "", ErrNoLanguageDetected + } + } + + mcc.supportedLangs = languages.CreateLanguages(mcc.dest) + + if mcc.createConfig.LanguageType != "" { + log.Debug("using configuration language") + lowerLang := strings.ToLower(mcc.createConfig.LanguageType) + langConfig := mcc.supportedLangs.GetConfig(lowerLang) + if langConfig == nil { + return nil, "", ErrNoLanguageDetected + } + + return langConfig, lowerLang, nil + } + + for _, lang := range langs { + detectedLang := linguist.Alias(lang) + log.Infof("--> Draft detected %s (%f%%)\n", detectedLang.Language, detectedLang.Percent) + lowerLang := strings.ToLower(detectedLang.Language) + + if mcc.supportedLangs.ContainsLanguage(lowerLang) { + if lowerLang == "go" && hasGo && hasGoMod { + log.Debug("detected go and go module") + lowerLang = "gomodule" + } + + langConfig := mcc.supportedLangs.GetConfig(lowerLang) + return langConfig, lowerLang, nil + } + log.Infof("--> Could not find a pack for %s. Trying to find the next likely language match...\n", detectedLang.Language) + } + return nil, "", ErrNoLanguageDetected +} \ No newline at end of file diff --git a/pkg/deployments/deployments.go b/pkg/deployments/deployments.go index 63e0588a..37471c1c 100644 --- a/pkg/deployments/deployments.go +++ b/pkg/deployments/deployments.go @@ -74,8 +74,8 @@ func (d *Deployments) loadConfig(lang string) (*config.DraftConfig, error) { return &draftConfig, nil } -func (d *Deployments) GetConfig(deployTyoe string) *config.DraftConfig { - val, ok := d.configs[deployTyoe] +func (d *Deployments) GetConfig(deployType string) *config.DraftConfig { + val, ok := d.configs[deployType] if !ok { return nil } diff --git a/pkg/languages/languages.go b/pkg/languages/languages.go index 6c4ba39b..fa24500c 100644 --- a/pkg/languages/languages.go +++ b/pkg/languages/languages.go @@ -64,7 +64,7 @@ func (l *Languages) loadConfig(lang string) (*config.DraftConfig, error) { return nil, err } - viper.SetConfigFile("yaml") + viper.SetConfigType("yaml") if err = viper.ReadConfig(bytes.NewBuffer(configBytes)); err != nil { return nil, err } From 250ca299b2e8e3e1feb0fde03f7dcbe2fdb38ed3 Mon Sep 17 00:00:00 2001 From: Israel Miller Date: Wed, 4 May 2022 14:22:21 -0400 Subject: [PATCH 02/23] Ismille/integrations (#74) * updating integration tests * updating integration tests * fixing kustomize * fixing tag * fixing kustomize * fixing kustomize * adding more debug log * fixing output for pod name * adding more logging * adding more logging * adding more logging * fixing kustomize --- .github/workflows/integration-linux.yml | 330 +++++++++++++++--- deployTypes/kustomize/base/deployment.yaml | 12 +- deployTypes/kustomize/base/service.yaml | 4 +- .../overlays/production/deployment.yaml | 6 +- .../overlays/production/service.yaml | 2 +- test/gen_integration.sh | 34 +- 6 files changed, 314 insertions(+), 74 deletions(-) diff --git a/.github/workflows/integration-linux.yml b/.github/workflows/integration-linux.yml index d9f647ea..0b314829 100644 --- a/.github/workflows/integration-linux.yml +++ b/.github/workflows/integration-linux.yml @@ -65,7 +65,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -88,7 +88,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -111,9 +111,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 gomodule-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -136,14 +144,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -154,6 +162,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -169,7 +179,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -180,12 +190,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 gomodule-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -274,7 +294,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -297,7 +317,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -320,9 +340,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 python-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -345,14 +373,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -363,6 +391,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -378,7 +408,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -389,12 +419,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 python-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -483,7 +523,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -506,7 +546,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -529,9 +569,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 rust-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -554,14 +602,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -572,6 +620,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -587,7 +637,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -598,12 +648,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 rust-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -692,7 +752,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -715,7 +775,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -738,9 +798,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 javascript-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -763,14 +831,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -781,6 +849,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -796,7 +866,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -807,12 +877,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 javascript-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -901,7 +981,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -924,7 +1004,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -947,9 +1027,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 ruby-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -972,14 +1060,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -990,6 +1078,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -1005,7 +1095,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1016,12 +1106,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 ruby-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1110,7 +1210,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1133,7 +1233,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1156,9 +1256,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 csharp-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1181,14 +1289,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1199,6 +1307,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -1214,7 +1324,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1225,12 +1335,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 csharp-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1319,7 +1439,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1342,7 +1462,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1365,9 +1485,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 java-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1390,14 +1518,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1408,6 +1536,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -1423,7 +1553,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1434,12 +1564,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 java-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1528,7 +1668,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1551,7 +1691,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1574,9 +1714,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 gradle-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1599,14 +1747,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1617,6 +1765,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -1632,7 +1782,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1643,12 +1793,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 gradle-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1737,7 +1897,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1760,7 +1920,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1783,9 +1943,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 swift-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1808,14 +1976,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1826,6 +1994,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -1841,7 +2011,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -1852,12 +2022,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 swift-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -1946,7 +2126,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1969,7 +2149,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -1992,9 +2172,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 erlang-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -2017,14 +2205,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -2035,6 +2223,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -2050,7 +2240,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -2061,12 +2251,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 erlang-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -2155,7 +2355,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -2178,7 +2378,7 @@ jobs: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -2201,9 +2401,17 @@ jobs: - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 clojure-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -2226,14 +2434,14 @@ jobs: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -2244,6 +2452,8 @@ jobs: with: action: deploy manifests: ${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -2259,7 +2469,7 @@ jobs: run: | export SHELL=/bin/bash eval $(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n verifying images: docker images # Deploys application based on manifest files from previous step @@ -2270,12 +2480,22 @@ jobs: with: action: deploy manifests: ${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6 clojure-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 diff --git a/deployTypes/kustomize/base/deployment.yaml b/deployTypes/kustomize/base/deployment.yaml index 65b45fa3..1f5c1be6 100644 --- a/deployTypes/kustomize/base/deployment.yaml +++ b/deployTypes/kustomize/base/deployment.yaml @@ -1,21 +1,21 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: my-app + name: {{APPNAME}} labels: - app: my-app + app: {{APPNAME}} spec: replicas: 1 selector: matchLabels: - app: my-app + app: {{APPNAME}} template: metadata: labels: - app: my-app + app: {{APPNAME}} spec: containers: - - name: my-app - image: my-app + - name: {{APPNAME}} + image: {{APPNAME}}:latest ports: - containerPort: {{PORT}} \ No newline at end of file diff --git a/deployTypes/kustomize/base/service.yaml b/deployTypes/kustomize/base/service.yaml index b832101c..bdd16ac8 100644 --- a/deployTypes/kustomize/base/service.yaml +++ b/deployTypes/kustomize/base/service.yaml @@ -1,11 +1,11 @@ apiVersion: v1 kind: Service metadata: - name: my-app + name: {{APPNAME}} spec: type: LoadBalancer selector: - app: my-app + app: {{APPNAME}} ports: - protocol: TCP port: 80 diff --git a/deployTypes/kustomize/overlays/production/deployment.yaml b/deployTypes/kustomize/overlays/production/deployment.yaml index df77c98a..d052245f 100644 --- a/deployTypes/kustomize/overlays/production/deployment.yaml +++ b/deployTypes/kustomize/overlays/production/deployment.yaml @@ -1,12 +1,12 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: my-app + name: {{APPNAME}} labels: - app: my-app + app: {{APPNAME}} spec: template: spec: containers: - - name: my-app + - name: {{APPNAME}} image: {{APPNAME}} \ No newline at end of file diff --git a/deployTypes/kustomize/overlays/production/service.yaml b/deployTypes/kustomize/overlays/production/service.yaml index 841378cc..2a0c1d16 100644 --- a/deployTypes/kustomize/overlays/production/service.yaml +++ b/deployTypes/kustomize/overlays/production/service.yaml @@ -1,6 +1,6 @@ apiVersion: v1 kind: Service metadata: - name: my-app + name: {{APPNAME}} spec: type: LoadBalancer diff --git a/test/gen_integration.sh b/test/gen_integration.sh index d6bce54a..af9d6d93 100755 --- a/test/gen_integration.sh +++ b/test/gen_integration.sh @@ -158,7 +158,7 @@ languageVariables: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -181,7 +181,7 @@ languageVariables: with: renderEngine: 'helm' helmChart: ./langtest/charts - overrideFiles: ./langtest/charts/production.yaml + overrideFiles: ./langtest/charts/values.yaml overrides: | replicas:2 helm-version: 'latest' @@ -203,12 +203,20 @@ languageVariables: manifests: \${{ steps.bake2.outputs.manifestsBundle }} - name: Check default namespace if: steps.deploy2.outcome != 'success' - run: kubectl get po" >> ../.github/workflows/integration-linux.yml + run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6" >> ../.github/workflows/integration-linux.yml # create kustomize workflow echo " $lang-kustomize: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 @@ -231,14 +239,14 @@ languageVariables: uses: azure/k8s-bake@v2.1 with: renderEngine: 'kustomize' - kustomizationPath: ./langtest/overlays/production + kustomizationPath: ./langtest/base kubectl-version: 'latest' id: bake - name: Build image run: | export SHELL=/bin/bash eval \$(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n "verifying images:" docker images # Deploys application based on manifest files from previous step @@ -249,6 +257,8 @@ languageVariables: with: action: deploy manifests: \${{ steps.bake.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy.outcome != 'success' run: kubectl get po @@ -264,7 +274,7 @@ languageVariables: run: | export SHELL=/bin/bash eval \$(minikube -p minikube docker-env) - docker build -f ./langtest/Dockerfile -t testapp ./langtest/ + docker build -f ./langtest/Dockerfile -t testapp:curr ./langtest/ echo -n "verifying images:" docker images # Deploys application based on manifest files from previous step @@ -275,14 +285,24 @@ languageVariables: with: action: deploy manifests: \${{ steps.bake2.outputs.manifestsBundle }} + images: | + testapp:curr - name: Check default namespace if: steps.deploy2.outcome != 'success' - run: kubectl get po" >> ../.github/workflows/integration-linux.yml + run: kubectl get po + - name: Fail if any error + if: steps.deploy2.outcome != 'success' || steps.deploy.outcome != 'success' + run: exit 6" >> ../.github/workflows/integration-linux.yml # create manifests workflow echo " $lang-manifests: runs-on: ubuntu-latest + services: + registry: + image: registry:2 + ports: + - 5000:5000 needs: build steps: - uses: actions/checkout@v2 From a68135d515b112f4dc7638f939bda1d0f65f23bf Mon Sep 17 00:00:00 2001 From: Karen Chen Date: Wed, 4 May 2022 13:41:05 -0700 Subject: [PATCH 03/23] Add readme (#77) * updated readme * small changes --- README.md | 180 +++--------------------------------------------------- 1 file changed, 10 insertions(+), 170 deletions(-) diff --git a/README.md b/README.md index 47e2d419..3d9f420b 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,14 @@
- - - - -[![Contributors][contributors-shield]][contributors-url] -[![Forks][forks-shield]][forks-url] -[![Stargazers][stars-shield]][stars-url] -[![Issues][issues-shield]][issues-url] -[![MIT License][license-shield]][license-url] -[![LinkedIn][linkedin-shield]][linkedin-url] - - - -
- - Logo - - -

draft

- +

Draft

- A tool to help developers hit the ground running with k8s + A tool to help developers hit the ground running with Kubernetes.
Explore the docs »

- View Demo - · Report Bug · Request Feature @@ -43,60 +16,17 @@

- - -
- Table of Contents -
    -
  1. - About The Project - -
  2. -
  3. - Getting Started - -
  4. -
  5. Usage
  6. -
  7. Roadmap
  8. -
  9. Contributing
  10. -
  11. License
  12. -
  13. Contact
  14. -
  15. Acknowledgments
  16. -
-
- - - ## About The Project -[![Draft Screen Shot][product-screenshot]](https://example.com) - -draft aims to simplify starting out with k8s. draft will create both a working Dockerfile for your application and create the necessary kubernetes manifests needed to hit the ground running with tools like Skaffold. -

(back to top)

- - +Draft makes it easier for developers to get started building apps that run on Kubernetes by taking a non-containerized application and generating the Dockerfiles, Kubernetes manifests, Helm charts, Kustomize configuration, and other artifacts associated with a containerized application. Draft can also generate a GitHub Action workflow file to quickly build and deploy applications onto any Kubernetes cluster. -### Built With +* `draft create` adds the minimum required files for your deployment to the project directory. +* `draft setup-gh` automates the Github OIDC setup process for your project. +* `draft generate-workflow` generates a Github workflow for automatic build and deploy to AKS. +* `draft update` automatically updates your application to be internet accessible. -* [Go](https://go.dev/) -* [Draft](https://github.com/Azure/draft/) - -

(back to top)

- - - - -## Getting Started - -### Prerequisites - -draft requires Go version 1.17.x. +Draft requires Go version 1.18.x. * Go ```sh go version @@ -113,101 +43,11 @@ draft requires Go version 1.17.x. make ``` 3. Add the binary to your path - ```js + ```sh mv draft $GOPATH/bin/ ``` -

(back to top)

- - - - -## Usage - -Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources. - -_For more examples, please refer to the [Documentation](https://example.com)_ - -

(back to top)

- - - - -## Roadmap - -- [] Feature 1 -- [] Feature 2 -- [] Feature 3 - - [] Nested Feature - -See the [open issues](https://github.com/Azure/draft/issues) for a full list of proposed features (and known issues). - -

(back to top)

- - - - -## Contributing - -Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. - -If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". -Don't forget to give the project a star! Thanks again! - -1. Fork the Project -2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) -3. Regenerate Integration Tests by running `./test/gen_integration.sh` -4. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) -5. Push to the Branch (`git push origin feature/AmazingFeature`) -6. Open a Pull Request - -

(back to top)

- - - - ## License -Distributed under the MIT License. See `LICENSE.txt` for more information. - -

(back to top)

- - - - -## Contact - -Your Name - [@twitter_handle](https://twitter.com/twitter_handle) - email@email_client.com - -Project Link: [https://github.com/Azure/draft](https://github.com/Azure/draft) - -

(back to top)

- - - - -## Acknowledgments - -* []() -* []() -* []() - -

(back to top)

- - +Distributed under the MIT License. See [LISENCE](https://github.com/Azure/draft/blob/main/LICENSE) for more information. - - -[contributors-shield]: https://img.shields.io/github/contributors/Azure/draft.svg?style=for-the-badge -[contributors-url]: https://github.com/Azure/draft/graphs/contributors -[forks-shield]: https://img.shields.io/github/forks/Azure/draft.svg?style=for-the-badge -[forks-url]: https://github.com/Azure/draft/network/members -[stars-shield]: https://img.shields.io/github/stars/Azure/draft.svg?style=for-the-badge -[stars-url]: https://github.com/Azure/draft/stargazers -[issues-shield]: https://img.shields.io/github/issues/Azure/draft.svg?style=for-the-badge -[issues-url]: https://github.com/Azure/draft/issues -[license-shield]: https://img.shields.io/github/license/Azure/draft.svg?style=for-the-badge -[license-url]: https://github.com/Azure/draft/blob/master/LICENSE.txt -[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555 -[linkedin-url]: https://linkedin.com/in/linkedin_username -[product-screenshot]: images/screenshot.png \ No newline at end of file From 9d203e18c8fa89c34f06599bd26c69d1f2b6f255 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 12:26:50 -0400 Subject: [PATCH 04/23] unit test for create command --- cmd/create_test.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/cmd/create_test.go b/cmd/create_test.go index 8869eded..16652f2a 100644 --- a/cmd/create_test.go +++ b/cmd/create_test.go @@ -6,41 +6,40 @@ import ( "testing" "github.com/Azure/draft/pkg/config" - "github.com/Azure/draft/pkg/filematches" + //"github.com/Azure/draft/pkg/filematches" "github.com/Azure/draft/pkg/languages" "github.com/Azure/draft/pkg/linguist" "github.com/stretchr/testify/assert" log "github.com/sirupsen/logrus" ) -type mockCreateCmd struct { - appName string - lang string - dest string - - dockerfileOnly bool - deploymentOnly bool - - createConfigPath string - createConfig *config.CreateConfig - - supportedLangs *languages.Languages - fileMatches *filematches.FileMatches -} - func TestRun(t *testing.T) { - mockCC := &mockCreateCmd{} + mockCC := &createCmd{} mockCC.createConfig = &config.CreateConfig{} mockCC.dest = "./.." + mockCC.createConfig.DeployType = "helm" + mockCC.createConfig.LanguageVariables = []config.UserInputs{} + mockCC.createConfig.DeployVariables = []config.UserInputs{} + mockPortInput := config.UserInputs{Name: "PORT", Value: "8080"} + mockAppNameInput := config.UserInputs{Name: "APPNAME", Value: "testingCreateCommand"} + mockCC.createConfig.DeployVariables = append(mockCC.createConfig.DeployVariables, mockPortInput, mockAppNameInput) + mockCC.createConfig.LanguageVariables = append(mockCC.createConfig.LanguageVariables, mockPortInput) detectedLang, lowerLang, err := mockCC.mockDetectLanguage() assert.False(t, detectedLang == nil) assert.False(t, lowerLang == "") assert.True(t, err == nil) + + err = mockCC.generateDockerfile(detectedLang, lowerLang) + assert.True(t, err == nil) + + + err = mockCC.createDeployment() + assert.True(t, err == nil) } -func (mcc *mockCreateCmd) mockDetectLanguage() (*config.DraftConfig, string, error) { +func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error) { hasGo := false hasGoMod := false var langs []*linguist.Language From 21284118d3324795c5d5a6c160f116b25b89b264 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:36:07 -0400 Subject: [PATCH 05/23] increasing create cmd test coverage --- cmd/create.go | 2 +- cmd/create_test.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 5c74f3ff..b644c0ec 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -71,7 +71,7 @@ func (cc *createCmd) initConfig() error { return err } - viper.SetConfigFile("yaml") + viper.SetConfigType("yaml") if err = viper.ReadConfig(bytes.NewBuffer(configBytes)); err != nil { return err } diff --git a/cmd/create_test.go b/cmd/create_test.go index 16652f2a..d671fff0 100644 --- a/cmd/create_test.go +++ b/cmd/create_test.go @@ -5,12 +5,13 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" + log "github.com/sirupsen/logrus" + "github.com/Azure/draft/pkg/config" //"github.com/Azure/draft/pkg/filematches" "github.com/Azure/draft/pkg/languages" "github.com/Azure/draft/pkg/linguist" - "github.com/stretchr/testify/assert" - log "github.com/sirupsen/logrus" ) func TestRun(t *testing.T) { @@ -39,6 +40,17 @@ func TestRun(t *testing.T) { assert.True(t, err == nil) } +func TestInitConfig(t *testing.T) { + mockCC := &createCmd{} + mockCC.createConfig = &config.CreateConfig{} + mockCC.dest = "./.." + mockCC.createConfigPath = "./../test/templates/config.yaml" + + err := mockCC.initConfig() + assert.True(t, err == nil) + assert.True(t, mockCC.createConfig != nil) +} + func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error) { hasGo := false hasGoMod := false From 7914e28ce2208f48813f10fb8fea7af2059c1999 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:36:55 -0400 Subject: [PATCH 06/23] upping cmd package test coverage overall --- cmd/root_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 cmd/root_test.go diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 00000000..95cd87e3 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,16 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRootInitConfig(t *testing.T) { + initConfig() + + cfgFile := "./../test/templates/config.yaml" + initConfig() + + assert.True(t, cfgFile != "") +} \ No newline at end of file From a64bb5cf011cbd456c10672e95ef0d87ef234480 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:39:44 -0400 Subject: [PATCH 07/23] testing service update function --- pkg/web/web_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/web/web_test.go b/pkg/web/web_test.go index b58b216e..45e40bf8 100644 --- a/pkg/web/web_test.go +++ b/pkg/web/web_test.go @@ -8,6 +8,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/Azure/draft/pkg/osutil" ) func createTempManifest(path string) (*os.File, error) { @@ -54,6 +56,27 @@ func TestAddAnnotationsKustomize(t *testing.T) { assert.Equal(t, annotations, eKustomizeYaml.Annotations) } +func TestUpdateServiceFile(t *testing.T) { + tempDest := "./../.." + tempFile := tempDest + "/manifests/service.yaml" + mockSa := &ServiceAnnotations{Host: "mockHost", Cert: "mockCert"} + + osutil.EnsureDirectory(tempDest + "/manifests") + defer os.Remove(tempDest + "/manifests") + osutil.EnsureFile(tempFile) + defer os.Remove(tempFile) + + contents, err := ioutil.ReadFile("../../test/templates/service_w_annotations.yaml") + assert.Nil(t, err) + ioutil.WriteFile(tempFile, contents, 0644) + + err = UpdateServiceFile(mockSa, tempDest) + assert.Nil(t, err) + newContents, _ := ioutil.ReadFile(tempFile) + + assert.NotEqual(t, contents, newContents) +} + func TestAddAnnotationsHelm(t *testing.T) { annotations := map[string]string{ "kubernetes.azure.com/ingress-host": "test.SA", @@ -82,3 +105,4 @@ func TestAddAnnotationsHelm(t *testing.T) { assert.NotNil(t, eHelmYaml.Service.Annotations) assert.Equal(t, annotations, eHelmYaml.Service.Annotations) } + From 225d3bb96f604e6b43ce3dba064b76d70b0d0583 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:41:10 -0400 Subject: [PATCH 08/23] testing ensure dir/file --- pkg/osutil/osutil_test.go | 47 +++++++++++++++++++++++++++++++++ test/templates/ensure_file.yaml | 0 2 files changed, 47 insertions(+) create mode 100644 test/templates/ensure_file.yaml diff --git a/pkg/osutil/osutil_test.go b/pkg/osutil/osutil_test.go index 6a4193fc..979af679 100644 --- a/pkg/osutil/osutil_test.go +++ b/pkg/osutil/osutil_test.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "testing" + + "github.com/stretchr/testify/assert" ) func TestExists(t *testing.T) { @@ -57,3 +59,48 @@ func TestSymlinkWithFallback(t *testing.T) { t.Errorf("expected no error when calling SymlinkWithFallback() on a file that exists, got %v", err) } } + +func TestEnsureDir(t *testing.T) { + validDir := "./../../test/templates" + assert.DirExists(t, validDir) + + err := EnsureDirectory(validDir) + assert.Nil(t, err) + + invalidDir := "./../../test/EnsureDirTest" + err = EnsureDirectory(invalidDir) + + assert.Nil(t, err) + assert.DirExists(t, invalidDir) + + os.Remove(invalidDir) +} + +func TestEnsureFile(t *testing.T) { + validFile := "./../../test/templates/ensure_file.yaml" + assert.FileExists(t, validFile) + + err := EnsureFile(validFile) + assert.Nil(t, err) + + invalidFile := "./../../test/templates/ensure_file_create.yaml" + err = EnsureFile(invalidFile) + + assert.Nil(t, err) + assert.FileExists(t, invalidFile) + + os.Remove(invalidFile) +} + +// func TestCopyDir(t *testing.T) { +// //go:generate cp -r ../../deployTypes ./deployTypes +// //go:embed all:deployTypes +// var mockDeployTypes embed.FS +// mockSrc := "deployTypes/kustomize" +// mockDest := "." +// mockConfig := &config.DraftConfig{} +// mockInputs := map[string]string{"PORT": "8080"} + +// err := CopyDir(mockDeployTypes, mockSrc, mockDest, mockConfig, mockInputs) +// assert.Nil(t, err) +// } \ No newline at end of file diff --git a/test/templates/ensure_file.yaml b/test/templates/ensure_file.yaml new file mode 100644 index 00000000..e69de29b From 6bb6f26c22e128bcf628bf4f5b8e1c948c2dbad7 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:42:15 -0400 Subject: [PATCH 09/23] increasing setup coverage --- cmd/setup-gh_test.go | 24 ++++++++++++++++++++++++ pkg/providers/providersutils_test.go | 11 +++++++++++ 2 files changed, 35 insertions(+) create mode 100644 cmd/setup-gh_test.go diff --git a/cmd/setup-gh_test.go b/cmd/setup-gh_test.go new file mode 100644 index 00000000..05824352 --- /dev/null +++ b/cmd/setup-gh_test.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/Azure/draft/pkg/providers" +) + +func TestSetUpConfig(t *testing.T) { + mockSetUpCmd := &providers.SetUpCmd{} + mockSetUpCmd.AppName = "testingSetUpCommand" + mockSetUpCmd.Provider = "Google" + mockSetUpCmd.Repo = "test/repo" + mockSetUpCmd.ResourceGroupName = "testResourceGroup" + mockSetUpCmd.SubscriptionID = "123456789" + + fillSetUpConfig(mockSetUpCmd) + + err := runProviderSetUp(mockSetUpCmd) + + assert.True(t, err == nil) +} \ No newline at end of file diff --git a/pkg/providers/providersutils_test.go b/pkg/providers/providersutils_test.go index 1afa4514..1e4fd78a 100644 --- a/pkg/providers/providersutils_test.go +++ b/pkg/providers/providersutils_test.go @@ -12,4 +12,15 @@ func TestLoggedInToAz(t *testing.T) { func TestLoggedInToGh(t *testing.T) { assert.False(t, IsLoggedInToGh(), "Github is returning logged in even when logged out") +} + +func TestCheckAzCliInstalled(t *testing.T) { + var err error + CheckAzCliInstalled() + + assert.Nil(t, err) +} + +func TestHasGhCli(t *testing.T) { + assert.True(t, HasGhCli(), "Github CLI is not installed") } \ No newline at end of file From c95d982d6680815a45210df1c1d3b99b1c8686b3 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:45:22 -0400 Subject: [PATCH 10/23] template config for create and root cmds --- test/templates/config.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/templates/config.yaml diff --git a/test/templates/config.yaml b/test/templates/config.yaml new file mode 100644 index 00000000..50c86eba --- /dev/null +++ b/test/templates/config.yaml @@ -0,0 +1,8 @@ +DeployType: "kustomize" +LanguageType: "go" +DeployVariables: + - Name: "PORT" + Value: "8080" +LanguageVariables: + - Name: "PORT" + Value: "8080" \ No newline at end of file From cce862462578c1d54161d9f37c96a1c2b94b349a Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 17:58:19 -0400 Subject: [PATCH 11/23] cleaning up after my tests --- cmd/create_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/create_test.go b/cmd/create_test.go index d671fff0..821ea3ab 100644 --- a/cmd/create_test.go +++ b/cmd/create_test.go @@ -2,11 +2,13 @@ package cmd import ( "fmt" + "io/ioutil" + "os" "strings" "testing" - "github.com/stretchr/testify/assert" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" "github.com/Azure/draft/pkg/config" //"github.com/Azure/draft/pkg/filematches" @@ -26,6 +28,9 @@ func TestRun(t *testing.T) { mockCC.createConfig.DeployVariables = append(mockCC.createConfig.DeployVariables, mockPortInput, mockAppNameInput) mockCC.createConfig.LanguageVariables = append(mockCC.createConfig.LanguageVariables, mockPortInput) + oldDockerfile, _ := ioutil.ReadFile("./../Dockerfile") + oldDockerignore, _ := ioutil.ReadFile("./../.dockerignore") + detectedLang, lowerLang, err := mockCC.mockDetectLanguage() assert.False(t, detectedLang == nil) @@ -38,6 +43,17 @@ func TestRun(t *testing.T) { err = mockCC.createDeployment() assert.True(t, err == nil) + err = ioutil.WriteFile("./../Dockerfile", oldDockerfile, 0644) + if err != nil { + t.Error(err) + } + + err = ioutil.WriteFile("./../.dockerignore", oldDockerignore, 0644) + if err != nil { + t.Error(err) + } + + os.RemoveAll("./../charts") } func TestInitConfig(t *testing.T) { From 721ca03a32a0158bdff3a55ff9d5cb9c8e0aad90 Mon Sep 17 00:00:00 2001 From: eborders Date: Mon, 9 May 2022 18:01:08 -0400 Subject: [PATCH 12/23] cleaning up comments --- pkg/osutil/osutil_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/pkg/osutil/osutil_test.go b/pkg/osutil/osutil_test.go index 979af679..5886fe8c 100644 --- a/pkg/osutil/osutil_test.go +++ b/pkg/osutil/osutil_test.go @@ -91,16 +91,3 @@ func TestEnsureFile(t *testing.T) { os.Remove(invalidFile) } - -// func TestCopyDir(t *testing.T) { -// //go:generate cp -r ../../deployTypes ./deployTypes -// //go:embed all:deployTypes -// var mockDeployTypes embed.FS -// mockSrc := "deployTypes/kustomize" -// mockDest := "." -// mockConfig := &config.DraftConfig{} -// mockInputs := map[string]string{"PORT": "8080"} - -// err := CopyDir(mockDeployTypes, mockSrc, mockDest, mockConfig, mockInputs) -// assert.Nil(t, err) -// } \ No newline at end of file From d9aafb704377b4432e765b05dbc8bd25f7daf81c Mon Sep 17 00:00:00 2001 From: eborders Date: Tue, 10 May 2022 15:26:41 -0400 Subject: [PATCH 13/23] removing extra slash --- pkg/osutil/osutil.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index a24e6fa4..682756e5 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -88,7 +88,7 @@ func CopyDir( } srcPath := src + "/" + f.Name() - destPath := dest + "/" + f.Name() + destPath := dest + f.Name() if f.IsDir() { if err = EnsureDirectory(destPath); err != nil { @@ -113,7 +113,7 @@ func CopyDir( fileName := f.Name() if config != nil { - log.Debugf("checking name override for srcPath: %s, destPath: %s, destPrefix: %s/", + log.Debugf("checking name override for srcPath: %s, destPath: %s, destPrefix: %s", srcPath, destPath, dest) if prefix := config.GetNameOverride(fileName); prefix != "" { log.Debugf("overriding file: %s with prefix: %s", destPath, prefix) From 26a98b7cd56012400a65877e5037c8ce685a4fe3 Mon Sep 17 00:00:00 2001 From: eborders Date: Wed, 11 May 2022 16:13:14 -0400 Subject: [PATCH 14/23] removing extra slash --- pkg/workflows/workflows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/workflows/workflows.go b/pkg/workflows/workflows.go index 2478fb11..10b2c384 100644 --- a/pkg/workflows/workflows.go +++ b/pkg/workflows/workflows.go @@ -68,7 +68,7 @@ func updateProductionDeployments(deployType, dest string, config *WorkflowConfig case "helm": return setHelmContainerImage(dest+"/charts/production.yaml", productionImage) case "kustomize": - return setDeploymentContainerImage(dest+"/overlays/production/deployment.yaml", productionImage) + return setDeploymentContainerImage(dest+"overlays/production/deployment.yaml", productionImage) } return nil } From a9df0f2c1578839496aa7b046d41bd102db66983 Mon Sep 17 00:00:00 2001 From: eborders Date: Wed, 11 May 2022 16:17:47 -0400 Subject: [PATCH 15/23] removing comment --- cmd/create_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/create_test.go b/cmd/create_test.go index 821ea3ab..5e6a08de 100644 --- a/cmd/create_test.go +++ b/cmd/create_test.go @@ -11,7 +11,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/Azure/draft/pkg/config" - //"github.com/Azure/draft/pkg/filematches" "github.com/Azure/draft/pkg/languages" "github.com/Azure/draft/pkg/linguist" ) From 002d0e20d5dc6df9936b6414a330ca194e317772 Mon Sep 17 00:00:00 2001 From: eborders Date: Wed, 11 May 2022 17:03:14 -0400 Subject: [PATCH 16/23] adding slash test --- pkg/osutil/osutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index 682756e5..150b3791 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -113,7 +113,7 @@ func CopyDir( fileName := f.Name() if config != nil { - log.Debugf("checking name override for srcPath: %s, destPath: %s, destPrefix: %s", + log.Debugf("checking name override for srcPath: %s, destPath: %s, destPrefix: %s/", srcPath, destPath, dest) if prefix := config.GetNameOverride(fileName); prefix != "" { log.Debugf("overriding file: %s with prefix: %s", destPath, prefix) From 568a71eaf0ee740146c62c8773a059ae235478f6 Mon Sep 17 00:00:00 2001 From: eborders Date: Wed, 11 May 2022 17:15:34 -0400 Subject: [PATCH 17/23] readding slash --- pkg/osutil/osutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index 150b3791..a24e6fa4 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -88,7 +88,7 @@ func CopyDir( } srcPath := src + "/" + f.Name() - destPath := dest + f.Name() + destPath := dest + "/" + f.Name() if f.IsDir() { if err = EnsureDirectory(destPath); err != nil { From 2406669d876b4bb7d99bd53f87754accc3d7dd9b Mon Sep 17 00:00:00 2001 From: Israel Miller Date: Wed, 11 May 2022 21:59:53 -0400 Subject: [PATCH 18/23] adding required open source documents (#78) --- CODE_OF_CONDUCT.md | 9 +++++++++ CONTRIBUTING.md | 14 ++++++++++++++ README.md | 3 +++ SECURITY.md | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 SECURITY.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..6257f2e7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,9 @@ +# Microsoft Open Source Code of Conduct + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). + +Resources: + +- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) +- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) +- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..aab38c64 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,14 @@ +Contributing + +This project welcomes contributions and suggestions. Most contributions require you to +agree to a Contributor License Agreement (CLA) declaring that you have the right to, +and actually do, grant us the rights to use your contribution. For details, visit +https://cla.microsoft.com. + +When you submit a pull request, a CLA-bot will automatically determine whether you need +to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the +instructions provided by the bot. You will only need to do this once across all repositories using our CLA. + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) +or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. \ No newline at end of file diff --git a/README.md b/README.md index 3d9f420b..4e5ebaad 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,6 @@ Draft requires Go version 1.18.x. Distributed under the MIT License. See [LISENCE](https://github.com/Azure/draft/blob/main/LICENSE) for more information. +## Trademark Notice +Trademarks This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies. + diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000..46bf9ec0 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,41 @@ + + +## Security + +Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). + +If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc751383(v=technet.10)), please report it to us as described below. + +## Reporting Security Issues + +**Please do not report security vulnerabilities through public GitHub issues.** + +Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://msrc.microsoft.com/create-report). + +If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc). + +You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc). + +Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: + +* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) +* Full paths of source file(s) related to the manifestation of the issue +* The location of the affected source code (tag/branch/commit or direct URL) +* Any special configuration required to reproduce the issue +* Step-by-step instructions to reproduce the issue +* Proof-of-concept or exploit code (if possible) +* Impact of the issue, including how an attacker might exploit the issue + +This information will help us triage your report more quickly. + +If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://microsoft.com/msrc/bounty) page for more details about our active programs. + +## Preferred Languages + +We prefer all communications to be in English. + +## Policy + +Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd). + + \ No newline at end of file From 61327e529e2aba9c298e5a896fd5fe639da0cb6c Mon Sep 17 00:00:00 2001 From: eborders Date: Thu, 12 May 2022 17:42:05 -0400 Subject: [PATCH 19/23] stopping spinner for gh login then restarting --- cmd/setup-gh.go | 2 +- pkg/providers/azure.go | 13 +++++++++++++ pkg/spinner/spinner.go | 10 ++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cmd/setup-gh.go b/cmd/setup-gh.go index 9fffaf64..0fe43bed 100644 --- a/cmd/setup-gh.go +++ b/cmd/setup-gh.go @@ -24,7 +24,7 @@ application and service principle, and will configure that application to trust RunE: func(cmd *cobra.Command, args []string) error { fillSetUpConfig(sc) - s := spinner.GetSpinner("--> Setting up Github OIDC...") + s := spinner.CreateSpinner("--> Setting up Github OIDC...") s.Start() err := runProviderSetUp(sc) s.Stop() diff --git a/pkg/providers/azure.go b/pkg/providers/azure.go index 9a027475..67838620 100644 --- a/pkg/providers/azure.go +++ b/pkg/providers/azure.go @@ -7,6 +7,8 @@ import ( "os/exec" "time" + "github.com/Azure/draft/pkg/spinner" + log "github.com/sirupsen/logrus" bo "github.com/cenkalti/backoff/v4" ) @@ -25,11 +27,14 @@ type SetUpCmd struct { func InitiateAzureOIDCFlow(sc *SetUpCmd) error { log.Debug("Commencing github connection with azure...") + s := spinner.GetSpinner() if !HasGhCli() || !IsLoggedInToGh() { + s.Stop() if err := LogInToGh(); err != nil { log.Fatal(err) } + s.Start() } if err := sc.ValidateSetUpConfig(); err != nil { @@ -74,6 +79,8 @@ func InitiateAzureOIDCFlow(sc *SetUpCmd) error { func (sc *SetUpCmd) createAzApp() error { log.Debug("Commencing Azure app creation...") + start := time.Now() + log.Debug(start) createApp := func () error { createAppCmd := exec.Command("az", "ad", "app", "create", "--only-show-errors", "--display-name", sc.AppName) @@ -91,7 +98,9 @@ func (sc *SetUpCmd) createAzApp() error { sc.appId = appId + end := time.Since(start) log.Debug("App created successfully!") + log.Debug(end) return nil } @@ -112,6 +121,8 @@ func (sc *SetUpCmd) createAzApp() error { func (sc *SetUpCmd) CreateServicePrincipal() error { log.Debug("Creating Azure service principal...") + start := time.Now() + log.Debug(start) createServicePrincipal := func () error { createSpCmd := exec.Command("az", "ad", "sp", "create", "--id", sc.appId, "--only-show-errors") @@ -128,6 +139,8 @@ func (sc *SetUpCmd) CreateServicePrincipal() error { sc.spObjectId = objectId log.Debug("Service principal created successfully!") + end := time.Since(start) + log.Debug(end) return nil } diff --git a/pkg/spinner/spinner.go b/pkg/spinner/spinner.go index 8f9915af..36c686c8 100644 --- a/pkg/spinner/spinner.go +++ b/pkg/spinner/spinner.go @@ -8,10 +8,16 @@ import ( "github.com/briandowns/spinner" ) -func GetSpinner(msg string) *spinner.Spinner { +var s *spinner.Spinner + +func CreateSpinner(msg string) *spinner.Spinner { cyan := color.New(color.Bold, color.FgCyan).SprintFunc() - s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) + s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) s.Prefix = fmt.Sprintf("%s %s ", cyan("[Draft]"), msg) s.Suffix = " " return s +} + +func GetSpinner() *spinner.Spinner { + return s } \ No newline at end of file From df3932c2421b6092388bb687ed8faade05a0471a Mon Sep 17 00:00:00 2001 From: eborders Date: Fri, 13 May 2022 11:40:10 -0400 Subject: [PATCH 20/23] singleton spinner --- cmd/setup-gh.go | 2 +- pkg/providers/azure.go | 10 +++++----- pkg/spinner/spinner.go | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/setup-gh.go b/cmd/setup-gh.go index 0fe43bed..349037eb 100644 --- a/cmd/setup-gh.go +++ b/cmd/setup-gh.go @@ -24,7 +24,7 @@ application and service principle, and will configure that application to trust RunE: func(cmd *cobra.Command, args []string) error { fillSetUpConfig(sc) - s := spinner.CreateSpinner("--> Setting up Github OIDC...") + s := spinner.GetSpinner() s.Start() err := runProviderSetUp(sc) s.Stop() diff --git a/pkg/providers/azure.go b/pkg/providers/azure.go index 67838620..0b51436d 100644 --- a/pkg/providers/azure.go +++ b/pkg/providers/azure.go @@ -82,7 +82,7 @@ func (sc *SetUpCmd) createAzApp() error { start := time.Now() log.Debug(start) - createApp := func () error { + // createApp := func () error { createAppCmd := exec.Command("az", "ad", "app", "create", "--only-show-errors", "--display-name", sc.AppName) out, err := createAppCmd.CombinedOutput() @@ -105,12 +105,12 @@ func (sc *SetUpCmd) createAzApp() error { } return errors.New("app not found") - } + // } - backoff := bo.NewExponentialBackOff() - backoff.MaxElapsedTime = 30 * time.Second + // backoff := bo.NewExponentialBackOff() + // backoff.MaxElapsedTime = 30 * time.Second - err := bo.Retry(createApp, backoff) + // err := bo.Retry(createApp, backoff) if err != nil { log.Debug(err) return err diff --git a/pkg/spinner/spinner.go b/pkg/spinner/spinner.go index 36c686c8..71208517 100644 --- a/pkg/spinner/spinner.go +++ b/pkg/spinner/spinner.go @@ -10,6 +10,10 @@ import ( var s *spinner.Spinner +func init() { + CreateSpinner("--> Setting up Github OIDC...") +} + func CreateSpinner(msg string) *spinner.Spinner { cyan := color.New(color.Bold, color.FgCyan).SprintFunc() s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) From a5f4bbdd0399e6bd29019486b4c78ecdc2184349 Mon Sep 17 00:00:00 2001 From: eborders Date: Fri, 13 May 2022 12:08:29 -0400 Subject: [PATCH 21/23] refactoring --- pkg/spinner/spinner.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/spinner/spinner.go b/pkg/spinner/spinner.go index 71208517..51a0abc8 100644 --- a/pkg/spinner/spinner.go +++ b/pkg/spinner/spinner.go @@ -8,13 +8,17 @@ import ( "github.com/briandowns/spinner" ) +// type Spinner struct { +// spinner *spinner.Spinner +// } + var s *spinner.Spinner func init() { - CreateSpinner("--> Setting up Github OIDC...") + s = createSpinner("--> Setting up Github OIDC...") } -func CreateSpinner(msg string) *spinner.Spinner { +func createSpinner(msg string) *spinner.Spinner { cyan := color.New(color.Bold, color.FgCyan).SprintFunc() s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) s.Prefix = fmt.Sprintf("%s %s ", cyan("[Draft]"), msg) From 2d0eeb27d9fc26a2782da6f73f2a7aca37b5a41c Mon Sep 17 00:00:00 2001 From: eborders Date: Fri, 13 May 2022 14:18:35 -0400 Subject: [PATCH 22/23] preventing bugs in the case that spinner is set to nil --- pkg/spinner/spinner.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/spinner/spinner.go b/pkg/spinner/spinner.go index 51a0abc8..4466eb4a 100644 --- a/pkg/spinner/spinner.go +++ b/pkg/spinner/spinner.go @@ -27,5 +27,9 @@ func createSpinner(msg string) *spinner.Spinner { } func GetSpinner() *spinner.Spinner { + if s == nil { + s = createSpinner("--> Setting up Github OIDC...") + } + return s } \ No newline at end of file From 08af991582039edee6f7b3ca38e835c35881f941 Mon Sep 17 00:00:00 2001 From: eborders Date: Fri, 13 May 2022 15:58:20 -0400 Subject: [PATCH 23/23] using an interface insteaad --- cmd/setup-gh.go | 8 ++++---- cmd/setup-gh_test.go | 4 +++- pkg/providers/azure.go | 13 ++++++------- pkg/spinner/spinner.go | 23 +++++------------------ 4 files changed, 18 insertions(+), 30 deletions(-) diff --git a/cmd/setup-gh.go b/cmd/setup-gh.go index 349037eb..2ed2425a 100644 --- a/cmd/setup-gh.go +++ b/cmd/setup-gh.go @@ -24,9 +24,9 @@ application and service principle, and will configure that application to trust RunE: func(cmd *cobra.Command, args []string) error { fillSetUpConfig(sc) - s := spinner.GetSpinner() + s := spinner.CreateSpinner("--> Setting up Github OIDC...") s.Start() - err := runProviderSetUp(sc) + err := runProviderSetUp(sc, s) s.Stop() if err != nil { return err @@ -76,11 +76,11 @@ func fillSetUpConfig(sc *providers.SetUpCmd) { } } -func runProviderSetUp(sc *providers.SetUpCmd) error { +func runProviderSetUp(sc *providers.SetUpCmd, s spinner.Spinner) error { provider := strings.ToLower(sc.Provider) if provider == "azure" { // call azure provider logic - return providers.InitiateAzureOIDCFlow(sc) + return providers.InitiateAzureOIDCFlow(sc, s) } else { // call logic for user-submitted provider diff --git a/cmd/setup-gh_test.go b/cmd/setup-gh_test.go index 05824352..4dc0c4dc 100644 --- a/cmd/setup-gh_test.go +++ b/cmd/setup-gh_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/Azure/draft/pkg/providers" + "github.com/Azure/draft/pkg/spinner" ) func TestSetUpConfig(t *testing.T) { @@ -15,10 +16,11 @@ func TestSetUpConfig(t *testing.T) { mockSetUpCmd.Repo = "test/repo" mockSetUpCmd.ResourceGroupName = "testResourceGroup" mockSetUpCmd.SubscriptionID = "123456789" + s := spinner.CreateSpinner("--> Setting up Github OIDC...") fillSetUpConfig(mockSetUpCmd) - err := runProviderSetUp(mockSetUpCmd) + err := runProviderSetUp(mockSetUpCmd, s) assert.True(t, err == nil) } \ No newline at end of file diff --git a/pkg/providers/azure.go b/pkg/providers/azure.go index 0b51436d..a2993a44 100644 --- a/pkg/providers/azure.go +++ b/pkg/providers/azure.go @@ -25,9 +25,8 @@ type SetUpCmd struct { spObjectId string } -func InitiateAzureOIDCFlow(sc *SetUpCmd) error { +func InitiateAzureOIDCFlow(sc *SetUpCmd, s spinner.Spinner) error { log.Debug("Commencing github connection with azure...") - s := spinner.GetSpinner() if !HasGhCli() || !IsLoggedInToGh() { s.Stop() @@ -82,7 +81,7 @@ func (sc *SetUpCmd) createAzApp() error { start := time.Now() log.Debug(start) - // createApp := func () error { + createApp := func () error { createAppCmd := exec.Command("az", "ad", "app", "create", "--only-show-errors", "--display-name", sc.AppName) out, err := createAppCmd.CombinedOutput() @@ -105,12 +104,12 @@ func (sc *SetUpCmd) createAzApp() error { } return errors.New("app not found") - // } + } - // backoff := bo.NewExponentialBackOff() - // backoff.MaxElapsedTime = 30 * time.Second + backoff := bo.NewExponentialBackOff() + backoff.MaxElapsedTime = 30 * time.Second - // err := bo.Retry(createApp, backoff) + err := bo.Retry(createApp, backoff) if err != nil { log.Debug(err) return err diff --git a/pkg/spinner/spinner.go b/pkg/spinner/spinner.go index 4466eb4a..014e76a3 100644 --- a/pkg/spinner/spinner.go +++ b/pkg/spinner/spinner.go @@ -8,28 +8,15 @@ import ( "github.com/briandowns/spinner" ) -// type Spinner struct { -// spinner *spinner.Spinner -// } - -var s *spinner.Spinner - -func init() { - s = createSpinner("--> Setting up Github OIDC...") +type Spinner interface { + Start() + Stop() } -func createSpinner(msg string) *spinner.Spinner { +func CreateSpinner(msg string) *spinner.Spinner { cyan := color.New(color.Bold, color.FgCyan).SprintFunc() - s = spinner.New(spinner.CharSets[11], 100*time.Millisecond) + s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) s.Prefix = fmt.Sprintf("%s %s ", cyan("[Draft]"), msg) s.Suffix = " " return s } - -func GetSpinner() *spinner.Spinner { - if s == nil { - s = createSpinner("--> Setting up Github OIDC...") - } - - return s -} \ No newline at end of file