Skip to content

Commit

Permalink
chore: stop waiting for aliases in the API
Browse files Browse the repository at this point in the history
This change introduces a small paradigm change in how aliases are
handled. The AliasAssigned field is not a pointer that is only set once
we know that the alias controller has processed the object. A caller can
check for nil versus false to determine whether the controller has
process the object and the alias will not be assigned.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Dec 3, 2024
1 parent 98789f8 commit 0adf82e
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 83 deletions.
2 changes: 1 addition & 1 deletion apiclient/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Client) ListAgents(ctx context.Context, opts ListAgentsOptions) (result
if opts.Alias != "" {
var filtered types.AgentList
for _, agent := range result.Items {
if agent.Alias == opts.Alias && agent.AliasAssigned {
if agent.Alias == opts.Alias && agent.AliasAssigned != nil && *agent.AliasAssigned {
filtered.Items = append(filtered.Items, agent)
}
}
Expand Down
2 changes: 1 addition & 1 deletion apiclient/types/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type Agent struct {
Metadata
AgentManifest
AliasAssigned bool `json:"aliasAssigned,omitempty"`
AliasAssigned *bool `json:"aliasAssigned,omitempty"`
AuthStatus map[string]OAuthAppLoginAuthStatus `json:"authStatus,omitempty"`
TextEmbeddingModel string `json:"textEmbeddingModel,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion apiclient/types/emailreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package types
type EmailReceiver struct {
Metadata
EmailReceiverManifest
AddressAssigned bool `json:"aliasAssigned,omitempty"`
AddressAssigned *bool `json:"aliasAssigned,omitempty"`
EmailAddress string `json:"emailAddress,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion apiclient/types/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ModelList List[Model]

type ModelStatus struct {
ModelProviderStatus
AliasAssigned bool `json:"aliasAssigned,omitempty"`
AliasAssigned *bool `json:"aliasAssigned,omitempty"`
}

type ModelProviderStatus struct {
Expand Down
2 changes: 1 addition & 1 deletion apiclient/types/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package types
type Webhook struct {
Metadata
WebhookManifest
AliasAssigned bool `json:"aliasAssigned,omitempty"`
AliasAssigned *bool `json:"aliasAssigned,omitempty"`
LastSuccessfulRunCompleted *Time `json:"lastSuccessfulRunCompleted,omitempty"`
HasToken bool `json:"hasToken,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion apiclient/types/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "strings"
type Workflow struct {
Metadata
WorkflowManifest
AliasAssigned bool `json:"aliasAssigned,omitempty"`
AliasAssigned *bool `json:"aliasAssigned,omitempty"`
AuthStatus map[string]OAuthAppLoginAuthStatus `json:"authStatus,omitempty"`
TextEmbeddingModel string `json:"textEmbeddingModel,omitempty"`
}
Expand Down
25 changes: 25 additions & 0 deletions apiclient/types/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion apiclient/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Client) ListWorkflows(ctx context.Context, opts ListWorkflowsOptions) (
if opts.Alias != "" {
var filtered types.WorkflowList
for _, workflow := range result.Items {
if workflow.Alias == opts.Alias && workflow.AliasAssigned {
if workflow.Alias == opts.Alias && workflow.AliasAssigned != nil && *workflow.AliasAssigned {
filtered.Items = append(filtered.Items, workflow)
}
}
Expand Down
24 changes: 10 additions & 14 deletions pkg/api/handlers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ func (a *AgentHandler) Update(req api.Context) error {
return err
}

processedAgent, err := wait.For(req.Context(), req.Storage, &agent, func(agent *v1.Agent) (bool, error) {
return agent.Generation == agent.Status.AliasObservedGeneration, nil
})
if err != nil {
return fmt.Errorf("failed to update agent: %w", err)
}

resp, err := convertAgent(*processedAgent, req)
resp, err := convertAgent(agent, req)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,17 +84,15 @@ func (a *AgentHandler) Create(req api.Context) error {
},
}

agent, err := wait.For(req.Context(), req.Storage, agent, func(agent *v1.Agent) (bool, error) {
return agent.Generation == agent.Status.AliasObservedGeneration, nil
}, wait.Option{Create: true})
if err != nil {
return fmt.Errorf("failed to create agent: %w", err)
if err := req.Create(agent); err != nil {
return err
}

resp, err := convertAgent(*agent, req)
if err != nil {
return err
}

return req.WriteCreated(resp)
}

Expand All @@ -125,10 +116,15 @@ func convertAgent(agent v1.Agent, req api.Context) (*types.Agent, error) {
}
}

var aliasAssigned *bool
if agent.Generation == agent.Status.AliasObservedGeneration {
aliasAssigned = &agent.Status.AliasAssigned
}

return &types.Agent{
Metadata: MetadataFrom(&agent, links...),
AgentManifest: agent.Spec.Manifest,
AliasAssigned: agent.Status.AliasAssigned,
AliasAssigned: aliasAssigned,
AuthStatus: agent.Status.AuthStatus,
TextEmbeddingModel: embeddingModel,
}, nil
Expand Down
28 changes: 10 additions & 18 deletions pkg/api/handlers/emailreceiver.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package handlers

import (
"fmt"

"github.com/otto8-ai/otto8/apiclient/types"
"github.com/otto8-ai/otto8/pkg/alias"
"github.com/otto8-ai/otto8/pkg/api"
v1 "github.com/otto8-ai/otto8/pkg/storage/apis/otto.otto8.ai/v1"
"github.com/otto8-ai/otto8/pkg/system"
"github.com/otto8-ai/otto8/pkg/wait"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -42,14 +39,7 @@ func (e *EmailReceiverHandler) Update(req api.Context) error {
return err
}

processedEr, err := wait.For(req.Context(), req.Storage, &er, func(er *v1.EmailReceiver) (bool, error) {
return er.Generation == er.Status.AliasObservedGeneration, nil
})
if err != nil {
return fmt.Errorf("failed to update email receiver: %w", err)
}

return req.Write(convertEmailReceiver(*processedEr, e.hostname))
return req.Write(convertEmailReceiver(er, e.hostname))
}

func (e *EmailReceiverHandler) Delete(req api.Context) error {
Expand Down Expand Up @@ -81,24 +71,26 @@ func (e *EmailReceiverHandler) Create(req api.Context) error {
},
}

er, err := wait.For(req.Context(), req.Storage, er, func(er *v1.EmailReceiver) (bool, error) {
return er.Generation == er.Status.AliasObservedGeneration, nil
}, wait.Option{Create: true})
if err != nil {
return fmt.Errorf("failed to create email receiver: %w", err)
if err := req.Create(er); err != nil {
return err
}

return req.WriteCreated(convertEmailReceiver(*er, e.hostname))
}

func convertEmailReceiver(emailReceiver v1.EmailReceiver, hostname string) *types.EmailReceiver {
manifest := emailReceiver.Spec.EmailReceiverManifest

var aliasAssigned *bool
if emailReceiver.Generation == emailReceiver.Status.AliasObservedGeneration {
aliasAssigned = &emailReceiver.Status.AliasAssigned
}
er := &types.EmailReceiver{
Metadata: MetadataFrom(&emailReceiver),
EmailReceiverManifest: manifest,
AddressAssigned: emailReceiver.Status.AliasAssigned,
AddressAssigned: aliasAssigned,
}
if hostname != "" && er.AddressAssigned {
if hostname != "" && er.AddressAssigned != nil && *er.AddressAssigned {
er.EmailAddress = emailReceiver.Spec.User + "@" + hostname
}
return er
Expand Down
24 changes: 9 additions & 15 deletions pkg/api/handlers/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
v1 "github.com/otto8-ai/otto8/pkg/storage/apis/otto.otto8.ai/v1"
"github.com/otto8-ai/otto8/pkg/storage/selectors"
"github.com/otto8-ai/otto8/pkg/system"
"github.com/otto8-ai/otto8/pkg/wait"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -79,14 +78,7 @@ func (a *ModelHandler) Update(req api.Context) error {
return err
}

processedModel, err := wait.For(req.Context(), req.Storage, &existing, func(model *v1.Model) (bool, error) {
return model.Generation == model.Status.AliasObservedGeneration, nil
})
if err != nil {
return fmt.Errorf("failed to update model: %w", err)
}

resp, err := convertModel(req.Context(), req.Storage, *processedModel)
resp, err := convertModel(req.Context(), req.Storage, existing)
if err != nil {
return err
}
Expand Down Expand Up @@ -127,11 +119,8 @@ func (a *ModelHandler) Create(req api.Context) error {
return err
}

model, err := wait.For(req.Context(), req.Storage, model, func(model *v1.Model) (bool, error) {
return model.Generation == model.Status.AliasObservedGeneration, nil
}, wait.Option{Create: true})
if err != nil {
return fmt.Errorf("failed to create model: %w", err)
if err := req.Create(model); err != nil {
return err
}

resp, err := convertModel(req.Context(), req.Storage, *model)
Expand Down Expand Up @@ -172,12 +161,17 @@ func convertModel(ctx context.Context, c kclient.Client, model v1.Model) (types.
return types.Model{}, err
}

var aliasAssigned *bool
if model.Generation == model.Status.AliasObservedGeneration {
aliasAssigned = &model.Status.AliasAssigned
}

return types.Model{
Metadata: MetadataFrom(&model),
ModelManifest: model.Spec.Manifest,
ModelStatus: types.ModelStatus{
ModelProviderStatus: *convertModelProviderToolRef(toolRef),
AliasAssigned: model.Status.AliasAssigned,
AliasAssigned: aliasAssigned,
},
}, nil
}
Expand Down
24 changes: 9 additions & 15 deletions pkg/api/handlers/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/otto8-ai/otto8/pkg/api"
v1 "github.com/otto8-ai/otto8/pkg/storage/apis/otto.otto8.ai/v1"
"github.com/otto8-ai/otto8/pkg/system"
"github.com/otto8-ai/otto8/pkg/wait"
"golang.org/x/crypto/bcrypt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -78,14 +77,7 @@ func (a *WebhookHandler) Update(req api.Context) error {
return err
}

processedWh, err := wait.For(req.Context(), req.Storage, &wh, func(wh *v1.Webhook) (bool, error) {
return wh.Generation == wh.Status.AliasObservedGeneration, nil
})
if err != nil {
return fmt.Errorf("failed to update webhook: %w", err)
}

return req.Write(convertWebhook(*processedWh, req.APIBaseURL))
return req.Write(convertWebhook(wh, req.APIBaseURL))
}

func (a *WebhookHandler) Delete(req api.Context) error {
Expand Down Expand Up @@ -130,11 +122,8 @@ func (a *WebhookHandler) Create(req api.Context) error {
wh.Spec.Headers[i] = textproto.CanonicalMIMEHeaderKey(h)
}

wh, err := wait.For(req.Context(), req.Storage, wh, func(wh *v1.Webhook) (bool, error) {
return wh.Generation == wh.Status.AliasObservedGeneration, nil
}, wait.Option{Create: true})
if err != nil {
return fmt.Errorf("failed to create webhook: %w", err)
if err := req.Create(wh); err != nil {
return err
}

return req.WriteCreated(convertWebhook(*wh, req.APIBaseURL))
Expand All @@ -150,11 +139,16 @@ func convertWebhook(webhook v1.Webhook, urlPrefix string) *types.Webhook {
links = []string{"invoke", fmt.Sprintf("%s/webhooks/%s/%s", urlPrefix, webhook.Namespace, path)}
}

var aliasAssigned *bool
if webhook.Generation == webhook.Status.AliasObservedGeneration {
aliasAssigned = &webhook.Status.AliasAssigned
}

manifest := webhook.Spec.WebhookManifest
wh := &types.Webhook{
Metadata: MetadataFrom(&webhook, links...),
WebhookManifest: manifest,
AliasAssigned: webhook.Status.AliasAssigned,
AliasAssigned: aliasAssigned,
LastSuccessfulRunCompleted: v1.NewTime(webhook.Status.LastSuccessfulRunCompleted),
HasToken: len(webhook.Spec.TokenHash) > 0,
}
Expand Down
Loading

0 comments on commit 0adf82e

Please sign in to comment.