Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/v1beta1/temporalnamespace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ type TemporalNamespaceSpec struct {
// If not set, the default cluster configuration is used.
// +optional
Archival *TemporalNamespaceArchivalSpec `json:"archival,omitempty"`
// CustomSearchAttributes is an optional mapping of custom search attribute names to types.
// Supported types: Text, Keyword, Int, Double, Bool, DateTime, KeywordList.
// +optional
CustomSearchAttributes map[string]string `json:"customSearchAttributes,omitempty"`
// AllowSearchAttributeDeletion makes the controller remove custom search attributes
// from the Temporal server if they are not present in the spec.
// +optional
AllowSearchAttributeDeletion bool `json:"allowSearchAttributeDeletion,omitempty"`
}

// TemporalNamespaceStatus defines the observed state of Namespace.
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions config/crd/bases/temporal.io_temporalnamespaces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ spec:
AllowDeletion makes the controller delete the Temporal namespace if the
CRD is deleted.
type: boolean
allowSearchAttributeDeletion:
description: |-
AllowSearchAttributeDeletion makes the controller remove custom search attributes
from the Temporal server if they are not present in the spec.
type: boolean
archival:
description: |-
Archival is a per-namespace archival configuration.
Expand Down Expand Up @@ -130,6 +135,13 @@ spec:
items:
type: string
type: array
customSearchAttributes:
additionalProperties:
type: string
description: |-
CustomSearchAttributes is an optional mapping of custom search attribute names to types.
Supported types: Text, Keyword, Int, Double, Bool, DateTime, KeywordList.
type: object
data:
additionalProperties:
type: string
Expand Down
15 changes: 15 additions & 0 deletions controllers/temporalnamespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ func (r *TemporalNamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Re
}
}

// Reconcile custom search attributes if any are configured or deletion is allowed.
if len(namespace.Spec.CustomSearchAttributes) > 0 || namespace.Spec.AllowSearchAttributeDeletion {
clusterClient, err := temporal.GetClusterClient(ctx, r.Client, cluster)
if err != nil {
err = fmt.Errorf("can't create cluster client for search attributes: %w", err)
return r.handleError(namespace, v1beta1.ReconcileErrorReason, err)
}
defer clusterClient.Close()

if err := temporal.ReconcileSearchAttributes(ctx, clusterClient.OperatorService(), namespace); err != nil {
err = fmt.Errorf("can't reconcile search attributes for \"%s\" namespace: %w", namespace.GetName(), err)
return r.handleError(namespace, v1beta1.ReconcileErrorReason, err)
}
}

logger.Info("Successfully reconciled namespace", "namespace", namespace.GetName())

v1beta1.SetTemporalNamespaceReady(namespace, metav1.ConditionTrue, v1beta1.TemporalNamespaceCreatedReason, "Namespace successfully created")
Expand Down
182 changes: 182 additions & 0 deletions pkg/temporal/search_attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Licensed to Alexandre VILAIN under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Alexandre VILAIN licenses this file to you 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 temporal

import (
"context"
"fmt"

"github.com/alexandrevilain/temporal-operator/api/v1beta1"
"go.temporal.io/api/enums/v1"
"go.temporal.io/api/operatorservice/v1"
"google.golang.org/grpc"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// searchAttributeTypes maps user-facing type names to Temporal IndexedValueType.
var searchAttributeTypes = map[string]enums.IndexedValueType{
"Text": enums.INDEXED_VALUE_TYPE_TEXT,
"Keyword": enums.INDEXED_VALUE_TYPE_KEYWORD,
"Int": enums.INDEXED_VALUE_TYPE_INT,
"Double": enums.INDEXED_VALUE_TYPE_DOUBLE,
"Bool": enums.INDEXED_VALUE_TYPE_BOOL,
"DateTime": enums.INDEXED_VALUE_TYPE_DATETIME,
"KeywordList": enums.INDEXED_VALUE_TYPE_KEYWORD_LIST,
}

// searchAttributeTypeNames is the reverse mapping from IndexedValueType to string.
var searchAttributeTypeNames = map[enums.IndexedValueType]string{
enums.INDEXED_VALUE_TYPE_TEXT: "Text",
enums.INDEXED_VALUE_TYPE_KEYWORD: "Keyword",
enums.INDEXED_VALUE_TYPE_INT: "Int",
enums.INDEXED_VALUE_TYPE_DOUBLE: "Double",
enums.INDEXED_VALUE_TYPE_BOOL: "Bool",
enums.INDEXED_VALUE_TYPE_DATETIME: "DateTime",
enums.INDEXED_VALUE_TYPE_KEYWORD_LIST: "KeywordList",
}

// SearchAttributeTypeFromString converts a user-facing type name to its IndexedValueType.
func SearchAttributeTypeFromString(s string) (enums.IndexedValueType, error) {
t, ok := searchAttributeTypes[s]
if !ok {
return enums.INDEXED_VALUE_TYPE_UNSPECIFIED, fmt.Errorf("invalid search attribute type %q: valid types are Text, Keyword, Int, Double, Bool, DateTime, KeywordList", s)
}
return t, nil
}

// SearchAttributeTypeToString converts an IndexedValueType to its user-facing name.
func SearchAttributeTypeToString(t enums.IndexedValueType) (string, error) {
name, ok := searchAttributeTypeNames[t]
if !ok {
return "", fmt.Errorf("unknown IndexedValueType: %v", t)
}
return name, nil
}

// OperatorServiceClient is an interface for the Temporal OperatorService gRPC methods
// needed by search attribute reconciliation. It is satisfied by the client returned
// from temporalclient.Client.OperatorService().
type OperatorServiceClient interface {
ListSearchAttributes(ctx context.Context, in *operatorservice.ListSearchAttributesRequest, opts ...grpc.CallOption) (*operatorservice.ListSearchAttributesResponse, error)
AddSearchAttributes(ctx context.Context, in *operatorservice.AddSearchAttributesRequest, opts ...grpc.CallOption) (*operatorservice.AddSearchAttributesResponse, error)
RemoveSearchAttributes(ctx context.Context, in *operatorservice.RemoveSearchAttributesRequest, opts ...grpc.CallOption) (*operatorservice.RemoveSearchAttributesResponse, error)
}

// parseDesiredAttributes converts the spec's string type map into typed IndexedValueType map.
func parseDesiredAttributes(spec map[string]string) (map[string]enums.IndexedValueType, error) {
desired := make(map[string]enums.IndexedValueType, len(spec))
for name, typeStr := range spec {
t, err := SearchAttributeTypeFromString(typeStr)
if err != nil {
return nil, fmt.Errorf("search attribute %q: %w", name, err)
}
desired[name] = t
}
return desired, nil
}

// computeAttributesToAdd returns attributes present in desired but not in existing,
// and returns an error if any existing attribute has a type mismatch.
func computeAttributesToAdd(desired, existing map[string]enums.IndexedValueType) (map[string]enums.IndexedValueType, error) {
toAdd := make(map[string]enums.IndexedValueType)
for name, desiredType := range desired {
existingType, exists := existing[name]
if !exists {
toAdd[name] = desiredType
continue
}
if existingType != desiredType {
existingTypeName, _ := SearchAttributeTypeToString(existingType)
desiredTypeName, _ := SearchAttributeTypeToString(desiredType)
return nil, fmt.Errorf("search attribute %q has type %s on server but %s in spec; Temporal does not allow type changes", name, existingTypeName, desiredTypeName)
}
}
return toAdd, nil
}

// computeAttributesToRemove returns attribute names present in existing but not in desired.
func computeAttributesToRemove(desired, existing map[string]enums.IndexedValueType) []string {
var toRemove []string
for name := range existing {
if _, inSpec := desired[name]; !inSpec {
toRemove = append(toRemove, name)
}
}
return toRemove
}

// ReconcileSearchAttributes ensures the custom search attributes on the Temporal server
// match the desired state declared in the TemporalNamespace spec.
func ReconcileSearchAttributes(ctx context.Context, operatorSvc OperatorServiceClient, namespace *v1beta1.TemporalNamespace) error {
logger := log.FromContext(ctx)
nsName := namespace.GetName()

listResp, err := operatorSvc.ListSearchAttributes(ctx, &operatorservice.ListSearchAttributesRequest{
Namespace: nsName,
})
if err != nil {
return fmt.Errorf("listing search attributes: %w", err)
}

existing := listResp.GetCustomAttributes()
logger.V(1).Info("Listed existing custom search attributes", "namespace", nsName, "count", len(existing))

desired, err := parseDesiredAttributes(namespace.Spec.CustomSearchAttributes)
if err != nil {
return err
}

toAdd, err := computeAttributesToAdd(desired, existing)
if err != nil {
return err
}

var toRemove []string
if namespace.Spec.AllowSearchAttributeDeletion {
toRemove = computeAttributesToRemove(desired, existing)
}

if len(toAdd) == 0 && len(toRemove) == 0 {
logger.V(1).Info("Search attributes are up to date", "namespace", nsName)
return nil
}

if len(toAdd) > 0 {
logger.Info("Adding search attributes", "namespace", nsName, "count", len(toAdd))
_, err := operatorSvc.AddSearchAttributes(ctx, &operatorservice.AddSearchAttributesRequest{
SearchAttributes: toAdd,
Namespace: nsName,
})
if err != nil {
return fmt.Errorf("adding search attributes: %w", err)
}
}

if len(toRemove) > 0 {
logger.Info("Removing search attributes", "namespace", nsName, "count", len(toRemove), "attributes", toRemove)
_, err := operatorSvc.RemoveSearchAttributes(ctx, &operatorservice.RemoveSearchAttributesRequest{
SearchAttributes: toRemove,
Namespace: nsName,
})
if err != nil {
return fmt.Errorf("removing search attributes: %w", err)
}
}

return nil
}
Loading
Loading