Skip to content

Fix parsing of --kubernetes-host_aliases command-line flag

Stan Hu requested to merge sh-fix-issue-37802 into main

What does this MR do?

Previously the --kubernetes-host_aliases flag would be ignored. The CLI flag module uses Go Reflection to recursively generate command-line flags for the RunnerConfig structure. However, the flag parser inside golang-cli-helpers does not attempt to unmarshal a slice of KubernetesHostAliases structs because it does not know how to map a string to that struct.

As documented in https://github.com/jessevdk/go-flags, a struct can define an UnmarshalFlag method. We can define this method to parse JSON data from the command-line.

Using Go Reflection, I also determined that this was the only command-line argument that needed this custom parsing. I omitted that test case to keep the review straightforward, but I can add it in a separate commit.

Why was this MR needed?

Because you couldn't use this command-line argument in gitlab-runner register otherwise.

What's the best way to test this MR?

  1. Create a new project runner.
  2. Register it via:
./out/binaries/gitlab-runner register -c register-test.toml --kubernetes-host_aliases '[{"ip":"192.168.1.100","hostnames":["myservice.local"]},{"ip":"192.168.1.101","hostnames":["otherservice.local"]}]'
  1. Choose kubernetes as the exector.
  2. register-test.toml should have these aliases:
  [runners.kubernetes]
    host = ""
    bearer_token_overwrite_allowed = false
    image = ""
    namespace = ""
    namespace_overwrite_allowed = ""
    namespace_per_job = false
    node_selector_overwrite_allowed = ""
    node_tolerations_overwrite_allowed = ""
    pod_labels_overwrite_allowed = ""
    service_account_overwrite_allowed = ""
    pod_annotations_overwrite_allowed = ""
    [runners.kubernetes.init_permissions_container_security_context]
      [runners.kubernetes.init_permissions_container_security_context.capabilities]
    [runners.kubernetes.build_container_security_context]
      [runners.kubernetes.build_container_security_context.capabilities]
    [runners.kubernetes.helper_container_security_context]
      [runners.kubernetes.helper_container_security_context.capabilities]
    [runners.kubernetes.service_container_security_context]
      [runners.kubernetes.service_container_security_context.capabilities]
    [runners.kubernetes.volumes]

    [[runners.kubernetes.host_aliases]]
      ip = "192.168.1.100"
      hostnames = ["myservice.local"]

    [[runners.kubernetes.host_aliases]]
      ip = "192.168.1.101"
      hostnames = ["otherservice.local"]

What are the relevant issue numbers?

Relates to #37802 (closed)

Edited by Stan Hu

Merge request reports