github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/CONTRIBUTING.md (about)

     1  # Contributing to Sealer
     2  
     3  It is warmly welcomed if you have interest to hack on Sealer. First, we encourage this kind of willing very much. And here is a list of contributing guide for you.
     4  
     5  ## Topics
     6  
     7  * [Reporting security issues](#reporting-security-issues)
     8  * [Reporting general issues](#reporting-general-issues)
     9  * [Code and doc contribution](#code-and-doc-contribution)
    10  * [Engage to help anything](#engage-to-help-anything)
    11  
    12  ## Reporting security issues
    13  
    14  Security issues are always treated seriously. As our usual principle, we discourage anyone to spread security issues. If you find a security issue of Sealer, please do not discuss it in public and even do not open a public issue. Instead, we encourage you to send us a private email to [sealer@list.alibaba-inc.com](mailto:sealer@list.alibaba-inc.com) to report this.
    15  
    16  ## Reporting general issues
    17  
    18  To be honest, we regard every user of Sealer as a very kind contributor. After experiencing Sealer, you may have some feedback for the project. Then feel free to open an issue via [NEW ISSUE](https://github.com/sealerio/sealer/issues/new/choose).
    19  
    20  Since we collaborate project Sealer in a distributed way, we appreciate **WELL-WRITTEN**, **DETAILED**, **EXPLICIT** issue reports. To make the communication more efficient, we wish everyone could search if your issue is an existing one in the searching list. If you find it existing, please add your details in comments under the existing issue instead of opening a brand new one.
    21  
    22  To make the issue details as standard as possible, we set up an [ISSUE TEMPLATE](./.github/ISSUE_TEMPLATE) for issue reporters. You can find three kinds of issue templates there: question, bug report and feature request. Please **BE SURE** to follow the instructions to fill fields in template.
    23  
    24  There are a lot of cases when you could open an issue:
    25  
    26  * bug report
    27  * feature request
    28  * performance issues
    29  * feature proposal
    30  * feature design
    31  * help wanted
    32  * doc incomplete
    33  * test improvement
    34  * any questions on project
    35  * and so on
    36  
    37  Also, we must remind that when filing a new issue, please remember to remove the sensitive data from your post. Sensitive data could be password, secret key, network locations, private business data and so on.
    38  
    39  ## Code and doc contribution
    40  
    41  Every action to make project Sealer better is encouraged. On GitHub, every improvement for Sealer could be via a PR (short for pull request).
    42  
    43  * If you find a typo, try to fix it!
    44  * If you find a bug, try to fix it!
    45  * If you find some redundant codes, try to remove them!
    46  * If you find some test cases missing, try to add them!
    47  * If you could enhance a feature, please **DO NOT** hesitate!
    48  * If you find code implicit, try to add comments to make it clear!
    49  * If you find code ugly, try to refactor that!
    50  * If you can help to improve documents, it could not be better!
    51  * If you find document incorrect, just do it and fix that!
    52  * ...
    53  
    54  Actually it is impossible to list them completely. Just remember one principle:
    55  
    56  > WE ARE LOOKING FORWARD TO ANY PR FROM YOU.
    57  
    58  Since you are ready to improve Sealer with a PR, we suggest you could take a look at the PR rules here.
    59  
    60  * [Workspace Preparation](#workspace-preparation)
    61  * [Branch Definition](#branch-definition)
    62  * [Commit Rules](#commit-rules)
    63  * [PR Description](#pr-description)
    64  * [Developing Environment](#developing-environment)
    65  * [Run E2E Test or Write E2E Test Cases](#Run-E2E-Test-or-Write-E2E-Test-Cases)
    66  
    67  ### Workspace Preparation
    68  
    69  To put forward a PR, we assume you have registered a GitHub ID. Then you could finish the preparation in the following steps:
    70  
    71  1. **FORK** Sealer to your repository. To make this work, you just need to click the button Fork in right-left of [sealerio/sealer](https://github.com/sealerio/sealer) main page. Then you will end up with your repository in `https://github.com/<your-username>/sealer`, in which `your-username` is your GitHub username.
    72  
    73  1. **CLONE** your own repository to develop locally. Use `git clone https://github.com/<your-username>/sealer.git` to clone repository to your local machine. Then you can create new branches to finish the change you wish to make.
    74  
    75  1. **Set Remote** upstream to be `https://github.com/sealerio/sealer.git` using the following two commands:
    76  
    77  ```
    78  	git remote add upstream https://github.com/sealerio/sealer.git
    79  	git remote set-url --push upstream no-pushing
    80  ```
    81  
    82  	With this remote setting, you can check your git remote configuration like this:
    83  
    84  ```
    85  	$ git remote -v
    86  	origin     https://github.com/<your-username>/sealer.git (fetch)
    87  	origin     https://github.com/<your-username>/sealer.git (push)
    88  	upstream   https://github.com/sealerio/sealer.git (fetch)
    89  	upstream   no-pushing (push)
    90  ```
    91  
    92  	Adding this, we can easily synchronize local branches with upstream branches.
    93  
    94  1. **Create a branch** to add a new feature or fix issues
    95  
    96      Update local working directory and remote forked repository:
    97  
    98     ```
    99     cd sealer
   100     git fetch upstream
   101     git checkout main
   102     git rebase upstream/main
   103     git push	// default origin, update your forked repository
   104     ```
   105  
   106     Create a new branch:
   107  
   108     ```
   109     git checkout -b <new-branch>
   110     ```
   111  
   112     Make any change on the `new-branch` then build and test your codes.
   113  
   114  1. **Push your branch** to your forked repository, try not to generate multiple commit message within a pr.
   115  
   116     ```
   117     golangci-lint run -c .golangci.yml	// lint
   118     git commit -a -m "message for your changes" --signoff	// -a is git add .
   119     git rebase -i	<commit-id>// do this if your pr has multiple commits
   120     git push	// push to your forked repository after rebase done
   121     ```
   122  
   123  1. **File a pull request** to sealerio/sealer:main
   124  
   125  ### Branch Definition
   126  
   127  Right now we assume every contribution via pull request is for [branch master](https://github.com/sealerio/sealer/tree/main) in Sealer. Before contributing, be aware of branch definition would help a lot.
   128  
   129  As a contributor, keep in mind again that every contribution via pull request is for branch master. While in project sealer, there are several other branches, we generally call them rc branches, release branches and backport branches.
   130  
   131  Before officially releasing a version, we will check out a rc(release candidate) branch. In this branch, we will test more than branch main.
   132  
   133  When officially releasing a version, there will be a release branch before tagging. After tagging, we will delete the release branch.
   134  
   135  When backport some fixes to existing released version, we will check out backport branches. After backporting, the backporting effects will be in PATCH number in MAJOR.MINOR.PATCH of [SemVer](http://semver.org/).
   136  
   137  ### Commit Rules
   138  
   139  Actually in Sealer, we take two rules serious when committing:
   140  
   141  * [Commit Message](#commit-message)
   142  * [Commit Content](#commit-content)
   143  
   144  #### Commit Message
   145  
   146  Commit message could help reviewers better understand what the purpose of submitted PR is. It could help accelerate the code review procedure as well. We encourage contributors to use **EXPLICIT** commit message rather than ambiguous message. In general, we advocate the following commit message type:
   147  
   148  * docs: xxxx. For example, "docs: add docs about storage installation".
   149  * feature: xxxx.For example, "feature: make result show in sorted order".
   150  * bugfix: xxxx. For example, "bugfix: fix panic when input nil parameter".
   151  * style: xxxx. For example, "style: format the code style of Constants.java".
   152  * refactor: xxxx. For example, "refactor: simplify to make codes more readable".
   153  * test: xxx. For example, "test: add unit test case for func InsertIntoArray".
   154  * chore: xxx. For example, "chore: integrate travis-ci". It's the type of mantainance change.
   155  * other readable and explicit expression ways.
   156  
   157  On the other side, we discourage contributors from committing message like the following ways:
   158  
   159  * ~~fix bug~~
   160  * ~~update~~
   161  * ~~add doc~~
   162  
   163  #### Commit Content
   164  
   165  Commit content represents all content changes included in one commit. We had better include things in one single commit which could support reviewer's complete review without any other commits' help. In another word, contents in one single commit can pass the CI to avoid code mess. In brief, there are two minor rules for us to keep in mind:
   166  
   167  * avoid very large change in a commit;
   168  * complete and reviewable for each commit.
   169  
   170  No matter what the commit message, or commit content is, we do take more emphasis on code review.
   171  
   172  ### PR Description
   173  
   174  PR is the only way to make change to Sealer project files. To help reviewers better get your purpose, PR description could not be too detailed. We encourage contributors to follow the [PR template](./.github/PULL_REQUEST_TEMPLATE.md) to finish the pull request.
   175  
   176  ### Developing Environment
   177  
   178  As a contributor, if you want to make any contribution to Sealer project, we should reach an agreement on the version of tools used in the development environment.
   179  Here are some dependents with specific version:
   180  
   181  * golang : v1.14
   182  * golangci-lint: 1.39.0
   183  * gpgme(brew install gpgme)
   184  
   185  When you develop the Sealer project at the local environment, you should use subcommands of Makefile to help yourself to check and build the latest version of Sealer. For the convenience of developers, we use the docker to build Sealer. It can reduce problems of the developing environment.
   186  
   187  ### Run E2E Test or Write E2E Test Cases
   188  Before you commit a pull request, you need to run E2E test first. If you have modified the code or added features, you need to write corresponding E2E test cases, or you want to contribute E2E test cases for sealer, please read this section.
   189  
   190  #### Principle of sealer k8s in docker
   191  
   192  Sealer E2E test is inspired by kind, uses a container to emulate a node by running systemd inside the container and hosting other processes with systemd.
   193  Sealer uses [pkg/infra/container/imagecontext/base/Dockerfile](https://github.com/sealerio/sealer/blob/main/pkg/infra/container/imagecontext/base/Dockerfile) to build a basic image: `sealerio/sealer-base-image:v1`,and use this image to start the container to emulate node。
   194  
   195  After starting the container, the sealer binary is transferred to the container through ssh, and the sealer commands such as: sealer run and sealer apply are invoked through ssh to create the k8s cluster (k8s in docker), as shown in the figure below:
   196  
   197  ![sealer E2E drawio (1)](https://user-images.githubusercontent.com/56665618/217173635-3f27033d-3cf5-47e2-9b4c-66173eb89821.png)
   198  
   199  #### How to run E2E test locally
   200  
   201  The code in test repository is a set of [Ginkgo](http://onsi.github.io/ginkgo) and [Gomega](http://onsi.github.io/gomega) based integration tests that execute commands using the sealer CLI.
   202  
   203  * Prerequisites
   204  
   205  Before you run the tests, you'll need a sealer binary in your machine executable path and install docker.
   206  
   207  * Run the Tests
   208  
   209  To run a single test or set of tests, you'll need the [Ginkgo](https://github.com/onsi/ginkgo) tool installed on your machine:
   210  
   211  ```bash
   212  go install github.com/onsi/ginkgo/ginkgo@v1.16.2
   213  ```
   214  
   215  To install Sealer and prepare the test environment:
   216  
   217  ```bash
   218  #build sealer source code to binary for local e2e-test in containers
   219  git clone https://github.com/sealerio/sealer.git
   220  cd sealer/ && make build-in-docker
   221  cp _output/bin/sealer/linux_amd64/sealer /usr/local/bin
   222  
   223  #prepare test environment
   224  export REGISTRY_URL={your registry}
   225  export REGISTRY_USERNAME={user name}
   226  export REGISTRY_PASSWORD={password}
   227  #default test image name: docker.io/sealerio/kubernetes:v1-22-15-sealerio-2
   228  export IMAGE_NAME={test image name}
   229  ```
   230  
   231  To execute the entire test suite:
   232  
   233  ```bash
   234  cd sealer && ginkgo test
   235  ```
   236  
   237  You can then use the `--focus` option to run subsets of the test suite:
   238  
   239  ```bash
   240  ginkgo --focus="sealer login" test
   241  ```
   242  
   243  You can then use the `-v` option to print out default reporter as all specs begin:
   244  
   245  ```bash
   246  ginkgo -v test
   247  ```
   248  
   249  More ginkgo helpful info see:
   250  
   251  ```bash
   252  ginkgo --help
   253  ```
   254  
   255  #### How to run E2E Tests using github action
   256  
   257  Before we run the CI by using github aciton, please make sure that these four variables: `REGISTRY_URL={your registry}`, `REGISTRY_USERNAME={user name}`, `REGISTRY_PASSWORD={password}`, `IMAGE_NAME={test image name}` have been setted in github's secret (for sealer login and sealer image tests).
   258  
   259  CI is triggered when we push a branch with name starts with release or comment `/test all` in PR,you can also comment `/test {test to run}` in PR to run subsets of the test suite.
   260  
   261  #### How to write E2E test cases for sealer
   262  
   263  E2E tests required by sealer can be divided into those that need cluster construction and those that do not. For tests that need cluster construction, container needs to be started to simulate node (such as sealer run and sealer apply). If you don't need to build a cluster, you just need to execute it on the machine (e.g., sealer pull, sealer tag, sealer build).
   264  
   265  * E2E test entry
   266  
   267  Sealer all E2E test files are in the `test` directory, where `e2e_test.go` is the entry to E2E test.
   268  
   269  The function: `func TestSealerTests(t *testing.T)`is the entry point for Ginkgo - the go test runner will run this function when you run `go test` or `ginkgo`. `RegisterFailHandler(Fail)`is the single line of glue code connecting Ginkgo to Gomega. If we were to avoid dot-imports this would read as `gomega.RegisterFailHandler(ginkgo.Fail)`- what we're doing here is telling our matcher library (Gomega) which function to call (Ginkgo's `Fail`) in the event a failure is detected.
   270  
   271  * Writing Specs
   272  
   273  You can use `ginkgo generate` to generate a E2E test file (eg. `ginkgo generate sealer_alpha`)
   274  
   275  Ginkgo allows you to hierarchically organize the specs in your suite using container nodes. Ginkgo provides three synonymous nouns for creating container nodes: `Describe`, `Context`, and `When`. These three are functionally identical and are provided to help the spec narrative flow. You usually `Describe`different capabilities of your code and explore the behavior of each capability across different `Context`s.
   276  
   277  For example:
   278  
   279  ```go
   280  var _ = Describe("sealer login", func() {
   281  	Context("login docker registry", func() {
   282  		AfterEach(func() {
   283  			registry.Logout()
   284  		})
   285  		It("with correct name and password", func() {
   286  			image.CheckLoginResult(
   287  				settings.RegistryURL,
   288  				settings.RegistryUsername,
   289  				settings.RegistryPasswd,
   290  				true)
   291  		})
   292  		It("with incorrect name and password", func() {
   293  			image.CheckLoginResult(
   294  				settings.RegistryURL,
   295  				settings.RegistryPasswd,
   296  				settings.RegistryUsername,
   297  				false)
   298  		})
   299  		It("with only name", func() {
   300  			image.CheckLoginResult(
   301  				settings.RegistryURL,
   302  				settings.RegistryUsername,
   303  				"",
   304  				false)
   305  		})
   306  		It("with only password", func() {
   307  			image.CheckLoginResult(
   308  				settings.RegistryURL,
   309  				"",
   310  				settings.RegistryPasswd,
   311  				false)
   312  		})
   313  		It("with only registryURL", func() {
   314  			image.CheckLoginResult(
   315  				settings.RegistryURL,
   316  				"",
   317  				"",
   318  				false)
   319  		})
   320  	})
   321  })
   322  ```
   323  
   324  The configuration of e2e test is in `test/testhelper/settings`, The test data and methods for e2e test are here: `test/suites`.
   325  
   326  More information about ginkgo and gomega can be found in the documentation:[Ginkgo](http://onsi.github.io/ginkgo) and [Gomega](http://onsi.github.io/gomega).
   327  
   328  #### Configure github action file
   329  
   330  The github action file for e2e test is under the `.github/workflows` directory. You need to configure the file here for CI.
   331  
   332  For example:
   333  
   334  ```yaml
   335  name: {Test name}
   336  
   337  on:
   338    push:
   339      branches: "release*"
   340    issue_comment:
   341      types:
   342        - created
   343    workflow_dispatch: {}
   344    pull_request_target:
   345      types: [opened, synchronize, reopened]
   346      branches: "*"
   347      paths-ignore:
   348        - 'docs/**'
   349        - '*.md'
   350        - '*.yml'
   351        - '.github'
   352  
   353  permissions:
   354    statuses: write
   355  
   356  jobs:
   357    build:
   358      name: test
   359      runs-on: ubuntu-latest
   360      if: ${{ (github.event.issue.pull_request && (github.event.comment.body == '/test all' || github.event.comment.body == '/test {name}')) || github.event_name == 'push' || github.event_name == 'pull_request_target' }}
   361      env:
   362        GO111MODULE: on
   363      steps:
   364        - name: Get PR details
   365          if: ${{ github.event_name == 'issue_comment'}}
   366          uses: xt0rted/pull-request-comment-branch@v1
   367          id: comment-branch
   368  
   369        - name: Set commit status as pending
   370          if: ${{ github.event_name == 'issue_comment'}}
   371          uses: myrotvorets/set-commit-status-action@master
   372          with:
   373            sha: ${{ steps.comment-branch.outputs.head_sha }}
   374            token: ${{ secrets.GITHUB_TOKEN }}
   375            status: pending
   376  
   377        - name: Github API Request
   378          id: request
   379          uses: octokit/request-action@v2.1.7
   380          with:
   381            route: ${{ github.event.issue.pull_request.url }}
   382          env:
   383            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
   384        - name: Get PR informations
   385          id: pr_data
   386          run: |
   387            echo "repo_name=${{ fromJson(steps.request.outputs.data).head.repo.full_name }}" >> $GITHUB_STATE
   388            echo "repo_clone_url=${{ fromJson(steps.request.outputs.data).head.repo.clone_url }}" >> $GITHUB_STATE
   389            echo "repo_ssh_url=${{ fromJson(steps.request.outputs.data).head.repo.ssh_url }}" >> $GITHUB_STATE
   390        - name: Check out code into the Go module directory
   391          uses: actions/checkout@v3
   392          with:
   393            token: ${{ secrets.GITHUB_TOKEN }}
   394            repository: ${{fromJson(steps.request.outputs.data).head.repo.full_name}}
   395            ref: ${{fromJson(steps.request.outputs.data).head.ref}}
   396            path: src/github.com/sealerio/sealer
   397        - name: Install deps
   398          run: |
   399            sudo su
   400            sudo apt-get update
   401            sudo apt-get install -y libgpgme-dev libbtrfs-dev libdevmapper-dev
   402            sudo mkdir /var/lib/sealer
   403        - name: Set up Go 1.17
   404          uses: actions/setup-go@v3
   405          with:
   406            go-version: 1.17
   407          id: go
   408  
   409        - name: Install sealer and ginkgo
   410          shell: bash
   411          run: |
   412            docker run --rm -v ${PWD}:/usr/src/sealer -w /usr/src/sealer registry.cn-qingdao.aliyuncs.com/sealer-io/sealer-build:v1 make linux
   413            export SEALER_DIR=${PWD}/_output/bin/sealer/linux_amd64
   414            echo "$SEALER_DIR" >> $GITHUB_PATH
   415            go install github.com/onsi/ginkgo/ginkgo@v1.16.2
   416            go install github.com/onsi/gomega/...@v1.12.0
   417            GOPATH=`go env GOPATH`
   418            echo "$GOPATH/bin" >> $GITHUB_PATH
   419          working-directory: src/github.com/sealerio/sealer
   420  
   421        - name: Run **{Test name}** test and generate coverage
   422          shell: bash
   423          working-directory: src/github.com/sealerio/sealer
   424          env:
   425            REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
   426            REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
   427            REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
   428            IMAGE_NAME: ${{ secrets.IMAGE_NAME}}
   429            ACCESSKEYID: ${{ secrets.ACCESSKEYID }}
   430            ACCESSKEYSECRET: ${{ secrets.ACCESSKEYSECRET }}
   431            RegionID: ${{ secrets.RegionID }}
   432          run: | # Your main focus is here
   433            ginkgo -v -focus="{your test}" -cover -covermode=atomic -coverpkg=./... -coverprofile=/tmp/coverage.out -trace test
   434  
   435        - name: Upload coverage to Codecov
   436          uses: codecov/codecov-action@v3
   437          with:
   438            token: ${{ secrets.CODECOV_TOKEN }}
   439            files: /tmp/coverage.out
   440            flags: e2e-tests
   441            name: codecov-umbrella
   442  
   443        - name: Set final commit status
   444          uses: myrotvorets/set-commit-status-action@master
   445          if: contains(github.event.comment.body, '/test') && always()
   446          with:
   447            sha: ${{ steps.comment-branch.outputs.head_sha }}
   448            token: ${{ secrets.GITHUB_TOKEN }}
   449            status: ${{ job.status }}
   450  ```
   451  
   452  More information about github action can be found in the documentation:[Github action](https://docs.github.com/en/actions/quickstart).
   453  
   454  ## Engage to help anything
   455  
   456  We choose GitHub as the primary place for Sealer to collaborate. So the latest updates of Sealer are always here. Although contributions via PR is an explicit way to help, we still call for any other ways.
   457  
   458  * reply to other's issues if you could;
   459  * help solve other user's problems;
   460  * help review other's PR design;
   461  * help review other's codes in PR;
   462  * discuss Sealer to make things clearer;
   463  * advocate Sealer technology beyond GitHub;
   464  * write blogs on Sealer and so on.
   465  
   466  In a word, **ANY HELP IS CONTRIBUTION.**