buildkite / agent-stack-k8s

Spin up an autoscaling stack of Buildkite Agents on Kubernetes
MIT License
77 stars 30 forks source link

Fix resource.Quantity parsing #301

Closed DrJosh9000 closed 4 months ago

DrJosh9000 commented 5 months ago

Fixes #293

Fortunately this error was easy enough to replicate without Helm or having to set up a local registry: just run --org (org) --buildkite-token (token) --debug -f config.yaml where config.yaml was:

pod-spec-patch:
  containers:
    - name: container-0
      resources:
        requests:
          cpu: 100m

For parsing our config once the controller starts, Viper gets the first bite (pun intended), and it uses mapstructure. Immediately before the failing call (v.UnmarshalExact(...)), Viper contains something like (obtained with v.Debug()):

Config:
map[string]interface {}{"pod-spec-patch":map[string]interface {}{"containers":[]interface {}{map[string]interface {}{"name":"container-0", "resources":map[string]interface {}{"requests":map[string]interface {}{"cpu":"100m"}}}}}}

Viper doesn't know how to put "100m" into a resource.Quantity. resource.Quantity knows how to unmarshal itself from JSON, but neither mapstructure nor Viper call that. Instead we must alter the decode hook used by Viper from its default (that conveniently understands time.Duration strings and comma-separated lists).