Jenny M
Jenny M

Reputation: 1023

Parse byte array for yaml value - kubebuilder

We are using kubebuilder to build our custom controller, the problem is we are not able to parse raw data as it comes empty when you apply the file.

I’ve created very minimal example which describe the issue.

apiVersion: mygroup.test.com/v1alpha1

kind: Rawtest
metadata:
  name: rawtest-sample
spec:
  system:
    type: test
    provider:
      type: aws
      infrastructureConfig:
        kind: InfrastructureConfig
        apiVersion: v1alpha1
        networks:
          vpc:
            cidr: aaa
          zones:
            - name: abc
              internal: 123
      workers:
        - name: myworker
          machine:
            type: "mt"
            image:
              name: name1
              version: "2"
          maximum: 2
          minimum: 1
          maxUnavailable: 0
          volume:
            type: a1
            size: 20Gi
          zones:
            - zone1

In runtime I was able to get the the spec.system.type value=test and spec.system.provider.type value="aws", however I wasn’t able to get all the data under the infrastructureConfig: (line 10) any idea how can I overcome this ?

I’ve created this very simple project to demonstrate the issue , See the api/type folder, after getting the reconcile object (after apply the config/sample/ file ,you see that the infrastructureconfig and all related data are

https://github.com/JennyMet/

Here is the code which is trying to read the raw value https://github.com/JennyMet/kuberaw/blob/master/controllers/rawtest_controller.go#L57

&rawtest should contain all the data

please see the type https://github.com/JennyMet/kuberaw/blob/master/api/v1alpha1/rawtest_types.go#L32

raw type https://github.com/gardener/gardener/blob/bf32324d9d1a366d8a0a7514956dc39c2f22f7b7/pkg/apis/core/v1beta1/types_shoot.go#L945

https://github.com/gardener/gardener/blob/bf32324d9d1a366d8a0a7514956dc39c2f22f7b7/pkg/apis/core/types_shoot.go#L774

https://github.com/gardener/gardener/blob/bf32324d9d1a366d8a0a7514956dc39c2f22f7b7/vendor/k8s.io/apimachinery/pkg/runtime/types.go#L94:6

I need a way to make it work in the kubebuilder, as while I apply the file I dont get the values in debug ...

debug pic

enter image description here

if something is missing please let me know.

Upvotes: 3

Views: 2462

Answers (1)

Vorsprung
Vorsprung

Reputation: 34427

I did the following

  1. visit a site such as https://yaml.to-go.online/ convert the yaml to a go Struct

  2. make a struct

    t := AutoGenerated{}

  3. I assume that the yaml data is loaded into memory as []byte

  4. using "gopkg.in/yaml.v2", Unmarshall the yaml to the struct

    err := yaml.Unmarshal([]byte(data), &t)

  5. the infrastructureconfig field is available as t.Spec.System.Provider.InfrastructureConfig

see https://play.golang.org/p/syx8v7gAmDH

Upvotes: 0

Related Questions