|
| 1 | +--- |
| 2 | +weight: 20 |
| 3 | +--- |
| 4 | + |
| 5 | +# GitLab Connector |
| 6 | + |
| 7 | +The GitLab connector is a specialized connector that you can use to connect to GitLab Server (both GitLab.com and self-hosted instances). |
| 8 | + |
| 9 | +You can use the GitLab Connector to securely clone private repositories in CI/CD pipelines, use GitLab CLI (glab) for advanced operations, or use custom CLI tools in containerized workloads to interact with GitLab without directly handling credentials. |
| 10 | + |
| 11 | +Additionally, you can centralize the management of GitLab access configurations across namespaces, avoiding the need to repeat the configuration of GitLab credentials in each namespace. |
| 12 | + |
| 13 | +**Notes**: |
| 14 | + |
| 15 | +- The GitLab connector supports both GitLab.com and self-hosted GitLab instances. |
| 16 | +- The GitLab connector supports using CLI tools (Git CLI, GitLab CLI, and custom CLI tools) to interact with GitLab. |
| 17 | + |
| 18 | +This document will describe: |
| 19 | + |
| 20 | +- Requirements for GitLab servers |
| 21 | +- How to create a GitLab connector based on the GitLab connectorclass |
| 22 | +- Proxy and configuration capabilities of the GitLab connector |
| 23 | + |
| 24 | +## Requirements for GitLab Servers and GitLab CLI (glab) |
| 25 | + |
| 26 | +The GitLab server to be accessed must meet the following conditions: |
| 27 | + |
| 28 | +1. It must support HTTP/HTTPS transport protocols and is only compatible with the Git "smart HTTP" protocol mode. This mode is the standard implementation for modern Git servers, supporting efficient data transfer and authentication mechanisms. |
| 29 | + |
| 30 | +2. For GitLab API operations when using GitLab CLI (glab), the GitLab server must meet the version requirements of glab CLI. More details: [GitLab CLI (glab) Version Requirements](https://gitlab.com/gitlab-org/cli#requirements) |
| 31 | + |
| 32 | +## Creating a GitLab Connector Based on the GitLab ConnectorClass |
| 33 | + |
| 34 | +### Quick Start |
| 35 | + |
| 36 | +```yaml |
| 37 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 38 | +kind: Connector |
| 39 | +metadata: |
| 40 | + name: gitlab-demo |
| 41 | +spec: |
| 42 | + connectorClassName: gitlab |
| 43 | + address: https://gitlab.com |
| 44 | + auth: |
| 45 | + name: patAuth |
| 46 | + secretRef: |
| 47 | + name: gitlab-secret |
| 48 | +``` |
| 49 | +
|
| 50 | +### spec.connectorClassName |
| 51 | +
|
| 52 | +Constant value `gitlab`. |
| 53 | + |
| 54 | +### Description |
| 55 | + |
| 56 | +You can add description information for the GitLab connector through the `annotations` field. |
| 57 | + |
| 58 | +- `cpaas.io/description`: Description information for the GitLab connector. |
| 59 | + |
| 60 | +For example: |
| 61 | + |
| 62 | +```yaml |
| 63 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 64 | +kind: Connector |
| 65 | +metadata: |
| 66 | + name: gitlab-demo |
| 67 | + annotations: |
| 68 | + cpaas.io/description: "Connect to GitLab for accessing team repositories" |
| 69 | +``` |
| 70 | + |
| 71 | +### Address |
| 72 | + |
| 73 | +`spec.address` specifies the access address of the GitLab server. |
| 74 | + |
| 75 | +**Important**: This address is used for repository cloning, not the API address of the GitLab server. When using GitLab CLI (glab), the connector will automatically construct the API address by appending `/api/v4` to this address. |
| 76 | + |
| 77 | +**Examples**: |
| 78 | +- For GitLab.com: `https://gitlab.com` |
| 79 | +- For self-hosted instances: `https://gitlab.example.com` |
| 80 | + |
| 81 | +### Authentication |
| 82 | + |
| 83 | +The GitLab connector supports the following authentication types: |
| 84 | + |
| 85 | +- `patAuth`: Private Access Token authentication (optional), corresponding secret type: `connectors.cpaas.io/gitlab-pat-auth` |
| 86 | + |
| 87 | +#### Using Private Access Token Authentication |
| 88 | + |
| 89 | +Private Access Token (PAT) is the recommended authentication method for GitLab. It provides fine-grained access control and can be easily revoked without changing passwords. |
| 90 | + |
| 91 | +For example: |
| 92 | + |
| 93 | +```yaml |
| 94 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 95 | +kind: Connector |
| 96 | +metadata: |
| 97 | + name: gitlab-demo |
| 98 | +spec: |
| 99 | + connectorClassName: gitlab |
| 100 | + address: https://gitlab.com |
| 101 | + auth: |
| 102 | + name: patAuth |
| 103 | + secretRef: |
| 104 | + name: gitlab-secret |
| 105 | +``` |
| 106 | + |
| 107 | +You need to create a `Secret` in the same namespace, for example: |
| 108 | + |
| 109 | +```yaml |
| 110 | +apiVersion: v1 |
| 111 | +kind: Secret |
| 112 | +metadata: |
| 113 | + name: gitlab-secret |
| 114 | +type: connectors.cpaas.io/gitlab-pat-auth |
| 115 | +stringData: |
| 116 | + token: glpat-xxxxxxxxxxxxxxxxxxxx # Your GitLab Private Access Token |
| 117 | +``` |
| 118 | + |
| 119 | +**Note**: The secret type must be `connectors.cpaas.io/gitlab-pat-auth`. |
| 120 | + |
| 121 | +#### Creating a GitLab Private Access Token |
| 122 | + |
| 123 | +For detailed instructions on how to create a Personal Access Token in GitLab, please refer to the official documentation: `{your-gitlab-server}/help/user/profile/personal_access_tokens.md#create-a-personal-access-token` |
| 124 | + |
| 125 | +**Required Token Scopes:** |
| 126 | + |
| 127 | +When creating the token, select the following scopes based on your needs: |
| 128 | +- `read_repository`: Required for cloning repositories |
| 129 | +- `write_repository`: Required for pushing changes if you want to push changes to the repository |
| 130 | +- `api`: Required for GitLab API operations, e.g. list projects, list branches, list merge requests, etc. |
| 131 | + |
| 132 | +**Security Best Practices:** |
| 133 | + |
| 134 | +For security best practices, we recommend creating tokens with minimal required permissions. When additional privileges are needed, create separate Connectors with more privileged tokens and use namespace isolation to control which users can access each Connector. |
| 135 | + |
| 136 | +#### Using Without Authentication |
| 137 | + |
| 138 | +If the GitLab server does not require authentication (for public repositories), you can omit the `secretRef` field: |
| 139 | + |
| 140 | +```yaml |
| 141 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 142 | +kind: Connector |
| 143 | +metadata: |
| 144 | + name: gitlab-demo |
| 145 | +spec: |
| 146 | + connectorClassName: gitlab |
| 147 | + address: https://gitlab.com |
| 148 | + auth: |
| 149 | + name: patAuth |
| 150 | +``` |
| 151 | + |
| 152 | +## Proxy and Configuration |
| 153 | + |
| 154 | +To enable clients to access GitLab servers without directly handling credentials, the GitLab ConnectorClass provides a proxy server that automatically injects authentication information. |
| 155 | + |
| 156 | +Clients with access to the connector can use this proxy server to access GitLab servers without needing to configure credentials on the client side. |
| 157 | + |
| 158 | +To simplify usage, the GitLab ConnectorClass provides configuration files that can be mounted into Pods via CSI Driver. When executing Git CLI, GitLab CLI (glab), or custom CLI operations in the Pod, these configurations automatically route requests through the proxy service. |
| 159 | + |
| 160 | +### Proxy Address |
| 161 | + |
| 162 | +When creating a GitLab connector, the system will automatically create a Service for proxying access to the GitLab repository. |
| 163 | + |
| 164 | +The system will record the proxy address in the `status.proxy.httpAddress` field. |
| 165 | + |
| 166 | +For example: |
| 167 | + |
| 168 | +```yaml |
| 169 | +apiVersion: connectors.alauda.io/v1alpha1 |
| 170 | +kind: Connector |
| 171 | +metadata: |
| 172 | + name: gitlab-demo |
| 173 | +spec: |
| 174 | + # . . . |
| 175 | +status: |
| 176 | + conditions: |
| 177 | + # . . . |
| 178 | + proxy: |
| 179 | + httpAddress: |
| 180 | + url: http://c-gitlab-demo.default.svc.cluster.local |
| 181 | +``` |
| 182 | + |
| 183 | +### Configuration |
| 184 | + |
| 185 | +The GitLab connector provides the following configurations: |
| 186 | + |
| 187 | +- `gitconfig`: Git configuration file for Git CLI operations |
| 188 | +- `gitlabconfig`: Configuration file for GitLab CLI (glab) operations |
| 189 | + |
| 190 | +The connector also inherits the system built-in configurations from connectors-csi. For more details, see: [Connectors System Built-in Configurations](../../connectors/concepts/connectors_csi.mdx#built-in-configurations) |
| 191 | + |
| 192 | +#### gitconfig |
| 193 | + |
| 194 | +Git configuration file for Git CLI operations. |
| 195 | + |
| 196 | +- Provides a `.gitconfig` file in which the GitLab server address is rewritten to use the proxy address |
| 197 | +- When combined with the connectors-csi-driver, this file is mounted into the Pod |
| 198 | +- Enables access to the GitLab server through the proxy without needing to configure credentials on the client side |
| 199 | + |
| 200 | +Example of the `.gitconfig` file generated in the Pod: |
| 201 | + |
| 202 | +```ini |
| 203 | +[http] |
| 204 | + extraHeader = Authorization: Basic OmV5Smhixxxxxxxxx== |
| 205 | +[url "http://c-gitlab-demo.default.svc"] |
| 206 | + insteadOf = https://gitlab.com |
| 207 | +``` |
| 208 | + |
| 209 | +**How it works:** |
| 210 | +1. The `[http]` section adds an Authorization header with a Kubernetes API server token (a temporary token used for requests to the GitLab connector proxy service) |
| 211 | +2. The `[url]` section rewrites the GitLab server address to use the proxy service |
| 212 | +3. When Git CLI performs operations, it automatically uses these settings |
| 213 | + |
| 214 | +**Usage:** |
| 215 | + |
| 216 | +To use the `gitconfig` configuration in your Pod: |
| 217 | + |
| 218 | +1. Mount the configuration using CSI driver with `configuration.names: "gitconfig"` |
| 219 | +2. Copy the `.gitconfig` file to the user's home directory (typically `/root/`) |
| 220 | +3. Set `.gitconfig` file permissions to `644` |
| 221 | +4. Git CLI will automatically use this configuration for all operations |
| 222 | + |
| 223 | +**Example CSI volume configuration:** |
| 224 | + |
| 225 | +```yaml |
| 226 | +volumes: |
| 227 | +- name: gitconfig |
| 228 | + csi: |
| 229 | + readOnly: true |
| 230 | + driver: connectors-csi |
| 231 | + volumeAttributes: |
| 232 | + connector.name: "gitlab-demo" |
| 233 | + configuration.names: "gitconfig" |
| 234 | +``` |
| 235 | + |
| 236 | +For complete usage examples, see: [Quick Start Guide](../quick_start.mdx) |
| 237 | + |
| 238 | +#### gitlabconfig |
| 239 | + |
| 240 | +Configuration file for GitLab CLI (glab) operations. |
| 241 | + |
| 242 | +- Provides a `config.yml` configuration file for GitLab CLI (glab) |
| 243 | +- Contains GitLab server information, authentication token, and API endpoints |
| 244 | +- Enables glab commands to work without manual configuration |
| 245 | + |
| 246 | +Example of the configuration file generated in the Pod: |
| 247 | + |
| 248 | +```yaml |
| 249 | +git_protocol: http |
| 250 | +host: c-gitlab-demo.default.svc.cluster.local |
| 251 | +no_prompt: true |
| 252 | +telemetry: false |
| 253 | +hosts: |
| 254 | + c-gitlab-demo.default.svc.cluster.local: |
| 255 | + token: k8s-api-token-xxxxxxxx |
| 256 | + container_registry_domains: c-gitlab-demo.default.svc.cluster.local,c-gitlab-demo.default.svc.cluster.local:443,registry.c-gitlab-demo.default.svc.cluster.local |
| 257 | + api_host: c-gitlab-demo.default.svc.cluster.local |
| 258 | + git_protocol: http |
| 259 | + api_protocol: http |
| 260 | + user: any-user |
| 261 | +``` |
| 262 | + |
| 263 | +**How it works:** |
| 264 | + |
| 265 | +1. The configuration directs glab to use the proxy service |
| 266 | +2. The token is provided by the Kubernetes API server (a temporary token used for requests to the GitLab connector proxy service) |
| 267 | +3. Both HTTP and API protocols are configured to use the proxy, which injects authentication information into backend requests |
| 268 | + |
| 269 | +**Usage:** |
| 270 | + |
| 271 | +To use the `gitlabconfig` configuration in your Pod: |
| 272 | + |
| 273 | +1. Mount the configuration using CSI driver with `configuration.names: "gitlabconfig"` |
| 274 | +2. Copy the `config.yml` file to `~/.config/glab-cli/` |
| 275 | +3. Set file permissions to `600` for security |
| 276 | +4. GitLab CLI (glab) will automatically use this configuration for all operations |
| 277 | + |
| 278 | +**Example CSI volume configuration:** |
| 279 | + |
| 280 | +```yaml |
| 281 | +volumes: |
| 282 | +- name: gitlab-config |
| 283 | + csi: |
| 284 | + driver: connectors-csi |
| 285 | + readOnly: true |
| 286 | + volumeAttributes: |
| 287 | + connector.name: "gitlab-demo" |
| 288 | + configuration.names: "gitlabconfig" |
| 289 | +``` |
| 290 | + |
| 291 | +For complete usage examples with glab commands, see: [Using GitLab CLI (glab)](../how_to/using-glab-cli.mdx) |
| 292 | + |
| 293 | +#### Using Multiple Configurations Together |
| 294 | + |
| 295 | +You can mount multiple configurations in the same Pod by separating them with commas in the `configuration.names` field: |
| 296 | + |
| 297 | +```yaml |
| 298 | +volumeAttributes: |
| 299 | + connector.name: "gitlab-demo" |
| 300 | + configuration.names: "gitlabconfig,gitconfig" |
| 301 | +``` |
| 302 | + |
| 303 | +This enables you to use both Git CLI and GitLab CLI (glab) operations in the same Pod. For practical examples, see: |
| 304 | +- [Using GitLab CLI (glab)](../how_to/using-glab-cli.mdx) - Clone Repositories with glab |
| 305 | + |
| 306 | +### Using system built-in configurations |
| 307 | + |
| 308 | +When mounting the gitlab connector, the following built-in configuration files are available: |
| 309 | + |
| 310 | +- `context.token`: The Private Access Token for GitLab authentication |
| 311 | +- `connector.status.proxyAddress`: The proxy service address |
| 312 | + |
| 313 | +These files are useful for custom integrations or CLI tools that need direct access to the authentication token or proxy address. |
| 314 | + |
| 315 | +For a complete example of using built-in configurations, see: [Using Reviewdog with GitLab Connector](../how_to/using-reviewdog-cli.mdx) |
| 316 | + |
| 317 | +More details about system built-in configurations, see: [Connectors-CSI Built-in Configurations](../../connectors/concepts/connectors_csi.mdx#built-in-configurations) |
| 318 | + |
| 319 | +## Further Reading |
| 320 | + |
| 321 | +- [Quick Start Guide](../quick_start.mdx) - Get started with GitLab Connector |
| 322 | +- [Using GitLab CLI (glab)](../how_to/using-glab-cli.mdx) - Learn how to use glab with the connector |
| 323 | +- [Using Reviewdog](../how_to/using-reviewdog-cli.mdx) - Learn how to use custom CLI tools like reviewdog to interact with GitLab |
| 324 | +- [CI/CD Integration](../how_to/using-in-cicd.mdx) - Use in CI/CD pipelines |
| 325 | + |
0 commit comments