登录
首页 >  Golang >  Go问答

Golang Api Kind 结构创建中类型冲突问题

来源:stackoverflow

时间:2024-02-18 16:21:22 324浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《Golang Api Kind 结构创建中类型冲突问题》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我在 client-go 中创建 types.go 时遇到问题。

kind: kafka
metadata:
  name: my-cluster
  namespace: sample-system
spec:
  kafka:
    version: 2.5.0
    replicas: 3
    listeners:
      plain:
        authentiation:
          type: scram-sha-512
      tls:
        authentiation:
          type: tls

如下所示“kafka” ...

我的 kafka kind 架构如下

// kafka is the schema for the kafkas api
type kafka struct {
    metav1.typemeta   `json:",inline"`
    metav1.objectmeta `json:"metadata,omitempty"`
    spec   kafkaspec   `json:"spec,omitempty"`
    status kafkastatus `json:"status,omitempty"`
}

当我需要定义 spec.kafka 类型时

// KafkaSpec defines the desired state of KafkaBundle
type KafkaSpec struct {
    // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
    // Important: Run "make" to regenerate code after modifying this file
    Kafka          *Kafka      `json:"kafka"`
    
    // Foo is an example field of Kafka. Edit Kafka_types.go to remove/update
    Foo string `json:"foo,omitempty"`
}

我面临两种kafka结构类型的问题。这是错误的。


解决方案


您的结构 kafka 有一个依赖结构 kafkaspec 并且您的结构 kafkaspec 依赖于结构 kafka。如果您研究结构,您就会创建循环依赖关系,这可能是一个问题。我建议遵循以下结构,

主结构kafka

// kafka is the schema for the kafkas api
type kafka struct {
    metav1.typemeta   `json:",inline"`
    metav1.objectmeta `json:"metadata,omitempty"`
    spec   kafkaspec   `json:"spec,omitempty"`
    status kafkastatus `json:"status,omitempty"`
}

结构 speckafka 的子级。

// spec defines the desired state of kafkabundle
type spec struct {
    // insert additional spec fields - desired state of cluster
    // important: run "make" to regenerate code after modifying this file
    kafka          *kafkaspec      `json:"kafka"`
    
    // foo is an example field of kafka. edit kafka_types.go to remove/update
    foo string `json:"foo,omitempty"`
}

结构体 kafkaspec 是结构体 spec 的子结构

type KafkaSpec struct {
    Replicas int `json:"replicas"`
    Listeners *Listners
}

类似地为 plainauthentication 创建一个结构。

希望这能解决您的问题。

今天关于《Golang Api Kind 结构创建中类型冲突问题》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>