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

WIP: DiscoveryEngineDataStoreTargetSite: mockgcp #3426

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
58 changes: 58 additions & 0 deletions apis/common/projects/mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package projects

import (
"context"
"fmt"
"strconv"
"strings"

resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
"cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb"
)

type ProjectMapper struct {
client *resourcemanager.ProjectsClient
}

func NewProjectMapper(client *resourcemanager.ProjectsClient) *ProjectMapper {
return &ProjectMapper{
client: client,
}
}

func (m *ProjectMapper) ReplaceProjectNumberWithID(ctx context.Context, projectID string) (string, error) {
if _, err := strconv.ParseInt(projectID, 10, 64); err != nil {
// Not a project number, no need to map
return projectID, nil
}

req := &resourcemanagerpb.GetProjectRequest{
Name: "projects/" + projectID,
}
project, err := m.client.GetProject(ctx, req)
if err != nil {
return "", fmt.Errorf("error getting project %q: %w", req.Name, err)
}
return project.ProjectId, nil
}

func (m *ProjectMapper) LookupProjectNumber(ctx context.Context, projectID string) (int64, error) {
// Check if the project number is already a valid integer
// If not, we need to look it up
projectNumber, err := strconv.ParseInt(projectID, 10, 64)
if err != nil {
req := &resourcemanagerpb.GetProjectRequest{
Name: "projects/" + projectID,
}
project, err := m.client.GetProject(ctx, req)
if err != nil {
return 0, fmt.Errorf("error getting project %q: %w", req.Name, err)
}
n, err := strconv.ParseInt(strings.TrimPrefix(project.Name, "projects/"), 10, 64)
if err != nil {
return 0, fmt.Errorf("error parsing project number for %q: %w", project.Name, err)
}
projectNumber = n
}
return projectNumber, nil
}
155 changes: 155 additions & 0 deletions apis/discoveryengine/v1alpha1/targetsite_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// 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"

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 = &TargetSiteRef{}

// TargetSiteRef defines the resource reference to DiscoveryEngineDataStoreTargetSite, which "External" field
// holds the GCP identifier for the KRM object.
type TargetSiteRef struct {
// A reference to an externally managed DiscoveryEngineDataStoreTargetSite resource.
// Should be in the format "projects/<projectID>/locations/<location>/targetsites/<targetsiteID>".
External string `json:"external,omitempty"`

// The name of a DiscoveryEngineDataStoreTargetSite resource.
Name string `json:"name,omitempty"`

// The namespace of a DiscoveryEngineDataStoreTargetSite resource.
Namespace string `json:"namespace,omitempty"`
}

// TargetSiteLink defines the full identity for a DataStoreTargetSite
//
// +k8s:deepcopy-gen=false
type TargetSiteLink struct {
*DiscoveryEngineDataStoreID
TargetSite string
}

func (l *TargetSiteLink) String() string {
return l.DiscoveryEngineDataStoreID.String() + "/siteSearchEngine/targetSites/" + l.TargetSite
}

// NormalizedExternal provision the "External" value for other resource that depends on DiscoveryEngineDataStoreTargetSite.
// If the "External" is given in the other resource's spec.DiscoveryEngineDataStoreTargetSiteRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual DiscoveryEngineDataStoreTargetSite object from the cluster.
func (r *TargetSiteRef) 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", DiscoveryEngineDataStoreTargetSiteGVK.Kind)
}
// From given External
if r.External != "" {
if _, err := ParseTargetSiteExternal(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(DiscoveryEngineDataStoreTargetSiteGVK)
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", DiscoveryEngineDataStoreTargetSiteGVK, 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
}

// NewTargetSiteLinkFromObject builds a TargetSiteLink from the Config Connector object.
func NewTargetSiteLinkFromObject(ctx context.Context, reader client.Reader, obj *DiscoveryEngineDataStoreTargetSite) (*DiscoveryEngineDataStoreID, *TargetSiteLink, error) {
if obj.Spec.DataStoreRef == nil {
return nil, nil, fmt.Errorf("spec.dataStoreRef not set")
}
dataStoreRef := *obj.Spec.DataStoreRef
if _, err := dataStoreRef.NormalizedExternal(ctx, reader, obj.GetNamespace()); err != nil {
return nil, nil, fmt.Errorf("resolving spec.dataStoreRef: %w", err)
}
dataStoreLink, err := ParseDiscoveryEngineDataStoreExternal(dataStoreRef.External)
if err != nil {
return nil, nil, fmt.Errorf("parsing dataStoreRef.external=%q: %w", dataStoreRef.External, err)
}

var link *TargetSiteLink

// Validate the status.externalRef, if set
externalRef := valueOf(obj.Status.ExternalRef)
if externalRef != "" {
// Validate desired with actual
externalLink, err := ParseTargetSiteExternal(externalRef)
if err != nil {
return nil, nil, err
}
if externalLink.DiscoveryEngineDataStoreID.String() != dataStoreLink.String() {
return nil, nil, fmt.Errorf("cannot change object key after creation; status=%q, new=%q",
externalLink.DiscoveryEngineDataStoreID.String(), dataStoreLink.String())
}
link = externalLink
}
return dataStoreLink, link, nil
}

func ParseTargetSiteExternal(external string) (*TargetSiteLink, error) {
s := strings.TrimPrefix(external, "//discoveryengine.googleapis.com/")
s = strings.TrimPrefix(s, "/")
tokens := strings.Split(s, "/")
if len(tokens) == 11 && tokens[0] == "projects" && tokens[2] == "locations" && tokens[4] == "collections" && tokens[6] == "dataStores" && tokens[8] == "siteSearchEngine" && tokens[9] == "targetSites" {
projectAndLocation := &ProjectAndLocation{
ProjectID: tokens[1],
Location: tokens[3],
}
collection := &CollectionLink{
ProjectAndLocation: projectAndLocation,
Collection: tokens[5],
}
dataStoreLink := &DiscoveryEngineDataStoreID{
CollectionLink: collection,
DataStore: tokens[7],
}
targetStoreLink := &TargetSiteLink{
DiscoveryEngineDataStoreID: dataStoreLink,
TargetSite: tokens[10],
}
return targetStoreLink, nil
}
return nil, fmt.Errorf("format of DiscoveryEngineDataStoreTargetSite external=%q was not known (use projects/{{projectId}}/locations/{{location}}/collections/{{collectionID}}/dataStores/{{dataStoreID}}/siteSearchEngine/targetSites/{{targetSiteID}})", external)
}
118 changes: 118 additions & 0 deletions apis/discoveryengine/v1alpha1/targetsite_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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 (
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var DiscoveryEngineDataStoreTargetSiteGVK = GroupVersion.WithKind("DiscoveryEngineDataStoreTargetSite")

// DiscoveryEngineDataStoreTargetSiteSpec defines the desired state of DiscoveryEngineDataStoreTargetSite
// +kcc:proto=google.cloud.discoveryengine.v1.TargetSite
type DiscoveryEngineDataStoreTargetSiteSpec struct {
// The DataStore this target site should be part of.
DataStoreRef *DiscoveryEngineDataStoreRef `json:"dataStoreRef,omitempty"`

// The resource ID is server-generated, so no ResourceID field

// Required. Input only. The user provided URI pattern from which the
// `generated_uri_pattern` is generated.
ProvidedUriPattern *string `json:"providedUriPattern,omitempty"`

// The type of the target site, e.g., whether the site is to be included or
// excluded.
Type *string `json:"type,omitempty"`

// Input only. If set to false, a uri_pattern is generated to include all
// pages whose address contains the provided_uri_pattern. If set to true, an
// uri_pattern is generated to try to be an exact match of the
// provided_uri_pattern or just the specific page if the provided_uri_pattern
// is a specific one. provided_uri_pattern is always normalized to
// generate the URI pattern to be used by the search engine.
ExactMatch *bool `json:"exactMatch,omitempty"`
}

// DiscoveryEngineDataStoreTargetSiteStatus defines the config connector machine state of DiscoveryEngineDataStoreTargetSite
type DiscoveryEngineDataStoreTargetSiteStatus struct {
/* Conditions represent the latest available observations of the
object's current state. */
Conditions []v1alpha1.Condition `json:"conditions,omitempty"`

// ObservedGeneration is the generation of the resource that was most recently observed by the Config Connector controller. If this is equal to metadata.generation, then that means that the current reported status reflects the most recent desired state of the resource.
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`

// A unique specifier for the DiscoveryEngineDataStoreTargetSite resource in GCP.
ExternalRef *string `json:"externalRef,omitempty"`

// ObservedState is the state of the resource as most recently observed in GCP.
ObservedState *DiscoveryEngineDataStoreTargetSiteObservedState `json:"observedState,omitempty"`
}

// DiscoveryEngineDataStoreTargetSiteObservedState is the state of the DiscoveryEngineDataStoreTargetSite resource as most recently observed in GCP.
// +kcc:proto=google.cloud.discoveryengine.v1.TargetSite
type DiscoveryEngineDataStoreTargetSiteObservedState struct {
// Output only. This is system-generated based on the provided_uri.
GeneratedUriPattern *string `json:"generatedUriPattern,omitempty"`

// Output only. Root domain of the provided_uri.
RootDomainUri *string `json:"rootDomainUri,omitempty"`

// Output only. Site ownership and validity verification status.
SiteVerificationInfo *SiteVerificationInfo `json:"siteVerificationInfo,omitempty"`

// Output only. Indexing status.
IndexingStatus *string `json:"indexingStatus,omitempty"`

// Output only. The target site's last updated time.
UpdateTime *string `json:"updateTime,omitempty"`

// Output only. Failure reason.
FailureReason *TargetSite_FailureReason `json:"failureReason,omitempty"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp,shortName=gcpdiscoveryenginedatastoretargetsite;gcpdiscoveryenginedatastoretargetsites
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/system=true"
// +kubebuilder:printcolumn:name="Age",JSONPath=".metadata.creationTimestamp",type="date"
// +kubebuilder:printcolumn:name="Ready",JSONPath=".status.conditions[?(@.type=='Ready')].status",type="string",description="When 'True', the most recent reconcile of the resource succeeded"
// +kubebuilder:printcolumn:name="Status",JSONPath=".status.conditions[?(@.type=='Ready')].reason",type="string",description="The reason for the value in 'Ready'"
// +kubebuilder:printcolumn:name="Status Age",JSONPath=".status.conditions[?(@.type=='Ready')].lastTransitionTime",type="date",description="The last transition time for the value in 'Status'"

// DiscoveryEngineDataStoreTargetSite is the Schema for the DiscoveryEngineDataStoreTargetSite API
// +k8s:openapi-gen=true
type DiscoveryEngineDataStoreTargetSite struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// +required
Spec DiscoveryEngineDataStoreTargetSiteSpec `json:"spec,omitempty"`
Status DiscoveryEngineDataStoreTargetSiteStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// DiscoveryEngineDataStoreTargetSiteList contains a list of DiscoveryEngineDataStoreTargetSite
type DiscoveryEngineDataStoreTargetSiteList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DiscoveryEngineDataStoreTargetSite `json:"items"`
}

func init() {
SchemeBuilder.Register(&DiscoveryEngineDataStoreTargetSite{}, &DiscoveryEngineDataStoreTargetSiteList{})
}
Loading
Loading