-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DiscoveryEngineTargetSite: initial types
- Loading branch information
Showing
6 changed files
with
500 additions
and
0 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,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) | ||
} |
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,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{}) | ||
} |
Oops, something went wrong.