weaveworks / weave-gitops-enterprise

This repo provides the enterprise level features for the weave-gitops product, including CAPI cluster creation and team workspaces.
https://docs.gitops.weave.works/
Apache License 2.0
160 stars 29 forks source link

Error connecting cluster causing panic #3672

Open bigkevmcd opened 11 months ago

bigkevmcd commented 11 months ago

Describe the bug

❯ gitops-ee connect cluster test
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x209da22]

goroutine 1 [running]:
github.com/weaveworks/weave-gitops-enterprise/pkg/cluster/connector.getSecretNameFromCluster({0x3658790, 0xc001192f30}, {0x36383a0, 0xc0013f51d0}, 0xc0000c3950?, {{0x2f3b6d2, 0x7}, {0x7ffd1083b106, 0x4}})
    /home/runner/work/weave-gitops-enterprise/weave-gitops-enterprise/pkg/cluster/connector/secret.go:38 +0x3c2
github.com/weaveworks/weave-gitops-enterprise/pkg/cluster/connector.getSecretNameForConfig({0x3658790, 0xc001192f30}, 0xc0000c39a0?, 0xc00103fc28)
    /home/runner/work/weave-gitops-enterprise/weave-gitops-enterprise/pkg/cluster/connector/connector.go:66 +0x71
github.com/weaveworks/weave-gitops-enterprise/pkg/cluster/connector.ConnectCluster({0x3658790, 0xc001192f30}, 0xc00149fc28)
    /home/runner/work/weave-gitops-enterprise/weave-gitops-enterprise/pkg/cluster/connector/connector.go:87 +0x136
github.com/weaveworks/weave-gitops-enterprise/cmd/gitops/app/connect/clusters.connectClusterCmdRunE.func1(0xc000f81800, {0xc000d35610?, 0x1?, 0x1?})
    /home/runner/work/weave-gitops-enterprise/weave-gitops-enterprise/cmd/gitops/app/connect/clusters/cmd.go:61 +0x1bb
github.com/spf13/cobra.(*Command).execute(0xc000f81800, {0xc000d355e0, 0x1, 0x1})
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:940 +0x862
github.com/spf13/cobra.(*Command).ExecuteC(0xc000004900)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3bd
github.com/spf13/cobra.(*Command).Execute(0xc000062060?)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:992 +0x19
main.main()
    /home/runner/work/weave-gitops-enterprise/weave-gitops-enterprise/cmd/gitops/main.go:14 +0x6a

To Reproduce

Actual behaviour

Expected behaviour

Additional context

darrylweaver commented 11 months ago

This was generated with a GitopsCluster manually added to the WGE management cluster:

apiVersion: gitops.weave.works/v1alpha1
kind: GitopsCluster
metadata:  
  name: test
  namespace: default

The spec is missing as well as any labels, which could be causing this issue.

darrylweaver commented 11 months ago

The in-cluster object was:

apiVersion: gitops.weave.works/v1alpha1
kind: GitopsCluster
metadata:
  creationTimestamp: '2023-11-23T13:56:27Z'
  generation: 1
  labels:
    kustomize.toolkit.fluxcd.io/name: flux-system
    kustomize.toolkit.fluxcd.io/namespace: flux-system
  managedFields:
    - apiVersion: gitops.weave.works/v1alpha1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:labels:
            f:kustomize.toolkit.fluxcd.io/name: {}
            f:kustomize.toolkit.fluxcd.io/namespace: {}
      manager: kustomize-controller
      operation: Apply
      time: '2023-11-23T13:56:27Z'
  name: test
  namespace: default
  resourceVersion: '148517'
  uid: a52a145d-d278-4ecc-980c-0b24f32139a2
  selfLink: /apis/gitops.weave.works/v1alpha1/namespaces/default/gitopsclusters/test

which of course has labels, but still no spec.

bigkevmcd commented 11 months ago

Two things to fix then...

  1. We need an admission controller that checks that the GitopsCluster has at least a capi reference, or secret reference.
  2. Connecting a cluster should not error for invalid Clusters.
darrylweaver commented 11 months ago

I added the spec to the object and it didn't fix the issue, I still get the same error.
I created an empty secret for the kubeconfig reference, in case it needed to exist, but I still get the same error. I added data for a kubeconfig to the secret, same error. I added an empty secret for certs too, but the same error.

I haven't found a way to avoid it yet, so no clue what is wrong here. Something missing probably. I tried with debug, but I don't get much more information, just that it detects the GitopsCluster object exists, then I hit the error.

bigkevmcd commented 11 months ago

If it's the same error, it's failing here: https://github.com/weaveworks/weave-gitops-enterprise/blob/498fa43fe1a6e5515ba8a2e70c2ffbbe8a6a3301/pkg/cluster/connector/secret.go#L38

Which implies that the secretRef isn't configured?

bigkevmcd commented 11 months ago

Can you paste the YAML for your GitopsCluster?

darrylweaver commented 11 months ago

For reference, here is the GitopsCluster object with the reference added.

apiVersion: gitops.weave.works/v1alpha1
kind: GitopsCluster
metadata:
  creationTimestamp: '2023-11-23T13:56:27Z'
  generation: 2
  labels:
    kustomize.toolkit.fluxcd.io/name: flux-system
    kustomize.toolkit.fluxcd.io/namespace: flux-system
  managedFields:
    - apiVersion: gitops.weave.works/v1alpha1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:labels:
            f:kustomize.toolkit.fluxcd.io/name: {}
            f:kustomize.toolkit.fluxcd.io/namespace: {}
        f:spec:
          f:capiClusterRef:
            f:name: {}
      manager: kustomize-controller
      operation: Apply
      time: '2023-11-23T17:07:15Z'
  name: test
  namespace: default
  resourceVersion: '193300'
  uid: a52a145d-d278-4ecc-980c-0b24f32139a2
  selfLink: /apis/gitops.weave.works/v1alpha1/namespaces/default/gitopsclusters/test
spec:
  capiClusterRef:
    name: test
bigkevmcd commented 11 months ago

Ahh...you don't need to connect a capi cluster...it just "works"?

But we can definitely detect this and improve the message.

darrylweaver commented 11 months ago

Of course it does, I copied it from another GitopsCluster object. I have now added the correct secretRef and removed the extra secrets I manually created. That has solved the problem, so the missing secretRef was the issue.

Output now:

gitops-ee connect cluster test
{"level":"info","ts":1700760829.72523,"logger":"gitops","caller":"connector/connector.go:117","msg":"Successfully connected cluster","cluster":{"name":"test","namespace":"default"}}
bigkevmcd commented 11 months ago

We'll definitely improve the errors around this.

bigkevmcd commented 11 months ago

this cluster does not reference a secret - can only connect clusters that reference a secret