Skip to content

Commit

Permalink
Merge branch 'master' into escape_uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
MUzairS15 committed Jul 25, 2023
2 parents c2fb4b6 + f909496 commit ef0e2df
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 19 deletions.
4 changes: 2 additions & 2 deletions helpers/component_info.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "meshkit",
"type": "library",
"next_error_code": 11096
}
"next_error_code": 11097
}
13 changes: 13 additions & 0 deletions models/meshmodel/registry/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package registry

import (
"github.com/layer5io/meshkit/errors"
)

var (
ErrUnknownHostCode = "11097"
)

func ErrUnknownHost(err error) error {
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"})
}
97 changes: 97 additions & 0 deletions models/meshmodel/registry/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package registry

import (
"errors"
"fmt"
"strings"
"time"

"github.com/google/uuid"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/models/oam/core/v1alpha1"
"github.com/layer5io/meshkit/utils/kubernetes"
"gorm.io/gorm"
)

type Host struct {
ID uuid.UUID `json:"-"`
Hostname string
Port int
Metadata string
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
IHost IHost `gorm:"-"`
}

func createHost(db *database.Handler, h Host) (uuid.UUID, error) {
h.ID = uuid.New()
err := db.Create(&h).Error
return h.ID, err
}

func (h *Host) AfterFind(tx *gorm.DB) error {
switch h.Hostname {
case "artifacthub":
h.IHost = ArtifactHub{}
case "kubernetes":
h.IHost = Kubernetes{}
default:
return ErrUnknownHost(errors.New("unable to find compatible host for the component"))
}
return nil
}

// Each host from where meshmodels can be generated needs to implement this interface
// HandleDependents, contains host specific logic for provisioning required CRDs/operators for corresponding components.
type IHost interface {
HandleDependents(comp v1alpha1.Component, kc *kubernetes.Client, isDeploy bool) (string, error)
String() string
}

type ArtifactHub struct{}

func (ah ArtifactHub) HandleDependents(comp v1alpha1.Component, kc *kubernetes.Client, isDeploy bool) (summary string, err error) {
source_uri := comp.Annotations[fmt.Sprintf("%s.model.source_uri", v1alpha1.MesheryAnnotationPrefix)]
act := kubernetes.UNINSTALL
if isDeploy {
act = kubernetes.INSTALL
}

if source_uri != "" {
err = kc.ApplyHelmChart(kubernetes.ApplyHelmChartConfig{
URL: source_uri,
Namespace: comp.Namespace,
CreateNamespace: true,
Action: act,
SkipUpgradeIfInstalled: true,
})
if err != nil {
if !isDeploy {
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)
} else {
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)
}
} else {
if !isDeploy {
summary = fmt.Sprintf("Undeployed helm chart%s: %s", strings.TrimSuffix(comp.Spec.Type, ".K8s"), comp.Name)
} else {
summary = fmt.Sprintf("Deployed helm chart%s: %s", strings.TrimSuffix(comp.Spec.Type, ".K8s"), comp.Name)
}
}
}
return summary, err
}

func (ah ArtifactHub) String() string {
return "artifacthub"
}

type Kubernetes struct{}

func (k Kubernetes) HandleDependents(comp v1alpha1.Component, kc *kubernetes.Client, isDeploy bool) (summary string, err error) {
return summary, err
}

func (k Kubernetes) String() string {
return "kubernetes"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package meshmodel
package registry

import (
"fmt"
Expand Down Expand Up @@ -31,20 +31,6 @@ type Registry struct {
CreatedAt time.Time
UpdatedAt time.Time
}
type Host struct {
ID uuid.UUID `json:"-"`
Hostname string
Port int
ContextID string
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}

func createHost(db *database.Handler, h Host) (uuid.UUID, error) {
h.ID = uuid.New()
err := db.Create(&h).Error
return h.ID, err
}

// Entity is referred as any type of schema managed by the registry
// ComponentDefinitions and PolicyDefinitions are examples of entities
Expand Down
4 changes: 4 additions & 0 deletions models/oam/core/v1alpha1/application_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func GetAnnotationsForWorkload(w v1alpha1.ComponentDefinition) map[string]string
res[strings.ReplaceAll(fmt.Sprintf("%s.%s", MesheryAnnotationPrefix, key), " ", "")] = v
}
}
sourceURI, ok := w.Model.Metadata["source_uri"].(string)
if ok {
res[fmt.Sprintf("%s.model.source_uri", MesheryAnnotationPrefix)] = sourceURI
}
res[fmt.Sprintf("%s.model.name", MesheryAnnotationPrefix)] = w.Model.Name
res[fmt.Sprintf("%s.k8s.APIVersion", MesheryAnnotationPrefix)] = w.APIVersion
res[fmt.Sprintf("%s.k8s.Kind", MesheryAnnotationPrefix)] = w.Kind
Expand Down
4 changes: 4 additions & 0 deletions utils/artifacthub/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (pkg AhPackage) GenerateComponents() ([]v1alpha1.ComponentDefinition, error
if comp.Metadata == nil {
comp.Metadata = make(map[string]interface{})
}
if comp.Model.Metadata == nil {
comp.Model.Metadata = make(map[string]interface{})
}
comp.Model.Metadata["source_uri"] = pkg.ChartUrl
comp.Model.Version = pkg.Version
comp.Model.Name = pkg.Name
comp.Model.Category = v1alpha1.Category{
Expand Down
5 changes: 4 additions & 1 deletion utils/kubernetes/apply-helm-chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ type ApplyHelmChartConfig struct {
// SkipCRDs while installation
SkipCRDs bool

// Skip upgrade, if release is already installed
SkipUpgradeIfInstalled bool

// URL is the url for charts
//
// Either ChartLocation or URL can be defined, if both of them
Expand Down Expand Up @@ -265,7 +268,7 @@ func (client *Client) ApplyHelmChart(cfg ApplyHelmChartConfig) error {

// Before installing a helm chart, check if it already exists in the cluster
// this is a workaround make the helm chart installation idempotent
if cfg.Action == INSTALL {
if cfg.Action == INSTALL && !cfg.SkipUpgradeIfInstalled {
if err := updateActionIfReleaseFound(actionConfig, &cfg, *helmChart); err != nil {
return ErrApplyHelmChart(err)
}
Expand Down
2 changes: 1 addition & 1 deletion utils/kubernetes/describe/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package describe
import "github.com/layer5io/meshkit/errors"

var (
ErrGetDescriberFuncCode = "not set"
ErrGetDescriberFuncCode = "11096"
)

func ErrGetDescriberFunc() error {
Expand Down

0 comments on commit ef0e2df

Please sign in to comment.