config

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2023 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PackageNameConfig is the name of the provider subpackage that contains
	// the base resources (e.g., ProviderConfig, ProviderConfigUsage,
	// StoreConfig. etc.).
	// TODO: we should be careful that there may also exist short groups with
	// these names. We can consider making these configurable by the provider
	// maintainer.
	PackageNameConfig = "config"
	// PackageNameMonolith is the name of the backwards-compatible
	// provider subpackage that contains the all the resources.
	PackageNameMonolith = "monolith"
)

Variables

View Source
var (
	DefaultBasePackages = BasePackages{
		APIVersion: []string{

			"apis/v1alpha1",
			"apis/v1beta1",
		},

		Controller: []string{

			"internal/controller/providerconfig",
		},
		ControllerMap: map[string]string{

			"internal/controller/providerconfig": PackageNameConfig,
		},
	}

	// NopSensitive does nothing.
	NopSensitive = Sensitive{
		AdditionalConnectionDetailsFn: NopAdditionalConnectionDetails,
	}
)

Commonly used resource configurations.

View Source
var (
	// NameAsIdentifier uses "name" field in the arguments as the identifier of
	// the resource.
	NameAsIdentifier = ExternalName{
		SetIdentifierArgumentFn: func(base map[string]any, name string) {
			base["name"] = name
		},
		GetExternalNameFn: IDAsExternalName,
		GetIDFn:           ExternalNameAsID,
		OmittedFields: []string{
			"name",
			"name_prefix",
		},
	}

	// IdentifierFromProvider is used in resources whose identifier is assigned by
	// the remote client, such as AWS VPC where it gets an identifier like
	// vpc-2213das instead of letting user choose a name.
	IdentifierFromProvider = ExternalName{
		SetIdentifierArgumentFn: NopSetIdentifierArgument,
		GetExternalNameFn:       IDAsExternalName,
		GetIDFn:                 ExternalNameAsID,
		DisableNameInitializer:  true,
	}
)

Functions

func GetExternalNameFromTemplated

func GetExternalNameFromTemplated(tmpl, val string) (string, error)

GetExternalNameFromTemplated takes a Terraform ID and the template it's produced from and reverse it to get the external name. For example, you can supply "/subscription/{{ .paramters.some }}/{{ .external_name }}" with "/subscription/someval/myname" and get "myname" returned.

func GetSchema

func GetSchema(sch *schema.Resource, fieldpath string) *schema.Schema

GetSchema returns the schema of the field whose fieldpath is given. Returns nil if Schema is not found at the specified path.

func ManipulateEveryField

func ManipulateEveryField(r *schema.Resource, op func(sch *schema.Schema))

ManipulateEveryField manipulates all fields in the schema by input function.

func MarkAsRequired

func MarkAsRequired(sch *schema.Resource, fieldpaths ...string)

MarkAsRequired marks the schema of the given fieldpath as required. It's most useful in cases where external name contains an optional parameter that is defaulted by the provider but we need it to exist or to fix plain buggy schemas.

func MoveToStatus

func MoveToStatus(sch *schema.Resource, fieldpaths ...string)

MoveToStatus moves given fields and their leaf fields to the status as a whole. It's used mostly in cases where there is a field that is represented as a separate CRD, hence you'd like to remove that field from spec.

Types

type AdditionalConnectionDetailsFn

type AdditionalConnectionDetailsFn func(attr map[string]any) (map[string][]byte, error)

AdditionalConnectionDetailsFn functions adds custom keys to connection details secret using input terraform attributes

var NopAdditionalConnectionDetails AdditionalConnectionDetailsFn = func(_ map[string]any) (map[string][]byte, error) {
	return nil, nil
}

NopAdditionalConnectionDetails does nothing, when no additional connection details configuration function provided.

type BasePackages

type BasePackages struct {
	APIVersion []string
	// Deprecated: Use ControllerMap instead.
	Controller    []string
	ControllerMap map[string]string
}

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 ExternalName

type ExternalName struct {
	// SetIdentifierArgumentFn sets the name of the resource in Terraform argument
	// map. In many cases, there is a field called "name" in the HCL schema, however,
	// there are cases like RDS DB Cluster where the name field in HCL is called
	// "cluster_identifier". This function is the place that you can take external
	// name and assign it to that specific key for that resource type.
	SetIdentifierArgumentFn SetIdentifierArgumentsFn

	// GetExternalNameFn returns the external name extracted from TF State. In most cases,
	// "id" field contains all the information you need. You'll need to extract
	// the format that is decided for external name annotation to use.
	// For example the following is an Azure resource ID:
	// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1
	// The function should return "mygroup1" so that it can be used to set external
	// name if it was not set already.
	GetExternalNameFn GetExternalNameFn

	// GetIDFn returns the string that will be used as "id" key in TF state. In
	// many cases, external name format is the same as "id" but when it is not
	// we may need information from other places to construct it. For example,
	// the following is an Azure resource ID:
	// /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1
	// The function here should use information from supplied arguments to
	// construct this ID, i.e. "mygroup1" from external name, subscription ID
	// from terraformProviderConfig, and others from parameters map if needed.
	GetIDFn GetIDFn

	// OmittedFields are the ones you'd like to be removed from the schema since
	// they are specified via external name. For example, if you set
	// "cluster_identifier" in SetIdentifierArgumentFn, then you need to omit
	// that field.
	// You can omit only the top level fields.
	// No field is omitted by default.
	OmittedFields []string

	// DisableNameInitializer allows you to specify whether the name initializer
	// that sets external name to metadata.name if none specified should be disabled.
	// It needs to be disabled for resources whose external identifier is randomly
	// assigned by the provider, like AWS VPC where it gets vpc-21kn123 identifier
	// and not let you name it.
	DisableNameInitializer bool

	// IdentifierFields are the fields that are used to construct external
	// resource identifier. We need to know these fields no matter what the
	// management policy is including the Observe Only, different from other
	// (required) fields.
	IdentifierFields []string
}

ExternalName contains all information that is necessary for naming operations, such as removal of those fields from spec schema and calling Configure function to fill attributes with information given in external name.

func ParameterAsIdentifier

func ParameterAsIdentifier(param string) ExternalName

ParameterAsIdentifier uses the given field name in the arguments as the identifier of the resource.

func TemplatedStringAsIdentifier

func TemplatedStringAsIdentifier(nameFieldPath, tmpl string) ExternalName

TemplatedStringAsIdentifier accepts a template as the shape of the Terraform ID and lets you provide a field path for the argument you're using as external name. The available variables you can use in the template are as follows: parameters: A tree of parameters that you'd normally see in a Terraform HCL

file. You can use TF registry documentation of given resource to
see what's available.

setup.configuration: The Terraform configuration object of the provider. You can

take a look at the TF registry provider configuration object
to see what's available. Not to be confused with ProviderConfig
custom resource of the Crossplane provider.

setup.client_metadata: The Terraform client metadata available for the provider,

such as the AWS account ID for the AWS provider.

external_name: The value of external name annotation of the custom resource.

It is required to use this as part of the template.

The following template functions are available: ToLower: Converts the contents of the pipeline to lower-case ToUpper: Converts the contents of the pipeline to upper-case Please note that it's currently *not* possible to use the template functions on the .external_name template variable. Example usages: TemplatedStringAsIdentifier("index_name", "/subscriptions/{{ .setup.configuration.subscription }}/{{ .external_name }}") TemplatedStringAsIdentifier("index_name", "/resource/{{ .external_name }}/static") TemplatedStringAsIdentifier("index_name", "{{ .parameters.cluster_id }}:{{ .parameters.node_id }}:{{ .external_name }}") TemplatedStringAsIdentifier("", "arn:aws:network-firewall:{{ .setup.configuration.region }}:{{ .setup.client_metadata.account_id }}:{{ .parameters.type | ToLower }}-rulegroup/{{ .external_name }}")

type GetExternalNameFn

type GetExternalNameFn func(tfstate map[string]any) (string, error)

GetExternalNameFn returns the external name extracted from the TF State.

var IDAsExternalName GetExternalNameFn = func(tfstate map[string]any) (string, error) {
	if id, ok := tfstate["id"].(string); ok && id != "" {
		return id, nil
	}
	return "", errors.New("cannot find id in tfstate")
}

IDAsExternalName returns the TF State ID as external name.

type GetIDFn

type GetIDFn func(ctx context.Context, externalName string, parameters map[string]any, terraformProviderConfig map[string]any) (string, error)

GetIDFn returns the ID to be used in TF State file, i.e. "id" field in terraform.tfstate.

var ExternalNameAsID GetIDFn = func(_ context.Context, externalName string, _ map[string]any, _ map[string]any) (string, error) {
	return externalName, nil
}

ExternalNameAsID returns the name to be used as ID in TF State file.

type LateInitializer

type LateInitializer struct {
	// IgnoredFields are the field paths to be skipped during
	// late-initialization. Similar to other configurations, these paths are
	// Terraform field paths concatenated with dots. For example, if we want to
	// ignore "ebs" block in "aws_launch_template", we should add
	// "block_device_mappings.ebs".
	IgnoredFields []string
	// contains filtered or unexported fields
}

LateInitializer represents configurations that control late-initialization behaviour

func (*LateInitializer) AddIgnoredCanonicalFields

func (l *LateInitializer) AddIgnoredCanonicalFields(cf string)

AddIgnoredCanonicalFields sets ignored canonical fields

func (*LateInitializer) GetIgnoredCanonicalFields

func (l *LateInitializer) GetIgnoredCanonicalFields() []string

GetIgnoredCanonicalFields returns the ignoredCanonicalFields

type NewInitializerFn

type NewInitializerFn func(client client.Client) managed.Initializer

NewInitializerFn returns the Initializer with a client.

var TagInitializer NewInitializerFn = func(client client.Client) managed.Initializer {
	return NewTagger(client, "tags")
}

TagInitializer returns a tagger to use default tag initializer.

type OperationTimeouts

type OperationTimeouts struct {
	Read   time.Duration
	Create time.Duration
	Update time.Duration
	Delete time.Duration
}

OperationTimeouts allows configuring resource operation timeouts: https://www.terraform.io/language/resources/syntax#operation-timeouts Please note that, not all resources support configuring timeouts.

type Provider

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

	// IncludeList is a list of regex for the Terraform resources to be
	// included. 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

	// Resources is a map holding resource configurations where key is Terraform
	// resource name.
	Resources map[string]*Resource
	// contains filtered or unexported fields
}

Provider holds configuration for a provider to be generated with Upjet.

func NewProvider

func NewProvider(schema []byte, prefix string, modulePath string, metadata []byte, opts ...ProviderOption) *Provider

NewProvider builds and returns a new Provider from provider tfjson schema, that is generated using Terraform CLI with: `terraform providers schema --json`

func (*Provider) AddResourceConfigurator

func (p *Provider) AddResourceConfigurator(resource string, c ResourceConfiguratorFn)

AddResourceConfigurator adds resource specific configurators.

func (*Provider) ConfigureResources

func (p *Provider) ConfigureResources()

ConfigureResources configures resources with provided ResourceConfigurator's

func (*Provider) GetSkippedResourceNames added in v0.8.0

func (p *Provider) GetSkippedResourceNames() []string

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 (*Provider) SetResourceConfigurator

func (p *Provider) SetResourceConfigurator(resource string, c ResourceConfigurator)

SetResourceConfigurator sets ResourceConfigurator for a resource. This will override all previously added ResourceConfigurators for this resource.

type ProviderOption

type ProviderOption func(*Provider)

A ProviderOption configures a Provider.

func WithBasePackages

func WithBasePackages(b BasePackages) ProviderOption

WithBasePackages configures BasePackages for this Provider.

func WithDefaultResourceOptions

func WithDefaultResourceOptions(opts ...ResourceOption) ProviderOption

WithDefaultResourceOptions configures DefaultResourceOptions for this Provider.

func WithFeaturesPackage added in v0.9.0

func WithFeaturesPackage(s string) ProviderOption

WithFeaturesPackage configures FeaturesPackage for this Provider.

func WithIncludeList

func WithIncludeList(l []string) ProviderOption

WithIncludeList configures IncludeList for this Provider.

func WithMainTemplate added in v0.9.0

func WithMainTemplate(template string) ProviderOption

func WithReferenceInjectors

func WithReferenceInjectors(refInjectors []ReferenceInjector) ProviderOption

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 WithRootGroup

func WithRootGroup(s string) ProviderOption

WithRootGroup configures RootGroup for resources of this Provider.

func WithShortName

func WithShortName(s string) ProviderOption

WithShortName configures ShortName for resources of this Provider.

func WithSkipList

func WithSkipList(l []string) ProviderOption

WithSkipList configures SkipList for this Provider.

type Reference

type Reference struct {
	// Type is the type name of the CRD if it is in the same package or
	// <package-path>.<type-name> if it is in a different package.
	Type string
	// TerraformName is the name of the Terraform resource
	// which will be referenced. The supplied resource name is
	// converted to a type name of the corresponding CRD using
	// the configured TerraformTypeMapper.
	TerraformName string
	// Extractor is the function to be used to extract value from the
	// referenced type. Defaults to getting external name.
	// Optional
	Extractor string
	// RefFieldName is the field name for the Reference field. Defaults to
	// <field-name>Ref or <field-name>Refs.
	// Optional
	RefFieldName string
	// SelectorFieldName is the field name for the Selector field. Defaults to
	// <field-name>Selector.
	// Optional
	SelectorFieldName string
}

Reference represents the Crossplane options used to generate reference resolvers for fields

type ReferenceInjector

type ReferenceInjector interface {
	InjectReferences(map[string]*Resource) error
}

ReferenceInjector injects cross-resource references across the resources of this Provider.

type References

type References map[string]Reference

References represents reference resolver configurations for the fields of a given resource. Key should be the field path of the field to be referenced.

type Resource

type Resource struct {
	// Name is the name of the resource type in Terraform,
	// e.g. aws_rds_cluster.
	Name string

	// TerraformResource is the Terraform representation of the resource.
	TerraformResource *schema.Resource

	// ShortGroup is the short name of the API group of this CRD. The full
	// CRD API group is calculated by adding the group suffix of the provider.
	// For example, ShortGroup could be `ec2` where group suffix of the
	// provider is `aws.crossplane.io` and in that case, the full group would
	// be `ec2.aws.crossplane.io`
	ShortGroup string

	// Version is the version CRD will have.
	Version string

	// Kind is the kind of the CRD.
	Kind string

	// UseAsync should be enabled for resource whose creation and/or deletion
	// takes more than 1 minute to complete such as Kubernetes clusters or
	// databases.
	UseAsync bool

	InitializerFns []NewInitializerFn

	// OperationTimeouts allows configuring resource operation timeouts.
	OperationTimeouts OperationTimeouts

	// ExternalName allows you to specify a custom ExternalName.
	ExternalName ExternalName

	// References keeps the configuration to build cross resource references
	References References

	// Sensitive keeps the configuration to handle sensitive information
	Sensitive Sensitive

	// LateInitializer configuration to control late-initialization behaviour
	LateInitializer LateInitializer

	// MetaResource is the metadata associated with the resource scraped from
	// the Terraform registry.
	MetaResource *registry.Resource

	// Path is the resource path for the API server endpoint. It defaults to
	// the plural name of the generated CRD. Overriding this sets both the
	// path and the plural name for the generated CRD.
	Path string
}

Resource is the set of information that you can override at different steps of the code generation pipeline.

func DefaultResource

func DefaultResource(name string, terraformSchema *schema.Resource, terraformRegistry *registry.Resource, opts ...ResourceOption) *Resource

DefaultResource keeps an initial default configuration for all resources of a provider.

type ResourceConfigurator

type ResourceConfigurator interface {
	Configure(r *Resource)
}

ResourceConfigurator configures a Resource

type ResourceConfiguratorChain

type ResourceConfiguratorChain []ResourceConfigurator

A ResourceConfiguratorChain chains multiple ResourceConfigurators.

func (ResourceConfiguratorChain) Configure

func (cc ResourceConfiguratorChain) Configure(r *Resource)

Configure configures a resource by calling each ResourceConfigurator in the chain serially.

type ResourceConfiguratorFn

type ResourceConfiguratorFn func(r *Resource)

ResourceConfiguratorFn is a function that implements the ResourceConfigurator interface

func (ResourceConfiguratorFn) Configure

func (c ResourceConfiguratorFn) Configure(r *Resource)

Configure configures a resource by calling ResourceConfiguratorFn

type ResourceOption

type ResourceOption func(*Resource)

ResourceOption allows setting optional fields of a Resource object.

type Sensitive

type Sensitive struct {
	// AdditionalConnectionDetailsFn is the path for function adding additional
	// connection details keys
	AdditionalConnectionDetailsFn AdditionalConnectionDetailsFn
	// contains filtered or unexported fields
}

Sensitive represents configurations to handle sensitive information

func (*Sensitive) AddFieldPath

func (s *Sensitive) AddFieldPath(tf, xp string)

AddFieldPath adds the given tf path and xp path to the fieldPaths map.

func (*Sensitive) GetFieldPaths

func (s *Sensitive) GetFieldPaths() map[string]string

GetFieldPaths returns the fieldPaths map for Sensitive

type SetIdentifierArgumentsFn

type SetIdentifierArgumentsFn func(base map[string]any, externalName string)

SetIdentifierArgumentsFn sets the name of the resource in Terraform attributes map, i.e. Main HCL file.

var NopSetIdentifierArgument SetIdentifierArgumentsFn = func(_ map[string]any, _ string) {}

NopSetIdentifierArgument does nothing. It's useful for cases where the external name is calculated by provider and doesn't have any effect on spec fields.

type Tagger

type Tagger struct {
	// contains filtered or unexported fields
}

Tagger implements the Initialize function to set external tags

func NewTagger

func NewTagger(kube client.Client, fieldName string) *Tagger

NewTagger returns a Tagger object.

func (*Tagger) Initialize

func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error

Initialize is a custom initializer for setting external tags

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL