-
Notifications
You must be signed in to change notification settings - Fork 87
/
provider.go
459 lines (395 loc) · 17.5 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
package config
import (
"context"
"fmt"
"regexp"
tfjson "github.com/hashicorp/terraform-json"
fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
"github.com/crossplane/upjet/pkg/registry"
"github.com/crossplane/upjet/pkg/schema/traverser"
conversiontfjson "github.com/crossplane/upjet/pkg/types/conversion/tfjson"
)
// ResourceConfiguratorFn is a function that implements the ResourceConfigurator
// interface
type ResourceConfiguratorFn func(r *Resource)
// Configure configures a resource by calling ResourceConfiguratorFn
func (c ResourceConfiguratorFn) Configure(r *Resource) {
c(r)
}
// ResourceConfigurator configures a Resource
type ResourceConfigurator interface {
Configure(r *Resource)
}
// A ResourceConfiguratorChain chains multiple ResourceConfigurators.
type ResourceConfiguratorChain []ResourceConfigurator
// Configure configures a resource by calling each ResourceConfigurator in the
// chain serially.
func (cc ResourceConfiguratorChain) Configure(r *Resource) {
for _, c := range cc {
c.Configure(r)
}
}
// BasePackages keeps lists of packages that needs to be registered as API
// and controllers. Typically, we expect to see ProviderConfig packages here.
// These APIs and controllers belong to non-generated (manually maintained)
// resources.
type BasePackages struct {
APIVersion []string
// Deprecated: Use ControllerMap instead.
Controller []string
ControllerMap map[string]string
}
// Provider holds configuration for a provider to be generated with Upjet.
type Provider struct {
// TerraformResourcePrefix is the prefix used in all resources of this
// Terraform provider, e.g. "aws_". Defaults to "<prefix>_". This is being
// used while setting some defaults like Kind of the resource. For example,
// for "aws_rds_cluster", we drop "aws_" prefix and its group ("rds") to set
// Kind of the resource as "Cluster".
TerraformResourcePrefix string
// RootGroup is the root group that all CRDs groups in the provider are based
// on, e.g. "aws.upbound.io".
// Defaults to "<TerraformResourcePrefix>.upbound.io".
RootGroup string
// ShortName is the short name of the provider. Typically, added as a CRD
// category, e.g. "awsjet". Default to "<prefix>jet". For more details on CRD
// categories, see: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#categories
ShortName string
// ModulePath is the go module path for the Crossplane provider repo, e.g.
// "github.com/upbound/provider-aws"
ModulePath string
// FeaturesPackage is the relative package patch for the features package to
// configure the features behind the feature gates.
FeaturesPackage string
// BasePackages keeps lists of base packages that needs to be registered as
// API and controllers. Typically, we expect to see ProviderConfig packages
// here.
BasePackages BasePackages
// DefaultResourceOptions is a list of config.ResourceOption that will be
// applied to all resources before any user-provided options are applied.
DefaultResourceOptions []ResourceOption
// SkipList is a list of regex for the Terraform resources to be skipped.
// For example, to skip generation of "aws_shield_protection_group", one
// can add "aws_shield_protection_group$". To skip whole aws waf group, one
// can add "aws_waf.*" to the list.
SkipList []string
// MainTemplate is the template string to be used to render the
// provider subpackage main program. If this is set, the generated provider
// is broken up into subpackage families partitioned across the API groups.
// A monolithic provider is also generated to
// ensure backwards-compatibility.
MainTemplate string
// skippedResourceNames is a list of Terraform resource names
// available in the Terraform provider schema, but
// not in the include list or in the skip list, meaning that
// the corresponding managed resources are not generated.
skippedResourceNames []string
// IncludeList is a list of regex for the Terraform resources to be
// included and reconciled via the Terraform CLI.
// For example, to include "aws_shield_protection_group" into
// the generated resources, one can add "aws_shield_protection_group$".
// To include whole aws waf group, one can add "aws_waf.*" to the list.
// Defaults to []string{".+"} which would include all resources.
IncludeList []string
// TerraformPluginSDKIncludeList is a list of regex for the Terraform resources
// implemented with Terraform Plugin SDKv2 to be included and reconciled
// in the no-fork architecture (without the Terraform CLI).
// For example, to include "aws_shield_protection_group" into
// the generated resources, one can add "aws_shield_protection_group$".
// To include whole aws waf group, one can add "aws_waf.*" to the list.
// Defaults to []string{".+"} which would include all resources.
TerraformPluginSDKIncludeList []string
// TerraformPluginFrameworkIncludeList is a list of regex for the Terraform
// resources implemented with Terraform Plugin Framework to be included and
// reconciled in the no-fork architecture (without the Terraform CLI).
// For example, to include "aws_shield_protection_group" into
// the generated resources, one can add "aws_shield_protection_group$".
// To include whole aws waf group, one can add "aws_waf.*" to the list.
// Defaults to []string{".+"} which would include all resources.
TerraformPluginFrameworkIncludeList []string
// Resources is a map holding resource configurations where key is Terraform
// resource name.
Resources map[string]*Resource
// TerraformProvider is the Terraform provider in Terraform Plugin SDKv2
// compatible format
TerraformProvider *schema.Provider
// TerraformPluginFrameworkProvider is the Terraform provider reference
// in Terraform Plugin Framework compatible format
TerraformPluginFrameworkProvider fwprovider.Provider
// refInjectors is an ordered list of `ReferenceInjector`s for
// injecting references across this Provider's resources.
refInjectors []ReferenceInjector
// resourceConfigurators is a map holding resource configurators where key
// is Terraform resource name.
resourceConfigurators map[string]ResourceConfiguratorChain
// schemaTraversers is a chain of schema traversers to be used with
// this Provider configuration. Schema traversers can be used to inspect or
// modify the Provider configuration based on the underlying Terraform
// resource schemas.
schemaTraversers []traverser.SchemaTraverser
}
// ReferenceInjector injects cross-resource references across the resources
// of this Provider.
type ReferenceInjector interface {
InjectReferences(map[string]*Resource) error
}
// A ProviderOption configures a Provider.
type ProviderOption func(*Provider)
// WithRootGroup configures RootGroup for resources of this Provider.
func WithRootGroup(s string) ProviderOption {
return func(p *Provider) {
p.RootGroup = s
}
}
// WithShortName configures ShortName for resources of this Provider.
func WithShortName(s string) ProviderOption {
return func(p *Provider) {
p.ShortName = s
}
}
// WithIncludeList configures IncludeList for this Provider.
func WithIncludeList(l []string) ProviderOption {
return func(p *Provider) {
p.IncludeList = l
}
}
// WithTerraformPluginSDKIncludeList configures the TerraformPluginSDKIncludeList for this Provider,
// with the given Terraform Plugin SDKv2-based resource name list
func WithTerraformPluginSDKIncludeList(l []string) ProviderOption {
return func(p *Provider) {
p.TerraformPluginSDKIncludeList = l
}
}
// WithTerraformPluginFrameworkIncludeList configures the
// TerraformPluginFrameworkIncludeList for this Provider, with the given
// Terraform Plugin Framework-based resource name list
func WithTerraformPluginFrameworkIncludeList(l []string) ProviderOption {
return func(p *Provider) {
p.TerraformPluginFrameworkIncludeList = l
}
}
// WithTerraformProvider configures the TerraformProvider for this Provider.
func WithTerraformProvider(tp *schema.Provider) ProviderOption {
return func(p *Provider) {
p.TerraformProvider = tp
}
}
// WithTerraformPluginFrameworkProvider configures the
// TerraformPluginFrameworkProvider for this Provider.
func WithTerraformPluginFrameworkProvider(tp fwprovider.Provider) ProviderOption {
return func(p *Provider) {
p.TerraformPluginFrameworkProvider = tp
}
}
// WithSkipList configures SkipList for this Provider.
func WithSkipList(l []string) ProviderOption {
return func(p *Provider) {
p.SkipList = l
}
}
// WithBasePackages configures BasePackages for this Provider.
func WithBasePackages(b BasePackages) ProviderOption {
return func(p *Provider) {
p.BasePackages = b
}
}
// WithDefaultResourceOptions configures DefaultResourceOptions for this
// Provider.
func WithDefaultResourceOptions(opts ...ResourceOption) ProviderOption {
return func(p *Provider) {
p.DefaultResourceOptions = opts
}
}
// WithReferenceInjectors configures an ordered list of `ReferenceInjector`s
// for this Provider. The configured reference resolvers are executed in order
// to inject cross-resource references across this Provider's resources.
func WithReferenceInjectors(refInjectors []ReferenceInjector) ProviderOption {
return func(p *Provider) {
p.refInjectors = refInjectors
}
}
// WithFeaturesPackage configures FeaturesPackage for this Provider.
func WithFeaturesPackage(s string) ProviderOption {
return func(p *Provider) {
p.FeaturesPackage = s
}
}
// WithMainTemplate configures the provider family main module file's path.
// This template file will be used to generate the main modules of the
// family's members.
func WithMainTemplate(template string) ProviderOption {
return func(p *Provider) {
p.MainTemplate = template
}
}
// WithSchemaTraversers configures a chain of schema traversers to be used with
// this Provider configuration. Schema traversers can be used to inspect or
// modify the Provider configuration based on the underlying Terraform
// resource schemas.
func WithSchemaTraversers(traversers ...traverser.SchemaTraverser) ProviderOption {
return func(p *Provider) {
p.schemaTraversers = traversers
}
}
// NewProvider builds and returns a new Provider from provider
// tfjson schema, that is generated using Terraform CLI with:
// `terraform providers schema --json`
func NewProvider(schema []byte, prefix string, modulePath string, metadata []byte, opts ...ProviderOption) *Provider { //nolint:gocyclo
ps := tfjson.ProviderSchemas{}
if err := ps.UnmarshalJSON(schema); err != nil {
panic(errors.Wrap(err, "failed to unmarshal the Terraform JSON schema"))
}
if len(ps.Schemas) != 1 {
panic(fmt.Sprintf("there should exactly be 1 provider schema but there are %d", len(ps.Schemas)))
}
var rs map[string]*tfjson.Schema
for _, v := range ps.Schemas {
rs = v.ResourceSchemas
break
}
resourceMap := conversiontfjson.GetV2ResourceMap(rs)
providerMetadata, err := registry.NewProviderMetadataFromFile(metadata)
if err != nil {
panic(errors.Wrap(err, "cannot load provider metadata"))
}
p := &Provider{
ModulePath: modulePath,
TerraformResourcePrefix: fmt.Sprintf("%s_", prefix),
RootGroup: fmt.Sprintf("%s.upbound.io", prefix),
ShortName: prefix,
BasePackages: DefaultBasePackages,
IncludeList: []string{
// Include all Resources
".+",
},
Resources: map[string]*Resource{},
resourceConfigurators: map[string]ResourceConfiguratorChain{},
}
for _, o := range opts {
o(p)
}
p.skippedResourceNames = make([]string, 0, len(resourceMap))
terraformPluginFrameworkResourceFunctionsMap := terraformPluginFrameworkResourceFunctionsMap(p.TerraformPluginFrameworkProvider)
for name, terraformResource := range resourceMap {
if len(terraformResource.Schema) == 0 {
// There are resources with no schema, that we will address later.
fmt.Printf("Skipping resource %s because it has no schema\n", name)
}
// if in both of the include lists, the new behavior prevails
isTerraformPluginSDK := matches(name, p.TerraformPluginSDKIncludeList)
isPluginFrameworkResource := matches(name, p.TerraformPluginFrameworkIncludeList)
isCLIResource := matches(name, p.IncludeList)
if (isTerraformPluginSDK && isPluginFrameworkResource) || (isTerraformPluginSDK && isCLIResource) || (isPluginFrameworkResource && isCLIResource) {
panic(errors.Errorf(`resource %q is specified in more than one include list. It should appear in at most one of the lists "IncludeList", "TerraformPluginSDKIncludeList" or "TerraformPluginFrameworkIncludeList"`, name))
}
if len(terraformResource.Schema) == 0 || matches(name, p.SkipList) || (!matches(name, p.IncludeList) && !isTerraformPluginSDK && !isPluginFrameworkResource) {
p.skippedResourceNames = append(p.skippedResourceNames, name)
continue
}
if isTerraformPluginSDK {
if p.TerraformProvider == nil || p.TerraformProvider.ResourcesMap[name] == nil {
panic(errors.Errorf("resource %q is configured to be reconciled with Terraform Plugin SDK"+
"but either config.Provider.TerraformProvider is not configured or the Go schema does not exist for the resource", name))
}
terraformResource = p.TerraformProvider.ResourcesMap[name]
if terraformResource.Schema == nil {
if terraformResource.SchemaFunc == nil {
p.skippedResourceNames = append(p.skippedResourceNames, name)
fmt.Printf("Skipping resource %s because it has no schema and no schema function\n", name)
continue
}
terraformResource.Schema = terraformResource.SchemaFunc()
}
}
var terraformPluginFrameworkResource fwresource.Resource
if isPluginFrameworkResource {
resourceFunc := terraformPluginFrameworkResourceFunctionsMap[name]
if p.TerraformPluginFrameworkProvider == nil || resourceFunc == nil {
panic(errors.Errorf("resource %q is configured to be reconciled with Terraform Plugin Framework"+
"but either config.Provider.TerraformPluginFrameworkProvider is not configured or the provider doesn't have the resource.", name))
}
terraformPluginFrameworkResource = resourceFunc()
}
p.Resources[name] = DefaultResource(name, terraformResource, terraformPluginFrameworkResource, providerMetadata.Resources[name], p.DefaultResourceOptions...)
p.Resources[name].useTerraformPluginSDKClient = isTerraformPluginSDK
p.Resources[name].useTerraformPluginFrameworkClient = isPluginFrameworkResource
// traverse the Terraform resource schema to initialize the upjet Resource
// configurations
if err := TraverseSchemas(name, p.Resources[name], p.schemaTraversers...); err != nil {
panic(errors.Wrap(err, "failed to execute the Terraform schema traverser chain"))
}
}
for i, refInjector := range p.refInjectors {
if err := refInjector.InjectReferences(p.Resources); err != nil {
panic(errors.Wrapf(err, "cannot inject references using the configured ReferenceInjector at index %d", i))
}
}
return p
}
// AddResourceConfigurator adds resource specific configurators.
func (p *Provider) AddResourceConfigurator(resource string, c ResourceConfiguratorFn) { //nolint:interfacer
// Note(turkenh): nolint reasoning - easier to provide a function without
// converting to an explicit type supporting the ResourceConfigurator
// interface. Since this function would be a frequently used one, it should
// be a reasonable simplification.
p.resourceConfigurators[resource] = append(p.resourceConfigurators[resource], c)
}
// SetResourceConfigurator sets ResourceConfigurator for a resource. This will
// override all previously added ResourceConfigurators for this resource.
func (p *Provider) SetResourceConfigurator(resource string, c ResourceConfigurator) {
p.resourceConfigurators[resource] = ResourceConfiguratorChain{c}
}
// ConfigureResources configures resources with provided ResourceConfigurator's
func (p *Provider) ConfigureResources() {
for name, c := range p.resourceConfigurators {
// if not skipped & included & configured via the default configurator
if r, ok := p.Resources[name]; ok {
c.Configure(r)
}
}
}
// GetSkippedResourceNames returns a list of Terraform resource names
// available in the Terraform provider schema, but
// not in the include list or in the skip list, meaning that
// the corresponding managed resources are not generated.
func (p *Provider) GetSkippedResourceNames() []string {
return p.skippedResourceNames
}
func matches(name string, regexList []string) bool {
for _, r := range regexList {
ok, err := regexp.MatchString(r, name)
if err != nil {
panic(errors.Wrap(err, "cannot match regular expression"))
}
if ok {
return true
}
}
return false
}
func terraformPluginFrameworkResourceFunctionsMap(provider fwprovider.Provider) map[string]func() fwresource.Resource {
if provider == nil {
return make(map[string]func() fwresource.Resource, 0)
}
ctx := context.TODO()
resourceFunctions := provider.Resources(ctx)
resourceFunctionsMap := make(map[string]func() fwresource.Resource, len(resourceFunctions))
providerMetadata := fwprovider.MetadataResponse{}
provider.Metadata(ctx, fwprovider.MetadataRequest{}, &providerMetadata)
for _, resourceFunction := range resourceFunctions {
resource := resourceFunction()
resourceTypeNameReq := fwresource.MetadataRequest{
ProviderTypeName: providerMetadata.TypeName,
}
resourceTypeNameResp := fwresource.MetadataResponse{}
resource.Metadata(ctx, resourceTypeNameReq, &resourceTypeNameResp)
resourceFunctionsMap[resourceTypeNameResp.TypeName] = resourceFunction
}
return resourceFunctionsMap
}