-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support AlloyDBInstance type, mapper and direct controller
- Loading branch information
1 parent
dd2d99c
commit 230a0fd
Showing
25 changed files
with
3,736 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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. | ||
|
||
// +kcc:proto=google.cloud.alloydb.v1beta | ||
package v1alpha1 |
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,33 @@ | ||
// 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. | ||
|
||
// +kubebuilder:object:generate=true | ||
// +groupName=alloydb.cnrm.cloud.google.com | ||
package v1alpha1 | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/scheme" | ||
) | ||
|
||
var ( | ||
// GroupVersion is group version used to register these objects | ||
GroupVersion = schema.GroupVersion{Group: "alloydb.cnrm.cloud.google.com", Version: "v1alpha1"} | ||
|
||
// SchemeBuilder is used to add go types to the GroupVersionKind scheme | ||
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} | ||
|
||
// AddToScheme adds the types in this group-version to the given scheme. | ||
AddToScheme = SchemeBuilder.AddToScheme | ||
) |
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,123 @@ | ||
// 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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-config-connector/apis/common" | ||
refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const serviceDomain = "//alloydb.googleapis.com" | ||
|
||
// InstanceIdentity defines the resource reference to AlloyDBInstance, which "External" field | ||
// holds the GCP identifier for the KRM object. | ||
type InstanceIdentity struct { | ||
parent *InstanceParent | ||
id string | ||
} | ||
|
||
func (i *InstanceIdentity) String() string { | ||
return i.parent.String() + "/instances/" + i.id | ||
} | ||
|
||
func (i *InstanceIdentity) ID() string { | ||
return i.id | ||
} | ||
|
||
func (i *InstanceIdentity) Parent() *InstanceParent { | ||
return i.parent | ||
} | ||
|
||
// AsExternalRef builds a externalRef from a PrivilegedAccessManagerEntitlement. | ||
func (i *InstanceIdentity) AsExternalRef() *string { | ||
er := serviceDomain + "/" + i.String() | ||
return &er | ||
} | ||
|
||
type InstanceParent struct { | ||
clusterName string | ||
} | ||
|
||
func (p *InstanceParent) String() string { | ||
return p.clusterName | ||
} | ||
|
||
// New builds a InstanceIdentity from the Config Connector Instance object. | ||
func NewInstanceIdentity(ctx context.Context, reader client.Reader, obj *AlloyDBInstance) (*InstanceIdentity, error) { | ||
|
||
// Get Parent | ||
clusterRef, err := refsv1beta1.ResolveAlloyDBCluster(ctx, reader, obj, obj.Spec.ClusterRef) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot resolve AlloyDBCluster ref: %w", err) | ||
} | ||
|
||
// Get desired ID | ||
resourceID := common.ValueOf(obj.Spec.ResourceID) | ||
if resourceID == "" { | ||
resourceID = obj.GetName() | ||
} | ||
if resourceID == "" { | ||
return nil, fmt.Errorf("cannot resolve resource ID") | ||
} | ||
|
||
// Use approved ExternalRef | ||
externalRef := common.ValueOf(obj.Status.ExternalRef) | ||
if externalRef != "" { | ||
// Validate desired with actual | ||
actualParent, actualResourceID, err := ParseInstanceExternalRef(externalRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if actualParent.clusterName != clusterRef.String() { | ||
return nil, fmt.Errorf("spec.clusterRef changed, expect %s, got %s", actualParent.clusterName, clusterRef) | ||
} | ||
if actualResourceID != resourceID { | ||
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s", | ||
resourceID, actualResourceID) | ||
} | ||
} | ||
return &InstanceIdentity{ | ||
parent: &InstanceParent{ | ||
clusterName: clusterRef.String(), | ||
}, | ||
id: resourceID, | ||
}, nil | ||
} | ||
|
||
func ParseInstanceExternalRef(externalRef string) (parent *InstanceParent, resourceID string, err error) { | ||
if !strings.HasPrefix(externalRef, serviceDomain) { | ||
return nil, "", fmt.Errorf("externalRef should have prefix %s, got %s", serviceDomain, externalRef) | ||
} | ||
path := strings.TrimPrefix(externalRef, serviceDomain+"/") | ||
return ParseInstanceExternal(path) | ||
} | ||
|
||
func ParseInstanceExternal(external string) (parent *InstanceParent, resourceID string, err error) { | ||
tokens := strings.Split(external, "/") | ||
if len(tokens) != 8 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "clusters" || tokens[6] != "instances" { | ||
return nil, "", fmt.Errorf("format of AlloyDBInstance external=%q was not known (use projects/<projectId>/locations/<location>/clusters/<clusterID>/instances/<instanceID>)", external) | ||
} | ||
parent = &InstanceParent{ | ||
clusterName: fmt.Sprintf("%s/%s/%s/%s/%s/%s", tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]), | ||
} | ||
resourceID = tokens[7] | ||
return parent, resourceID, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ refsv1beta1.ExternalNormalizer = &InstanceRef{} | ||
|
||
// InstanceRef defines the resource reference to AlloyDBInstance, which "External" field | ||
// holds the GCP identifier for the KRM object. | ||
type InstanceRef struct { | ||
// A reference to an externally managed AlloyDBInstance resource. | ||
// Should be in the format "projects/<projectID>/locations/<location>/instances/<instanceID>". | ||
External string `json:"external,omitempty"` | ||
|
||
// The name of a AlloyDBInstance resource. | ||
Name string `json:"name,omitempty"` | ||
|
||
// The namespace of a AlloyDBInstance resource. | ||
Namespace string `json:"namespace,omitempty"` | ||
} | ||
|
||
// NormalizedExternal provision the "External" value for other resource that depends on AlloyDBInstance. | ||
// If the "External" is given in the other resource's spec.AlloyDBInstanceRef, the given value will be used. | ||
// Otherwise, the "Name" and "Namespace" will be used to query the actual AlloyDBInstance object from the cluster. | ||
func (r *InstanceRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) { | ||
if r.External != "" && r.Name != "" { | ||
return "", fmt.Errorf("cannot specify both name and external on %s reference", AlloyDBInstanceGVK.Kind) | ||
} | ||
// From given External | ||
if r.External != "" { | ||
if _, _, err := ParseInstanceExternal(r.External); err != nil { | ||
return "", err | ||
} | ||
return r.External, nil | ||
} | ||
|
||
// From the Config Connector object | ||
if r.Namespace == "" { | ||
r.Namespace = otherNamespace | ||
} | ||
key := types.NamespacedName{Name: r.Name, Namespace: r.Namespace} | ||
u := &unstructured.Unstructured{} | ||
u.SetGroupVersionKind(AlloyDBInstanceGVK) | ||
if err := reader.Get(ctx, key, u); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return "", k8s.NewReferenceNotFoundError(u.GroupVersionKind(), key) | ||
} | ||
return "", fmt.Errorf("reading referenced %s %s: %w", AlloyDBInstanceGVK, key, err) | ||
} | ||
// Get external from status.externalRef. This is the most trustworthy place. | ||
actualExternalRef, _, err := unstructured.NestedString(u.Object, "status", "externalRef") | ||
if err != nil { | ||
return "", fmt.Errorf("reading status.externalRef: %w", err) | ||
} | ||
if actualExternalRef == "" { | ||
return "", k8s.NewReferenceNotReadyError(u.GroupVersionKind(), key) | ||
} | ||
r.External = actualExternalRef | ||
return r.External, nil | ||
} |
Oops, something went wrong.