From 338d253fc37987e27a7581e840efe61db45950b0 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 10 Sep 2022 12:35:03 +0200 Subject: [PATCH 01/21] Add ManagedZone to apis folder Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/managed_zone_types.go | 129 ++++++++++++++++++++++++ apis/dns/v1alpha1/register.go | 12 ++- 2 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 apis/dns/v1alpha1/managed_zone_types.go diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go new file mode 100644 index 000000000..94f69ef64 --- /dev/null +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -0,0 +1,129 @@ +/* +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"` +} + +// 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. + Networks []*ManagedZonePrivateVisibilityConfigNetwork `json:"networks"` +} + +// 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} + NetworkURL *string `json:"networkUrl"` +} + +// 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="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status" +// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].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="DESCRIPTION",type="string",JSONPath=".spec.forProvider.description" +// +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/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{}) } From e3102933dc717929a3d88df246ceb2be7d834ad7 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 10 Sep 2022 12:37:49 +0200 Subject: [PATCH 02/21] Generate code and manifests Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/zz_generated.deepcopy.go | 206 +++++++++++++ apis/dns/v1alpha1/zz_generated.managed.go | 66 ++++ apis/dns/v1alpha1/zz_generated.managedlist.go | 9 + .../dns.gcp.crossplane.io_managedzones.yaml | 286 ++++++++++++++++++ 4 files changed, 567 insertions(+) create mode 100644 package/crds/dns.gcp.crossplane.io_managedzones.yaml diff --git a/apis/dns/v1alpha1/zz_generated.deepcopy.go b/apis/dns/v1alpha1/zz_generated.deepcopy.go index d4582dc61..cee9372a2 100644 --- a/apis/dns/v1alpha1/zz_generated.deepcopy.go +++ b/apis/dns/v1alpha1/zz_generated.deepcopy.go @@ -25,6 +25,212 @@ import ( 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.CreationTime != nil { + in, out := &in.CreationTime, &out.CreationTime + *out = new(string) + **out = **in + } + if in.ID != nil { + in, out := &in.ID, &out.ID + *out = new(uint64) + **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 + } +} + +// 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/package/crds/dns.gcp.crossplane.io_managedzones.yaml b/package/crds/dns.gcp.crossplane.io_managedzones.yaml new file mode 100644 index 000000000..bbb0954ac --- /dev/null +++ b/package/crds/dns.gcp.crossplane.io_managedzones.yaml @@ -0,0 +1,286 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.8.0 + 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: .status.conditions[?(@.type=='Ready')].status + name: READY + type: string + - jsonPath: .status.conditions[?(@.type=='Synced')].status + name: SYNCED + type: string + - jsonPath: .spec.forProvider.dnsName + name: DNS NAME + type: string + - jsonPath: .spec.forProvider.visibility + name: VISIBILITY + type: string + - jsonPath: .spec.forProvider.description + name: DESCRIPTION + 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: + 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 + required: + - networkUrl + type: object + type: array + required: + - networks + 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 + 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 + 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 + 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: {} +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] From 37c0da3d84dc60d0e7e515d36976c58ddd546df7 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 10 Sep 2022 12:40:10 +0200 Subject: [PATCH 03/21] Add example Signed-off-by: Daniel Kozlowski --- examples/dns/managedzone.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 examples/dns/managedzone.yaml 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 From 3e9783d94a3ef15252cc299ab7832907c4809ce6 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 10 Sep 2022 12:46:09 +0200 Subject: [PATCH 04/21] Add ManagedZone client and controller Signed-off-by: Daniel Kozlowski --- pkg/clients/managedzone/managed_zone.go | 101 ++++ pkg/clients/managedzone/managed_zone_test.go | 205 ++++++++ pkg/controller/dns/managed_zone.go | 218 +++++++++ pkg/controller/dns/managed_zone_test.go | 475 +++++++++++++++++++ pkg/controller/gcp.go | 1 + 5 files changed, 1000 insertions(+) create mode 100644 pkg/clients/managedzone/managed_zone.go create mode 100644 pkg/clients/managedzone/managed_zone_test.go create mode 100644 pkg/controller/dns/managed_zone.go create mode 100644 pkg/controller/dns/managed_zone_test.go diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go new file mode 100644 index 000000000..a8cf5c751 --- /dev/null +++ b/pkg/clients/managedzone/managed_zone.go @@ -0,0 +1,101 @@ +/* +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{ + Networks: make([]*dns.ManagedZonePrivateVisibilityConfigNetwork, len(spec.PrivateVisibilityConfig.Networks)), + } + + for i, v := range spec.PrivateVisibilityConfig.Networks { + mz.PrivateVisibilityConfig.Networks[i] = &dns.ManagedZonePrivateVisibilityConfigNetwork{ + 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) +} + +// 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..b42efd9fb --- /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" + fakeVisibility = "public" +) + +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.Visibility = nil + }), + external: managedZone(func(mz *dns.ManagedZone) { + mz.Visibility = fakeVisibility + }), + }, + want: want{ + params: params(func(p *v1alpha1.ManagedZoneParameters) { + p.Visibility = &fakeVisibility + }), + }, + }, + "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..fd3ed5598 --- /dev/null +++ b/pkg/controller/dns/managed_zone.go @@ -0,0 +1,218 @@ +/* +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" + errUpdateManagedZone = "Cannot update DNS ManagedZone custom resource" + 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) + } + + mz, 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, *mz) + if !cmp.Equal(currentSpec, &cr.Spec.ForProvider) { + if err := e.kube.Update(ctx, cr); err != nil { + return managed.ExternalObservation{}, errors.Wrap(err, errUpdateManagedZone) + } + lateInit = true + } + + cr.Status.AtProvider = mzclient.GenerateManagedZoneObservation(mz) + + cr.SetConditions(xpv1.Available()) + + UpToDate, err := mzclient.IsUpToDate( + meta.GetExternalName(cr), + &cr.Spec.ForProvider, + mz) + 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, + ) + + cr.SetConditions(xpv1.Creating()) + _, err := e.dns.Create( + e.projectID, + args, + ).Context(ctx).Do() + if err != nil { + return managed.ExternalCreation{}, errors.Wrap(err, errCreateManagedZone) + } + + return managed.ExternalCreation{}, nil +} + +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() + if err != nil { + return managed.ExternalUpdate{}, err + } + + return managed.ExternalUpdate{}, nil +} + +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() + if gcp.IsErrorNotFound(err) { + return nil + } + 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..12b526e8d --- /dev/null +++ b/pkg/controller/dns/managed_zone_test.go @@ -0,0 +1,475 @@ +/* +Copyright 2021 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" +) + +type ManagedZoneOption func(*v1alpha1.ManagedZone) + +func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone { + mz := &v1alpha1.ManagedZone{} + + for _, f := range opts { + f(mz) + } + + return mz +} + +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: unexpectedObject, + }, + 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) + } + }), + }, + "UpdateResourceSpecFail": { + reason: "Should return an error if the internal update fails", + args: args{ + mg: newManagedZone(), + }, + want: want{ + e: managed.ExternalObservation{}, + err: errors.Wrap(errBoom, errUpdateManagedZone), + }, + 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() + 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(errBoom), + }, + }, + "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: unexpectedObject, + }, + 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: unexpectedObject, + }, + 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: unexpectedObject, + }, + 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, From 4eadfc860dd46e9eb5d0b722990275a6104be3ba Mon Sep 17 00:00:00 2001 From: danielinclouds Date: Thu, 15 Sep 2022 00:05:31 +0200 Subject: [PATCH 05/21] Don't late initialize Description and Visibility Signed-off-by: Daniel Kozlowski --- pkg/clients/managedzone/managed_zone.go | 2 -- pkg/clients/managedzone/managed_zone_test.go | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go index a8cf5c751..859f2e138 100644 --- a/pkg/clients/managedzone/managed_zone.go +++ b/pkg/clients/managedzone/managed_zone.go @@ -70,8 +70,6 @@ func GenerateManagedZone(name string, spec v1alpha1.ManagedZoneParameters, mz *d // 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) } diff --git a/pkg/clients/managedzone/managed_zone_test.go b/pkg/clients/managedzone/managed_zone_test.go index b42efd9fb..36b715a02 100644 --- a/pkg/clients/managedzone/managed_zone_test.go +++ b/pkg/clients/managedzone/managed_zone_test.go @@ -34,7 +34,7 @@ const ( var ( testDescription = "test description" testVisibility = "private" - fakeVisibility = "public" + fakeLabels = map[string]string{"label": "one"} ) func params(m ...func(*v1alpha1.ManagedZoneParameters)) *v1alpha1.ManagedZoneParameters { @@ -127,15 +127,15 @@ func TestLateInitializeSpec(t *testing.T) { "SomeFields": { args: args{ spec: params(func(p *v1alpha1.ManagedZoneParameters) { - p.Visibility = nil + p.Labels = nil }), external: managedZone(func(mz *dns.ManagedZone) { - mz.Visibility = fakeVisibility + mz.Labels = fakeLabels }), }, want: want{ params: params(func(p *v1alpha1.ManagedZoneParameters) { - p.Visibility = &fakeVisibility + p.Labels = fakeLabels }), }, }, From 094742aef03d48428e1969ea28ae8d7543b67178 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 18 Sep 2022 00:38:03 +0100 Subject: [PATCH 06/21] Add missing test UpdateResourceSpecSuccess and fix test UpdateResourceSpecFail Signed-off-by: Daniel Kozlowski --- pkg/controller/dns/managed_zone_test.go | 59 +++++++++++++++++++++---- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/pkg/controller/dns/managed_zone_test.go b/pkg/controller/dns/managed_zone_test.go index 12b526e8d..89ec508b7 100644 --- a/pkg/controller/dns/managed_zone_test.go +++ b/pkg/controller/dns/managed_zone_test.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. @@ -43,6 +43,12 @@ const ( managedZoneProjectID = "myproject-id-1234" ) +var ( + nonManagedZone resource.Managed + errManagedZoneBoom = errors.New("boom") + managedZoneLabels = map[string]string{"foo": "bar"} +) + type ManagedZoneOption func(*v1alpha1.ManagedZone) func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone { @@ -55,6 +61,12 @@ func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone { 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, @@ -82,7 +94,7 @@ func TestManagedZoneObserve(t *testing.T) { "NotManagedZone": { reason: "Should return an error if the resource is not ManagedZone", args: args{ - mg: unexpectedObject, + mg: nonManagedZone, }, want: want{ e: managed.ExternalObservation{}, @@ -136,7 +148,7 @@ func TestManagedZoneObserve(t *testing.T) { }, want: want{ e: managed.ExternalObservation{}, - err: errors.Wrap(errBoom, errUpdateManagedZone), + err: errors.Wrap(errManagedZoneBoom, errUpdateManagedZone), }, handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _ = r.Body.Close() @@ -144,7 +156,7 @@ func TestManagedZoneObserve(t *testing.T) { t.Errorf("r: -want, +got:\n%s", diff) } w.WriteHeader(http.StatusOK) - cr := newManagedZone() + 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 { @@ -152,7 +164,37 @@ func TestManagedZoneObserve(t *testing.T) { } }), kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(errBoom), + MockUpdate: test.NewMockUpdateFn(errManagedZoneBoom), + }, + }, + "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": { @@ -191,6 +233,7 @@ func TestManagedZoneObserve(t *testing.T) { 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) } @@ -219,7 +262,7 @@ func TestManagedZoneCreate(t *testing.T) { "NotManagedZone": { reason: "Should return an error if the resource is not ManagedZone", args: args{ - mg: unexpectedObject, + mg: nonManagedZone, }, want: want{ e: managed.ExternalCreation{}, @@ -306,7 +349,7 @@ func TestManagedZoneUpdate(t *testing.T) { "NotManagedZone": { reason: "Should return an error if the resource is not ManagedZone", args: args{ - mg: unexpectedObject, + mg: nonManagedZone, }, want: want{ e: managed.ExternalUpdate{}, @@ -392,7 +435,7 @@ func TestManagedZoneDelete(t *testing.T) { "NotManagedZone": { reason: "Should return an error if the resource is not ManagedZone", args: args{ - mg: unexpectedObject, + mg: nonManagedZone, }, want: want{ err: errors.New(errNotManagedZone), From 7dceeb5e819bb072771ccc6406bc5c7a36da8a18 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 18 Sep 2022 00:44:56 +0100 Subject: [PATCH 07/21] Changed ManagedZoneObservation parameters to non-pointers Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/managed_zone_types.go | 4 ++-- apis/dns/v1alpha1/zz_generated.deepcopy.go | 10 ---------- pkg/clients/managedzone/managed_zone.go | 4 ++-- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go index 94f69ef64..0969abf49 100644 --- a/apis/dns/v1alpha1/managed_zone_types.go +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -79,10 +79,10 @@ 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"` + CreationTime string `json:"creationTime,omitempty"` // Id: Unique identifier for the resource; defined by the server (output only) - ID *uint64 `json:"id,omitempty"` + ID uint64 `json:"id,omitempty"` // NameServers: Delegate your managed_zone to these virtual name // servers; defined by the server (output only) diff --git a/apis/dns/v1alpha1/zz_generated.deepcopy.go b/apis/dns/v1alpha1/zz_generated.deepcopy.go index cee9372a2..b0b7249ed 100644 --- a/apis/dns/v1alpha1/zz_generated.deepcopy.go +++ b/apis/dns/v1alpha1/zz_generated.deepcopy.go @@ -87,16 +87,6 @@ func (in *ManagedZoneList) DeepCopyObject() runtime.Object { // 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.CreationTime != nil { - in, out := &in.CreationTime, &out.CreationTime - *out = new(string) - **out = **in - } - if in.ID != nil { - in, out := &in.ID, &out.ID - *out = new(uint64) - **out = **in - } if in.NameServers != nil { in, out := &in.NameServers, &out.NameServers *out = make([]string, len(*in)) diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go index 859f2e138..833cc9ed0 100644 --- a/pkg/clients/managedzone/managed_zone.go +++ b/pkg/clients/managedzone/managed_zone.go @@ -92,8 +92,8 @@ func IsUpToDate(name string, spec *v1alpha1.ManagedZoneParameters, observed *dns // *ManagedZoneObservation. func GenerateManagedZoneObservation(observed *dns.ManagedZone) v1alpha1.ManagedZoneObservation { return v1alpha1.ManagedZoneObservation{ - CreationTime: &observed.CreationTime, - ID: &observed.Id, + CreationTime: observed.CreationTime, + ID: observed.Id, NameServers: observed.NameServers, } } From bcb1704eb7b1c5792469169fe5ed757be04c7802 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 18 Sep 2022 00:51:36 +0100 Subject: [PATCH 08/21] Add TODO for missing Managed Zone parameters Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/managed_zone_types.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go index 0969abf49..0e7eefa35 100644 --- a/apis/dns/v1alpha1/managed_zone_types.go +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -55,6 +55,14 @@ type ManagedZoneParameters struct { // +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 From 07d3d5e27ba9b846189ffc0651ef4a07545c66dc Mon Sep 17 00:00:00 2001 From: danielinclouds <32359449+danielinclouds@users.noreply.github.com> Date: Sun, 25 Sep 2022 09:49:02 +0100 Subject: [PATCH 09/21] Improve consistency between error messages Co-authored-by: Gabriel Ferreira Signed-off-by: Daniel Kozlowski --- pkg/controller/dns/managed_zone.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go index fd3ed5598..2899fdce8 100644 --- a/pkg/controller/dns/managed_zone.go +++ b/pkg/controller/dns/managed_zone.go @@ -46,7 +46,7 @@ const ( errGetManagedZone = "cannot get the DNS ManagedZone" errCreateManagedZone = "cannot create DNS ManagedZone" errDeleteManagedZone = "cannot delete DNS ManagedZone" - errUpdateManagedZone = "Cannot update DNS ManagedZone custom resource" + errUpdateManagedZone = "cannot update DNS ManagedZone custom resource" errUpToDateManagedZone = "cannot determine if ManagedZone is up to date" ) From 1ae637d969f952aa8bac924a1fb5149c22c6a5e3 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 25 Sep 2022 09:58:26 +0100 Subject: [PATCH 10/21] Change variable to lower case Signed-off-by: Daniel Kozlowski --- pkg/controller/dns/managed_zone.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go index 2899fdce8..221d026dc 100644 --- a/pkg/controller/dns/managed_zone.go +++ b/pkg/controller/dns/managed_zone.go @@ -134,7 +134,7 @@ func (e *managedZoneExternal) Observe(ctx context.Context, mg resource.Managed) cr.SetConditions(xpv1.Available()) - UpToDate, err := mzclient.IsUpToDate( + upToDate, err := mzclient.IsUpToDate( meta.GetExternalName(cr), &cr.Spec.ForProvider, mz) @@ -144,7 +144,7 @@ func (e *managedZoneExternal) Observe(ctx context.Context, mg resource.Managed) return managed.ExternalObservation{ ResourceExists: true, - ResourceUpToDate: UpToDate, + ResourceUpToDate: upToDate, ResourceLateInitialized: lateInit, }, nil } From b7aa49697eb0d288a238165c83bffb40a2ffda64 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 25 Sep 2022 10:05:22 +0100 Subject: [PATCH 11/21] Remove description printer column Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/managed_zone_types.go | 1 - package/crds/dns.gcp.crossplane.io_managedzones.yaml | 3 --- 2 files changed, 4 deletions(-) diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go index 0e7eefa35..d590bab6e 100644 --- a/apis/dns/v1alpha1/managed_zone_types.go +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -117,7 +117,6 @@ type ManagedZoneStatus struct { // +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].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="DESCRIPTION",type="string",JSONPath=".spec.forProvider.description" // +kubebuilder:resource:scope=Cluster,categories={crossplane,managed,gcp} type ManagedZone struct { metav1.TypeMeta `json:",inline"` diff --git a/package/crds/dns.gcp.crossplane.io_managedzones.yaml b/package/crds/dns.gcp.crossplane.io_managedzones.yaml index bbb0954ac..0e9c965c0 100644 --- a/package/crds/dns.gcp.crossplane.io_managedzones.yaml +++ b/package/crds/dns.gcp.crossplane.io_managedzones.yaml @@ -32,9 +32,6 @@ spec: - jsonPath: .spec.forProvider.visibility name: VISIBILITY type: string - - jsonPath: .spec.forProvider.description - name: DESCRIPTION - type: string name: v1alpha1 schema: openAPIV3Schema: From ccfd5f4922eca04b208f0cebce1b519842055f12 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 25 Sep 2022 10:39:52 +0100 Subject: [PATCH 12/21] Remove redundant code in Delete function Signed-off-by: Daniel Kozlowski --- pkg/controller/dns/managed_zone.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go index 221d026dc..c19905334 100644 --- a/pkg/controller/dns/managed_zone.go +++ b/pkg/controller/dns/managed_zone.go @@ -211,8 +211,6 @@ func (e *managedZoneExternal) Delete(ctx context.Context, mg resource.Managed) e e.projectID, meta.GetExternalName(cr), ).Context(ctx).Do() - if gcp.IsErrorNotFound(err) { - return nil - } + return errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errDeleteManagedZone) } From 5391d03776712b19025d5b3dbd6e9d805755a1f6 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 25 Sep 2022 10:44:51 +0100 Subject: [PATCH 13/21] Simplify code Signed-off-by: Daniel Kozlowski --- pkg/controller/dns/managed_zone.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go index c19905334..60f61fd2a 100644 --- a/pkg/controller/dns/managed_zone.go +++ b/pkg/controller/dns/managed_zone.go @@ -168,11 +168,8 @@ func (e *managedZoneExternal) Create(ctx context.Context, mg resource.Managed) ( e.projectID, args, ).Context(ctx).Do() - if err != nil { - return managed.ExternalCreation{}, errors.Wrap(err, errCreateManagedZone) - } - return managed.ExternalCreation{}, nil + return managed.ExternalCreation{}, errors.Wrap(err, errCreateManagedZone) } func (e *managedZoneExternal) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) { @@ -193,11 +190,8 @@ func (e *managedZoneExternal) Update(ctx context.Context, mg resource.Managed) ( meta.GetExternalName(cr), args, ).Context(ctx).Do() - if err != nil { - return managed.ExternalUpdate{}, err - } - return managed.ExternalUpdate{}, nil + return managed.ExternalUpdate{}, err } func (e *managedZoneExternal) Delete(ctx context.Context, mg resource.Managed) error { From 84aa3fa49e2775f7304466007b995cae2865fda7 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 25 Sep 2022 10:47:01 +0100 Subject: [PATCH 14/21] Remove redundant code in Create function Signed-off-by: Daniel Kozlowski --- pkg/controller/dns/managed_zone.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go index 60f61fd2a..6240688dd 100644 --- a/pkg/controller/dns/managed_zone.go +++ b/pkg/controller/dns/managed_zone.go @@ -163,7 +163,6 @@ func (e *managedZoneExternal) Create(ctx context.Context, mg resource.Managed) ( args, ) - cr.SetConditions(xpv1.Creating()) _, err := e.dns.Create( e.projectID, args, From b0193912f83819f3d157f310b650f35c4c943795 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 25 Sep 2022 12:50:03 +0100 Subject: [PATCH 15/21] Add description, visibility and networks to late initialization Signed-off-by: Daniel Kozlowski --- pkg/clients/managedzone/managed_zone.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go index 833cc9ed0..d950adf3f 100644 --- a/pkg/clients/managedzone/managed_zone.go +++ b/pkg/clients/managedzone/managed_zone.go @@ -70,7 +70,21 @@ func GenerateManagedZone(name string, spec v1alpha1.ManagedZoneParameters, mz *d // 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 From cf88e93de3032a2791863e4b0229aaa4a9c81b29 Mon Sep 17 00:00:00 2001 From: danielinclouds Date: Sun, 2 Oct 2022 14:59:38 +0100 Subject: [PATCH 16/21] Don't update k8s resource in Observe function Signed-off-by: Daniel Kozlowski --- .../dns.gcp.crossplane.io_managedzones.yaml | 75 +++++++++++++++++++ pkg/clients/managedzone/managed_zone.go | 2 +- pkg/controller/dns/managed_zone.go | 12 +-- pkg/controller/dns/managed_zone_test.go | 31 +------- 4 files changed, 82 insertions(+), 38 deletions(-) diff --git a/package/crds/dns.gcp.crossplane.io_managedzones.yaml b/package/crds/dns.gcp.crossplane.io_managedzones.yaml index 0e9c965c0..730e944b9 100644 --- a/package/crds/dns.gcp.crossplane.io_managedzones.yaml +++ b/package/crds/dns.gcp.crossplane.io_managedzones.yaml @@ -125,6 +125,31 @@ spec: 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 @@ -136,6 +161,31 @@ spec: 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 @@ -155,6 +205,31 @@ spec: 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 diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go index d950adf3f..a47cb7df8 100644 --- a/pkg/clients/managedzone/managed_zone.go +++ b/pkg/clients/managedzone/managed_zone.go @@ -104,7 +104,7 @@ func IsUpToDate(name string, spec *v1alpha1.ManagedZoneParameters, observed *dns // GenerateManagedZoneObservation takes a dns.ManagedZone and returns // *ManagedZoneObservation. -func GenerateManagedZoneObservation(observed *dns.ManagedZone) v1alpha1.ManagedZoneObservation { +func GenerateManagedZoneObservation(observed dns.ManagedZone) v1alpha1.ManagedZoneObservation { return v1alpha1.ManagedZoneObservation{ CreationTime: observed.CreationTime, ID: observed.Id, diff --git a/pkg/controller/dns/managed_zone.go b/pkg/controller/dns/managed_zone.go index 6240688dd..6b8d974b7 100644 --- a/pkg/controller/dns/managed_zone.go +++ b/pkg/controller/dns/managed_zone.go @@ -46,7 +46,6 @@ const ( errGetManagedZone = "cannot get the DNS ManagedZone" errCreateManagedZone = "cannot create DNS ManagedZone" errDeleteManagedZone = "cannot delete DNS ManagedZone" - errUpdateManagedZone = "cannot update DNS ManagedZone custom resource" errUpToDateManagedZone = "cannot determine if ManagedZone is up to date" ) @@ -109,7 +108,7 @@ func (e *managedZoneExternal) Observe(ctx context.Context, mg resource.Managed) return managed.ExternalObservation{}, errors.New(errNotManagedZone) } - mz, err := e.dns.Get( + observed, err := e.dns.Get( e.projectID, meta.GetExternalName(cr), ).Context(ctx).Do() @@ -122,22 +121,19 @@ func (e *managedZoneExternal) Observe(ctx context.Context, mg resource.Managed) lateInit := false currentSpec := cr.Spec.ForProvider.DeepCopy() - mzclient.LateInitializeSpec(&cr.Spec.ForProvider, *mz) + mzclient.LateInitializeSpec(&cr.Spec.ForProvider, *observed) if !cmp.Equal(currentSpec, &cr.Spec.ForProvider) { - if err := e.kube.Update(ctx, cr); err != nil { - return managed.ExternalObservation{}, errors.Wrap(err, errUpdateManagedZone) - } lateInit = true } - cr.Status.AtProvider = mzclient.GenerateManagedZoneObservation(mz) + cr.Status.AtProvider = mzclient.GenerateManagedZoneObservation(*observed) cr.SetConditions(xpv1.Available()) upToDate, err := mzclient.IsUpToDate( meta.GetExternalName(cr), &cr.Spec.ForProvider, - mz) + observed) if err != nil { return managed.ExternalObservation{}, errors.Wrap(err, errUpToDateManagedZone) } diff --git a/pkg/controller/dns/managed_zone_test.go b/pkg/controller/dns/managed_zone_test.go index 89ec508b7..ba498f7b6 100644 --- a/pkg/controller/dns/managed_zone_test.go +++ b/pkg/controller/dns/managed_zone_test.go @@ -44,9 +44,8 @@ const ( ) var ( - nonManagedZone resource.Managed - errManagedZoneBoom = errors.New("boom") - managedZoneLabels = map[string]string{"foo": "bar"} + nonManagedZone resource.Managed + managedZoneLabels = map[string]string{"foo": "bar"} ) type ManagedZoneOption func(*v1alpha1.ManagedZone) @@ -141,32 +140,6 @@ func TestManagedZoneObserve(t *testing.T) { } }), }, - "UpdateResourceSpecFail": { - reason: "Should return an error if the internal update fails", - args: args{ - mg: newManagedZone(), - }, - want: want{ - e: managed.ExternalObservation{}, - err: errors.Wrap(errManagedZoneBoom, errUpdateManagedZone), - }, - 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(errManagedZoneBoom), - }, - }, "UpdateResourceSpecSuccess": { reason: "Should not return an error if the internal update succeeds", args: args{ From f8dd5877c19823feafe7c8a8185c99aa30fa1404 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sun, 16 Oct 2022 00:34:36 +0100 Subject: [PATCH 17/21] Add ability to reference networks Signed-off-by: Daniel Kozlowski --- apis/compute/v1beta1/referencers.go | 11 +++ apis/dns/v1alpha1/managed_zone_types.go | 14 ++- apis/dns/v1alpha1/referencers.go | 52 +++++++++++ apis/dns/v1alpha1/zz_generated.deepcopy.go | 11 +++ .../dns.gcp.crossplane.io_managedzones.yaml | 90 +++++++++++++++++-- 5 files changed, 168 insertions(+), 10 deletions(-) create mode 100644 apis/dns/v1alpha1/referencers.go 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 index d590bab6e..f038d8105 100644 --- a/apis/dns/v1alpha1/managed_zone_types.go +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -79,7 +79,19 @@ 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} - NetworkURL *string `json:"networkUrl"` + // +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 diff --git a/apis/dns/v1alpha1/referencers.go b/apis/dns/v1alpha1/referencers.go new file mode 100644 index 000000000..498c7febf --- /dev/null +++ b/apis/dns/v1alpha1/referencers.go @@ -0,0 +1,52 @@ +/* +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 { + 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/zz_generated.deepcopy.go b/apis/dns/v1alpha1/zz_generated.deepcopy.go index b0b7249ed..ac0f50943 100644 --- a/apis/dns/v1alpha1/zz_generated.deepcopy.go +++ b/apis/dns/v1alpha1/zz_generated.deepcopy.go @@ -22,6 +22,7 @@ limitations under the License. package v1alpha1 import ( + "github.com/crossplane/crossplane-runtime/apis/common/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -175,6 +176,16 @@ func (in *ManagedZonePrivateVisibilityConfigNetwork) DeepCopyInto(out *ManagedZo *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. diff --git a/package/crds/dns.gcp.crossplane.io_managedzones.yaml b/package/crds/dns.gcp.crossplane.io_managedzones.yaml index 730e944b9..79ddcc22e 100644 --- a/package/crds/dns.gcp.crossplane.io_managedzones.yaml +++ b/package/crds/dns.gcp.crossplane.io_managedzones.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.8.0 + controller-gen.kubebuilder.io/version: v0.9.2 creationTimestamp: null name: managedzones.dns.gcp.crossplane.io spec: @@ -92,12 +92,90 @@ spec: 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 - required: - - networkUrl type: object type: array required: @@ -350,9 +428,3 @@ spec: storage: true subresources: status: {} -status: - acceptedNames: - kind: "" - plural: "" - conditions: [] - storedVersions: [] From 1234435e95b77b12cc15de5f656a4c617bdc5543 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Thu, 20 Oct 2022 23:38:58 +0100 Subject: [PATCH 18/21] Update apis/dns/v1alpha1/managed_zone_types.go Co-authored-by: Gabriel Ferreira Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/managed_zone_types.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go index f038d8105..47280d3d9 100644 --- a/apis/dns/v1alpha1/managed_zone_types.go +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -125,10 +125,10 @@ type ManagedZoneStatus struct { // ManagedZone is a managed resource that represents a Managed Zone in Cloud DNS // +kubebuilder:subresource:status -// +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: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"` From d8ba224898926e1a789bfe20a2d60a2caffccb75 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 29 Oct 2022 10:52:17 +0100 Subject: [PATCH 19/21] Update apis/dns/v1alpha1/managed_zone_types.go Co-authored-by: Gabriel Ferreira Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/managed_zone_types.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apis/dns/v1alpha1/managed_zone_types.go b/apis/dns/v1alpha1/managed_zone_types.go index 47280d3d9..9496b9257 100644 --- a/apis/dns/v1alpha1/managed_zone_types.go +++ b/apis/dns/v1alpha1/managed_zone_types.go @@ -70,7 +70,8 @@ type ManagedZoneParameters struct { type ManagedZonePrivateVisibilityConfig struct { // Networks: The list of VPC networks that can see this zone. - Networks []*ManagedZonePrivateVisibilityConfigNetwork `json:"networks"` + // +optional + Networks []*ManagedZonePrivateVisibilityConfigNetwork `json:"networks,omitempty"` } // ManagedZonePrivateVisibilityConfigNetwork is a list of VPC networks From c8909a5c2dbcf2b50e1d0382e64c457c5cc25a80 Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 29 Oct 2022 13:37:02 +0100 Subject: [PATCH 20/21] Fix infinite update loop Signed-off-by: Daniel Kozlowski --- pkg/clients/managedzone/managed_zone.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/clients/managedzone/managed_zone.go b/pkg/clients/managedzone/managed_zone.go index a47cb7df8..a89f40735 100644 --- a/pkg/clients/managedzone/managed_zone.go +++ b/pkg/clients/managedzone/managed_zone.go @@ -54,11 +54,13 @@ func GenerateManagedZone(name string, spec v1alpha1.ManagedZoneParameters, mz *d 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, } } From 0a1571595bd5d64805945dc90c3b7df668f1b00f Mon Sep 17 00:00:00 2001 From: Daniel Kozlowski Date: Sat, 29 Oct 2022 16:44:55 +0100 Subject: [PATCH 21/21] Fix null pointer exception in managedzone referencers Signed-off-by: Daniel Kozlowski --- apis/dns/v1alpha1/referencers.go | 5 +++++ .../crds/dns.gcp.crossplane.io_managedzones.yaml | 14 ++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/apis/dns/v1alpha1/referencers.go b/apis/dns/v1alpha1/referencers.go index 498c7febf..1e40ce876 100644 --- a/apis/dns/v1alpha1/referencers.go +++ b/apis/dns/v1alpha1/referencers.go @@ -30,6 +30,11 @@ import ( // 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 diff --git a/package/crds/dns.gcp.crossplane.io_managedzones.yaml b/package/crds/dns.gcp.crossplane.io_managedzones.yaml index 79ddcc22e..55df2ca97 100644 --- a/package/crds/dns.gcp.crossplane.io_managedzones.yaml +++ b/package/crds/dns.gcp.crossplane.io_managedzones.yaml @@ -20,18 +20,18 @@ spec: scope: Cluster versions: - additionalPrinterColumns: - - jsonPath: .status.conditions[?(@.type=='Ready')].status - name: READY - type: string - - jsonPath: .status.conditions[?(@.type=='Synced')].status - name: SYNCED - type: string - 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: @@ -178,8 +178,6 @@ spec: type: string type: object type: array - required: - - networks type: object visibility: description: "Visibility: The zone's visibility: public zones