forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
264 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 v1beta1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func ComputeNetworkRef_ConvertToExternal(ctx context.Context, reader client.Reader, src client.Object, ref *v1alpha1.ResourceRef) (*v1alpha1.ResourceRef, error) { | ||
// Validate the ResourceRef | ||
if err := ValidateResourceRef(ref); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Validate and get external references if exist | ||
if ref.External != "" { | ||
if err := validateExternalComputeNetwork(ref); err != nil { | ||
return nil, err | ||
} | ||
return ref, nil | ||
} | ||
|
||
// Convert references to external references | ||
// Parse the ComputeNetwork resource | ||
computenetwork, err := ParseResourceRef(ctx, reader, src, ref, "compute.cnrm.cloud.google.com", "v1beta1", "ComputeNetwork") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Get the resourceID | ||
computenetworkID, err := GetResourceID(computenetwork) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Get the project ID | ||
computeNetworkProjectID, err := GetProjectID(ctx, reader, computenetwork) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &v1alpha1.ResourceRef{ | ||
External: fmt.Sprintf("projects/%s/global/networks/%s", computeNetworkProjectID, computenetworkID), | ||
}, nil | ||
} | ||
|
||
func ComputeTargetHTTPProxyRef_ConvertToExternal(ctx context.Context, reader client.Reader, src client.Object, ref *v1alpha1.ResourceRef) (*v1alpha1.ResourceRef, error) { | ||
if err := ValidateResourceRef(ref); err != nil { | ||
return nil, err | ||
} | ||
|
||
if ref.External != "" { | ||
if err := validateExternalComputeTargetHTTPProxy(ref); err != nil { | ||
return nil, err | ||
} | ||
return ref, nil | ||
} | ||
|
||
computeTargetHTTPProxy, err := ParseResourceRef(ctx, reader, src, ref, "compute.cnrm.cloud.google.com", "v1beta1", "computeTargetHTTPProxy") | ||
if err != nil { | ||
return nil, err | ||
} | ||
computeTargetHTTPProxyID, err := GetResourceID(computeTargetHTTPProxy) | ||
if err != nil { | ||
return nil, err | ||
} | ||
computeTargetHTTPProxyProjectID, err := GetProjectID(ctx, reader, computeTargetHTTPProxy) | ||
if err != nil { | ||
return nil, err | ||
} | ||
location, err := GetLocation(computeTargetHTTPProxy) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var external string | ||
if location == "global" { | ||
external = fmt.Sprintf("projects/%s/global/targetHttpProxies/%s", computeTargetHTTPProxyProjectID, computeTargetHTTPProxyID) | ||
} else { | ||
external = fmt.Sprintf("projects/%s/location/%s/targetHttpProxies/%s", computeTargetHTTPProxyProjectID, location, computeTargetHTTPProxyID) | ||
} | ||
|
||
return &v1alpha1.ResourceRef{ | ||
External: external, | ||
}, nil | ||
} | ||
|
||
func validateExternalComputeNetwork(ref *v1alpha1.ResourceRef) error { | ||
tokens := strings.Split(ref.External, "/") | ||
if len(tokens) == 5 && tokens[0] == "projects" && tokens[2] == "global" && tokens[3] == "networks" { | ||
return nil | ||
} | ||
return fmt.Errorf( | ||
"format of ComputeNetwork external=%q was not known (use projects/<projectId>/global/networks/<networkid>)", | ||
ref.External) | ||
} | ||
|
||
func validateExternalComputeTargetHTTPProxy(ref *v1alpha1.ResourceRef) error { | ||
tokens := strings.Split(ref.External, "/") | ||
if len(tokens) == 5 && tokens[0] == "projects" && tokens[2] == "global" && tokens[3] == "targetHttpProxies" { | ||
return nil | ||
} else if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "location" && tokens[4] == "targetHttpProxies" { | ||
return nil | ||
} | ||
return fmt.Errorf( | ||
"format of ComputeTargetHTTPProxy external=%q was not known "+ | ||
"(use projects/<projectId>/global/targetHttpProxies/<proxyId> or projects/<projectId>/location/<location>/targetHttpProxies/<proxyId>)", | ||
ref.External) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 v1beta1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func ValidateResourceRef(ref *v1alpha1.ResourceRef) error { | ||
if ref == nil { | ||
return nil | ||
} | ||
if ref.Name == "" && ref.External == "" { | ||
return fmt.Errorf("must specify either name or external on reference") | ||
} | ||
if ref.Name != "" && ref.External != "" { | ||
return fmt.Errorf("cannot specify both name and external on reference") | ||
} | ||
return nil | ||
} | ||
|
||
func ParseResourceRef(ctx context.Context, reader client.Reader, src client.Object, ref *v1alpha1.ResourceRef, group, version, kind string) (*unstructured.Unstructured, error) { | ||
key := types.NamespacedName{ | ||
Namespace: ref.Namespace, | ||
Name: ref.Name, | ||
} | ||
if key.Namespace == "" { | ||
key.Namespace = src.GetNamespace() | ||
} | ||
|
||
resource := &unstructured.Unstructured{} | ||
resource.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: group, | ||
Version: version, | ||
Kind: kind, | ||
}) | ||
if err := reader.Get(ctx, key, resource); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return nil, fmt.Errorf("referenced %s %v not found", kind, key) | ||
} | ||
return nil, fmt.Errorf("error reading referenced %s %v: %w", kind, key, err) | ||
} | ||
return resource, nil | ||
} | ||
|
||
func GetResourceID(u *unstructured.Unstructured) (string, error) { | ||
resourceID, _, err := unstructured.NestedString(u.Object, "spec", "resourceID") | ||
if err != nil { | ||
return "", fmt.Errorf("reading spec.resourceID from %v %v/%v: %w", u.GroupVersionKind().Kind, u.GetNamespace(), u.GetName(), err) | ||
} | ||
if resourceID == "" { | ||
resourceID = u.GetName() | ||
} | ||
return resourceID, nil | ||
} | ||
|
||
func GetProjectID(ctx context.Context, reader client.Reader, obj *unstructured.Unstructured) (string, error) { | ||
projectRefExternal, _, _ := unstructured.NestedString(obj.Object, "spec", "projectRef", "external") | ||
if projectRefExternal != "" { | ||
projectRef := ProjectRef{ | ||
External: projectRefExternal, | ||
} | ||
|
||
project, err := ResolveProject(ctx, reader, obj, &projectRef) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot parse projectRef.external %q in %v %v/%v: %w", projectRefExternal, obj.GetKind(), obj.GetNamespace(), obj.GetName(), err) | ||
} | ||
return project.ProjectID, nil | ||
} | ||
|
||
projectRefName, _, _ := unstructured.NestedString(obj.Object, "spec", "projectRef", "name") | ||
if projectRefName != "" { | ||
projectRefNamespace, _, _ := unstructured.NestedString(obj.Object, "spec", "projectRef", "namespace") | ||
|
||
projectRef := ProjectRef{ | ||
Name: projectRefName, | ||
Namespace: projectRefNamespace, | ||
} | ||
if projectRef.Namespace == "" { | ||
projectRef.Namespace = obj.GetNamespace() | ||
} | ||
|
||
project, err := ResolveProject(ctx, reader, obj, &projectRef) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot parse projectRef in %v %v/%v: %w", obj.GetKind(), obj.GetNamespace(), obj.GetName(), err) | ||
} | ||
return project.ProjectID, nil | ||
} | ||
|
||
if projectID := obj.GetAnnotations()["cnrm.cloud.google.com/project-id"]; projectID != "" { | ||
return projectID, nil | ||
} | ||
|
||
return "", fmt.Errorf("cannot find project id for %v %v/%v", obj.GetKind(), obj.GetNamespace(), obj.GetName()) | ||
} | ||
|
||
// todo(yuhou): Location can be optional. Use default location when it's unset. | ||
func GetLocation(obj *unstructured.Unstructured) (string, error) { | ||
location, _, err := unstructured.NestedString(obj.Object, "spec", "location") | ||
if err != nil { | ||
return "", fmt.Errorf("cannot get location for referenced %s %v: %w", obj.GetKind(), obj.GetNamespace(), err) | ||
} | ||
if location == "" { | ||
return "", fmt.Errorf("cannot get location for referenced %s %v (spec.location not set)", obj.GetKind(), obj.GetNamespace()) | ||
} | ||
return location, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters