Skip to content

Commit

Permalink
Merge pull request #97 from iawia002/icon
Browse files Browse the repository at this point in the history
Handle icon correctly for the local environment
  • Loading branch information
iawia002 authored Jun 18, 2024
2 parents bffa364 + 475a3c8 commit d3a56f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
3 changes: 1 addition & 2 deletions pkg/extension/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ func LoadMetadata(path string) (*Metadata, error) {
if err = metadata.Validate(); err != nil {
return nil, err
}
if err = metadata.Init(); err != nil {
if err = metadata.Init(path); err != nil {
return nil, err
}
metadata.path = path
return metadata, nil
}

Expand Down
37 changes: 19 additions & 18 deletions pkg/extension/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var Categories = []string{
}

type Metadata struct {
path string

APIVersion string `json:"apiVersion" validate:"required"`
// The name of the chart. Required.
Name string `json:"name" validate:"required"`
Expand All @@ -59,6 +57,8 @@ type Metadata struct {
Namespace string `json:"namespace,omitempty"`
Images []string `json:"images,omitempty"`
ExternalDependencies []corev1alpha1.ExternalDependency `json:"externalDependencies,omitempty"`

base64EncodedIcon string
}

func validateLanguageCode(code corev1alpha1.LanguageCode) error {
Expand Down Expand Up @@ -94,19 +94,20 @@ func (md *Metadata) Validate() error {
return md.validateLanguageCode()
}

func (md *Metadata) Init() error {
func (md *Metadata) Init(root string) error {
if md.InstallationMode == "" {
md.InstallationMode = corev1alpha1.InstallationModeHostOnly
}
return nil
}

func (md *Metadata) ToChartYaml() (*chart.Metadata, error) {
icon, err := md.loadIcon()
icon, err := encodeIcon(root, md.Icon)
if err != nil {
return nil, err
return err
}
md.base64EncodedIcon = icon
return nil
}

func (md *Metadata) ToChartYaml() (*chart.Metadata, error) {
var c = chart.Metadata{
APIVersion: chart.APIVersionV2,
Name: md.Name,
Expand All @@ -117,27 +118,27 @@ func (md *Metadata) ToChartYaml() (*chart.Metadata, error) {
Home: md.Home,
Dependencies: md.Dependencies,
Description: string(md.Description[corev1alpha1.DefaultLanguageCode]),
Icon: icon,
Icon: md.base64EncodedIcon,
Maintainers: md.Maintainers,
}
return &c, nil
}

func (md *Metadata) loadIcon() (string, error) {
func encodeIcon(root, iconPath string) (string, error) {
// If the icon is url or base64, you can use it directly.
// Otherwise, load the file encoding as base64
if strings.HasPrefix(md.Icon, "http://") ||
strings.HasPrefix(md.Icon, "https://") ||
strings.HasPrefix(md.Icon, "data:image") {
return md.Icon, nil
if strings.HasPrefix(iconPath, "http://") ||
strings.HasPrefix(iconPath, "https://") ||
strings.HasPrefix(iconPath, "data:image") {
return iconPath, nil
}
content, err := os.ReadFile(path.Join(md.path, md.Icon))
content, err := os.ReadFile(path.Join(root, iconPath))
if err != nil {
return "", err
}
var base64Encoding string

mimeType := mime.TypeByExtension(path.Ext(md.Icon))
mimeType := mime.TypeByExtension(path.Ext(iconPath))
if mimeType == "" {
mimeType = http.DetectContentType(content)
}
Expand Down Expand Up @@ -191,7 +192,7 @@ func (ext *Extension) ToKubernetesResources() []runtimeclient.Object {
ExtensionInfo: corev1alpha1.ExtensionInfo{
Description: ext.Metadata.Description,
DisplayName: ext.Metadata.DisplayName,
Icon: ext.Metadata.Icon,
Icon: ext.Metadata.base64EncodedIcon,
Provider: ext.Metadata.Provider,
Created: metav1.Now(),
},
Expand All @@ -217,7 +218,7 @@ func (ext *Extension) ToKubernetesResources() []runtimeclient.Object {
ExtensionInfo: corev1alpha1.ExtensionInfo{
Description: ext.Metadata.Description,
DisplayName: ext.Metadata.DisplayName,
Icon: ext.Metadata.Icon,
Icon: ext.Metadata.base64EncodedIcon,
Provider: ext.Metadata.Provider,
Created: metav1.Now(),
},
Expand Down

0 comments on commit d3a56f4

Please sign in to comment.