|
| 1 | +/* |
| 2 | +Copyright 2026 The KodeRover Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package models |
| 18 | + |
| 19 | +import ( |
| 20 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 21 | + |
| 22 | + "github.com/koderover/zadig/v2/pkg/setting" |
| 23 | +) |
| 24 | + |
| 25 | +// ServiceModule is a first-class record of a deployable unit ("module" / |
| 26 | +// "container") attached to a service template. Modules can originate from: |
| 27 | +// |
| 28 | +// - Auto-discovery: parsed out of a service template's KubeYamls for the |
| 29 | +// workload kinds we recognize (Deployment, StatefulSet, Job, CronJob, |
| 30 | +// CloneSet). Auto records are bound to a specific service revision via |
| 31 | +// RevisionBound and are re-derived every time SetCurrentContainerImages |
| 32 | +// re-parses YAML. |
| 33 | +// |
| 34 | +// - Manual declaration: added through the manual-module API by users for |
| 35 | +// workload kinds we cannot parse (CRDs, DaemonSets, Argo Rollouts, etc.). |
| 36 | +// Manual records carry RevisionBound = 0 — they are version-agnostic and |
| 37 | +// load for every revision of the service. |
| 38 | +// |
| 39 | +// Read-merge rule (see ResolveServiceModules): records are unioned by Name with |
| 40 | +// time-of-creation precedence ("first-come-first-served" by CreateTime). |
| 41 | +// Conflicts are recorded and surfaced to the caller so the UI can warn users. |
| 42 | +type ServiceModule struct { |
| 43 | + ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` |
| 44 | + ProjectName string `bson:"project_name" json:"project_name"` |
| 45 | + ServiceName string `bson:"service_name" json:"service_name"` |
| 46 | + |
| 47 | + // IsManual: true if added explicitly by the user via the manual-module API. |
| 48 | + // Manual records MUST have RevisionBound = 0. |
| 49 | + IsManual bool `bson:"is_manual" json:"is_manual"` |
| 50 | + |
| 51 | + // RevisionBound: the service revision this record belongs to. |
| 52 | + // 0 -> manual / version-agnostic, loads for every revision |
| 53 | + // >0 -> auto-discovered, scoped to that revision only |
| 54 | + RevisionBound int64 `bson:"revision_bound" json:"revision_bound"` |
| 55 | + |
| 56 | + // Name is the module identifier. For auto records it equals the parsed |
| 57 | + // container's Name; for manual records it is the user-supplied name that |
| 58 | + // $<name>-image$ placeholders in YAML resolve against. |
| 59 | + Name string `bson:"name" json:"name"` |
| 60 | + |
| 61 | + // Type carries the legacy Container.Type distinction (normal vs init |
| 62 | + // container). No K8s deployment logic in this codebase branches on this |
| 63 | + // field — it is preserved for OpenAPI parity. Manual records default to |
| 64 | + // ContainerTypeNormal (""). |
| 65 | + Type setting.ContainerType `bson:"type,omitempty" json:"type,omitempty"` |
| 66 | + |
| 67 | + // Image is the resolved image URI. For auto records it is whatever the |
| 68 | + // parsed YAML had; for manual records it is what the user typed. Either |
| 69 | + // may be empty at creation time (an unbuilt module). |
| 70 | + Image string `bson:"image,omitempty" json:"image,omitempty"` |
| 71 | + |
| 72 | + // ImageName is the registry-friendly name used for build target paths and |
| 73 | + // image webhook matching. Required for manual records (enforced at the |
| 74 | + // service layer); for auto records it is derived from Image via |
| 75 | + // ExtractImageName, falling back to Name. |
| 76 | + ImageName string `bson:"image_name,omitempty" json:"image_name,omitempty"` |
| 77 | + ImagePath *ImagePathSpec `bson:"image_path,omitempty" json:"image_path,omitempty"` |
| 78 | + |
| 79 | + // Ignored marks an auto-discovered module as hidden for this revision. |
| 80 | + // It is used as a tombstone when users delete an auto module, so later YAML |
| 81 | + // re-parses (for example saving variables) don't silently add it back. |
| 82 | + Ignored bool `bson:"ignored,omitempty" json:"ignored,omitempty"` |
| 83 | + |
| 84 | + CreateTime int64 `bson:"create_time" json:"create_time"` |
| 85 | + UpdateTime int64 `bson:"update_time,omitempty" json:"update_time,omitempty"` |
| 86 | +} |
| 87 | + |
| 88 | +func (ServiceModule) TableName() string { |
| 89 | + return "service_module" |
| 90 | +} |
0 commit comments