kiwigrid / k8s-sidecar

This is a docker container intended to run inside a kubernetes cluster to collect config maps with a specified label and store the included files in a local folder.
MIT License
613 stars 183 forks source link

UnboundLocalError: cannot access local variable 'dest_folder' where it is not associated with a value - 1.22 #268

Closed mkjmdski closed 1 year ago

mkjmdski commented 1 year ago

While upgrading image version on helm deployment of grafana (this specific value -> https://github.com/grafana/helm-charts/blob/main/charts/grafana/values.yaml#L774), with given config:

        sidecar:
          image:
            repository: quay.io/kiwigrid/k8s-sidecar
            tag: 1.22.4

          resources:
            limits:
              cpu: 100m
              memory: 128Mi
            requests:
              cpu: 100m
              memory: 128Mi

          dashboards:
            # use when upgrading version of sidecar to spot issues
            # logLevel: DEBUG
            enabled: true
            label: grafana_dashboard
            folderAnnotation: grafana_dashboard_dir
            provider:
              foldersFromFilesStructure: true

          datasources:
            # use when upgrading version of sidecar to spot issues
            # logLevel: DEBUG
            enabled: true
            label: grafana_datasource
            skipReload: true
            initDatasources: true

last working value was: 1.19.2. Probably it's regression from 1.22 -> https://github.com/kiwigrid/k8s-sidecar/pull/231

anonymized stacktrace from DEBUG log of init-container grafana-init-sc-datasources. All of our datasources are secretes configured via ExternalSecret in extraObjects: key. They all contain k8s-sidecar-target-directory: "." annotation.

{"time": "2023-04-03T14:24:50.726994+00:00", "msg": "Working on secret: default/grafana-datasource", "level": "DEBUG"}
{"time": "2023-04-03T14:24:50.727101+00:00", "msg": "Found a folder override annotation, placing the grafana-datasource in: /etc/grafana/provisioning/datasources/.", "level": "INFO"}
{"time": "2023-04-03T14:24:50.727705+00:00", "msg": "Writing azure.yaml", "level": "DEBUG"}
{"time": "2023-04-03T14:24:50.727894+00:00", "msg": "Contents of azure.yaml haven't changed. Not overwriting existing file", "level": "DEBUG"}
... x 7 for all of the secrets
{"time": "2023-04-03T14:24:50.820574+00:00", "msg": "response body: {\"kind\":\"ConfigMapList\",\"apiVersion\":\"v1\",\"metadata\":{\"resourceVersion\":\"586142921\"},\"items\":[]}\n", "level": "DEBUG"}
Traceback (most recent call last):
  File "/app/sidecar.py", line 155, in <module>
    main()
  File "/app/sidecar.py", line 113, in main
    list_resources(label, label_value, target_folder, request_url, request_method, request_payload,
  File "/app/resources.py", line 128, in list_resources
    files_changed |= _process_config_map(dest_folder, item, resource, unique_filenames, enable_5xx, True)
                                         ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'dest_folder' where it is not associated with a value

cc: @AvihaiSam

bartoszcisek commented 1 year ago
Screenshot 2023-04-03 at 18 14 08

This looks like a good candidate.

jekkel commented 1 year ago

Thanks for the report! Since you seem to have found the issue already, any chance you could provide the fix as well?

mkjmdski commented 1 year ago

Hi @jekkel I don't feel like I have capacity at the moment to provide a fix. I hope someone else could pick this issue. I will have time later to look into that but that might not happen so soon.

mario-steinhoff-gcx commented 1 year ago

Hey, I fixed this in my local branch, PR will follow shortly.