Go语言 如何格式化使用Crossplane生成的提供程序资源?

vsmadaxz  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(91)

我希望建立一个跨平面供应商为我的组织,但我运行的问题与代码生成。
有没有一种方法可以操作资源名称是如何生成的?Upjet代码生成工具似乎不尊重某些资源的命名格式。
例如:
我们有这种格式的地形资源。provider_load_balancer在添加资源并运行make generate之后,将生成资源。但是,会生成以下内容。

├── apis
│   ├── generate.go
│   ├── load
│   │   └── v1alpha1
│   │       ├── zz_balancer_types.go
│   │       ├── zz_generated.deepcopy.go
│   │       ├── zz_generated.managed.go
│   │       ├── zz_generated.managedlist.go
│   │       ├── zz_generated_terraformed.go
│   │       └── zz_groupversion_info.go

├── examples-generated
│   ├── load
│   │   └── balancer.yaml

CRDS、yamls和文件结构生成不正确。理想情况下,目录和CRDS不应拆分资源名称,而应将它们作为一个字符串loadbalancer
下面我们可以看到,即使是deepcopy方法也会使用错误的名称生成。应该是loadbalancer而不是balancer

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Balancer) DeepCopyInto(out *Balancer) {
    *out = *in
    out.TypeMeta = in.TypeMeta
    in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
    in.Spec.DeepCopyInto(&out.Spec)
    in.Status.DeepCopyInto(&out.Status)
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Balancer.
func (in *Balancer) DeepCopy() *Balancer {
    if in == nil {
        return nil
    }
    out := new(Balancer)
    in.DeepCopyInto(out)
    return out
}

你觉得我在这里能做什么?

u3r8eeie

u3r8eeie1#

您可以通过为单个生成的资源添加ResourceConfigurator来自定义该组。这在upjet文档中的GitHub提供程序示例中简要提到。

cat <<EOF > config/repository/config.go
   package repository

   import "github.com/upbound/upjet/pkg/config"

   // Configure configures individual resources by adding custom ResourceConfigurators.
   func Configure(p *config.Provider) {
       p.AddResourceConfigurator("github_repository", func(r *config.Resource) {
           // We need to override the default group that upjet generated for
           // this resource, which would be "github"
           r.ShortGroup = "repository"
       })
   }
   EOF

关于定制的一个更重要的例子,看看provider-aws,它使用一个定制的Map和修剪函数来从某些terraform资源名称Map到特定的Groups和Kinds。

// GroupKindOverrides overrides the group and kind of the resource if it matches
// any entry in the GroupMap.
func GroupKindOverrides() config.ResourceOption {
    return func(r *config.Resource) {
        if f, ok := GroupMap[r.Name]; ok {
            r.ShortGroup, r.Kind = f(r.Name)
        }
    }
}

// KindOverrides overrides the kind of the resources given in KindMap.
func KindOverrides() config.ResourceOption {
    return func(r *config.Resource) {
        if k, ok := KindMap[r.Name]; ok {
            r.Kind = k
        }
    }
}

然后将其添加到config/provider.go中,以自定义生成器的行为。

// GetProvider returns provider configuration
func GetProvider() *config.Provider {
    modulePath := "github.com/upbound/provider-aws"
    pc := config.NewProvider([]byte(providerSchema), "aws",
        modulePath, providerMetadata,
        config.WithShortName("aws"),
        config.WithRootGroup("aws.upbound.io"),
...
        config.WithDefaultResourceOptions(
            GroupKindOverrides(),
            KindOverrides(),        
...
        ),
        config.WithMainTemplate(hack.MainTemplate),
    )
...

相关问题