Tool:Iw

From Wikitech
Jump to navigation Jump to search
Toolforge tools
Crystal Clear app package utilities.png
Website https://iw.toolforge.org
Description Redirect to Toolforge tools
Keywords interwiki
Maintainer(s) BryanDavis (View all)
License GNU General Public License 3.0 or later
Admin log Tools.iw/SAL

Iw is a redirection service designed to be used for creating interwiki links to Toolforge tools.

Interwiki links are used in many Wikimedia wikis including Wikitech to create links to Toolforge tools. The interwiki system is prefix based and does not currently robustly support a single interwiki prefix targeting multiple hosts. This creates a problem for migration of Toolforge tools from path based routing (https://tools.wmflabs.org/$TOOL) to host based routing (https://$TOOL.toolforge.org/). The iw tool is an attempt to bridge this gap by providing a prefix (https://iw.toolforge.org/) with a backend that will redirect to other Toolforge tools.

On-wiki usage

See meta:Interwiki map for configuration management. Request for current configuration.

Tool setup

Iw is an Ingress only tool running on Toolforge's 2020 Kubernetes cluster. "Ingress only" means that the tool does not run any application code, but instead does all of its work using Kubernetes Ingress objects to configure the Kubernetes cluster's nginx ingress controller.

$ ssh login.toolforge.org
$ become iw
$ vim ingress.yaml
$ kubectl apply --validate=true -f ingress.yaml
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: iw-domain
  namespace: tool-iw
  labels:
    name: iw-domain
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    # Redirect to the host based route for the target tool at *.toolforge.org
    nginx.ingress.kubernetes.io/temporal-redirect: https://$1.toolforge.org/$3$is_args$args
spec:
  rules:
    - host: iw.toolforge.org
      http:
        paths:
          - backend:
              serviceName: unused
              servicePort: 8000
            path: /([^/]+)(/|$)(.*)
            # Example path: /bash/random
            # Captures:
            # $0 == /bash/random
            # $1 == bash
            # $2 == /
            # $3 == random
---
# T257104: ingress to handle https://iw.toolforge.org/
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: iw-root
  namespace: tool-iw
  labels:
    name: iw-root
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/temporal-redirect: https://toolforge.org/$is_args$args
spec:
  rules:
    - host: iw.toolforge.org
      http:
        paths:
          - backend:
              serviceName: unused
              servicePort: 8000
            path: /$

Testing

You can run the following script to verify URLs redirect as expected.

import requests

urls = {
    '/': 'https://wikitech.wikimedia.org/wiki/Portal:Toolforge',
    '/test': 'https://test.toolforge.org/',
    '/test/path/': 'https://test.toolforge.org/path/',
    '/test/?query=foo': 'https://test.toolforge.org/?query=foo'
}

for iw, expected in urls.items():
    iw_url = 'https://iw.toolforge.org' + iw
    req = requests.get(iw_url)
    assert req.url == expected, f'{iw_url} redirected to {expected}'
    print('.', end='')

print('')
print('All URLs redirected properly')