|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/layer5io/meshkit/database" |
| 11 | + "github.com/layer5io/meshkit/models/oam/core/v1alpha1" |
| 12 | + "github.com/layer5io/meshkit/utils/kubernetes" |
| 13 | + "gorm.io/gorm" |
| 14 | +) |
| 15 | + |
| 16 | +type Host struct { |
| 17 | + ID uuid.UUID `json:"-"` |
| 18 | + Hostname string |
| 19 | + Port int |
| 20 | + Metadata string |
| 21 | + CreatedAt time.Time `json:"-"` |
| 22 | + UpdatedAt time.Time `json:"-"` |
| 23 | + IHost IHost `gorm:"-"` |
| 24 | +} |
| 25 | + |
| 26 | +func createHost(db *database.Handler, h Host) (uuid.UUID, error) { |
| 27 | + h.ID = uuid.New() |
| 28 | + err := db.Create(&h).Error |
| 29 | + return h.ID, err |
| 30 | +} |
| 31 | + |
| 32 | +func (h *Host) AfterFind(tx *gorm.DB) error { |
| 33 | + switch h.Hostname { |
| 34 | + case "artifacthub": |
| 35 | + h.IHost = ArtifactHub{} |
| 36 | + case "kubernetes": |
| 37 | + h.IHost = Kubernetes{} |
| 38 | + default: |
| 39 | + return ErrUnknownHost(errors.New("unable to find compatible host for the component")) |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// Each host from where meshmodels can be generated needs to implement this interface |
| 45 | +// HandleDependents, contains host specific logic for provisioning required CRDs/operators for corresponding components. |
| 46 | +type IHost interface { |
| 47 | + HandleDependents(comp v1alpha1.Component, kc *kubernetes.Client, isDeploy bool) (string, error) |
| 48 | + String() string |
| 49 | +} |
| 50 | + |
| 51 | +type ArtifactHub struct{} |
| 52 | + |
| 53 | +func (ah ArtifactHub) HandleDependents(comp v1alpha1.Component, kc *kubernetes.Client, isDeploy bool) (summary string, err error) { |
| 54 | + source_uri := comp.Annotations[fmt.Sprintf("%s.model.source_uri", v1alpha1.MesheryAnnotationPrefix)] |
| 55 | + act := kubernetes.UNINSTALL |
| 56 | + if isDeploy { |
| 57 | + act = kubernetes.INSTALL |
| 58 | + } |
| 59 | + |
| 60 | + if source_uri != "" { |
| 61 | + err = kc.ApplyHelmChart(kubernetes.ApplyHelmChartConfig{ |
| 62 | + URL: source_uri, |
| 63 | + Namespace: comp.Namespace, |
| 64 | + CreateNamespace: true, |
| 65 | + Action: act, |
| 66 | + SkipUpgradeIfInstalled: true, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + if !isDeploy { |
| 70 | + summary = fmt.Sprintf("error undeploying dependent resources for %s, please proceed with manual uninstall or try again: %s", strings.TrimSuffix(comp.Spec.Type, ".K8s"), comp.Name) |
| 71 | + } else { |
| 72 | + summary = fmt.Sprintf("error deploying dependent resources for %s, please procced with manual install or try again: %s", strings.TrimSuffix(comp.Spec.Type, ".K8s"), comp.Name) |
| 73 | + } |
| 74 | + } else { |
| 75 | + if !isDeploy { |
| 76 | + summary = fmt.Sprintf("Undeployed helm chart%s: %s", strings.TrimSuffix(comp.Spec.Type, ".K8s"), comp.Name) |
| 77 | + } else { |
| 78 | + summary = fmt.Sprintf("Deployed helm chart%s: %s", strings.TrimSuffix(comp.Spec.Type, ".K8s"), comp.Name) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return summary, err |
| 83 | +} |
| 84 | + |
| 85 | +func (ah ArtifactHub) String() string { |
| 86 | + return "artifacthub" |
| 87 | +} |
| 88 | + |
| 89 | +type Kubernetes struct{} |
| 90 | + |
| 91 | +func (k Kubernetes) HandleDependents(comp v1alpha1.Component, kc *kubernetes.Client, isDeploy bool) (summary string, err error) { |
| 92 | + return summary, err |
| 93 | +} |
| 94 | + |
| 95 | +func (k Kubernetes) String() string { |
| 96 | + return "kubernetes" |
| 97 | +} |
0 commit comments