|
7 | 7 | "strings" |
8 | 8 | "sync" |
9 | 9 |
|
| 10 | + "github.com/google/uuid" |
10 | 11 | "github.com/highcard-dev/daemon/internal/core/domain" |
11 | 12 | "github.com/highcard-dev/daemon/internal/core/ports" |
12 | 13 | coreservices "github.com/highcard-dev/daemon/internal/core/services" |
@@ -83,54 +84,44 @@ func (s *RuntimeSupervisor) Create(artifact string, name string, registryCredent |
83 | 84 |
|
84 | 85 | func (s *RuntimeSupervisor) CreateWithOwner(artifact string, name string, ownerID string, namespace string, registryCredentials []domain.RegistryCredential) (*domain.RuntimeScroll, error) { |
85 | 86 | id := coreservices.RuntimeScrollIDFromName(name) |
86 | | - var placeholder *domain.RuntimeScroll |
87 | | - if id != "" { |
88 | | - if _, err := s.store.GetScroll(id); err == nil { |
89 | | - return nil, fmt.Errorf("%w: %s", domain.ErrRuntimeScrollAlreadyExists, id) |
90 | | - } else if !errors.Is(err, domain.ErrRuntimeScrollNotFound) { |
91 | | - return nil, err |
92 | | - } |
93 | | - placeholder = &domain.RuntimeScroll{ |
94 | | - ID: id, |
95 | | - OwnerID: ownerID, |
96 | | - Artifact: artifact, |
97 | | - Root: s.runtimeBackend.RootRef(id, namespace), |
98 | | - Status: domain.RuntimeScrollStatusCreated, |
99 | | - Commands: map[string]domain.LockStatus{}, |
100 | | - } |
101 | | - if err := s.store.CreateScroll(placeholder); err != nil { |
102 | | - return nil, err |
103 | | - } |
| 87 | + if id == "" { |
| 88 | + id = uuid.NewString() |
| 89 | + } |
| 90 | + if _, err := s.store.GetScroll(id); err == nil { |
| 91 | + return nil, fmt.Errorf("%w: %s", domain.ErrRuntimeScrollAlreadyExists, id) |
| 92 | + } else if !errors.Is(err, domain.ErrRuntimeScrollNotFound) { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + placeholder := &domain.RuntimeScroll{ |
| 96 | + ID: id, |
| 97 | + OwnerID: ownerID, |
| 98 | + Artifact: artifact, |
| 99 | + Root: s.runtimeBackend.RootRef(id, namespace), |
| 100 | + Status: domain.RuntimeScrollStatusCreated, |
| 101 | + Commands: map[string]domain.LockStatus{}, |
| 102 | + } |
| 103 | + if err := s.store.CreateScroll(placeholder); err != nil { |
| 104 | + return nil, err |
104 | 105 | } |
105 | 106 | markPlaceholderError := func(cause error) { |
106 | | - if placeholder == nil { |
107 | | - return |
108 | | - } |
109 | 107 | placeholder.Status = domain.RuntimeScrollStatusError |
110 | 108 | placeholder.LastError = cause.Error() |
111 | 109 | _ = s.store.UpdateScroll(placeholder) |
112 | 110 | } |
113 | 111 |
|
114 | | - materialized, err := s.materializeNewScroll(context.Background(), s.runtimeBackend, artifact, name, namespace, registryCredentials) |
| 112 | + materialized, err := s.materializeNewScroll(context.Background(), s.runtimeBackend, artifact, id, namespace, registryCredentials) |
115 | 113 | if err != nil { |
116 | 114 | markPlaceholderError(err) |
117 | 115 | return nil, err |
118 | 116 | } |
119 | 117 | if materialized.Artifact != "" { |
120 | 118 | artifact = materialized.Artifact |
121 | 119 | } |
122 | | - if placeholder != nil { |
123 | | - placeholder, err = s.applyMaterializedScroll(placeholder, artifact, materialized) |
124 | | - if err != nil { |
125 | | - return nil, err |
126 | | - } |
127 | | - return placeholder, nil |
128 | | - } |
129 | | - runtimeScroll, err := s.manager.CreateWithDigest(artifact, materialized.ArtifactDigest, name, ownerID, materialized.Root, materialized.ScrollYAML) |
| 120 | + placeholder, err = s.applyMaterializedScroll(placeholder, artifact, materialized) |
130 | 121 | if err != nil { |
131 | 122 | return nil, err |
132 | 123 | } |
133 | | - return runtimeScroll, nil |
| 124 | + return placeholder, nil |
134 | 125 | } |
135 | 126 |
|
136 | 127 | func (s *RuntimeSupervisor) Ensure(artifact string, name string, registryCredentials []domain.RegistryCredential) (*domain.RuntimeScroll, error) { |
@@ -161,7 +152,7 @@ func (s *RuntimeSupervisor) EnsureWithOwner(artifact string, name string, ownerI |
161 | 152 | if artifact == "" { |
162 | 153 | artifact = runtimeScroll.Artifact |
163 | 154 | } |
164 | | - materialized, err := s.materializeNewScroll(context.Background(), s.runtimeBackend, artifact, name, namespace, registryCredentials) |
| 155 | + materialized, err := s.materializeNewScroll(context.Background(), s.runtimeBackend, artifact, id, namespace, registryCredentials) |
165 | 156 | if err != nil { |
166 | 157 | runtimeScroll.Status = domain.RuntimeScrollStatusError |
167 | 158 | runtimeScroll.LastError = err.Error() |
|
0 commit comments