krmtotf

package
v1.91.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2022 License: Apache-2.0 Imports: 29 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertTFObjToKCCObj

func ConvertTFObjToKCCObj(state map[string]interface{}, prevSpec map[string]interface{},
	schemas map[string]*tfschema.Schema, rc *corekccv1alpha1.ResourceConfig, prefix string,
	managedFields *fieldpath.Set) map[string]interface{}

ConvertTFObjToKCCObj takes the state (which should be a Terraform resource), and returns a map that is formatted to KCC's custom resource schema for the appropriate Kind.

prevSpec is used for multiple purposes:

  • ensures the returned result has a similar order for objects in lists, reducing the percieved diff when applied.
  • if server-side apply is used, the prevSpec value for a field will be used over the value in state if it is managed by KCC.
  • for sets (which are represented as lists), the result is a merger of both the state and the prevSpec.

func CtyValToMap

func CtyValToMap(val cty.Value, t cty.Type) map[string]interface{}

func ExpandComputeInstanceMetadata

func ExpandComputeInstanceMetadata(state map[string]interface{}, prev *Resource) map[string]interface{}

func FetchLiveState

func FetchLiveState(ctx context.Context, resource *Resource, provider *tfschema.Provider, kubeClient client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (*terraform.InstanceState, error)

FetchLiveState is a combination of a resource import + read. It returns the state of the underlying resource as seen by the TF provider.

func FlattenComputeInstanceMetadata

func FlattenComputeInstanceMetadata(config map[string]interface{}) (map[string]interface{}, error)

func GetAnnotationsFromState

func GetAnnotationsFromState(resource *Resource, rawState *terraform.InstanceState) map[string]string

Get the directives and container annotation(s) from the state

func GetEtagFromState

func GetEtagFromState(resource *Resource, rawState *terraform.InstanceState) string

func GetKeyForReferenceField

func GetKeyForReferenceField(refConfig *corekccv1alpha1.ReferenceConfig) string

func GetLabelsFromState

func GetLabelsFromState(resource *Resource, rawState *terraform.InstanceState) map[string]string

func GetNameFromState

func GetNameFromState(resource *Resource, rawState *terraform.InstanceState) string

func GetSpecAndStatusFromState

func GetSpecAndStatusFromState(resource *Resource, state *terraform.InstanceState) (
	spec map[string]interface{}, status map[string]interface{})

GetSpecAndStatusFromState converts state into separate, KRM-compatible spec and status objects.

This function can handle partial state structs (ones that may fail if applied with terraform). The resource.Spec that is passed is assumed to be the desired state of the user, and as such fields that are specified by Kubernetes to be managed by Config Connector will use the values in resource.Spec rather than those in state in the returned spec and status. That said, this function returns spec as a mix of k8s user managed fields and defaulted state from APIs and returns status with the legacy format containing observed state for output-only fields only.

See ConvertTFObjToKCCObj for a complete description of the merging behavior of state and resource.Spec (passed as prevSpec)

func ImportState

func ImportState(ctx context.Context, id string, tfInfo *terraform.InstanceInfo, provider *tfschema.Provider) (*terraform.InstanceState, error)

ImportState parses the given id into a TF state. Note that this function does not make any network calls; it simply does a best effort to determine TF state by parsing the id.

As a result of this being best-effort, the returned state may not have every field required in a fully valid InstanceState.

func InstanceStateToMap

func InstanceStateToMap(r *schema.Resource, state *terraform.InstanceState) map[string]interface{}

InstanceStateToMap converts state into a map[string]interface{}, using the schema as defined in r to coerce values to the appropriate type.

func IsHierarchicalReference

func IsHierarchicalReference(ref corekccv1alpha1.ReferenceConfig, hierarchicalRefs []corekccv1alpha1.HierarchicalReference) bool

func IsReferenceField

func IsReferenceField(qualifiedName string, rc *corekccv1alpha1.ResourceConfig) (bool, *corekccv1alpha1.ReferenceConfig)

func IsRequiredParentReference

func IsRequiredParentReference(ref corekccv1alpha1.ReferenceConfig, resource *Resource) bool

func IsResourceIDFieldServerGenerated

func IsResourceIDFieldServerGenerated(rc *corekccv1alpha1.ResourceConfig) bool

func KRMNameToTerraformID

func KRMNameToTerraformID(name string) string

KRMNameToTerraformID converts a Kubernetes object name into an idiomatic Terraform ID.

func KRMObjectToTFObject

func KRMObjectToTFObject(obj map[string]interface{}, resource *tfschema.Resource) (map[string]interface{}, error)

func KRMObjectToTFObjectWithConfigurableFieldsOnly

func KRMObjectToTFObjectWithConfigurableFieldsOnly(obj map[string]interface{}, resource *tfschema.Resource) (map[string]interface{}, error)

func KRMResourceToTFResourceConfig

func KRMResourceToTFResourceConfig(r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (tfConfig *terraform.ResourceConfig, secretVersions map[string]string, err error)

KRMResourceToTFResourceConfig converts a KCC KRM resource to a Terraform resource config. Note: this function does not fully validate the input KRM config or output TF config to ensure that they correspond to valid GCP resources (e.g. if the input KRM config is missing a required field, the function won't complain and just output a TF config without that field). This function just converts one abstract data structure to another; validation of either the input KRM or output TF is left as the responsibility of other layers (e.g. webhooks, CRD schemas, GCP API, etc.)

func KRMResourceToTFResourceConfigFull

func KRMResourceToTFResourceConfigFull(r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader,
	liveState *terraform.InstanceState, jsonSchema *apiextensions.JSONSchemaProps, mustResolveSensitiveFields bool, defaultLabels map[string]string) (tfConfig *terraform.ResourceConfig, secretVersions map[string]string, err error)

KRMResourceToTFResourceConfigFull is a more flexible version of KRMResourceToTFResourceConfig, including the following additional flags:

  • liveState: if set, these values will be used as the default values of the returned tfConfig, subject to be overriden by r.spec, etc.
  • jsonSchema: if set, externally managed fields will be populated.
  • mustResolveSensitiveFields: if set, sensitive fields will be resolved.
  • defaultLabels: if set, these labels will be added to tfConfig.

func MapToCtyVal

func MapToCtyVal(m map[string]interface{}, t cty.Type) cty.Value

func MapToCtyValWithSchema

func MapToCtyValWithSchema(m map[string]interface{}, s map[string]*schema.Schema) cty.Value

func MapToInstanceState

func MapToInstanceState(r *schema.Resource, m map[string]interface{}) *terraform.InstanceState

func MapToResourceConfig

func MapToResourceConfig(r *schema.Resource, m map[string]interface{}) *terraform.ResourceConfig

func MergeClusterConfigsFromLiveStateForBigtableInstance

func MergeClusterConfigsFromLiveStateForBigtableInstance(config map[string]interface{}, liveState map[string]interface{}, r *tfschema.Resource) (map[string]interface{}, error)

MergeClusterConfigsFromLiveStateForBigtableInstance is a resource specific function to deal with the following edge case. BigtableInstance has a `cluster` field that takes a full list of clusters associated with the instance. The list of clusters read from the API is unordered. Due to the terraform SDK limitation, if some optional field e.g. num_nodes is omitted, terraform SDK will determine the current value of the field from the cluster on the same index rather than from the cluster with the same cluster_id; plus the returned list is not in the same order as user specified, the partial config with optional fields omitted will result in unexpected behaviors. As a workarounds until migrating this resource to DCL, KCC will maintain this following resource specific code to merge the cluster config for omitted fields from cluster's live state by cluster_id. DCL is expected to have the similar logic on its side to merge the partial desired intent with the live state; once this resource is migrated to DCL, we should be able to remove the bespoke code.

func MutableButUnreadableFieldsAnnotationFor

func MutableButUnreadableFieldsAnnotationFor(r *Resource) (string, error)

func NewErrorFromDiagnostics

func NewErrorFromDiagnostics(diagnostics diag.Diagnostics) error

func RemoveFieldsFromStateThatConflictWithSpec

func RemoveFieldsFromStateThatConflictWithSpec(state map[string]interface{}, spec map[string]interface{},
	rc corekccv1alpha1.ResourceConfig, tfPath []string, schemaMap map[string]*tfschema.Schema) error

RemoveFieldsFromStateThatConflictWithSpec removes fields from 'state' that conflict with any of the fields found in 'spec'. This is useful for when we want to overlay 'state' onto 'spec' without ending up with an invalid resource configuration.

func ResolveLegacyGCPManagedFields

func ResolveLegacyGCPManagedFields(r *Resource, liveState *terraform.InstanceState, config map[string]interface{}) error

func ResolveReferenceObject

func ResolveReferenceObject(resourceRefValRaw map[string]interface{},
	refConfig corekccv1alpha1.ReferenceConfig, r *Resource, kubeClient client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (interface{}, error)

func ResolveResourceReference

func ResolveResourceReference(path []string, obj interface{}, refConfig v1alpha1.ReferenceConfig,
	r *Resource, kubeClient client.Client, smLoader *servicemappingloader.ServiceMappingLoader) error

func ResolveSpecAndStatus

func ResolveSpecAndStatus(resource *Resource, state *terraform.InstanceState) (
	spec map[string]interface{}, status map[string]interface{})

ResolveSpecAndStatus returns the resolved spec and status in different formats gated by the 'state-into-spec' annotation.

If the annotation takes the 'merge' value, the function returns spec as a mix of k8s user managed fields and defaulted state from APIs and returns status with the legacy format containing observed state for output-only fields only.

If the annotation takes the 'absent' value, the function will delegate to resolveDesiredStateInSpecAndObservedStateInStatus() to resolve the spec and the status.

func ResolveSpecAndStatusWithResourceID

func ResolveSpecAndStatusWithResourceID(resource *Resource, state *terraform.InstanceState) (
	spec map[string]interface{}, status map[string]interface{})

ResolveSpecAndStatusWithResourceID returns the resolved spec and status with the `resourceID` field is populated in the KRM spec.

func ResolveValueTemplate

func ResolveValueTemplate(template string, val string, r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (string, error)

func ResourceConfigToMap

func ResourceConfigToMap(config *terraform.ResourceConfig) map[string]interface{}

func ServerGeneratedIdToTemplate

func ServerGeneratedIdToTemplate(rc *corekccv1alpha1.ResourceConfig) string

func SetBlueprintAttribution

func SetBlueprintAttribution(s *terraform.InstanceState, r *Resource, p *tfschema.Provider) *terraform.InstanceState

SetBlueprintAttribution sets the module name to the blueprint name on the given instance state if the resource has the 'cnrm.cloud.google.com/blueprint' annotation. As a result, the blueprint name will be added into the user agent for requests to the particular GCP resource.

func SetUserAgentForTerraformProvider

func SetUserAgentForTerraformProvider()

Inject the KCC identifier into the user agent for HTTP requests to GCP APIs issued from terraform provider. This is achieved by setting the following global variable provided by terraform provider. This function should only be called once in the program.

Note that SetBlueprintAttribution will be used to add the blueprint attribution part into the user agent per resource if the resource has the 'cnrm.cloud.google.com/blueprint' annotation.

func SupportsHierarchicalReferences

func SupportsHierarchicalReferences(rc *corekccv1alpha1.ResourceConfig) bool

func SupportsIAM

func SupportsIAM(rc *corekccv1alpha1.ResourceConfig) bool

func SupportsResourceIDField

func SupportsResourceIDField(rc *corekccv1alpha1.ResourceConfig) bool

func SupportsServerGeneratedIDField

func SupportsServerGeneratedIDField(rc *corekccv1alpha1.ResourceConfig) bool

func WithFieldsPresetForRead

func WithFieldsPresetForRead(imported map[string]interface{}, r *Resource, kubeClient client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (map[string]interface{}, error)

Types

type Resource

type Resource struct {
	k8s.Resource `json:",inline"`

	Original *k8s.Resource `json:"-"`

	// Fields related to TF provider processing
	TFInfo         *terraform.InstanceInfo        `json:"-"`
	ResourceConfig corekccv1alpha1.ResourceConfig `json:"-"`
	TFResource     *tfschema.Resource             `json:"-"`
}

Resource is a wrapper around k8s.Resource and adds information regarding its corresponding Terraform resource and maintains an original copy of the k8s.Resource.

func GetReferencedResource

func GetReferencedResource(r *Resource, gvk schema.GroupVersionKind,
	resourceRef *v1alpha1.ResourceReference, kubeClient client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (rsrc *Resource, err error)

func NewResource

NewResource returns a Resource, populating the Resource information from u.Kind, using the structs found in sm and p.

func NewResourceFromResourceConfig

func NewResourceFromResourceConfig(rc *corekccv1alpha1.ResourceConfig, p *tfschema.Provider) (*Resource, error)

func (*Resource) ConstructServerGeneratedIDInStatusFromResourceID

func (r *Resource) ConstructServerGeneratedIDInStatusFromResourceID() (string, error)

func (*Resource) GetImportID

GetImportID returns the Terraform import ID for the resource. TODO(kcc-eng): Require ID templates for all resources and remove all implicit defaults.

func (*Resource) GetResourceID

func (r *Resource) GetResourceID() (string, error)

GetResourceID gets the resource's resource ID. The assumption is that the resource supports the `spec.resourceID` field. There are two cases: (1) If `spec.resourceID` is specified, return its value. (2) Otherwise, (happens during KCC upgrade or resource creation), fall back to:

  • Value of `metadata.name` if the resource ID is user-specified.
  • Value of the server generated ID field in status if the resource ID is server-generated.

func (*Resource) GetServerGeneratedID

func (r *Resource) GetServerGeneratedID() (string, error)

GetServerGeneratedID gets the value of the resource's server-generated ID. There are two cases: (1) If the resource supports a server-generated `spec.resourceID`, return

its value if specified.  If unspecified, continue to case (2) but
extract out the resource ID segment from the server-generated ID field
using the value template of the resource ID field.

(2) If the resource doesn't support a server-generated `spec.resourceID`

field, then look up the field defined in ResourceConfig.ServerGeneratedIDField
in `status` and return its value. Note: this value is not a resource ID,
but a raw value in the status field.

func (*Resource) HasIDTemplate

func (r *Resource) HasIDTemplate() bool

func (*Resource) HasServerGeneratedIDField

func (r *Resource) HasServerGeneratedIDField() bool

func (*Resource) ValidateResourceIDIfSupported

func (r *Resource) ValidateResourceIDIfSupported() error

Jump to

Keyboard shortcuts

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