Skip to content

Commit ef0e2df

Browse files
author
Mohd Uzair
authored
Merge branch 'master' into escape_uuid
2 parents c2fb4b6 + f909496 commit ef0e2df

File tree

8 files changed

+126
-19
lines changed

8 files changed

+126
-19
lines changed

helpers/component_info.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "meshkit",
33
"type": "library",
4-
"next_error_code": 11096
5-
}
4+
"next_error_code": 11097
5+
}

models/meshmodel/registry/error.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package registry
2+
3+
import (
4+
"github.com/layer5io/meshkit/errors"
5+
)
6+
7+
var (
8+
ErrUnknownHostCode = "11097"
9+
)
10+
11+
func ErrUnknownHost(err error) error {
12+
return errors.New(ErrUnknownHostCode, errors.Alert, []string{"host is not supported"}, []string{err.Error()}, []string{"The component's host is not supported by the version of server you are running"}, []string{"Try upgrading to latest available version"})
13+
}

models/meshmodel/registry/host.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

models/meshmodel/registry.go renamed to models/meshmodel/registry/registry.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package meshmodel
1+
package registry
22

33
import (
44
"fmt"
@@ -31,20 +31,6 @@ type Registry struct {
3131
CreatedAt time.Time
3232
UpdatedAt time.Time
3333
}
34-
type Host struct {
35-
ID uuid.UUID `json:"-"`
36-
Hostname string
37-
Port int
38-
ContextID string
39-
CreatedAt time.Time `json:"-"`
40-
UpdatedAt time.Time `json:"-"`
41-
}
42-
43-
func createHost(db *database.Handler, h Host) (uuid.UUID, error) {
44-
h.ID = uuid.New()
45-
err := db.Create(&h).Error
46-
return h.ID, err
47-
}
4834

4935
// Entity is referred as any type of schema managed by the registry
5036
// ComponentDefinitions and PolicyDefinitions are examples of entities

models/oam/core/v1alpha1/application_component.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func GetAnnotationsForWorkload(w v1alpha1.ComponentDefinition) map[string]string
5454
res[strings.ReplaceAll(fmt.Sprintf("%s.%s", MesheryAnnotationPrefix, key), " ", "")] = v
5555
}
5656
}
57+
sourceURI, ok := w.Model.Metadata["source_uri"].(string)
58+
if ok {
59+
res[fmt.Sprintf("%s.model.source_uri", MesheryAnnotationPrefix)] = sourceURI
60+
}
5761
res[fmt.Sprintf("%s.model.name", MesheryAnnotationPrefix)] = w.Model.Name
5862
res[fmt.Sprintf("%s.k8s.APIVersion", MesheryAnnotationPrefix)] = w.APIVersion
5963
res[fmt.Sprintf("%s.k8s.Kind", MesheryAnnotationPrefix)] = w.Kind

utils/artifacthub/package.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (pkg AhPackage) GenerateComponents() ([]v1alpha1.ComponentDefinition, error
4747
if comp.Metadata == nil {
4848
comp.Metadata = make(map[string]interface{})
4949
}
50+
if comp.Model.Metadata == nil {
51+
comp.Model.Metadata = make(map[string]interface{})
52+
}
53+
comp.Model.Metadata["source_uri"] = pkg.ChartUrl
5054
comp.Model.Version = pkg.Version
5155
comp.Model.Name = pkg.Name
5256
comp.Model.Category = v1alpha1.Category{

utils/kubernetes/apply-helm-chart.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ type ApplyHelmChartConfig struct {
122122
// SkipCRDs while installation
123123
SkipCRDs bool
124124

125+
// Skip upgrade, if release is already installed
126+
SkipUpgradeIfInstalled bool
127+
125128
// URL is the url for charts
126129
//
127130
// Either ChartLocation or URL can be defined, if both of them
@@ -265,7 +268,7 @@ func (client *Client) ApplyHelmChart(cfg ApplyHelmChartConfig) error {
265268

266269
// Before installing a helm chart, check if it already exists in the cluster
267270
// this is a workaround make the helm chart installation idempotent
268-
if cfg.Action == INSTALL {
271+
if cfg.Action == INSTALL && !cfg.SkipUpgradeIfInstalled {
269272
if err := updateActionIfReleaseFound(actionConfig, &cfg, *helmChart); err != nil {
270273
return ErrApplyHelmChart(err)
271274
}

utils/kubernetes/describe/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package describe
33
import "github.com/layer5io/meshkit/errors"
44

55
var (
6-
ErrGetDescriberFuncCode = "not set"
6+
ErrGetDescriberFuncCode = "11096"
77
)
88

99
func ErrGetDescriberFunc() error {

0 commit comments

Comments
 (0)