component.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package models
  2. import (
  3. "encoding/json"
  4. "time"
  5. "gorm.io/gorm"
  6. )
  7. type App struct {
  8. ID int64 `gorm:"column:rowid;primaryKey;autoIncrement" json:"id"` // Unique identifier
  9. Name string `json:"name" gorm:"not null"`
  10. Description string `json:"description"`
  11. Version string `json:"version" gorm:"index"`
  12. // Configuration as JSON string in DB
  13. ConfigJSON string `json:"-" gorm:"column:config;type:text"`
  14. // Virtual field for ORM serialization/deserialization
  15. Config AppConfig `json:"config" gorm:"-"`
  16. CreatedAt time.Time `json:"createdAt" gorm:"autoCreateTime"`
  17. UpdatedAt time.Time `json:"updatedAt" gorm:"autoUpdateTime"`
  18. CreatedBy string `json:"createdBy" gorm:"index"` // User ID who created the template
  19. DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // Soft delete support
  20. // Relationships
  21. Deployments []Deployment `json:"deployments" gorm:"foreignKey:AppID"` // Deployments using this app
  22. }
  23. type AppConfig struct {
  24. Components []ComponentConfig `json:"components"` // Components included in this app (renamed from apps)
  25. NetworkPolicies []NetworkPolicy `json:"networkPolicies"` // Network policies to apply
  26. EnvVariables map[string]string `json:"envVariables,omitempty"` // Environment variables
  27. Secrets []SecretConfig `json:"secrets,omitempty"` // Secret configurations
  28. }
  29. type ComponentConfig struct {
  30. ID int64 `json:"id"` // Reference to the component
  31. Name string `json:"name"` // Name of the component in this app
  32. ExposedPorts []int `json:"exposedPorts,omitempty"` // Ports to expose
  33. PublicAccess bool `json:"publicAccess"` // Whether the component is publicly accessible
  34. Resources ResourceConfig `json:"resources"` // Resource allocation
  35. Autoscaling AutoscalingConfig `json:"autoscaling,omitempty"` // Autoscaling configuration
  36. EnvOverrides map[string]string `json:"envOverrides,omitempty"` // Environment variable overrides
  37. ServiceMesh bool `json:"serviceMesh"` // Whether to include in service mesh
  38. }
  39. type ResourceConfig struct {
  40. CPU string `json:"cpu"` // e.g., "0.5"
  41. Memory string `json:"memory"` // e.g., "512Mi"
  42. Storage string `json:"storage"` // e.g., "1Gi"
  43. }
  44. type AutoscalingConfig struct {
  45. Enabled bool `json:"enabled"` // Whether autoscaling is enabled
  46. MinReplicas int `json:"minReplicas"` // Minimum number of replicas
  47. MaxReplicas int `json:"maxReplicas"` // Maximum number of replicas
  48. CPUThreshold int `json:"cpuThreshold"` // CPU threshold for scaling (percentage)
  49. Metric string `json:"metric"` // Metric to base scaling on (e.g., "cpu", "memory")
  50. }
  51. type NetworkPolicy struct {
  52. Name string `json:"name"` // Policy name
  53. FromComponents []string `json:"fromComponents"` // Source components (renamed from FromApps)
  54. ToComponents []string `json:"toComponents"` // Destination components (renamed from ToApps)
  55. Ports []int `json:"ports,omitempty"` // Allowed ports
  56. AllowEgress bool `json:"allowEgress"` // Whether to allow egress traffic
  57. }
  58. type SecretConfig struct {
  59. Name string `json:"name"` // Secret name
  60. Description string `json:"description"` // Secret description
  61. Required bool `json:"required"` // Whether the secret is required
  62. }
  63. // BeforeSave serializes the embedded JSON fields
  64. func (a *App) BeforeSave(tx *gorm.DB) error {
  65. // Marshal Config to JSON
  66. configJSON, err := json.Marshal(a.Config)
  67. if err != nil {
  68. return err
  69. }
  70. a.ConfigJSON = string(configJSON)
  71. return nil
  72. }
  73. // AfterFind deserializes the JSON fields
  74. func (a *App) AfterFind(tx *gorm.DB) error {
  75. // Unmarshal Config from JSON
  76. if a.ConfigJSON != "" {
  77. if err := json.Unmarshal([]byte(a.ConfigJSON), &a.Config); err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }