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
17 changed files
with
294 additions
and
197 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
apis/compute/v1beta1/computebackendservice_reference.go
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,145 @@ | ||
// 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/k8s" | ||
|
||
refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ refsv1beta1.ExternalNormalizer = &ComputeBackendServiceRef{} | ||
var ComputeBackendServiceGVK = GroupVersion.WithKind("ComputeBackendService") | ||
|
||
// ComputeBackendServiceRef defines the resource reference to ComputeBackendService, which "External" field | ||
// holds the GCP identifier for the KRM object. | ||
type ComputeBackendServiceRef struct { | ||
// The value of an externally managed ComputeBackendService resource. | ||
// Should be in the format "projects/{{project}}/global/backendServices/{{backendService}}" | ||
// or "projects/{{project}}/regions/{{region}}/backendServices/{{backendService}}". | ||
External string `json:"external,omitempty"` | ||
|
||
// The name of a ComputeBackendService resource. | ||
Name string `json:"name,omitempty"` | ||
|
||
// The namespace of a ComputeBackendService resource. | ||
Namespace string `json:"namespace,omitempty"` | ||
|
||
//parent *ComputeBackendServiceParent | ||
} | ||
|
||
// NormalizedExternal provision the "External" value for other resource that depends on ComputeBackendService. | ||
// If the "External" is given in the other resource's spec.ComputeBackendServiceRef, the given value will be used. | ||
// Otherwise, the "Name" and "Namespace" will be used to query the actual ComputeBackendService object from the cluster. | ||
func (r *ComputeBackendServiceRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) { | ||
// Get value from spec.ComputeBackendServiceRef.external | ||
if r.External != "" { | ||
if r.Name != "" { | ||
return "", fmt.Errorf("cannot specify both name and external on reference") | ||
} | ||
// To ensure backward compatibility for existing users, we do not enforce external format here | ||
return r.External, nil | ||
} | ||
|
||
if r.Name == "" { | ||
return "", fmt.Errorf("must specify either name or external on reference") | ||
} | ||
|
||
// Get value from the Config Connector object | ||
if r.Namespace == "" { | ||
r.Namespace = otherNamespace | ||
} | ||
key := types.NamespacedName{Name: r.Name, Namespace: r.Namespace} | ||
|
||
u, err := refsv1beta1.ResolveResourceName(ctx, reader, key, ComputeBackendServiceGVK) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Get value from object's status.externalRef(only support for objects created by direct controller) | ||
// This is the most trustworthy place | ||
actualExternalRef, found1, err1 := unstructured.NestedString(u.Object, "status", "externalRef") | ||
if !found1 { | ||
// Get value from object's status.selfLink(support for objects not created by direct controller) | ||
selfLink, found2, err2 := unstructured.NestedString(u.Object, "status", "selfLink") | ||
if !found2 { | ||
return "", fmt.Errorf("cannot get referenced %s %v", u.GetKind(), u.GetNamespace()) | ||
} | ||
if err2 != nil || selfLink == "" { | ||
return "", fmt.Errorf("cannot get selfLink for referenced %s %v (status.selfLink is empty)", u.GetKind(), u.GetNamespace()) | ||
} | ||
r.External = selfLink | ||
return r.External, nil | ||
} | ||
if err1 != nil || actualExternalRef == "" { | ||
return "", fmt.Errorf("cannot get externalRef for referenced %s %v (status.externalRef is empty)", u.GetKind(), u.GetNamespace()) | ||
} | ||
r.External = actualExternalRef | ||
return r.External, nil | ||
} | ||
|
||
func (r *ComputeBackendServiceRef) Parent() (*ComputeBackendServiceParent, error) { | ||
if r.External != "" { | ||
parent, _, err := ParseComputeBackendServiceExternal(r.External) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return parent, nil | ||
} | ||
return nil, fmt.Errorf("ComputeBackendServiceRef not initialized by `NewComputeBackendServiceRef` or resolved by `ResolveExternal`") | ||
} | ||
|
||
type ComputeBackendServiceParent struct { | ||
ProjectID string | ||
Region string | ||
} | ||
|
||
func (p *ComputeBackendServiceParent) String() string { | ||
if p.Region == "global" { | ||
return "projects/" + p.ProjectID + "/global" | ||
} else { | ||
return "projects/" + p.ProjectID + "/regions/" + p.Region | ||
} | ||
} | ||
|
||
func ParseComputeBackendServiceExternal(external string) (parent *ComputeBackendServiceParent, resourceID string, err error) { | ||
external = strings.TrimPrefix(external, "/") | ||
tokens := strings.Split(external, "/") | ||
if len(tokens) == 5 && tokens[0] == "projects" && tokens[3] == "backendServices" { | ||
parent = &ComputeBackendServiceParent{ | ||
ProjectID: tokens[1], | ||
Region: tokens[2], | ||
} | ||
resourceID = tokens[4] | ||
return parent, resourceID, nil | ||
} else if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "regions" && tokens[4] == "backendServices" { | ||
parent = &ComputeBackendServiceParent{ | ||
ProjectID: tokens[1], | ||
Region: tokens[3], | ||
} | ||
resourceID = tokens[5] | ||
return parent, resourceID, nil | ||
} | ||
acceptedFormat := "projects/{{project}}/global/backendServices/{{backendService}} or projects/{{project}}/regions/{{region}}/backendServices/{{backendService}}" | ||
return nil, "", k8s.NewInvalidFormatError(external, acceptedFormat) | ||
} |
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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 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 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 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 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
Oops, something went wrong.