简体   繁体   English

解析 yaml 值的字节数组 - kubebuilder

[英]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.我们正在使用kubebuilder构建我们的自定义 controller,问题是我们无法解析原始数据,因为当您应用文件时它为空。

I've created very minimal example which describe the issue.我创建了一个非常简单的例子来描述这个问题。

apiVersion: mygroup.test.com/v1alpha1 api版本: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?在运行时,我能够获得spec.system.type value=test 和spec.system.provider.type value="aws",但是我无法获得infrastructureConfig:第 10 行)知道如何克服这个问题吗?

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我创建了这个非常简单的项目来演示这个问题,查看 api/type 文件夹,在获得协调 object 之后(应用 config/sample/ 文件后,您会看到infrastructureconfig配置和所有相关数据是

https://github.com/JennyMet/ 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这是试图读取原始值https://github.com/JennyMet/kuberaw/blob/master/controllers/rawtest_controller.go#L57的代码

&rawtest should contain all the data &rawtest应该包含所有数据

please see the type https://github.com/JennyMet/kuberaw/blob/master/api/v1alpha1/rawtest_types.go#L32请查看类型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/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/pkg/apis/core/types_shoot.go#L774

https://github.com/gardener/gardener/blob/bf32324d9d1a366d8a0a7514956dc39c2f22f7b7/vendor/k8s.io/apimachinery/pkg/runtime/types.go#L94:6 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...我需要一种方法让它在 kubebuilder 中工作,因为当我应用文件时,我没有在调试中得到值......

debug pic调试图片

在此处输入图像描述

if something is missing please let me know.如果缺少某些东西,请告诉我。

I did the following我做了以下

  1. visit a site such as https://yaml.to-go.online/ convert the yaml to a go Struct访问诸如https://yaml.to-go.online/之类的站点,将 yaml 转换为 go 结构

  2. make a struct做一个结构

    t:= AutoGenerated{} t:= 自动生成{}

  3. I assume that the yaml data is loaded into memory as []byte我假设 yaml 数据作为 []byte 加载到 memory

  4. using "gopkg.in/yaml.v2", Unmarshall the yaml to the struct使用“gopkg.in/yaml.v2”,将 yaml 解组到结构

    err:= yaml.Unmarshal([]byte(data), &t)错误:= yaml.Unmarshal([]字节(数据),&t)

  5. the infrastructureconfig field is available as t.Spec.System.Provider.InfrastructureConfig Infrastructureconfig 字段可用作 t.Spec.System.Provider.InfrastructureConfig

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM