Skip to content
Open
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
19 changes: 10 additions & 9 deletions controller/namespace/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

ctrlreqlog "github.com/temporalio/kube-temporal/pkg/controller/request/log"
"github.com/temporalio/kube-temporal/pkg/types"
tcloudservicev1 "go.temporal.io/cloud-sdk/api/cloudservice/v1"
tcclient "go.temporal.io/cloud-sdk/cloudclient"
ctrlrt "sigs.k8s.io/controller-runtime"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -105,19 +106,19 @@ func (r *Reconciler) ResourceSetLatest(
// "pop" the underlying *tcloudv1alpha1.Namespace from the [types.Resource]
// wrapper. This is a pointer so by modifying it, you mutate the supplied
// `latest` parameter.
ns := latest.(*res).cr
_ = ns
cr := latest.(*res).cr
_ = cr

// TODO: call the Temporal Cloud API to fetch Namespace spec and status and
// mutate the supplied resource to set the spec and status fields
// accordingly.
//
// latestObserved, err := tcc.CloudService().GetNamespace(...)
// if err != nil {
// return fmt.Errorf("failed getting tcloud namespace: %w", err)
// }
// ns.Spec.ProjectID = latestObserved.ProjectId
// ...
req := tcloudservicev1.GetNamespaceRequest{}
resp, err := tcc.GetNamespace(ctx, &req)
if err != nil {
return fmt.Errorf("failed getting tcloud namespace: %w", err)
Comment on lines +115 to +118

@zainawaisn zainawaisn Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this would fail against the real Temporal server since an empty GetNamespaceRequest struct will cause GetNamespace() on the saas-control-plane side to emit an error. If this is just an item for later implementation, no worries then.

}
ns := resp.GetNamespace()
cr.Spec.Project = ns.GetProjectId()

return err
}
Expand Down
132 changes: 107 additions & 25 deletions controller/namespace/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package namespace_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"
tcloudv1alpha1 "github.com/temporalio/cloud-operator/api/cloud/v1alpha1"
"github.com/temporalio/cloud-operator/controller/namespace"
"github.com/temporalio/cloud-operator/pkg/testutil"
kttestutil "github.com/temporalio/kube-temporal/pkg/testutil"
"github.com/temporalio/cloud-operator/pkg/testutil/kube"
fakeserver "github.com/temporalio/cloud-operator/pkg/testutil/temporal/cloud/fake/server"
kttestutilcompare "github.com/temporalio/kube-temporal/pkg/testutil/compare"
kttestutilkube "github.com/temporalio/kube-temporal/pkg/testutil/kube"
"github.com/temporalio/kube-temporal/pkg/types"
tcloudservicev1 "go.temporal.io/cloud-sdk/api/cloudservice/v1"
tcloudnamespacev1 "go.temporal.io/cloud-sdk/api/namespace/v1"
ctrlrt "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
kubeNamespace = "kube-temporal"
kubeNamespace = "temporal-cloud-operator"
)

func TestReconciler_ResourceFromRequest(t *testing.T) {
Expand All @@ -41,17 +46,17 @@ func TestReconciler_ResourceFromRequest(t *testing.T) {
{
name: "fail if resource not found",
client: func() client.Client {
return testutil.NewFakeClient()
return kube.NewFakeClient()
}(),
req: kttestutil.NewResourceRequest(tnsName, kubeNamespace),
expErr: "namespaces.cloud.temporal.io \"kube-temporal\" not found",
req: kttestutilkube.NewResourceRequest(kubeNamespace, tnsName),
expErr: "namespaces.cloud.temporal.io \"tmprl-ns\" not found",
},
{
name: "yields resource",
client: func() client.Client {
return testutil.NewFakeClient(tns)
return kube.NewFakeClient(tns)
}(),
req: kttestutil.NewResourceRequest(kubeNamespace, tnsName),
req: kttestutilkube.NewResourceRequest(kubeNamespace, tnsName),
exp: namespace.NewResource(tns),
},
}
Expand All @@ -74,57 +79,134 @@ func TestReconciler_ResourceFromRequest(t *testing.T) {

require.Nil(err)
if tc.exp != nil {
require.EqualExportedValues(tc.exp, got)
require.Equal(tc.exp, got)
}
})
}
}

func TestReconciler_ResourceSetLatest(t *testing.T) {
tnsName := "tmprl-ns"
project := "tmprl-project"
projectOther := "tmprl-project-other"
tns := &tcloudv1alpha1.Namespace{}
tns.Name = tnsName
tns.Namespace = kubeNamespace

tcc, err := testutil.NewFakeCloudClient()
require.Nil(t, err)
tns.Spec.Project = project

cases := []struct {
name string
r *namespace.Reconciler
res types.Resource
expErr string
name string
r *namespace.Reconciler
reconOpts []namespace.NewReconcilerOption
serverOpts []fakeserver.NewOption
getNamespace fakeserver.GetNamespaceFunc
res types.Resource
latest types.Resource
expErr string
}{
{
name: "fail nil cloud connection manager or default client",
r: namespace.NewReconciler(),
res: namespace.NewResource(tns),
expErr: "connection manager nil and no default client set",
r: namespace.NewReconciler(), // imitate no conn mgr
res: namespace.NewResource(tns),
},
{
name: "mutates latest resource",
r: namespace.NewReconciler(
namespace.WithKubeClient(
testutil.NewFakeClient(tns),
name: "fail if tcloud get namespace errors",
serverOpts: []fakeserver.NewOption{
fakeserver.OnGetNamespaceReturn(
nil, fmt.Errorf("Indy! Bad dates!"),
),
namespace.WithDefaultCloudClient(*tcc),
),
},
res: namespace.NewResource(tns),
expErr: "failed getting tcloud namespace: rpc error: code = Unknown desc = Indy! Bad dates!",
},
{
name: "latest spec is mutated",
serverOpts: []fakeserver.NewOption{
fakeserver.OnGetNamespaceReturn(
&tcloudservicev1.GetNamespaceResponse{
Namespace: &tcloudnamespacev1.Namespace{
Spec: &tcloudnamespacev1.NamespaceSpec{
Name: tnsName,
},
ProjectId: projectOther,
},
}, nil,
),
},
res: namespace.NewResource(tns),
latest: func() types.Resource {
// We verify that if the Temporal Cloud client returns a
// GetNamespaceResponse containing a different ProjectId than
// the one on the supplied types.Resource's Spec.Project field,
// that the supplied types.Resource's Spec.Project field is
// mutated to match the response from the Temporal Cloud
// server.
ns := &tcloudv1alpha1.Namespace{}
ns.Name = tnsName
ns.Namespace = kubeNamespace
ns.Spec.Project = projectOther
return namespace.NewResource(ns)
}(),
},
}
for _, tc := range cases {
t.Run(tc.name, func(tt *testing.T) {
require := require.New(tt)

sopts := tc.serverOpts
if tc.getNamespace != nil {
sopts = append(
sopts, fakeserver.WithGetNamespace(tc.getNamespace),
)
}

srv, err := fakeserver.New(sopts...)
require.Nil(err)
stop := srv.Start()
tt.Cleanup(stop)

tcc, err := srv.Client()
require.Nil(err)

r := tc.r
if r == nil {
ropts := []namespace.NewReconcilerOption{
namespace.WithDefaultCloudClient(*tcc),
namespace.WithKubeClient(
kube.NewFakeClient(tns),
),
}
ropts = append(ropts, tc.reconOpts...)

r = namespace.NewReconciler(ropts...)
}

ctx := context.TODO()

err := tc.r.ResourceSetLatest(ctx, tc.res)
// We expect the types.Resource supplied to ResourceSetLatest to be
// mutated, so make a deep copy so we can track any changes made.
latest := tc.res.DeepCopy()

err = r.ResourceSetLatest(ctx, latest)
if tc.expErr != "" {
require.ErrorContains(err, tc.expErr)
return
}

require.Nil(err)
if tc.latest != nil {
require.True(
kttestutilcompare.EqualIgnoringFields(
tc.latest, latest,
kttestutilkube.ResourceIgnoredFields...,
),
kttestutilcompare.DiffIgnoringFields(
tc.latest, latest,
kttestutilkube.ResourceIgnoredFields...,
),
)
}
})
}
}
17 changes: 17 additions & 0 deletions controller/namespace/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package namespace
import (
"log/slog"

"github.com/temporalio/kube-temporal/pkg/kube/annotation"
"github.com/temporalio/kube-temporal/pkg/kube/transform"
"github.com/temporalio/kube-temporal/pkg/resource"
"github.com/temporalio/kube-temporal/pkg/types"
Expand All @@ -22,6 +23,11 @@ type res struct {
cr *tcloudv1alpha1.Namespace
}

// CR returns the underlying custom resource.
func (r res) CR() any {
return r.cr
}

// IsBeingDeleted returns true if the Kubernetes resource has a non-zero
// deletion timestamp
func (r res) IsBeingDeleted() bool {
Expand Down Expand Up @@ -58,6 +64,17 @@ func (r *res) ReplaceConditions(conditions []*metav1.Condition) {
r.cr.Status.Conditions = conditions
}

// Annotations returns the set of Annotations.
func (r *res) Annotations() annotation.Annotations {
return r.cr.ObjectMeta.Annotations
}

// ReplaceAnnotations replaces the resource's set of Annotations with
// the supplied Annotations.
func (r *res) ReplaceAnnotations(annos annotation.Annotations) {
r.cr.ObjectMeta.Annotations = annos
}

// DeepCopy returns a copy of the resource
func (r res) DeepCopy() types.Resource {
crCopy := r.cr.DeepCopy()
Expand Down
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ require (
github.com/samber/lo v1.53.0
github.com/spf13/pflag v1.0.10
github.com/stretchr/testify v1.12.1
github.com/temporalio/kube-temporal v0.4.0
go.temporal.io/cloud-sdk v0.7.1
github.com/temporalio/kube-temporal v0.5.0
go.temporal.io/cloud-sdk v0.21.0
google.golang.org/grpc v1.84.0
k8s.io/api v0.37.1
k8s.io/apimachinery v0.37.1
k8s.io/client-go v0.37.1
Expand All @@ -17,15 +18,13 @@ require (

require (
cel.dev/expr v0.25.2 // indirect
github.com/Masterminds/semver/v3 v3.5.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
github.com/evanphx/json-patch v5.9.11+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/felixge/httpsnoop v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.10.0 // indirect
Expand All @@ -45,9 +44,10 @@ require (
github.com/go-openapi/swag/stringutils v0.29.2 // indirect
github.com/go-openapi/swag/typeutils v0.29.2 // indirect
github.com/go-openapi/swag/yamlutils v0.29.2 // indirect
github.com/go-openapi/testify/v2 v2.8.0 // indirect
github.com/google/cel-go v0.29.2 // indirect
github.com/google/gnostic-models v0.7.1 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.4 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 // indirect
Expand All @@ -56,8 +56,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.33.0 // indirect
github.com/onsi/gomega v1.44.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.25.0 // indirect
github.com/prometheus/client_model v0.6.3 // indirect
Expand All @@ -75,7 +73,6 @@ require (
go.opentelemetry.io/otel/trace v1.46.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
go.temporal.io/api v1.63.5 // indirect
go.uber.org/zap v1.28.0 // indirect
go.yaml.in/yaml/v2 v2.4.4 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect
Expand All @@ -89,7 +86,6 @@ require (
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679 // indirect
google.golang.org/grpc v1.84.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+Q
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/temporalio/kube-temporal v0.4.0 h1:dvRaPoV6brzViW1QYG+1mPt5X/RCTTFn+ZjDgfpVZPs=
github.com/temporalio/kube-temporal v0.4.0/go.mod h1:QR80csThD/3jzUo2NS5wRoHReCQfEwTXUJiVEPP8Pbg=
github.com/temporalio/kube-temporal v0.5.0 h1:nOlbpYfK0AafxadyggFZ7ukpoMhJxj/sH/ZZ6VWVmxk=
github.com/temporalio/kube-temporal v0.5.0/go.mod h1:O0KWamVVgkckW+mrNrzl2Fs9kORYZlYMdITKQx2jbrs=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
Expand All @@ -162,8 +162,8 @@ go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6Tb
go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E=
go.temporal.io/api v1.63.5 h1:c11+kPYHkXXL3UiShPdbMD+xtvqGsbTibUA9ypmiCa4=
go.temporal.io/api v1.63.5/go.mod h1:SrlW2JMwVlDP4nRWSNznUFqnSHd+YeMDS1BkYo63HCQ=
go.temporal.io/cloud-sdk v0.7.1 h1:XZY81McyMuHSvTH7vkfjU5X/eN4eDYMVtBDvCVuIXP8=
go.temporal.io/cloud-sdk v0.7.1/go.mod h1:W2O9t9tvo3Q/LhGgYdj8JijWbN5C84os+cz/BadIHYI=
go.temporal.io/cloud-sdk v0.21.0 h1:netTthDj6WbnTIM0l0OJw3IIQCkBVuxAgOAdwUX8ntM=
go.temporal.io/cloud-sdk v0.21.0/go.mod h1:W2O9t9tvo3Q/LhGgYdj8JijWbN5C84os+cz/BadIHYI=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
12 changes: 4 additions & 8 deletions pkg/reconciler/base/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"fmt"

ctrlreqlog "github.com/temporalio/kube-temporal/pkg/controller/request/log"
"github.com/temporalio/kube-temporal/pkg/kube/annotation"
ktbase "github.com/temporalio/kube-temporal/pkg/reconciler/base"
"github.com/temporalio/kube-temporal/pkg/types"
tcloudservicev1 "go.temporal.io/cloud-sdk/api/cloudservice/v1"
tcclient "go.temporal.io/cloud-sdk/cloudclient"
"k8s.io/apimachinery/pkg/api/meta"
kclient "sigs.k8s.io/controller-runtime/pkg/client"

tcloudcommon "github.com/temporalio/cloud-operator/api/cloud/common"
Expand Down Expand Up @@ -161,13 +161,9 @@ func (r *Reconciler) CloudClientFor(
case string, *tcloudv1alpha1.ConnectionSpec:
cli, err = r.ccManager.Client(subject)
case types.Resource:
ro := subject.RuntimeObject()
metaAccessor := meta.NewAccessor()
annos, err := metaAccessor.Annotations(ro)
if err != nil {
return nil, fmt.Errorf("failed getting resource annotations: %w", err)
}
hp, ok := annos[tcloudcommon.AnnotationKeyTemporalCloudHostPort]
hp, ok := annotation.Value(
subject, tcloudcommon.AnnotationKeyTemporalCloudHostPort,
)
if !ok {
return nil, fmt.Errorf(
"no %q annotation on resource",
Expand Down
Loading
Loading