Skip to content

Commit

Permalink
Add ability to reference networks
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kozlowski <[email protected]>
  • Loading branch information
danielinclouds committed Oct 16, 2022
1 parent a404b3b commit f8dd587
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 10 deletions.
11 changes: 11 additions & 0 deletions apis/compute/v1beta1/referencers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion apis/dns/v1alpha1/managed_zone_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions apis/dns/v1alpha1/referencers.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions apis/dns/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 81 additions & 9 deletions package/crds/dns.gcp.crossplane.io_managedzones.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -350,9 +428,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []

0 comments on commit f8dd587

Please sign in to comment.