Skip to content

Commit

Permalink
resolve refs and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Jul 25, 2024
1 parent 8a43c31 commit 40893a5
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 312 deletions.
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
}
126 changes: 0 additions & 126 deletions apis/refs/v1beta1/resourceref.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)
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
Loading

0 comments on commit 40893a5

Please sign in to comment.