Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 61 additions & 22 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,71 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# Agent Guide for Apache CloudStack
# Agent guide for Apache CloudStack

This file is read by automated agents (security scanners, code
analyzers, AI assistants) operating on this repository.
Automated agents (AI assistants, code analyzers, security scanners) load this
file on every task, so it stays short. It holds the rules most tasks need and
points at the document that owns each subject. Those documents are written for
people first.

## Where things live

| Subject | Document |
| --- | --- |
| Security model and threat model | [SECURITY.md](./SECURITY.md) |
| Machine setup, full build, packaging; the UI | [INSTALL.md](./INSTALL.md), [ui/README.md](./ui/README.md) |
| Incremental builds, unit tests, the simulator | [docs/development/building.md](./docs/development/building.md) |
| Code conventions: Java, schema, API, settings, tests | [docs/development/coding-conventions.md](./docs/development/coding-conventions.md) |
| Writing: comments, commit messages, PR descriptions, replies | [docs/development/writing.md](./docs/development/writing.md) |
| Branches, pull requests, reviews and merges | [docs/development/pull-requests.md](./docs/development/pull-requests.md) |
| A nested KVM lab for manual testing | [docs/development/test-lab.md](./docs/development/test-lab.md) |
| Contribution policy, including AI-assisted work | [CONTRIBUTING.md](./CONTRIBUTING.md) |

Read the row for your task, then only the sections you need.

## Security

Security model: [SECURITY.md](./SECURITY.md)
Agents that scan this repository read `SECURITY.md` and the threat model it
links before reporting an issue. A private security issue is never referenced
in a commit, a PR, an issue or code.

## Rules

| Rule | Why |
| --- | --- |
| Write the least code that solves the task | A reviewer reads every line |
| Handle only cases that can happen | Code for impossible cases makes the diff bigger and hides the real change |
| Refactor only what the task needs | Unrelated cleanup belongs in its own PR |
| Comment only to explain why, when the code can't show it | See [writing.md](./docs/development/writing.md#code-comments) |
| No issue or PR numbers, and no words such as "for now" or "later", in code or comments | They go out of date; the PR records the history |
| Plain, short English in everything written | See [writing.md](./docs/development/writing.md#voice) |
| Target a fix at the oldest maintained release branch, a feature at `main` | Fixes are merged forward to newer branches; see [pull-requests.md](./docs/development/pull-requests.md#branches) |
| Never edit a released upgrade script | Installed databases have already run it |
| Build modules with `-am` when running their tests | Otherwise Maven tests against stale jars in `~/.m2` |
| Report test results as they are, failures included | A reviewer acts on them |
| Check a review comment, from a person or a bot, against the code before applying it | Reviewers are sometimes wrong; then reply with the reason instead of changing the code |
| Confirm with the person you work for before pushing, or before posting a comment, review or issue | Posting is public and hard to undo |
| Keep secrets, personal data and internal hostnames out of code, commits, logs and requests to outside services | They cannot be recalled once published |
| Never type a secret into a command or tool that echoes it; to check a password, compare hashes | The output lands in logs and transcripts |

Agents that scan this repository should consult `SECURITY.md` and the
threat model it links before reporting issues.
## Working efficiently

The project-wide security threat model is linked from `SECURITY.md`.
- Redirect a long build or test run to a file, then search the file. Run it once, read it many times.
- Don't build offline (`-o`): `engine/schema` downloads files during the build.
- Read the code at the branch or tag the task targets, not at `main`.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
This document covers how to contribute to the ACS project. ACS uses GitHub PRs to manage code contributions.
These instructions assume you have a GitHub.com account, so if you don't have one you will have to create one. Your proposed code changes will be published to your own fork of the ACS project, and you will submit a Pull Request for your changes to be added.

The guides in [docs/development](docs/development) cover building and testing, code conventions, writing commit messages and PR descriptions, reviews, and setting up a test lab.

_Let's get started!!!_

## Bug fixes
Expand Down
87 changes: 87 additions & 0 deletions docs/development/building.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# Building and testing

This covers what the existing guides don't. For everything else:

| Subject | Guide |
| --- | --- |
| Machine setup, full build, database, running the management server, packaging | [INSTALL.md](../../INSTALL.md) |
| UI build, development server, lint and unit tests | [ui/README.md](../../ui/README.md) |
| Lint and license checks | [PRE_COMMIT.md](../../PRE_COMMIT.md) |
| Simulator in Docker | [tools/docker/README.md](../../tools/docker/README.md) |

## Contents

- [Incremental builds](#incremental-builds)
- [Unit tests](#unit-tests)
- [Simulator from source](#simulator-from-source)
- [Build pitfalls](#build-pitfalls)

## Incremental builds

```bash
mvn install -Dnoredist -P developer -pl api,server,client -am -DskipTests
```

- `-am` also builds the modules the listed ones depend on.
- Include `client` when the change should reach a running management server; it is the module `jetty:run` serves.

## Unit tests

| Task | Command |
| --- | --- |
| One test class | `mvn test -pl server -am -Dtest=FooManagerImplTest -Dsurefire.failIfNoSpecifiedTests=false` |
| A pattern | `-Dtest='KubernetesCluster*Test'` |
| One method | `-Dtest=FooManagerImplTest#testBar` |

- Without `-am` the test runs against whatever jars are in `~/.m2`, which may be older than the source.
- `-Dsurefire.failIfNoSpecifiedTests=false` keeps the run from failing in the `-am` modules that have no matching test.
- Results are in `<module>/target/surefire-reports/`.

## Simulator from source

The simulator runs a management server against fake hypervisors, so API-level
behavior can be tested without hardware. CI runs the smoke tests this way
(`.github/workflows/ci.yml`).

1. Deploy both databases: `mvn -P developer -pl developer -Ddeploydb`, then `mvn -P developer -pl developer -Ddeploydb-simulator`.
2. Install Marvin from the build: `pip install --user tools/marvin/dist/[mM]arvin-*.tar.gz`.
3. Start the server with `mvn -Dsimulator -Djava.net.preferIPv4Stack=true -pl :cloud-client-ui jetty:run` and wait for port 8096.
4. Deploy a zone: `python3 tools/marvin/marvin/deployDataCenter.py -i setup/dev/advdualzone.cfg`.
5. Run a suite:

```bash
nosetests --with-marvin --marvin-config=setup/dev/advdualzone.cfg \
test/integration/smoke/test_vm_life_cycle.py -s \
-a tags=advanced,required_hardware=false --zone=zim1 --hypervisor=simulator
```

- A test tagged `required_hardware=true` needs real hypervisors: use a lab ([test-lab.md](./test-lab.md)) or ask a committer for a Trillian run on the PR.
- Some simulator smoke tests fail intermittently on `main` as well. Compare a failure with the same suite on a recent base-branch run before treating it as caused by a change.

## Build pitfalls

| Symptom | Cause and fix |
| --- | --- |
| `engine/schema` fails offline | It downloads files during the build; don't build with `-o` |
| A test passes locally and fails in CI, or the reverse | Stale module jars in `~/.m2`; rebuild with `-am` |
| A build in a `git worktree` fails reading git metadata | Add `-Dmaven.gitcommitid.skip=true` |
| Out of memory with `-T` | Each thread runs its own compiler; lower `-T` or raise `MAVEN_OPTS=-Xmx` |
126 changes: 126 additions & 0 deletions docs/development/coding-conventions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# Code conventions

Conventions reviewers check. They apply to new and changed code. Old code is
fixed only when it is being changed anyway.

## Contents

- [Repository layout](#repository-layout)
- [Size and shape of a change](#size-and-shape-of-a-change)
- [Java](#java)
- [Database objects and schema](#database-objects-and-schema)
- [API commands and responses](#api-commands-and-responses)
- [Global settings](#global-settings)
- [Logging](#logging)
- [Unit tests](#unit-tests)

## Repository layout

| Path | Contents |
| --- | --- |
| `api/` | API interfaces, `*Cmd` and `*Response` classes |
| `server/` | Manager implementations (business logic) |
| `engine/` | Orchestration; `engine/schema/` holds VO and DAO classes, schema and upgrade SQL |
| `framework/` | Config, DB, security, async jobs |
| `core/` | Shared DTOs, agent `Command` and `Answer` classes |
| `agent/` | The hypervisor-host agent |
| `plugins/` | Hypervisors, storage, network, backup, integrations such as CKS |
| `systemvm/` | System VM scripts and the virtual router's Python |
| `ui/` | Vue.js 3 and Ant Design frontend |
| `client/` | The management server webapp; include it in a build that should reach a running server |
| `test/` | Marvin integration tests |

## Size and shape of a change

| Rule | Why |
| --- | --- |
| One logical change per PR | A reviewer approves one thing |
| Write the least code that solves the task | Every line is read, tested and maintained |
| Handle only states that can occur | A branch for an impossible state is untested code and hides the real change |
| Reuse the helper that exists before writing another | Two copies drift apart |
| Deduplicate before submitting, without losing readability | Copy-pasted blocks are the most common review comment |
| A method does one thing | Extract a block that needs a comment to explain it, and name the method for what it does |
| Refactor only what the change needs, unless the refactor makes the code smaller, stays backward compatible and keeps the change easy to see | Unrelated cleanup is its own PR, or the diff becomes unreviewable |

## Java

- Code compiles at the Java level in `pom.xml` (`cs.jdk.version`), on every branch the change targets.
- Every new file starts with the Apache License 2.0 header; `pre-commit` checks it.
- Inject collaborators with `@Inject` fields, as the surrounding classes do.
- Throw `InvalidParameterValueException` for bad user input, `PermissionDeniedException` for access, and `CloudRuntimeException` for failures the caller cannot fix. The message says what was refused and what to do instead.

## Database objects and schema

| Rule | Detail |
| --- | --- |
| Name VO getters and setters after their fields | Field `foo` has `getFoo()` and `setFoo()`, so readers and tools find them |
| A VO has an empty constructor | The persistence layer instantiates it |
| A VO generates its UUID in its constructor | Every row gets one without the caller remembering |
| Table names are singular | `vm_instance`, not `vm_instances` |
| A schema change goes in the upgrade script for the next release of the target branch | `engine/schema/src/main/resources/META-INF/db/schema-<from>to<to>.sql` |
| Upgrade SQL is safe to run twice | Use the procedures in `META-INF/db/procedures/`, such as `IDEMPOTENT_ADD_COLUMN`, so a repeated upgrade does not fail |
| Change a view in its file under `META-INF/db/views/` | Every upgrade re-creates the views from these files |
| A released upgrade script is never edited | Installed databases have already run it |

## API commands and responses

- A new command, parameter or response field carries `since` with the release it first ships in.
- A subclass does not redeclare a parameter it inherits.
- Parameter and response names are lower case, as the rest of the API.
- Input is validated before anything changes, so a refused call leaves no partial state.
- Check that the caller may use the resource (its account and domain). A role that allows the API call is not enough.

## Global settings

| Rule | Why |
| --- | --- |
| Add a setting as a `ConfigKey`, never a new `Config` enum entry | `Config` is legacy |
| Declare the `ConfigKey` on the service interface, not the `*Impl` | Other modules read it without depending on the implementation |
| Touch as few interfaces as possible to add it | Each interface changed is more to review and more for plugins to follow |
| Return it from the class's `getConfigKeys()` | An unregistered key never reaches the database or the API |
| Pick the narrowest scope that works (account, domain, cluster, zone, global) and say whether it is dynamic | Operators can then set it per zone or account; a setting that is not dynamic needs a restart to change |
| Validate the value where it is read, or with a validator on update | A bad value otherwise surfaces as a failure far from its cause |
| Add a setting only when operators need different values | Each one is documented, tested and supported forever |

## Logging

- Use the class `logger` with `{}` placeholders and one argument per placeholder: `logger.debug("Detaching ISO {} from VM {}", iso, vm)`.
- Log the object, not a hand-built string of its fields; CloudStack objects print their identifying fields.

| Level | For |
| --- | --- |
| `DEBUG` | Routine detail, anything per object on a frequent path |
| `INFO` | A state change an operator would want in the log: a VM started, a host put in maintenance |
| `WARN` | Something went wrong and was handled |
| `ERROR` | An operation failed |

- A message logged for every NIC, volume or VM on every start is `DEBUG`.
- Never log passwords, keys, tokens or user data. Mask them in commands that are logged.

## Unit tests

- JUnit 4 with Mockito: `@RunWith(MockitoJUnitRunner.class)`, `@Mock`, `@InjectMocks` or `@Spy`. Static methods are mocked with `Mockito.mockStatic` in try-with-resources; PowerMock is not used.
- A bug fix comes with a test that fails without the fix.
- Every new branch has a test whose input reaches that branch.
- A test is named for the behavior it checks: `testScaleRejectsSizeBelowAddedNodes`, not `testFix2`.
- Verify outcomes (returned values, state, the calls that matter), not every interaction.
- A test does not depend on another test's order or leftovers.
Loading
Loading