diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index c12a22f..684c3ee 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -35,11 +35,13 @@ jobs: - name: Clean downloaded binaries run: make -s clean - test: + tests: strategy: matrix: +# os: [ubuntu-latest, macos-latest] os: [ubuntu-latest] runs-on: ${{ matrix.os }} + name: test (${{ matrix.os }}) steps: - name: Checkout uses: actions/checkout@v4 @@ -63,9 +65,18 @@ jobs: exit 1 fi shell: bash - - name: Test + - name: Run tests on ${{ matrix.os }} run: make -s test + staticcheck: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - uses: dominikh/staticcheck-action@v1.3.0 + with: + version: "2022.1.3" + codeql: runs-on: ubuntu-latest strategy: @@ -97,18 +108,9 @@ jobs: cache: true - name: Install Nginx run: | - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt update - sudo apt install nginx - sudo nginx - elif [ "$RUNNER_OS" == "macOS" ]; then - brew install nginx - sudo ln -s /usr/local/etc/nginx /etc/nginx - sudo nginx - else - echo "$RUNNER_OS not supported" - exit 1 - fi + sudo apt update + sudo apt install nginx + sudo nginx shell: bash - name: Coverage Test run: make -s test-with-coverage @@ -126,11 +128,12 @@ jobs: env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - build: + builds: strategy: matrix: os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} + name: build (${{ matrix.os }}) steps: - name: Checkout code uses: actions/checkout@v4 @@ -139,5 +142,5 @@ jobs: with: go-version-file: "go.mod" cache: true - - name: Test + - name: Build on ${{ matrix.os }} run: make -s build diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 3f72e3f..9178cbe 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -38,12 +38,13 @@ jobs: - name: Clean downloaded binaries run: make -s clean - - test: + tests: strategy: matrix: + # os: [ubuntu-latest, macos-latest] os: [ubuntu-latest] runs-on: ${{ matrix.os }} + name: test (${{ matrix.os }}) steps: - name: Checkout code uses: actions/checkout@v4 @@ -67,9 +68,18 @@ jobs: exit 1 fi shell: bash - - name: Test + - name: Run tests on ${{ matrix.os }} run: make -s test + staticcheck: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - uses: dominikh/staticcheck-action@v1.3.0 + with: + version: "2022.1.3" + codeql: runs-on: ubuntu-latest strategy: @@ -130,11 +140,12 @@ jobs: env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - build: + builds: strategy: matrix: os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} + name: build (${{ matrix.os }}) steps: - name: Checkout code uses: actions/checkout@v4 @@ -143,12 +154,12 @@ jobs: with: go-version-file: "go.mod" cache: true - - name: Build + - name: Build on ${{ matrix.os }} run: make -s build tag: runs-on: ubuntu-latest - needs: [lint, fmt, test, codeql, sonarcloud, build] + needs: [lint, fmt, tests, staticcheck, codeql, sonarcloud, builds] steps: - name: Checkout uses: actions/checkout@v4 diff --git a/README.md b/README.md index 7af444e..b4138d0 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=bilalcaliskan_nginx-conf-generator&metric=coverage)](https://sonarcloud.io/summary/new_code?id=bilalcaliskan_nginx-conf-generator) [![Release](https://img.shields.io/github/release/bilalcaliskan/nginx-conf-generator.svg)](https://github.com/bilalcaliskan/nginx-conf-generator/releases/latest) [![Go version](https://img.shields.io/github/go-mod/go-version/bilalcaliskan/nginx-conf-generator)](https://github.com/bilalcaliskan/nginx-conf-generator) +[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) That tool uses [client-go](https://github.com/kubernetes/client-go) to communicate with multi Kubernetes clusters and @@ -28,25 +29,25 @@ Usage: nginx-conf-generator [flags] Flags: - --customAnnotation string annotation to specify selectable services (default "nginx-conf-generator/enabled") - -h, --help help for nginx-conf-generator - --kubeConfigPaths string comma separated list of kubeconfig file paths to access with the cluster (default "/home/joshsagredo/.kube/config") - --metricsEndpoint string endpoint to provide prometheus metrics (default "/metrics") - --metricsPort int port of the metrics server (default 5000) - --templateInputFile string path of the template input file to be able to render and print to --templateOutputFile (default "resources/ncg.conf.tmpl") - --templateOutputFile string path of the template output file which is a valid Nginx conf file (default "/etc/nginx/conf.d/ncg.conf") - -v, --verbose verbose output of the logging library (default false) - --version version for nginx-conf-generator - --workerNodeLabel string label to specify worker nodes (default "worker") + --custom-annotation string annotation to specify selectable services (default "nginx-conf-generator/enabled") + -h, --help help for nginx-conf-generator + --kubeconfig-paths string comma separated list of kubeconfig file paths to access with the cluster (default "/home/joshsagredo/.kube/config") + --metrics-endpoint string endpoint to provide prometheus metrics (default "/metrics") + --metrics-port int port of the metrics server (default 5000) + --template-input-file string path of the template input file to be able to render and print to --template-output-file (default "resources/ncg.conf.tmpl") + --template-output-file string rendered output file path which is a valid Nginx conf file (default "/etc/nginx/conf.d/ncg.conf") + -v, --verbose verbose output of the logging library (default false) + --version version for nginx-conf-generator + --worker-node-label string label to specify worker nodes (default "worker") ``` > That tool should be run on a Linux host and the user who runs the binary file nginx-conf-generator -should have permissions to edit **--templateOutputFile** file and reload nginx process using below command: +should have permissions to edit **--template-output-file** file and reload nginx process using below command: > ```shell > $ nginx -s reload > ``` -> If you want to cover multiple kubernetes clusters, add comma seperated list of kubeconfig paths with **--kubeConfigPaths** argument. +> If you want to cover multiple kubernetes clusters, add comma seperated list of kubeconfig paths with **--kubeconfig-paths** argument. ## Installation ### Binary @@ -54,7 +55,7 @@ Binary can be downloaded from [Releases](https://github.com/bilalcaliskan/nginx- After then, you can simply run binary by providing required command line arguments: ```shell -$ ./nginx-conf-generator --kubeConfigPaths ~/.kube/config1,~/.kube/config2 --customAnnotation nginx-conf-generator/enabled +$ ./nginx-conf-generator --kubeconfig-paths=~/.kube/config1,~/.kube/config2 --custom-annotation nginx-conf-generator/enabled ``` ### Homebrew @@ -66,7 +67,7 @@ brew install bilalcaliskan/tap/nginx-conf-generator ## Development This project requires below tools while developing: -- [Golang 1.19](https://golang.org/doc/go1.19) +- [Golang 1.20](https://golang.org/doc/go1.20) - [pre-commit](https://pre-commit.com/) - [golangci-lint](https://golangci-lint.run/usage/install/) - required by [pre-commit](https://pre-commit.com/) - [gocyclo](https://github.com/fzipp/gocyclo) - required by [pre-commit](https://pre-commit.com/) diff --git a/build/ci/.pre-commit-config.yaml b/build/ci/.pre-commit-config.yaml index ad246d6..f57bd6b 100644 --- a/build/ci/.pre-commit-config.yaml +++ b/build/ci/.pre-commit-config.yaml @@ -27,5 +27,5 @@ repos: args: [-over=30] - id: no-go-testing - id: golangci-lint - # - id: go-unit-tests + - id: go-unit-tests - id: go-build diff --git a/cmd/root.go b/cmd/root.go index a861d9a..e99cbfe 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,25 +28,25 @@ var ( func init() { opts = options.GetNginxConfGeneratorOptions() - rootCmd.Flags().StringVarP(&opts.KubeConfigPaths, "kubeConfigPaths", "", filepath.Join(os.Getenv("HOME"), ".kube", "config"), + rootCmd.Flags().StringVarP(&opts.KubeConfigPaths, "kubeconfig-paths", "", filepath.Join(os.Getenv("HOME"), ".kube", "config"), "comma separated list of kubeconfig file paths to access with the cluster") - rootCmd.Flags().StringVarP(&opts.WorkerNodeLabel, "workerNodeLabel", "", "worker", + rootCmd.Flags().StringVarP(&opts.WorkerNodeLabel, "worker-node-label", "", "worker", "label to specify worker nodes") - rootCmd.Flags().StringVarP(&opts.CustomAnnotation, "customAnnotation", "", "nginx-conf-generator/enabled", + rootCmd.Flags().StringVarP(&opts.CustomAnnotation, "custom-annotation", "", "nginx-conf-generator/enabled", "annotation to specify selectable services") - rootCmd.Flags().StringVarP(&opts.TemplateInputFile, "templateInputFile", "", "resources/ncg.conf.tmpl", - "path of the template input file to be able to render and print to --templateOutputFile") - rootCmd.Flags().StringVarP(&opts.TemplateOutputFile, "templateOutputFile", "", "/etc/nginx/conf.d/ncg.conf", - "path of the template output file which is a valid Nginx conf file") - rootCmd.Flags().IntVarP(&opts.MetricsPort, "metricsPort", "", 5000, + rootCmd.Flags().StringVarP(&opts.TemplateInputFile, "template-input-file", "", "resources/ncg.conf.tmpl", + "path of the template input file to be able to render and print to --template-output-file") + rootCmd.Flags().StringVarP(&opts.TemplateOutputFile, "template-output-file", "", "/etc/nginx/conf.d/ncg.conf", + "rendered output file path which is a valid Nginx conf file") + rootCmd.Flags().IntVarP(&opts.MetricsPort, "metrics-port", "", 5000, "port of the metrics server") - rootCmd.Flags().StringVarP(&opts.BannerFilePath, "bannerFilePath", "", "build/ci/banner.txt", - "relative path of the banner file") - rootCmd.Flags().StringVarP(&opts.MetricsEndpoint, "metricsEndpoint", "", "/metrics", + rootCmd.Flags().StringVarP(&opts.MetricsEndpoint, "metrics-endpoint", "", "/metrics", "endpoint to provide prometheus metrics") + rootCmd.Flags().StringVarP(&opts.BannerFilePath, "banner-file-path", "", "build/ci/banner.txt", + "relative path of the banner file") rootCmd.Flags().BoolVarP(&opts.VerboseLog, "verbose", "v", false, "verbose output of the logging library (default false)") - if err := rootCmd.Flags().MarkHidden("bannerFilePath"); err != nil { + if err := rootCmd.Flags().MarkHidden("banner-file-path"); err != nil { panic("fatal error occured while hiding flag") } diff --git a/internal/k8s/informers/utils_test.go b/internal/k8s/informers/utils_test.go index 7c3691d..f003a94 100644 --- a/internal/k8s/informers/utils_test.go +++ b/internal/k8s/informers/utils_test.go @@ -7,7 +7,7 @@ import ( ) func TestGetClientSet(t *testing.T) { - restConfig, err := GetConfig("../../../mock/kubeconfig") + restConfig, err := GetConfig("../../../test/kubeconfig") assert.Nil(t, err) assert.NotNil(t, restConfig) @@ -15,7 +15,7 @@ func TestGetClientSet(t *testing.T) { assert.Nil(t, err) assert.NotNil(t, clientSet) - restConfig, err = GetConfig("../../../mock/broken_kubeconfig") + restConfig, err = GetConfig("../../../test/broken_kubeconfig") assert.NotNil(t, err) assert.Nil(t, restConfig) } diff --git a/hack/coverage.sh b/scripts/coverage.sh similarity index 100% rename from hack/coverage.sh rename to scripts/coverage.sh diff --git a/hack/setup_resources.sh b/scripts/setup_resources.sh similarity index 100% rename from hack/setup_resources.sh rename to scripts/setup_resources.sh diff --git a/mock/broken_kubeconfig b/test/broken_kubeconfig similarity index 100% rename from mock/broken_kubeconfig rename to test/broken_kubeconfig diff --git a/mock/ca.crt b/test/ca.crt similarity index 100% rename from mock/ca.crt rename to test/ca.crt diff --git a/mock/client.crt b/test/client.crt similarity index 100% rename from mock/client.crt rename to test/client.crt diff --git a/mock/client.key b/test/client.key similarity index 100% rename from mock/client.key rename to test/client.key diff --git a/mock/kubeconfig b/test/kubeconfig similarity index 100% rename from mock/kubeconfig rename to test/kubeconfig