cnrancher / autok3s

Run K3s Everywhere
https://www.suse.com
Apache License 2.0
741 stars 76 forks source link

[Feature Request] Support get the dynamic port of the k3d cluster #645

Open LinuxSuRen opened 9 months ago

LinuxSuRen commented 9 months ago

Is your feature request related to a problem? Please describe. I'm trying to create a k3d cluster via API of autok3s in a CI stage. Then deploy my application into that cluster. But I cannot get the port which exposed by k3d dynamically.

Describe the solution you'd like Return the exposed port via the following API:

/v1/clusters/k3d-name?link=nodes

Describe alternatives you've considered None.

Additional context In case of anyone want to reproduce it easily. Please see the following YAML file. And you can get atest from [here](run -p bin/test.yaml).

atest run -p bin/test.yaml

#!api-testing
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json
name: testing
param:
  k3sname: "{{randAlpha 4}}"
  server: "localhost"
  port: "9090"
items:
- name: createk3s
  request:
    api: http://{{.param.server}}:{{.param.port}}/v1/clusters
    method: POST
    body: |
      {
        "datastore-cafile-content": "",
        "datastore-certfile-content": "",
        "datastore-keyfile-content": "",
        "master": "1",
        "master-extra-args": "",
        "masters-memory": "",
        "name": "{{.param.k3sname}}",
        "network": "",
        "registry": "",
        "registry-content": "",
        "token": "",
        "worker": "1",
        "worker-extra-args": "",
        "workers-memory": "",
        "rollback": true,
        "provider": "k3d",
        "options": {
            "api-port": "{{.param.server}}:0",
            "envs": null,
            "gpus": "",
            "image": "docker.io/rancher/k3s:v1.27.6-k3s1",
            "labels": null,
            "masters-memory": "",
            "no-image-volume": false,
            "no-lb": false,
            "ports": ["30000:30000"],
            "volumes": null,
            "workers-memory": ""
        }
      }
  expect:
    statusCode: 201
- name: getKubeConfig
  before:
    items:
      - httpReady("http://{{.param.server}}:{{.param.port}}/v1/clusters/k3d-{{.param.k3sname}}?link=nodes", 300, "data.status == 'Running'")
  request:
    api: http://{{.param.server}}:{{.param.port}}/v1/clusters/k3d-{{.param.k3sname}}?action=download-kubeconfig
    method: POST
    header:
      Accept: application/json
  after:
    items:
      - writeFile("a.log", current.config)
      - command("helm install --kubeconfig a.log atest helm/api-testing --set persistence.enabled=false --set service.type=NodePort --set service.nodePort=30000")