Skip to content

Commit 6945caa

Browse files
committed
Limit actor env refs to secrets
1 parent 6bc9c55 commit 6945caa

12 files changed

Lines changed: 76 additions & 541 deletions

File tree

‎cmd/ateapi/internal/controlapi/functional_test.go‎

Lines changed: 3 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ func TestResumeActor(t *testing.T) {
758758
AteomPodIp: "127.0.0.1",
759759
},
760760
}
761-
if diff := cmp.Diff(want, getResp, protocmp.Transform(), protocmp.IgnoreFields(&ateapipb.Actor{}, "version", "last_resolved_env_sha256", "last_resolved_env_at")); diff != "" {
761+
if diff := cmp.Diff(want, getResp, protocmp.Transform(), protocmp.IgnoreFields(&ateapipb.Actor{}, "version")); diff != "" {
762762
t.Errorf("GetActor response mismatch (-want +got):\n%s", diff)
763763
}
764764

@@ -798,20 +798,7 @@ func TestResumeActorResolvesValueFromEnv(t *testing.T) {
798798
tc := setupTest(t, ns)
799799
defer tc.cleanup()
800800

801-
_, err := tc.k8sClient.CoreV1().ConfigMaps(ns).Create(context.Background(), &corev1.ConfigMap{
802-
ObjectMeta: metav1.ObjectMeta{
803-
Name: "settings",
804-
Namespace: ns,
805-
},
806-
Data: map[string]string{
807-
"interval": "45",
808-
},
809-
}, metav1.CreateOptions{})
810-
if err != nil {
811-
t.Fatalf("failed to create ConfigMap: %v", err)
812-
}
813-
814-
_, err = tc.k8sClient.CoreV1().Secrets(ns).Create(context.Background(), &corev1.Secret{
801+
_, err := tc.k8sClient.CoreV1().Secrets(ns).Create(context.Background(), &corev1.Secret{
815802
ObjectMeta: metav1.ObjectMeta{
816803
Name: "api-keys",
817804
Namespace: ns,
@@ -834,15 +821,6 @@ func TestResumeActorResolvesValueFromEnv(t *testing.T) {
834821
Name: "LITERAL",
835822
Value: "plain",
836823
},
837-
{
838-
Name: "INTERVAL_SECONDS",
839-
ValueFrom: &atev1alpha1.EnvVarSource{
840-
ConfigMapKeyRef: &atev1alpha1.ConfigMapKeySelector{
841-
Name: "settings",
842-
Key: "interval",
843-
},
844-
},
845-
},
846824
{
847825
Name: "ANTHROPIC_API_KEY",
848826
ValueFrom: &atev1alpha1.EnvVarSource{
@@ -885,141 +863,11 @@ func TestResumeActorResolvesValueFromEnv(t *testing.T) {
885863
}
886864
wantEnv := map[string]string{
887865
"LITERAL": "plain",
888-
"INTERVAL_SECONDS": "45",
889866
"ANTHROPIC_API_KEY": "sk-test",
890867
}
891868
if diff := cmp.Diff(wantEnv, gotEnv); diff != "" {
892869
t.Errorf("resolved env mismatch (-want +got):\n%s", diff)
893870
}
894-
895-
getResp, err := tc.client.GetActor(context.Background(), &ateapipb.GetActorRequest{
896-
ActorId: "id1",
897-
})
898-
if err != nil {
899-
t.Fatalf("GetActor failed: %v", err)
900-
}
901-
if got, want := getResp.GetActor().GetLastResolvedEnvSha256(), workloadSpecEnvSHA256(restoreReq.GetSpec()); got != want {
902-
t.Errorf("last_resolved_env_sha256 = %q, want %q", got, want)
903-
}
904-
if getResp.GetActor().GetLastResolvedEnvAt() == nil {
905-
t.Errorf("expected last_resolved_env_at to be set")
906-
}
907-
}
908-
909-
func TestResumeActorEnvHashChangeInvalidatesSnapshot(t *testing.T) {
910-
ns := namespaceForTest("ns-resume-env-drift")
911-
tc := setupTest(t, ns)
912-
defer tc.cleanup()
913-
914-
_, err := tc.k8sClient.CoreV1().ConfigMaps(ns).Create(context.Background(), &corev1.ConfigMap{
915-
ObjectMeta: metav1.ObjectMeta{
916-
Name: "settings",
917-
Namespace: ns,
918-
},
919-
Data: map[string]string{
920-
"interval": "45",
921-
},
922-
}, metav1.CreateOptions{})
923-
if err != nil {
924-
t.Fatalf("failed to create ConfigMap: %v", err)
925-
}
926-
927-
createTemplateWithContainers(t, tc, ns, []atev1alpha1.Container{
928-
{
929-
Name: "main",
930-
Image: "main@sha256:abc",
931-
Command: []string{"/main"},
932-
Env: []atev1alpha1.EnvVar{
933-
{
934-
Name: "INTERVAL_SECONDS",
935-
ValueFrom: &atev1alpha1.EnvVarSource{
936-
ConfigMapKeyRef: &atev1alpha1.ConfigMapKeySelector{
937-
Name: "settings",
938-
Key: "interval",
939-
},
940-
},
941-
},
942-
},
943-
},
944-
})
945-
createWorkerPod(t, tc, ns, "worker-1", "node1")
946-
947-
_, err = tc.client.CreateActor(context.Background(), &ateapipb.CreateActorRequest{
948-
ActorTemplateNamespace: ns,
949-
ActorTemplateName: "tmpl1",
950-
ActorId: "id1",
951-
})
952-
if err != nil {
953-
t.Fatalf("CreateActor failed: %v", err)
954-
}
955-
_, err = tc.client.ResumeActor(context.Background(), &ateapipb.ResumeActorRequest{
956-
ActorId: "id1",
957-
})
958-
if err != nil {
959-
t.Fatalf("initial ResumeActor failed: %v", err)
960-
}
961-
firstGet, err := tc.client.GetActor(context.Background(), &ateapipb.GetActorRequest{
962-
ActorId: "id1",
963-
})
964-
if err != nil {
965-
t.Fatalf("GetActor failed: %v", err)
966-
}
967-
firstEnvHash := firstGet.GetActor().GetLastResolvedEnvSha256()
968-
if firstEnvHash == "" {
969-
t.Fatalf("expected first resume to record an env hash")
970-
}
971-
972-
_, err = tc.client.SuspendActor(context.Background(), &ateapipb.SuspendActorRequest{
973-
ActorId: "id1",
974-
})
975-
if err != nil {
976-
t.Fatalf("SuspendActor failed: %v", err)
977-
}
978-
suspended, err := tc.client.GetActor(context.Background(), &ateapipb.GetActorRequest{
979-
ActorId: "id1",
980-
})
981-
if err != nil {
982-
t.Fatalf("GetActor after suspend failed: %v", err)
983-
}
984-
if suspended.GetActor().GetLastSnapshot() == "" {
985-
t.Fatalf("expected suspend to record a snapshot")
986-
}
987-
988-
cm, err := tc.k8sClient.CoreV1().ConfigMaps(ns).Get(context.Background(), "settings", metav1.GetOptions{})
989-
if err != nil {
990-
t.Fatalf("failed to get ConfigMap: %v", err)
991-
}
992-
cm.Data["interval"] = "90"
993-
if _, err := tc.k8sClient.CoreV1().ConfigMaps(ns).Update(context.Background(), cm, metav1.UpdateOptions{}); err != nil {
994-
t.Fatalf("failed to update ConfigMap: %v", err)
995-
}
996-
997-
tc.fakeAtelet.Reset()
998-
_, err = tc.client.ResumeActor(context.Background(), &ateapipb.ResumeActorRequest{
999-
ActorId: "id1",
1000-
})
1001-
if err != nil {
1002-
t.Fatalf("second ResumeActor failed: %v", err)
1003-
}
1004-
if !tc.fakeAtelet.RunCalled {
1005-
t.Fatalf("expected env hash change to boot from spec")
1006-
}
1007-
if tc.fakeAtelet.RestoreCalled {
1008-
t.Fatalf("expected env hash change to skip snapshot restore")
1009-
}
1010-
1011-
resumed, err := tc.client.GetActor(context.Background(), &ateapipb.GetActorRequest{
1012-
ActorId: "id1",
1013-
})
1014-
if err != nil {
1015-
t.Fatalf("GetActor after second resume failed: %v", err)
1016-
}
1017-
if resumed.GetActor().GetLastResolvedEnvSha256() == firstEnvHash {
1018-
t.Fatalf("expected env hash to change after ConfigMap update")
1019-
}
1020-
if resumed.GetActor().GetLastSnapshot() != "" {
1021-
t.Fatalf("expected invalidated snapshot reference to be cleared, got %q", resumed.GetActor().GetLastSnapshot())
1022-
}
1023871
}
1024872

1025873
// TestResumeActor_NoWorkers tests that resuming an actor fails when no free workers are available.
@@ -1197,7 +1045,7 @@ func TestSuspendActor(t *testing.T) {
11971045
},
11981046
}
11991047

1200-
if diff := cmp.Diff(want, getResp, protocmp.Transform(), protocmp.IgnoreFields(&ateapipb.Actor{}, "version", "last_snapshot", "last_resolved_env_sha256", "last_resolved_env_at")); diff != "" {
1048+
if diff := cmp.Diff(want, getResp, protocmp.Transform(), protocmp.IgnoreFields(&ateapipb.Actor{}, "version", "last_snapshot")); diff != "" {
12011049
t.Errorf("GetActor response mismatch (-want +got):\n%s", diff)
12021050
}
12031051

‎cmd/ateapi/internal/controlapi/workflow_resume.go‎

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ package controlapi
1616

1717
import (
1818
"context"
19-
"crypto/sha256"
20-
"encoding/hex"
2119
"errors"
2220
"fmt"
23-
"hash"
2421
"log/slog"
2522
"math/rand"
2623
"time"
@@ -32,7 +29,6 @@ import (
3229
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
3330
"google.golang.org/grpc/codes"
3431
"google.golang.org/grpc/status"
35-
"google.golang.org/protobuf/types/known/timestamppb"
3632
"k8s.io/apimachinery/pkg/util/wait"
3733
"k8s.io/client-go/kubernetes"
3834
)
@@ -45,11 +41,8 @@ type ResumeInput struct {
4541

4642
// ResumeState holds the mutable state loaded and modified during execution.
4743
type ResumeState struct {
48-
Actor *ateapipb.Actor
49-
ActorTemplate *atev1alpha1.ActorTemplate
50-
LastResolvedEnvSHA256 string
51-
LastResolvedEnvAt *timestamppb.Timestamp
52-
SnapshotInvalidated bool
44+
Actor *ateapipb.Actor
45+
ActorTemplate *atev1alpha1.ActorTemplate
5346
}
5447

5548
type LoadActorForResumeStep struct {
@@ -184,11 +177,6 @@ func (s *CallAteletRestoreStep) Execute(ctx context.Context, input *ResumeInput,
184177
if err != nil {
185178
return err
186179
}
187-
state.LastResolvedEnvSHA256 = workloadSpecEnvSHA256(workloadSpec)
188-
state.LastResolvedEnvAt = timestamppb.Now()
189-
envChangedSinceSnapshot := state.Actor.GetLastSnapshot() != "" &&
190-
state.Actor.GetLastResolvedEnvSha256() != "" &&
191-
state.Actor.GetLastResolvedEnvSha256() != state.LastResolvedEnvSHA256
192180

193181
runscCfg := &ateletpb.RunscConfig{}
194182
if state.ActorTemplate.Spec.Runsc.AMD64 != nil {
@@ -209,7 +197,7 @@ func (s *CallAteletRestoreStep) Execute(ctx context.Context, input *ResumeInput,
209197
runscCfg.Authentication = authnCfg
210198
}
211199

212-
if state.Actor.LastSnapshot != "" && !envChangedSinceSnapshot {
200+
if state.Actor.LastSnapshot != "" {
213201
slog.InfoContext(ctx, "Actor has snapshot; Restoring from snapshot")
214202

215203
req := &ateletpb.RestoreRequest{
@@ -226,28 +214,6 @@ func (s *CallAteletRestoreStep) Execute(ctx context.Context, input *ResumeInput,
226214
if err != nil {
227215
return fmt.Errorf("while restoring workload: %w", err)
228216
}
229-
return nil
230-
} else if envChangedSinceSnapshot {
231-
state.SnapshotInvalidated = true
232-
slog.InfoContext(ctx, "Actor snapshot env hash differs from resolved env hash; Booting from ActorTemplate spec",
233-
slog.String("actor_id", state.Actor.GetActorId()),
234-
slog.String("old_env_sha256", state.Actor.GetLastResolvedEnvSha256()),
235-
slog.String("new_env_sha256", state.LastResolvedEnvSHA256),
236-
)
237-
req := &ateletpb.RunRequest{
238-
TargetAteomNamespace: state.Actor.GetAteomPodNamespace(),
239-
TargetAteomName: state.Actor.GetAteomPodName(),
240-
ActorTemplateNamespace: state.Actor.GetActorTemplateNamespace(),
241-
ActorTemplateName: state.Actor.GetActorTemplateName(),
242-
ActorId: state.Actor.GetActorId(),
243-
Runsc: runscCfg,
244-
Spec: workloadSpec,
245-
}
246-
_, err = client.Run(ctx, req)
247-
if err != nil {
248-
return fmt.Errorf("while creating workload from spec after env change invalidated snapshot: %w", err)
249-
}
250-
251217
return nil
252218
} else if state.ActorTemplate.Status.GoldenSnapshot != "" && !input.Boot {
253219
slog.InfoContext(ctx, "Actor has no snapshot; ActorTemplate has golden snapshot; Restoring from golden snapshot")
@@ -292,24 +258,6 @@ func (s *CallAteletRestoreStep) Execute(ctx context.Context, input *ResumeInput,
292258

293259
func (s *CallAteletRestoreStep) RetryBackoff() *wait.Backoff { return nil }
294260

295-
func workloadSpecEnvSHA256(spec *ateletpb.WorkloadSpec) string {
296-
h := sha256.New()
297-
for _, ctr := range spec.GetContainers() {
298-
writeHashString(h, ctr.GetName())
299-
for _, env := range ctr.GetEnv() {
300-
writeHashString(h, env.GetName())
301-
writeHashString(h, env.GetValue())
302-
}
303-
}
304-
return hex.EncodeToString(h.Sum(nil))
305-
}
306-
307-
func writeHashString(h hash.Hash, s string) {
308-
fmt.Fprintf(h, "%d:", len(s))
309-
h.Write([]byte(s))
310-
h.Write([]byte{0})
311-
}
312-
313261
type FinalizeRunningStep struct {
314262
store store.Interface
315263
}
@@ -325,11 +273,6 @@ func (s *FinalizeRunningStep) Execute(ctx context.Context, input *ResumeInput, s
325273
}
326274

327275
latestActor.Status = ateapipb.Actor_STATUS_RUNNING
328-
latestActor.LastResolvedEnvSha256 = state.LastResolvedEnvSHA256
329-
latestActor.LastResolvedEnvAt = state.LastResolvedEnvAt
330-
if state.SnapshotInvalidated {
331-
latestActor.LastSnapshot = ""
332-
}
333276
err = s.store.UpdateActor(ctx, latestActor, latestActor.GetVersion())
334277
if err == nil {
335278
state.Actor = latestActor

0 commit comments

Comments
 (0)