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
2 changes: 1 addition & 1 deletion .mk/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test: test-unit ## Run all tests.

.PHONY: test-unit
test-unit: ## Run all unit tests.
@go test -v ./...
@go test -v ./... -coverprofile=cover.out

.PHONY: test-cluster-delete
test-cluster-delete: $(KIND) ## Delete the testing cluster.
Expand Down
6 changes: 6 additions & 0 deletions api/cloud/common/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package common

const (
AnnotationKeyPrefixTemporalCloud = "cloud.temporal.io/"
AnnotationKeyTemporalCloudHostPort = AnnotationKeyPrefixTemporalCloud + "hostport"
)
19 changes: 19 additions & 0 deletions api/cloud/v1alpha1/connection.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package v1alpha1

import (
"crypto/md5"

tcorev1 "github.com/temporalio/kube-temporal/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
DefaultConnectionHostPort = "saas-api.tmprl.cloud:443"
)

// ConnectionSpec describes the desired configuration of a Connection to a
// Temporal Cloud API server. It describes where credentials can be found for
// instantiating a Temporal Cloud client object to that Temporal Cloud API
Expand All @@ -31,6 +37,19 @@ type ConnectionSpec struct {
Credentials CredentialsConfig `json:"credentials"`
}

// Hash returns a deterministic MD5 hash of the ConnectionSpec's field
// components.
func (s ConnectionSpec) Hash() []byte {
out := md5.New()
out.Write([]byte{'|'})
out.Write([]byte(s.HostPort))
out.Write([]byte{'|'})
out.Write(s.TLS.Hash())
out.Write([]byte{'|'})
out.Write(s.Credentials.Hash())
return out.Sum(nil)
}

// ConnectionStatus defines the observed state of a Connection
type ConnectionStatus struct {
tcorev1.StatusBase `json:",inline"`
Expand Down
11 changes: 11 additions & 0 deletions api/cloud/v1alpha1/credentials.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1alpha1

import (
"crypto/md5"

tcorev1 "github.com/temporalio/kube-temporal/api/core/v1"
)

Expand All @@ -11,3 +13,12 @@ type CredentialsConfig struct {
// API server using Temporal API Key authentication.
APIKey tcorev1.APIKeyCredentialsConfig `json:"apiKey"`
}

// Hash returns a deterministic MD5 hash of the CredentialsConfig's field
// components.
func (c CredentialsConfig) Hash() []byte {
out := md5.New()
out.Write([]byte{'|'})
out.Write(c.APIKey.Hash())
return out.Sum(nil)
}
9 changes: 5 additions & 4 deletions cmd/resource-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
ctrlrtlog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/temporalio/kube-temporal/pkg/controller/config"
cloudv1alpha1 "github.com/temporalio/resource-operator/api/cloud/v1alpha1"
tcloudv1alpha1 "github.com/temporalio/resource-operator/api/cloud/v1alpha1"
ctrlnamespace "github.com/temporalio/resource-operator/controller/namespace"
"github.com/temporalio/resource-operator/pkg/version"
)
Expand All @@ -31,7 +31,7 @@ var (
)

func init() {
_ = cloudv1alpha1.AddToScheme(scheme)
_ = tcloudv1alpha1.AddToScheme(scheme)
}

func main() {
Expand Down Expand Up @@ -70,12 +70,13 @@ func main() {
rootLogger := slog.New(logr.ToSlogHandler(ctrlrtlog.Log))

setupLog.Info("initializing controller")
cc := ctrlnamespace.New(cfg, rootLogger)
c := ctrlnamespace.New(cfg, rootLogger)

if err = cc.BindManager(mgr); err != nil {
if err = c.BindManager(mgr); err != nil {
setupLog.Error(
err,
"failed binding controller to controller manager",
"controller", c.Name(),
)
os.Exit(1)
}
Expand Down
57 changes: 28 additions & 29 deletions controller/namespace/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,46 @@ package namespace
import (
"log/slog"

"github.com/temporalio/kube-temporal/pkg/controller/base"
"github.com/temporalio/kube-temporal/pkg/controller"
"github.com/temporalio/kube-temporal/pkg/controller/config"
"github.com/temporalio/kube-temporal/pkg/types"
ctrlrt "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/predicate"

cloudv1alpha1 "github.com/temporalio/resource-operator/api/cloud/v1alpha1"
tcloudv1alpha1 "github.com/temporalio/resource-operator/api/cloud/v1alpha1"
)

// +kubebuilder:rbac:groups=cloud.temporal.io,resources=namespaces,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=cloud.temporal.io,resources=namespaces/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cloud.temporal.io,resources=namespaces/finalizers,verbs=update

const (
controllerName = "namespace"
ControllerName = "namespace"
)

type NewOption func(*controller)

// New returns a new [types.Controller] that handles [cloudv1alpha1.Namespace]
// resources.
// New returns a new [controller.Controller] that handles
// [cloudv1alpha1.Namespace] resources.
func New(
cfg *config.Config,
cfg config.Config,
logger *slog.Logger,
) types.Controller {
return &controller{
Controller: base.New(cfg, logger),
}
) *controller.Controller {
return controller.New(
ControllerName,
cfg,
NewReconciler(),
controller.WithLogger(logger),
)
}

type controller struct {
base.Controller
}

// BindManager binds the Controller to the supplied [ctrlrt.Manager].
func (c *controller) BindManager(m ctrlrt.Manager) error {
c.Controller.Client = m.GetClient()
c.Controller.Reader = m.GetAPIReader()
builder := ctrlrt.NewControllerManagedBy(m).
Named(controllerName).
For(&cloudv1alpha1.Namespace{}).
WithEventFilter(predicate.GenerationChangedPredicate{})
return builder.Complete(c)
// NewWithDefaultCloudConnectionSpec returns a new [controller.Controller] that
// handles [tcloudv1alpha1.Namespace] resources and passes in a
// [tcloudv1alpha1.ConnectionSpec] to instruct the reconciler how to connect to
// a Temporal Cloud API endpoint.
func NewWithDefaultCloudConnectionSpec(
cfg config.Config,
logger *slog.Logger,
spec tcloudv1alpha1.ConnectionSpec,
) *controller.Controller {
return controller.New(
ControllerName,
cfg,
NewReconciler(WithDefaultCloudConnectionSpec(spec)),
controller.WithLogger(logger),
)
}
Loading
Loading