Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve resource reference(initial PR) #2337

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 0 additions & 92 deletions apis/refs/v1beta1/projectref.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@

package v1beta1

import (
"context"
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// The Project that this resource belongs to.
type ProjectRef struct {
/* The `projectID` field of a project, when not managed by KCC. */
Expand All @@ -39,82 +26,3 @@ type ProjectRef struct {
// +optional
Kind string `json:"kind,omitempty"`
}

// AsProjectRef converts a generic ResourceRef into a ProjectRef
func AsProjectRef(in *v1alpha1.ResourceRef) *ProjectRef {
if in == nil {
return nil
}
return &ProjectRef{
Namespace: in.Namespace,
Name: in.Name,
External: in.External,
Kind: in.Kind,
}
}

type Project struct {
ProjectID string
}

// ResolveProject will resolve a ProjectRef to a Project, with the ProjectID.
func ResolveProject(ctx context.Context, reader client.Reader, src client.Object, ref *ProjectRef) (*Project, error) {
if ref == nil {
return nil, nil
}

if ref.Kind != "" {
if ref.Kind != "Project" {
return nil, fmt.Errorf("kind is optional on project reference, but must be \"Project\" if provided")
}
}

if ref.External != "" {
if ref.Name != "" {
return nil, fmt.Errorf("cannot specify both name and external on project reference")
}

tokens := strings.Split(ref.External, "/")
if len(tokens) == 1 {
return &Project{ProjectID: tokens[0]}, nil
}
if len(tokens) == 2 && tokens[0] == "projects" {
return &Project{ProjectID: tokens[1]}, nil
}
return nil, fmt.Errorf("format of project external=%q was not known (use projects/<projectId> or <projectId>)", ref.External)
}

if ref.Name == "" {
return nil, fmt.Errorf("must specify either name or external on project reference")
}

key := types.NamespacedName{
Namespace: ref.Namespace,
Name: ref.Name,
}
if key.Namespace == "" {
key.Namespace = src.GetNamespace()
}

project := &unstructured.Unstructured{}
project.SetGroupVersionKind(schema.GroupVersionKind{
Group: "resourcemanager.cnrm.cloud.google.com",
Version: "v1beta1",
Kind: "Project",
})
if err := reader.Get(ctx, key, project); err != nil {
if apierrors.IsNotFound(err) {
return nil, fmt.Errorf("referenced Project %v not found", key)
}
return nil, fmt.Errorf("error reading referenced Project %v: %w", key, err)
}

projectID, err := GetResourceID(project)
if err != nil {
return nil, err
}

return &Project{
ProjectID: projectID,
}, nil
}
32 changes: 0 additions & 32 deletions apis/refs/v1beta1/resourceid.go

This file was deleted.

7 changes: 4 additions & 3 deletions pkg/controller/direct/cloudbuild/workerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"reflect"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

gcp "cloud.google.com/go/cloudbuild/apiv1/v2"
cloudbuildpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
"google.golang.org/api/option"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/cloudbuild/v1beta1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
}

// Get GCP Project
projectRef, err := refs.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
projectRef, err := resolverefs.ResolveProjectRef(ctx, reader, obj, obj.Spec.ProjectRef)
Copy link
Collaborator

@justinsb justinsb Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I moved the resolver functions into the API package recently. The intent was that we could then have the resolvers implement an interface, and make a lot of this generic.

WDYT - can we put the resolver functions into the refs package? (We could even just add a ResolveRef function to each ref in this PR?)

Copy link
Collaborator Author

@gemmahou gemmahou Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the placement of resolver functions, I don't have a strong preference. It could be api/refs/projectref.go or direct/refs/projectref.go. I think we should maintain consistency in the future. I'm open to moving the resolver functions to api/refs if that's preferable.

My main concern here is avoiding placing these functions in individual controller packages, for example direct/compute/networkref.go. Some references are used in multiple resources, like ComputeNetWork, which is referenced by CloudBuildWorkerPool and ComputeForwardingRule (and potentially others).

if err != nil {
return nil, err
}
Expand All @@ -104,7 +105,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u

// Get computeNetwork
if obj.Spec.PrivatePoolConfig.NetworkConfig != nil {
networkRef, err := refs.ResolveComputeNetwork(ctx, reader, obj, &obj.Spec.PrivatePoolConfig.NetworkConfig.PeeredNetworkRef)
networkRef, err := resolverefs.ResolveComputeNetworkRef(ctx, reader, obj, &obj.Spec.PrivatePoolConfig.NetworkConfig.PeeredNetworkRef)
if err != nil {
return nil, err

Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/direct/dataform/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"fmt"
"reflect"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/dataform/v1alpha1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
return nil, fmt.Errorf("cannot resolve resource ID")
}

projectRef, err := refs.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
projectRef, err := resolverefs.ResolveProjectRef(ctx, reader, obj, obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/direct/gkehub/featuremembership_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"reflect"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

featureapi "google.golang.org/api/gkehub/v1beta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -84,7 +86,7 @@ func (m *gkeHubModel) AdapterForObject(ctx context.Context, reader client.Reader
Namespace: obj.Spec.ProjectRef.Namespace,
External: obj.Spec.ProjectRef.External,
}
project, err := refs.ResolveProject(ctx, reader, obj, projectRef)
project, err := resolverefs.ResolveProjectRef(ctx, reader, obj, projectRef)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/direct/logging/logbucketref.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/logging/v1beta1"
Expand Down Expand Up @@ -99,7 +101,7 @@ func LogBucketRef_ConvertToExternal(ctx context.Context, reader client.Reader, s
Namespace: obj.Spec.ProjectRef.Namespace,
External: obj.Spec.ProjectRef.External,
}
project, err := refs.ResolveProject(ctx, reader, loggingLogBucket, projectRef)
project, err := resolverefs.ResolveProjectRef(ctx, reader, loggingLogBucket, projectRef)
if err != nil {
return fmt.Errorf("cannot get project for referenced LoggingLogBucket %v: %w", key, err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/direct/logging/logmetric_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"reflect"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

api "google.golang.org/api/logging/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -28,7 +30,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/logging/v1beta1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (m *logMetricModel) AdapterForObject(ctx context.Context, reader client.Rea
return nil, fmt.Errorf("cannot resolve resource ID")
}

projectRef, err := refs.ResolveProject(ctx, reader, obj, &obj.Spec.ProjectRef)
projectRef, err := resolverefs.ResolveProjectRef(ctx, reader, obj, &obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

api "cloud.google.com/go/monitoring/dashboard/apiv1"
pb "cloud.google.com/go/monitoring/dashboard/apiv1/dashboardpb"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -27,7 +29,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/monitoring/v1beta1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (m *dashboardModel) AdapterForObject(ctx context.Context, kube client.Reade
return nil, fmt.Errorf("cannot resolve resource ID")
}

projectRef, err := refs.ResolveProject(ctx, kube, obj, &obj.Spec.ProjectRef)
projectRef, err := resolverefs.ResolveProjectRef(ctx, kube, obj, &obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/controller/direct/monitoring/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/monitoring/v1beta1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1"
Expand Down Expand Up @@ -54,7 +56,7 @@ func normalizeResourceName(ctx context.Context, reader client.Reader, src client

switch ref.Kind {
case "Project":
project, err := refs.ResolveProject(ctx, reader, src, &refs.ProjectRef{
project, err := resolverefs.ResolveProjectRef(ctx, reader, src, &refs.ProjectRef{
Name: ref.Name,
Namespace: ref.Namespace,
External: ref.External,
Expand Down Expand Up @@ -88,7 +90,7 @@ func normalizeResourceName(ctx context.Context, reader client.Reader, src client
return ref, nil
}

func normalizeMonitoringAlertPolicyRef(ctx context.Context, reader client.Reader, src client.Object, project refs.Project, ref *refs.MonitoringAlertPolicyRef) (*refs.MonitoringAlertPolicyRef, error) {
func normalizeMonitoringAlertPolicyRef(ctx context.Context, reader client.Reader, src client.Object, project resolverefs.Project, ref *refs.MonitoringAlertPolicyRef) (*refs.MonitoringAlertPolicyRef, error) {
if ref == nil {
return nil, nil
}
Expand Down Expand Up @@ -138,12 +140,12 @@ func normalizeMonitoringAlertPolicyRef(ctx context.Context, reader client.Reader
return nil, fmt.Errorf("error reading referenced MonitoringAlertPolicy %v: %w", key, err)
}

alertPolicyResourceID, err := refs.GetResourceID(alertPolicy)
alertPolicyResourceID, err := resolverefs.GetResourceID(alertPolicy)
if err != nil {
return nil, err
}

alertPolicyProjectID, err := refs.ResolveProjectIDForObject(ctx, reader, alertPolicy)
alertPolicyProjectID, err := resolverefs.GetProjectID(ctx, reader, alertPolicy)
if err != nil {
return nil, err
}
Expand All @@ -160,7 +162,7 @@ func normalizeProjectRef(ctx context.Context, reader client.Reader, src client.O
return nil, nil
}

project, err := refs.ResolveProject(ctx, reader, src, ref)
project, err := resolverefs.ResolveProjectRef(ctx, reader, src, ref)
if err != nil {
return nil, err
}
Expand All @@ -174,7 +176,7 @@ type refNormalizer struct {
ctx context.Context
kube client.Reader
src client.Object
project refs.Project
project resolverefs.Project
}

func (r *refNormalizer) VisitField(path string, v any) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/direct/privateca/privatecapool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/resolverefs"

iampb "cloud.google.com/go/iam/apiv1/iampb"
api "cloud.google.com/go/security/privateca/apiv1"
pb "cloud.google.com/go/security/privateca/apiv1/privatecapb"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/privateca/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (m *caPoolModel) AdapterForObject(ctx context.Context, reader client.Reader
return nil, fmt.Errorf("cannot resolve location")
}

projectRef, err := refs.ResolveProject(ctx, reader, obj, refs.AsProjectRef(&obj.Spec.ProjectRef))
projectRef, err := resolverefs.ResolveProjectRef(ctx, reader, obj, resolverefs.AsProjectRef(&obj.Spec.ProjectRef))
if err != nil {
return nil, err
}
Expand Down
Loading
Loading