diff --git a/apis/compute/v1beta1/referencers.go b/apis/compute/v1beta1/referencers.go index 35a770b15..b81f3b0e3 100644 --- a/apis/compute/v1beta1/referencers.go +++ b/apis/compute/v1beta1/referencers.go @@ -39,6 +39,17 @@ func NetworkURL() reference.ExtractValueFn { } } +// NetworkSelfLink extracts the SelfLink of a Network. +func NetworkSelfLink() reference.ExtractValueFn { + return func(mg resource.Managed) string { + n, ok := mg.(*Network) + if !ok { + return "" + } + return n.Status.AtProvider.SelfLink + } +} + // SubnetworkURL extracts the partially qualified URL of a Subnetwork. func SubnetworkURL() reference.ExtractValueFn { return func(mg resource.Managed) string { diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go new file mode 100644 index 000000000..9496b9257 --- /dev/null +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -0,0 +1,149 @@ +/* +Copyright 2022 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" +) + +// ManagedZoneParameters define the desired state of a ManagedZone +type ManagedZoneParameters struct { + + // Description: A mutable string of at most 1024 characters associated + // with this resource for the user's convenience. Has no effect on the + // managed zone's function. Defaults to 'Managed by Crossplane' + // +optional + Description *string `json:"description,omitempty"` + + // DNSName: The DNS name of this managed zone, for instance "example.com.". + // +immutable + DNSName string `json:"dnsName"` + + // Labels: User labels. + // +optional + Labels map[string]string `json:"labels,omitempty"` + + // PrivateVisibilityConfig: For privately visible zones, the set of + // Virtual Private Cloud resources that the zone is visible from. + // +optional + PrivateVisibilityConfig *ManagedZonePrivateVisibilityConfig `json:"privateVisibilityConfig,omitempty"` + + // Visibility: The zone's visibility: public zones are exposed to the + // Internet, while private zones are visible only to Virtual Private + // Cloud resources. Defaults to 'public` + // + // Possible values: + // "public" + // "private" + // +optional + // +immutable + // +kubebuilder:validation:Enum=public;private + Visibility *string `json:"visibility,omitempty"` + + // TODO(danielinclouds): support CloudLoggingConfig parameters + // TODO(danielinclouds): support DnssecConfig parameters + // TODO(danielinclouds): support ForwardingConfig parameters + // TODO(danielinclouds): support NameServerSet parameters + // TODO(danielinclouds): support PeeringConfig parameters + // TODO(danielinclouds): support ReverseLookupConfig parameters + // TODO(danielinclouds): support ServiceDirectoryConfig parameters +} + +// ManagedZonePrivateVisibilityConfig the set of Virtual Private Cloud resources +// that the zone is visible from +type ManagedZonePrivateVisibilityConfig struct { + + // Networks: The list of VPC networks that can see this zone. + // +optional + Networks []*ManagedZonePrivateVisibilityConfigNetwork `json:"networks,omitempty"` +} + +// ManagedZonePrivateVisibilityConfigNetwork is a list of VPC networks +type ManagedZonePrivateVisibilityConfigNetwork struct { + + // NetworkUrl: The fully qualified URL of the VPC network to bind to. + // Format this URL like + // https://www.googleapis.com/compute/v1/projects/{project}/global/networks/{network} + // +optional + // +immutable + NetworkURL *string `json:"networkUrl,omitempty"` + + // NetworkRef references to a Network and retrieves its URI + // +optional + // +immutable + NetworkRef *xpv1.Reference `json:"networkRef,omitempty"` + + // NetworkSelector selects a reference to a Network and retrieves its URI + // +optional + // +immutable + NetworkSelector *xpv1.Selector `json:"networkSelector,omitempty"` +} + +// ManagedZoneObservation is used to show the observed state of the ManagedZone +type ManagedZoneObservation struct { + + // CreationTime: The time that this resource was created on the server. + // This is in RFC3339 text format. Output only. + CreationTime string `json:"creationTime,omitempty"` + + // Id: Unique identifier for the resource; defined by the server (output only) + ID uint64 `json:"id,omitempty"` + + // NameServers: Delegate your managed_zone to these virtual name + // servers; defined by the server (output only) + NameServers []string `json:"nameServers,omitempty"` +} + +// ManagedZoneSpec defines the desired state of a ManagedZone. +type ManagedZoneSpec struct { + xpv1.ResourceSpec `json:",inline"` + ForProvider ManagedZoneParameters `json:"forProvider"` +} + +// ManagedZoneStatus represents the observed state of a ManagedZone. +type ManagedZoneStatus struct { + xpv1.ResourceStatus `json:",inline"` + AtProvider ManagedZoneObservation `json:"atProvider,omitempty"` +} + +// +kubebuilder:object:root=true + +// ManagedZone is a managed resource that represents a Managed Zone in Cloud DNS +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="DNS NAME",type="string",JSONPath=".spec.forProvider.dnsName" +// +kubebuilder:printcolumn:name="VISIBILITY",type="string",JSONPath=".spec.forProvider.visibility" +// +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status" +// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].status" +// +kubebuilder:resource:scope=Cluster,categories={crossplane,managed,gcp} +type ManagedZone struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ManagedZoneSpec `json:"spec"` + Status ManagedZoneStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// ManagedZoneList contains a list of ManagedZones +type ManagedZoneList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []ManagedZone `json:"items"` +} diff --git a/apis/dns/v1alpha1/referencers.go b/apis/dns/v1alpha1/referencers.go new file mode 100644 index 000000000..1e40ce876 --- /dev/null +++ b/apis/dns/v1alpha1/referencers.go @@ -0,0 +1,57 @@ +/* +Copyright 2022 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "context" + + "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/crossplane/crossplane-runtime/pkg/errors" + + "github.com/crossplane/crossplane-runtime/pkg/reference" + + computev1beta1 "github.com/crossplane-contrib/provider-gcp/apis/compute/v1beta1" +) + +// ResolveReferences of ManagedZone +func (mg *ManagedZone) ResolveReferences(ctx context.Context, c client.Reader) error { + + if mg.Spec.ForProvider.PrivateVisibilityConfig == nil { + return nil + } + + r := reference.NewAPIResolver(c, mg) + + // Resolve spec.forProvider.privateVisibilityConfig.networks[*].NetworkURL + for i := range mg.Spec.ForProvider.PrivateVisibilityConfig.Networks { + rsp, err := r.Resolve(ctx, reference.ResolutionRequest{ + CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.PrivateVisibilityConfig.Networks[i].NetworkURL), + Reference: mg.Spec.ForProvider.PrivateVisibilityConfig.Networks[i].NetworkRef, + Selector: mg.Spec.ForProvider.PrivateVisibilityConfig.Networks[i].NetworkSelector, + To: reference.To{Managed: &computev1beta1.Network{}, List: &computev1beta1.NetworkList{}}, + Extract: computev1beta1.NetworkSelfLink(), + }) + if err != nil { + return errors.Wrapf(err, "spec.forProvider.PrivateVisibilityConfig.Networks[%d].NetworkURL", i) + } + mg.Spec.ForProvider.PrivateVisibilityConfig.Networks[i].NetworkURL = reference.ToPtrValue(rsp.ResolvedValue) + mg.Spec.ForProvider.PrivateVisibilityConfig.Networks[i].NetworkRef = rsp.ResolvedReference + } + + return nil +} diff --git a/apis/dns/v1alpha1/register.go b/apis/dns/v1alpha1/register.go index 7fbfc3494..d7b2b8ccd 100644 --- a/apis/dns/v1alpha1/register.go +++ b/apis/dns/v1alpha1/register.go @@ -1,5 +1,5 @@ /* -Copyright 2021 The Crossplane Authors. +Copyright 2022 The Crossplane Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -53,7 +53,15 @@ var ( PolicyGroupVersionKind = SchemeGroupVersion.WithKind(PolicyKind) ) +// ManagedZone type metadata +var ( + ManagedZoneKind = reflect.TypeOf(ManagedZone{}).Name() + ManagedZoneGroupKind = schema.GroupKind{Group: Group, Kind: ManagedZoneKind}.String() + ManagedZoneKindAPIVersion = ManagedZoneKind + "." + SchemeGroupVersion.String() + ManagedZoneGroupVersionKind = SchemeGroupVersion.WithKind(ManagedZoneKind) +) + func init() { SchemeBuilder.Register(&ResourceRecordSet{}, &ResourceRecordSetList{}, - &Policy{}, &PolicyList{}) + &Policy{}, &PolicyList{}, &ManagedZone{}, &ManagedZoneList{}) } diff --git a/apis/dns/v1alpha1/zz_generated.deepcopy.go b/apis/dns/v1alpha1/zz_generated.deepcopy.go index d4582dc61..ac0f50943 100644 --- a/apis/dns/v1alpha1/zz_generated.deepcopy.go +++ b/apis/dns/v1alpha1/zz_generated.deepcopy.go @@ -22,9 +22,216 @@ limitations under the License. package v1alpha1 import ( + "github.com/crossplane/crossplane-runtime/apis/common/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZone) DeepCopyInto(out *ManagedZone) { + *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 ManagedZone. +func (in *ManagedZone) DeepCopy() *ManagedZone { + if in == nil { + return nil + } + out := new(ManagedZone) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ManagedZone) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZoneList) DeepCopyInto(out *ManagedZoneList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ManagedZone, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZoneList. +func (in *ManagedZoneList) DeepCopy() *ManagedZoneList { + if in == nil { + return nil + } + out := new(ManagedZoneList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ManagedZoneList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZoneObservation) DeepCopyInto(out *ManagedZoneObservation) { + *out = *in + if in.NameServers != nil { + in, out := &in.NameServers, &out.NameServers + *out = make([]string, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZoneObservation. +func (in *ManagedZoneObservation) DeepCopy() *ManagedZoneObservation { + if in == nil { + return nil + } + out := new(ManagedZoneObservation) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZoneParameters) DeepCopyInto(out *ManagedZoneParameters) { + *out = *in + if in.Description != nil { + in, out := &in.Description, &out.Description + *out = new(string) + **out = **in + } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.PrivateVisibilityConfig != nil { + in, out := &in.PrivateVisibilityConfig, &out.PrivateVisibilityConfig + *out = new(ManagedZonePrivateVisibilityConfig) + (*in).DeepCopyInto(*out) + } + if in.Visibility != nil { + in, out := &in.Visibility, &out.Visibility + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZoneParameters. +func (in *ManagedZoneParameters) DeepCopy() *ManagedZoneParameters { + if in == nil { + return nil + } + out := new(ManagedZoneParameters) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZonePrivateVisibilityConfig) DeepCopyInto(out *ManagedZonePrivateVisibilityConfig) { + *out = *in + if in.Networks != nil { + in, out := &in.Networks, &out.Networks + *out = make([]*ManagedZonePrivateVisibilityConfigNetwork, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(ManagedZonePrivateVisibilityConfigNetwork) + (*in).DeepCopyInto(*out) + } + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZonePrivateVisibilityConfig. +func (in *ManagedZonePrivateVisibilityConfig) DeepCopy() *ManagedZonePrivateVisibilityConfig { + if in == nil { + return nil + } + out := new(ManagedZonePrivateVisibilityConfig) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZonePrivateVisibilityConfigNetwork) DeepCopyInto(out *ManagedZonePrivateVisibilityConfigNetwork) { + *out = *in + if in.NetworkURL != nil { + in, out := &in.NetworkURL, &out.NetworkURL + *out = new(string) + **out = **in + } + if in.NetworkRef != nil { + in, out := &in.NetworkRef, &out.NetworkRef + *out = new(v1.Reference) + (*in).DeepCopyInto(*out) + } + if in.NetworkSelector != nil { + in, out := &in.NetworkSelector, &out.NetworkSelector + *out = new(v1.Selector) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZonePrivateVisibilityConfigNetwork. +func (in *ManagedZonePrivateVisibilityConfigNetwork) DeepCopy() *ManagedZonePrivateVisibilityConfigNetwork { + if in == nil { + return nil + } + out := new(ManagedZonePrivateVisibilityConfigNetwork) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZoneSpec) DeepCopyInto(out *ManagedZoneSpec) { + *out = *in + in.ResourceSpec.DeepCopyInto(&out.ResourceSpec) + in.ForProvider.DeepCopyInto(&out.ForProvider) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZoneSpec. +func (in *ManagedZoneSpec) DeepCopy() *ManagedZoneSpec { + if in == nil { + return nil + } + out := new(ManagedZoneSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ManagedZoneStatus) DeepCopyInto(out *ManagedZoneStatus) { + *out = *in + in.ResourceStatus.DeepCopyInto(&out.ResourceStatus) + in.AtProvider.DeepCopyInto(&out.AtProvider) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManagedZoneStatus. +func (in *ManagedZoneStatus) DeepCopy() *ManagedZoneStatus { + if in == nil { + return nil + } + out := new(ManagedZoneStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Policy) DeepCopyInto(out *Policy) { *out = *in diff --git a/apis/dns/v1alpha1/zz_generated.managed.go b/apis/dns/v1alpha1/zz_generated.managed.go index 4dbadad43..2bb13429e 100644 --- a/apis/dns/v1alpha1/zz_generated.managed.go +++ b/apis/dns/v1alpha1/zz_generated.managed.go @@ -20,6 +20,72 @@ package v1alpha1 import xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" +// GetCondition of this ManagedZone. +func (mg *ManagedZone) GetCondition(ct xpv1.ConditionType) xpv1.Condition { + return mg.Status.GetCondition(ct) +} + +// GetDeletionPolicy of this ManagedZone. +func (mg *ManagedZone) GetDeletionPolicy() xpv1.DeletionPolicy { + return mg.Spec.DeletionPolicy +} + +// GetProviderConfigReference of this ManagedZone. +func (mg *ManagedZone) GetProviderConfigReference() *xpv1.Reference { + return mg.Spec.ProviderConfigReference +} + +/* +GetProviderReference of this ManagedZone. +Deprecated: Use GetProviderConfigReference. +*/ +func (mg *ManagedZone) GetProviderReference() *xpv1.Reference { + return mg.Spec.ProviderReference +} + +// GetPublishConnectionDetailsTo of this ManagedZone. +func (mg *ManagedZone) GetPublishConnectionDetailsTo() *xpv1.PublishConnectionDetailsTo { + return mg.Spec.PublishConnectionDetailsTo +} + +// GetWriteConnectionSecretToReference of this ManagedZone. +func (mg *ManagedZone) GetWriteConnectionSecretToReference() *xpv1.SecretReference { + return mg.Spec.WriteConnectionSecretToReference +} + +// SetConditions of this ManagedZone. +func (mg *ManagedZone) SetConditions(c ...xpv1.Condition) { + mg.Status.SetConditions(c...) +} + +// SetDeletionPolicy of this ManagedZone. +func (mg *ManagedZone) SetDeletionPolicy(r xpv1.DeletionPolicy) { + mg.Spec.DeletionPolicy = r +} + +// SetProviderConfigReference of this ManagedZone. +func (mg *ManagedZone) SetProviderConfigReference(r *xpv1.Reference) { + mg.Spec.ProviderConfigReference = r +} + +/* +SetProviderReference of this ManagedZone. +Deprecated: Use SetProviderConfigReference. +*/ +func (mg *ManagedZone) SetProviderReference(r *xpv1.Reference) { + mg.Spec.ProviderReference = r +} + +// SetPublishConnectionDetailsTo of this ManagedZone. +func (mg *ManagedZone) SetPublishConnectionDetailsTo(r *xpv1.PublishConnectionDetailsTo) { + mg.Spec.PublishConnectionDetailsTo = r +} + +// SetWriteConnectionSecretToReference of this ManagedZone. +func (mg *ManagedZone) SetWriteConnectionSecretToReference(r *xpv1.SecretReference) { + mg.Spec.WriteConnectionSecretToReference = r +} + // GetCondition of this Policy. func (mg *Policy) GetCondition(ct xpv1.ConditionType) xpv1.Condition { return mg.Status.GetCondition(ct) diff --git a/apis/dns/v1alpha1/zz_generated.managedlist.go b/apis/dns/v1alpha1/zz_generated.managedlist.go index 1ed96b775..bd1706e18 100644 --- a/apis/dns/v1alpha1/zz_generated.managedlist.go +++ b/apis/dns/v1alpha1/zz_generated.managedlist.go @@ -20,6 +20,15 @@ package v1alpha1 import resource "github.com/crossplane/crossplane-runtime/pkg/resource" +// GetItems of this ManagedZoneList. +func (l *ManagedZoneList) GetItems() []resource.Managed { + items := make([]resource.Managed, len(l.Items)) + for i := range l.Items { + items[i] = &l.Items[i] + } + return items +} + // GetItems of this PolicyList. func (l *PolicyList) GetItems() []resource.Managed { items := make([]resource.Managed, len(l.Items)) diff --git a/examples/dns/managedzone.yaml b/examples/dns/managedzone.yaml new file mode 100644 index 000000000..76fc0ad51 --- /dev/null +++ b/examples/dns/managedzone.yaml @@ -0,0 +1,11 @@ +apiVersion: dns.gcp.crossplane.io/v1alpha1 +kind: ManagedZone +metadata: + name: example +spec: + forProvider: + description: example + dnsName: example.com. + visibility: private + providerConfigRef: + name: example diff --git a/package/crds/dns.gcp.crossplane.io_managedzones.yaml b/package/crds/dns.gcp.crossplane.io_managedzones.yaml new file mode 100644 index 000000000..55df2ca97 --- /dev/null +++ b/package/crds/dns.gcp.crossplane.io_managedzones.yaml @@ -0,0 +1,428 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.9.2 + creationTimestamp: null + name: managedzones.dns.gcp.crossplane.io +spec: + group: dns.gcp.crossplane.io + names: + categories: + - crossplane + - managed + - gcp + kind: ManagedZone + listKind: ManagedZoneList + plural: managedzones + singular: managedzone + scope: Cluster + versions: + - additionalPrinterColumns: + - jsonPath: .spec.forProvider.dnsName + name: DNS NAME + type: string + - jsonPath: .spec.forProvider.visibility + name: VISIBILITY + type: string + - jsonPath: .status.conditions[?(@.type=='Ready')].status + name: READY + type: string + - jsonPath: .status.conditions[?(@.type=='Synced')].status + name: SYNCED + type: string + name: v1alpha1 + schema: + openAPIV3Schema: + description: ManagedZone is a managed resource that represents a Managed Zone + in Cloud DNS + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: ManagedZoneSpec defines the desired state of a ManagedZone. + properties: + deletionPolicy: + default: Delete + description: DeletionPolicy specifies what will happen to the underlying + external when this managed resource is deleted - either "Delete" + or "Orphan" the external resource. + enum: + - Orphan + - Delete + type: string + forProvider: + description: ManagedZoneParameters define the desired state of a ManagedZone + properties: + description: + description: 'Description: A mutable string of at most 1024 characters + associated with this resource for the user''s convenience. Has + no effect on the managed zone''s function. Defaults to ''Managed + by Crossplane''' + type: string + dnsName: + description: 'DNSName: The DNS name of this managed zone, for + instance "example.com.".' + type: string + labels: + additionalProperties: + type: string + description: 'Labels: User labels.' + type: object + privateVisibilityConfig: + description: 'PrivateVisibilityConfig: For privately visible zones, + the set of Virtual Private Cloud resources that the zone is + visible from.' + properties: + networks: + description: 'Networks: The list of VPC networks that can + see this zone.' + items: + description: ManagedZonePrivateVisibilityConfigNetwork is + a list of VPC networks + properties: + networkRef: + description: NetworkRef references to a Network and + retrieves its URI + properties: + name: + description: Name of the referenced object. + type: string + policy: + description: Policies for referencing. + properties: + resolution: + default: Required + description: Resolution specifies whether resolution + of this reference is required. The default + is 'Required', which means the reconcile will + fail if the reference cannot be resolved. + 'Optional' means this reference will be a + no-op if it cannot be resolved. + enum: + - Required + - Optional + type: string + resolve: + description: Resolve specifies when this reference + should be resolved. The default is 'IfNotPresent', + which will attempt to resolve the reference + only when the corresponding field is not present. + Use 'Always' to resolve the reference on every + reconcile. + enum: + - Always + - IfNotPresent + type: string + type: object + required: + - name + type: object + networkSelector: + description: NetworkSelector selects a reference to + a Network and retrieves its URI + properties: + matchControllerRef: + description: MatchControllerRef ensures an object + with the same controller reference as the selecting + object is selected. + type: boolean + matchLabels: + additionalProperties: + type: string + description: MatchLabels ensures an object with + matching labels is selected. + type: object + policy: + description: Policies for selection. + properties: + resolution: + default: Required + description: Resolution specifies whether resolution + of this reference is required. The default + is 'Required', which means the reconcile will + fail if the reference cannot be resolved. + 'Optional' means this reference will be a + no-op if it cannot be resolved. + enum: + - Required + - Optional + type: string + resolve: + description: Resolve specifies when this reference + should be resolved. The default is 'IfNotPresent', + which will attempt to resolve the reference + only when the corresponding field is not present. + Use 'Always' to resolve the reference on every + reconcile. + enum: + - Always + - IfNotPresent + type: string + type: object + type: object + networkUrl: + description: 'NetworkUrl: The fully qualified URL of + the VPC network to bind to. Format this URL like https://www.googleapis.com/compute/v1/projects/{project}/global/networks/{network}' + type: string + type: object + type: array + type: object + visibility: + description: "Visibility: The zone's visibility: public zones + are exposed to the Internet, while private zones are visible + only to Virtual Private Cloud resources. Defaults to 'public` + \n Possible values: \"public\" \"private\"" + enum: + - public + - private + type: string + required: + - dnsName + type: object + providerConfigRef: + default: + name: default + description: ProviderConfigReference specifies how the provider that + will be used to create, observe, update, and delete this managed + resource should be configured. + properties: + name: + description: Name of the referenced object. + type: string + policy: + description: Policies for referencing. + properties: + resolution: + default: Required + description: Resolution specifies whether resolution of this + reference is required. The default is 'Required', which + means the reconcile will fail if the reference cannot be + resolved. 'Optional' means this reference will be a no-op + if it cannot be resolved. + enum: + - Required + - Optional + type: string + resolve: + description: Resolve specifies when this reference should + be resolved. The default is 'IfNotPresent', which will attempt + to resolve the reference only when the corresponding field + is not present. Use 'Always' to resolve the reference on + every reconcile. + enum: + - Always + - IfNotPresent + type: string + type: object + required: + - name + type: object + providerRef: + description: 'ProviderReference specifies the provider that will be + used to create, observe, update, and delete this managed resource. + Deprecated: Please use ProviderConfigReference, i.e. `providerConfigRef`' + properties: + name: + description: Name of the referenced object. + type: string + policy: + description: Policies for referencing. + properties: + resolution: + default: Required + description: Resolution specifies whether resolution of this + reference is required. The default is 'Required', which + means the reconcile will fail if the reference cannot be + resolved. 'Optional' means this reference will be a no-op + if it cannot be resolved. + enum: + - Required + - Optional + type: string + resolve: + description: Resolve specifies when this reference should + be resolved. The default is 'IfNotPresent', which will attempt + to resolve the reference only when the corresponding field + is not present. Use 'Always' to resolve the reference on + every reconcile. + enum: + - Always + - IfNotPresent + type: string + type: object + required: + - name + type: object + publishConnectionDetailsTo: + description: PublishConnectionDetailsTo specifies the connection secret + config which contains a name, metadata and a reference to secret + store config to which any connection details for this managed resource + should be written. Connection details frequently include the endpoint, + username, and password required to connect to the managed resource. + properties: + configRef: + default: + name: default + description: SecretStoreConfigRef specifies which secret store + config should be used for this ConnectionSecret. + properties: + name: + description: Name of the referenced object. + type: string + policy: + description: Policies for referencing. + properties: + resolution: + default: Required + description: Resolution specifies whether resolution of + this reference is required. The default is 'Required', + which means the reconcile will fail if the reference + cannot be resolved. 'Optional' means this reference + will be a no-op if it cannot be resolved. + enum: + - Required + - Optional + type: string + resolve: + description: Resolve specifies when this reference should + be resolved. The default is 'IfNotPresent', which will + attempt to resolve the reference only when the corresponding + field is not present. Use 'Always' to resolve the reference + on every reconcile. + enum: + - Always + - IfNotPresent + type: string + type: object + required: + - name + type: object + metadata: + description: Metadata is the metadata for connection secret. + properties: + annotations: + additionalProperties: + type: string + description: Annotations are the annotations to be added to + connection secret. - For Kubernetes secrets, this will be + used as "metadata.annotations". - It is up to Secret Store + implementation for others store types. + type: object + labels: + additionalProperties: + type: string + description: Labels are the labels/tags to be added to connection + secret. - For Kubernetes secrets, this will be used as "metadata.labels". + - It is up to Secret Store implementation for others store + types. + type: object + type: + description: Type is the SecretType for the connection secret. + - Only valid for Kubernetes Secret Stores. + type: string + type: object + name: + description: Name is the name of the connection secret. + type: string + required: + - name + type: object + writeConnectionSecretToRef: + description: WriteConnectionSecretToReference specifies the namespace + and name of a Secret to which any connection details for this managed + resource should be written. Connection details frequently include + the endpoint, username, and password required to connect to the + managed resource. This field is planned to be replaced in a future + release in favor of PublishConnectionDetailsTo. Currently, both + could be set independently and connection details would be published + to both without affecting each other. + properties: + name: + description: Name of the secret. + type: string + namespace: + description: Namespace of the secret. + type: string + required: + - name + - namespace + type: object + required: + - forProvider + type: object + status: + description: ManagedZoneStatus represents the observed state of a ManagedZone. + properties: + atProvider: + description: ManagedZoneObservation is used to show the observed state + of the ManagedZone + properties: + creationTime: + description: 'CreationTime: The time that this resource was created + on the server. This is in RFC3339 text format. Output only.' + type: string + id: + description: 'Id: Unique identifier for the resource; defined + by the server (output only)' + format: int64 + type: integer + nameServers: + description: 'NameServers: Delegate your managed_zone to these + virtual name servers; defined by the server (output only)' + items: + type: string + type: array + type: object + conditions: + description: Conditions of the resource. + items: + description: A Condition that may apply to a resource. + properties: + lastTransitionTime: + description: LastTransitionTime is the last time this condition + transitioned from one status to another. + format: date-time + type: string + message: + description: A Message containing details about this condition's + last transition from one status to another, if any. + type: string + reason: + description: A Reason for this condition's last transition from + one status to another. + type: string + status: + description: Status of this condition; is it currently True, + False, or Unknown? + type: string + type: + description: Type of this condition. At most one of each condition + type may apply to a resource at any point in time. + type: string + required: + - lastTransitionTime + - reason + - status + - type + type: object + type: array + type: object + required: + - spec + type: object + served: true + storage: true + subresources: + status: {} diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go new file mode 100644 index 000000000..a89f40735 --- /dev/null +++ b/pkg/clients/managedzone/managed_zone.go @@ -0,0 +1,115 @@ +/* +Copyright 2022 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package managedzone + +import ( + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/mitchellh/copystructure" + dns "google.golang.org/api/dns/v1" + + "github.com/crossplane/crossplane-runtime/pkg/errors" + + "github.com/crossplane-contrib/provider-gcp/apis/dns/v1alpha1" + gcp "github.com/crossplane-contrib/provider-gcp/pkg/clients" +) + +const ( + errorCheckUpToDate = "unable to determine if external resource is up to date" +) + +// GenerateManagedZone generates *dns.ManagedZone instance from ManagedZoneParameters +func GenerateManagedZone(name string, spec v1alpha1.ManagedZoneParameters, mz *dns.ManagedZone) { + mz.Kind = "dns#managedZone" // This is the only valid value for this field + mz.Name = name + mz.DnsName = spec.DNSName + mz.Labels = spec.Labels + + // Value in describe parameter is required + if spec.Description != nil { + mz.Description = *spec.Description + } else { + mz.Description = "Managed by Crossplane" + } + + if spec.Visibility != nil { + mz.Visibility = *spec.Visibility + } else { + mz.Visibility = "public" + } + + if spec.PrivateVisibilityConfig != nil { + mz.PrivateVisibilityConfig = &dns.ManagedZonePrivateVisibilityConfig{ + Kind: "dns#managedZonePrivateVisibilityConfig", // This is the only valid value for this field + Networks: make([]*dns.ManagedZonePrivateVisibilityConfigNetwork, len(spec.PrivateVisibilityConfig.Networks)), + } + + for i, v := range spec.PrivateVisibilityConfig.Networks { + mz.PrivateVisibilityConfig.Networks[i] = &dns.ManagedZonePrivateVisibilityConfigNetwork{ + Kind: "dns#managedZonePrivateVisibilityConfigNetwork", // This is the only valid value for this field + NetworkUrl: *v.NetworkURL, + } + } + } + +} + +// LateInitializeSpec updates any unset (i.e. nil) optional fields of the +// supplied ManagedZoneParameters that are set (i.e. non-zero) on the supplied +// ManagedZone. +func LateInitializeSpec(spec *v1alpha1.ManagedZoneParameters, observed dns.ManagedZone) { + spec.Description = gcp.LateInitializeString(spec.Description, observed.Description) + spec.Visibility = gcp.LateInitializeString(spec.Visibility, observed.Visibility) + spec.Labels = gcp.LateInitializeStringMap(spec.Labels, observed.Labels) + + if observed.PrivateVisibilityConfig != nil && spec.PrivateVisibilityConfig == nil { + spec.PrivateVisibilityConfig = &v1alpha1.ManagedZonePrivateVisibilityConfig{} + if len(observed.PrivateVisibilityConfig.Networks) != 0 { + spec.PrivateVisibilityConfig.Networks = make([]*v1alpha1.ManagedZonePrivateVisibilityConfigNetwork, len(observed.PrivateVisibilityConfig.Networks)) + for i, network := range observed.PrivateVisibilityConfig.Networks { + spec.PrivateVisibilityConfig.Networks[i] = &v1alpha1.ManagedZonePrivateVisibilityConfigNetwork{ + NetworkURL: &network.NetworkUrl, + } + } + } + } +} + +// IsUpToDate checks whether current state is up-to-date compared to the given +// set of parameters. +func IsUpToDate(name string, spec *v1alpha1.ManagedZoneParameters, observed *dns.ManagedZone) (bool, error) { + generated, err := copystructure.Copy(observed) + if err != nil { + return true, errors.Wrap(err, errorCheckUpToDate) + } + desired, ok := generated.(*dns.ManagedZone) + if !ok { + return true, errors.New(errorCheckUpToDate) + } + GenerateManagedZone(name, *spec, desired) + return cmp.Equal(desired, observed, cmpopts.EquateEmpty()), nil +} + +// GenerateManagedZoneObservation takes a dns.ManagedZone and returns +// *ManagedZoneObservation. +func GenerateManagedZoneObservation(observed dns.ManagedZone) v1alpha1.ManagedZoneObservation { + return v1alpha1.ManagedZoneObservation{ + CreationTime: observed.CreationTime, + ID: observed.Id, + NameServers: observed.NameServers, + } +} diff --git a/pkg/clients/managedzone/managed_zone_test.go b/pkg/clients/managedzone/managed_zone_test.go new file mode 100644 index 000000000..36b715a02 --- /dev/null +++ b/pkg/clients/managedzone/managed_zone_test.go @@ -0,0 +1,205 @@ +/* +Copyright 2022 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package managedzone + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + + "google.golang.org/api/dns/v1" + + "github.com/crossplane-contrib/provider-gcp/apis/dns/v1alpha1" +) + +const ( + name = "test-mz" + dnsName = "test.local." +) + +var ( + testDescription = "test description" + testVisibility = "private" + fakeLabels = map[string]string{"label": "one"} +) + +func params(m ...func(*v1alpha1.ManagedZoneParameters)) *v1alpha1.ManagedZoneParameters { + p := &v1alpha1.ManagedZoneParameters{ + Description: &testDescription, + DNSName: dnsName, + Visibility: &testVisibility, + } + + for _, f := range m { + f(p) + } + return p +} + +func managedZone(m ...func(*dns.ManagedZone)) *dns.ManagedZone { + mz := &dns.ManagedZone{ + Kind: "dns#managedZone", + Description: testDescription, + DnsName: dnsName, + Name: name, + Visibility: testVisibility, + } + + for _, f := range m { + f(mz) + } + return mz +} + +func TestGenerateManagedZone(t *testing.T) { + type args struct { + name string + params v1alpha1.ManagedZoneParameters + } + type want struct { + managedZone *dns.ManagedZone + } + cases := map[string]struct { + args args + want want + }{ + "FullConversion": { + args: args{ + name: name, + params: *params(), + }, + want: want{ + managedZone: managedZone(), + }, + }, + "MissingFields": { + args: args{ + name: name, + params: *params(func(p *v1alpha1.ManagedZoneParameters) { + p.PrivateVisibilityConfig = nil + }), + }, + want: want{ + managedZone: managedZone(func(mz *dns.ManagedZone) { + mz.PrivateVisibilityConfig = nil + }), + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + mz := &dns.ManagedZone{} + GenerateManagedZone(tc.args.name, tc.args.params, mz) + if diff := cmp.Diff(tc.want.managedZone, mz); diff != "" { + t.Errorf("GenerateManagedZone(...): -want, +got:\n%s", diff) + } + }) + } +} + +func TestLateInitializeSpec(t *testing.T) { + type args struct { + spec *v1alpha1.ManagedZoneParameters + external *dns.ManagedZone + } + type want struct { + params *v1alpha1.ManagedZoneParameters + } + cases := map[string]struct { + args args + want want + }{ + "SomeFields": { + args: args{ + spec: params(func(p *v1alpha1.ManagedZoneParameters) { + p.Labels = nil + }), + external: managedZone(func(mz *dns.ManagedZone) { + mz.Labels = fakeLabels + }), + }, + want: want{ + params: params(func(p *v1alpha1.ManagedZoneParameters) { + p.Labels = fakeLabels + }), + }, + }, + "AllFilledAlready": { + args: args{ + spec: params(), + external: managedZone(), + }, + want: want{ + params: params(), + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + LateInitializeSpec(tc.args.spec, *tc.args.external) + if diff := cmp.Diff(tc.want.params, tc.args.spec); diff != "" { + t.Errorf("LateInitializeSpec(...): -want, +got:\n%s", diff) + } + }) + } +} + +func TestIsUpToDate(t *testing.T) { + type args struct { + params *v1alpha1.ManagedZoneParameters + mz *dns.ManagedZone + } + type want struct { + upToDate bool + } + cases := map[string]struct { + args args + want want + }{ + "IsUpToDate": { + args: args{ + params: params(), + mz: managedZone(), + }, + want: want{ + upToDate: true, + }, + }, + "NeedsUpdate": { + args: args{ + params: params(), + mz: managedZone(func(mz *dns.ManagedZone) { + mz.Description = "new description" + }), + }, + want: want{ + upToDate: false, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got, _ := IsUpToDate("test-mz", tc.args.params, tc.args.mz) + if diff := cmp.Diff(tc.want.upToDate, got); diff != "" { + t.Errorf("IsUpToDate(...): -want, +got:\n%s", diff) + } + }) + } +} diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go new file mode 100644 index 000000000..6b8d974b7 --- /dev/null +++ b/pkg/controller/dns/managed_zone.go @@ -0,0 +1,205 @@ +/* +Copyright 2022 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dns + +import ( + "context" + + "github.com/google/go-cmp/cmp" + dns "google.golang.org/api/dns/v1" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + + xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + "github.com/crossplane/crossplane-runtime/pkg/connection" + "github.com/crossplane/crossplane-runtime/pkg/controller" + "github.com/crossplane/crossplane-runtime/pkg/errors" + "github.com/crossplane/crossplane-runtime/pkg/event" + "github.com/crossplane/crossplane-runtime/pkg/meta" + "github.com/crossplane/crossplane-runtime/pkg/ratelimiter" + "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" + "github.com/crossplane/crossplane-runtime/pkg/resource" + + "github.com/crossplane-contrib/provider-gcp/apis/dns/v1alpha1" + scv1alpha1 "github.com/crossplane-contrib/provider-gcp/apis/v1alpha1" + gcp "github.com/crossplane-contrib/provider-gcp/pkg/clients" + mzclient "github.com/crossplane-contrib/provider-gcp/pkg/clients/managedzone" + "github.com/crossplane-contrib/provider-gcp/pkg/features" +) + +const ( + errNotManagedZone = "managed resource is not of type DNS ManagedZone" + errGetManagedZone = "cannot get the DNS ManagedZone" + errCreateManagedZone = "cannot create DNS ManagedZone" + errDeleteManagedZone = "cannot delete DNS ManagedZone" + errUpToDateManagedZone = "cannot determine if ManagedZone is up to date" +) + +// SetupManagedZone adds a controller that reconciles the +// DNS ManagedZone resources. +func SetupManagedZone(mgr ctrl.Manager, o controller.Options) error { + name := managed.ControllerName(v1alpha1.ManagedZoneGroupKind) + + cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())} + if o.Features.Enabled(features.EnableAlphaExternalSecretStores) { + cps = append(cps, connection.NewDetailsManager(mgr.GetClient(), scv1alpha1.StoreConfigGroupVersionKind)) + } + + r := managed.NewReconciler(mgr, + resource.ManagedKind(v1alpha1.ManagedZoneGroupVersionKind), + managed.WithExternalConnecter(&managedZoneConnector{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), + managed.WithPollInterval(o.PollInterval), + managed.WithLogger(o.Logger.WithValues("controller", name)), + managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), + managed.WithConnectionPublishers(cps...)) + + return ctrl.NewControllerManagedBy(mgr). + Named(name). + WithOptions(o.ForControllerRuntime()). + For(&v1alpha1.ManagedZone{}). + Complete(ratelimiter.NewReconciler(name, r, o.GlobalRateLimiter)) +} + +type managedZoneConnector struct { + kube client.Client +} + +func (c *managedZoneConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) { + projectID, opts, err := gcp.GetConnectionInfo(ctx, c.kube, mg) + if err != nil { + return nil, err + } + + d, err := dns.NewService(ctx, opts...) + if err != nil { + return nil, errors.Wrap(err, errNewClient) + } + return &managedZoneExternal{ + kube: c.kube, + dns: d.ManagedZones, + projectID: projectID, + }, nil +} + +type managedZoneExternal struct { + kube client.Client + dns *dns.ManagedZonesService + projectID string +} + +func (e *managedZoneExternal) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) { + cr, ok := mg.(*v1alpha1.ManagedZone) + if !ok { + return managed.ExternalObservation{}, errors.New(errNotManagedZone) + } + + observed, err := e.dns.Get( + e.projectID, + meta.GetExternalName(cr), + ).Context(ctx).Do() + if err != nil { + return managed.ExternalObservation{}, errors.Wrap( + resource.Ignore(gcp.IsErrorNotFound, err), + errGetManagedZone, + ) + } + + lateInit := false + currentSpec := cr.Spec.ForProvider.DeepCopy() + mzclient.LateInitializeSpec(&cr.Spec.ForProvider, *observed) + if !cmp.Equal(currentSpec, &cr.Spec.ForProvider) { + lateInit = true + } + + cr.Status.AtProvider = mzclient.GenerateManagedZoneObservation(*observed) + + cr.SetConditions(xpv1.Available()) + + upToDate, err := mzclient.IsUpToDate( + meta.GetExternalName(cr), + &cr.Spec.ForProvider, + observed) + if err != nil { + return managed.ExternalObservation{}, errors.Wrap(err, errUpToDateManagedZone) + } + + return managed.ExternalObservation{ + ResourceExists: true, + ResourceUpToDate: upToDate, + ResourceLateInitialized: lateInit, + }, nil +} + +func (e *managedZoneExternal) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) { + cr, ok := mg.(*v1alpha1.ManagedZone) + if !ok { + return managed.ExternalCreation{}, errors.New(errNotManagedZone) + } + + args := &dns.ManagedZone{} + + mzclient.GenerateManagedZone( + meta.GetExternalName(cr), + cr.Spec.ForProvider, + args, + ) + + _, err := e.dns.Create( + e.projectID, + args, + ).Context(ctx).Do() + + return managed.ExternalCreation{}, errors.Wrap(err, errCreateManagedZone) +} + +func (e *managedZoneExternal) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) { + cr, ok := mg.(*v1alpha1.ManagedZone) + if !ok { + return managed.ExternalUpdate{}, errors.New(errNotManagedZone) + } + + args := &dns.ManagedZone{} + mzclient.GenerateManagedZone( + meta.GetExternalName(cr), + cr.Spec.ForProvider, + args, + ) + + _, err := e.dns.Patch( + e.projectID, + meta.GetExternalName(cr), + args, + ).Context(ctx).Do() + + return managed.ExternalUpdate{}, err +} + +func (e *managedZoneExternal) Delete(ctx context.Context, mg resource.Managed) error { + cr, ok := mg.(*v1alpha1.ManagedZone) + if !ok { + return errors.New(errNotManagedZone) + } + + cr.SetConditions(xpv1.Deleting()) + err := e.dns.Delete( + e.projectID, + meta.GetExternalName(cr), + ).Context(ctx).Do() + + return errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errDeleteManagedZone) +} diff --git a/pkg/controller/dns/managed_zone_test.go b/pkg/controller/dns/managed_zone_test.go new file mode 100644 index 000000000..ba498f7b6 --- /dev/null +++ b/pkg/controller/dns/managed_zone_test.go @@ -0,0 +1,491 @@ +/* +Copyright 2022 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dns + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/google/go-cmp/cmp" + dns "google.golang.org/api/dns/v1" + "google.golang.org/api/googleapi" + "google.golang.org/api/option" + "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/crossplane/crossplane-runtime/pkg/errors" + "github.com/crossplane/crossplane-runtime/pkg/meta" + "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" + "github.com/crossplane/crossplane-runtime/pkg/resource" + "github.com/crossplane/crossplane-runtime/pkg/test" + + "github.com/crossplane-contrib/provider-gcp/apis/dns/v1alpha1" + mzclient "github.com/crossplane-contrib/provider-gcp/pkg/clients/managedzone" +) + +const ( + managedZoneProjectID = "myproject-id-1234" +) + +var ( + nonManagedZone resource.Managed + managedZoneLabels = map[string]string{"foo": "bar"} +) + +type ManagedZoneOption func(*v1alpha1.ManagedZone) + +func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone { + mz := &v1alpha1.ManagedZone{} + + for _, f := range opts { + f(mz) + } + + return mz +} + +func withLabels(l map[string]string) ManagedZoneOption { + return func(mz *v1alpha1.ManagedZone) { + mz.Spec.ForProvider.Labels = l + } +} + +func managedZoneGError(code int, message string) *googleapi.Error { + return &googleapi.Error{ + Code: code, + Body: "{}\n", + Message: message, + } +} + +func TestManagedZoneObserve(t *testing.T) { + type args struct { + mg resource.Managed + } + type want struct { + e managed.ExternalObservation + err error + } + + cases := map[string]struct { + reason string + handler http.Handler + kube client.Client + args args + want want + }{ + "NotManagedZone": { + reason: "Should return an error if the resource is not ManagedZone", + args: args{ + mg: nonManagedZone, + }, + want: want{ + e: managed.ExternalObservation{}, + err: errors.New(errNotManagedZone), + }, + }, + "ResourceNotFound": { + reason: "Should not return an error if the API response is 404", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalObservation{}, + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusNotFound) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + "InternalError": { + reason: "Should return an error if the error is different than 404", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalObservation{}, + err: errors.Wrap(managedZoneGError(http.StatusInternalServerError, ""), errGetManagedZone), + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusInternalServerError) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + "UpdateResourceSpecSuccess": { + reason: "Should not return an error if the internal update succeeds", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalObservation{ + ResourceLateInitialized: true, + ResourceExists: true, + ResourceUpToDate: true, + }, + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusOK) + cr := newManagedZone(withLabels(managedZoneLabels)) + mz := &dns.ManagedZone{} + mzclient.GenerateManagedZone(meta.GetExternalName(cr), cr.Spec.ForProvider, mz) + if err := json.NewEncoder(w).Encode(mz); err != nil { + t.Error(err) + } + }), + kube: &test.MockClient{ + MockUpdate: test.NewMockUpdateFn(nil), + }, + }, + "ResourceNotUpToDate": { + reason: "Should return upToDate as false if the resource is not up to date", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalObservation{ + ResourceExists: true, + ResourceUpToDate: false, + }, + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + server := httptest.NewServer(tc.handler) + defer server.Close() + s, _ := dns.NewService(context.Background(), option.WithEndpoint(server.URL), option.WithoutAuthentication()) + e := managedZoneExternal{ + kube: tc.kube, + projectID: managedZoneProjectID, + dns: s.ManagedZones, + } + got, err := e.Observe(context.Background(), tc.args.mg) + + if diff := cmp.Diff(tc.want.e, got); diff != "" { + t.Errorf("Observe(...): -want, +got:\n%s", diff) + } + if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + t.Errorf("Observe(...): -want error, +got error:\n%s", diff) + } + }) + } +} + +func TestManagedZoneCreate(t *testing.T) { + type args struct { + mg resource.Managed + } + type want struct { + e managed.ExternalCreation + err error + } + + cases := map[string]struct { + reason string + handler http.Handler + args args + want want + }{ + "NotManagedZone": { + reason: "Should return an error if the resource is not ManagedZone", + args: args{ + mg: nonManagedZone, + }, + want: want{ + e: managed.ExternalCreation{}, + err: errors.New(errNotManagedZone), + }, + }, + "Successful": { + reason: "Should succeed if the resource creation doesn't return an error", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalCreation{}, + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodPost, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + "Failed": { + reason: "Should fail if the resource creation returns an error", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalCreation{}, + err: errors.Wrap(managedZoneGError(http.StatusBadRequest, ""), errCreateManagedZone), + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodPost, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusBadRequest) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + server := httptest.NewServer(tc.handler) + defer server.Close() + s, _ := dns.NewService(context.Background(), option.WithEndpoint(server.URL), option.WithoutAuthentication()) + e := managedZoneExternal{ + projectID: managedZoneProjectID, + dns: s.ManagedZones, + } + got, err := e.Create(context.Background(), tc.args.mg) + if diff := cmp.Diff(tc.want.e, got); diff != "" { + t.Errorf("Create(...): -want, +got:\n%s", diff) + } + if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + t.Errorf("Create(...): -want error, +got error:\n%s", diff) + } + }) + } +} + +func TestManagedZoneUpdate(t *testing.T) { + type args struct { + mg resource.Managed + } + type want struct { + e managed.ExternalUpdate + err error + } + + cases := map[string]struct { + reason string + handler http.Handler + args args + want want + }{ + "NotManagedZone": { + reason: "Should return an error if the resource is not ManagedZone", + args: args{ + mg: nonManagedZone, + }, + want: want{ + e: managed.ExternalUpdate{}, + err: errors.New(errNotManagedZone), + }, + }, + "Successful": { + reason: "Should succeed if the resource update doesn't return an error", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalUpdate{}, + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodPatch, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + "Failed": { + reason: "Should fail if the resource update returns an error", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalUpdate{}, + err: managedZoneGError(http.StatusBadRequest, ""), + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodPatch, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusBadRequest) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + server := httptest.NewServer(tc.handler) + defer server.Close() + s, _ := dns.NewService(context.Background(), option.WithEndpoint(server.URL), option.WithoutAuthentication()) + e := managedZoneExternal{ + projectID: managedZoneProjectID, + dns: s.ManagedZones, + } + got, err := e.Update(context.Background(), tc.args.mg) + if diff := cmp.Diff(tc.want.e, got); diff != "" { + t.Errorf("Update(...): -want, +got:\n%s", diff) + } + if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + t.Errorf("Update(...): -want error, +got error:\n%s", diff) + } + }) + } +} + +func TestManagedZoneDelete(t *testing.T) { + type args struct { + mg resource.Managed + } + type want struct { + err error + } + + cases := map[string]struct { + reason string + handler http.Handler + args args + want want + }{ + "NotManagedZone": { + reason: "Should return an error if the resource is not ManagedZone", + args: args{ + mg: nonManagedZone, + }, + want: want{ + err: errors.New(errNotManagedZone), + }, + }, + "Successful": { + reason: "Should succeed if the resource update doesn't return an error", + args: args{ + mg: newManagedZone(), + }, + want: want{ + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodDelete, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + "Failed": { + reason: "Should fail if the resource update returns an error", + args: args{ + mg: newManagedZone(), + }, + want: want{ + err: errors.Wrap(managedZoneGError(http.StatusBadRequest, ""), errDeleteManagedZone), + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodDelete, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusBadRequest) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + "NotFound": { + reason: "Should not return an error if the resource is not found", + args: args{ + mg: newManagedZone(), + }, + want: want{ + err: nil, + }, + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = r.Body.Close() + if diff := cmp.Diff(http.MethodDelete, r.Method); diff != "" { + t.Errorf("r: -want, +got:\n%s", diff) + } + w.WriteHeader(http.StatusNotFound) + if err := json.NewEncoder(w).Encode(&dns.ManagedZone{}); err != nil { + t.Error(err) + } + }), + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + server := httptest.NewServer(tc.handler) + defer server.Close() + s, _ := dns.NewService(context.Background(), option.WithEndpoint(server.URL), option.WithoutAuthentication()) + e := managedZoneExternal{ + projectID: managedZoneProjectID, + dns: s.ManagedZones, + } + err := e.Delete(context.Background(), tc.args.mg) + if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { + t.Errorf("Delete(...): -want error, +got error:\n%s", diff) + } + }) + } +} diff --git a/pkg/controller/gcp.go b/pkg/controller/gcp.go index 98becec94..7fad5b765 100644 --- a/pkg/controller/gcp.go +++ b/pkg/controller/gcp.go @@ -49,6 +49,7 @@ func Setup(mgr ctrl.Manager, o controller.Options) error { container.SetupCluster, container.SetupNodePool, database.SetupCloudSQLInstance, + dns.SetupManagedZone, dns.SetupPolicy, dns.SetupResourceRecordSet, iam.SetupServiceAccount,