Skip to content

Commit

Permalink
Merge pull request #107 from ImitationImmortal/lint
Browse files Browse the repository at this point in the history
fix: the metadata should not modify in lint
  • Loading branch information
wansir authored Sep 2, 2024
2 parents 4e07c30 + 4d0a418 commit c5e55e9
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 271 deletions.
16 changes: 15 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ type createOptions struct {
from string
}

var Categories = []string{
"ai-machine-learning",
"computing",
"database",
"dev-tools",
"integration-delivery",
"observability",
"networking",
"security",
"storage",
"streaming-messaging",
"other",
}

func createExtensionCmd() *cobra.Command {
o := &createOptions{}

Expand All @@ -52,7 +66,7 @@ func (o *createOptions) run(_ *cobra.Command, _ []string) error {

categoryPromptContent := selectPromptContent{
text: fmt.Sprintf("What category does %s belong to?", name),
items: extension.Categories,
items: Categories,
}
category := promptGetSelect(categoryPromptContent)

Expand Down
11 changes: 4 additions & 7 deletions cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"helm.sh/helm/v3/pkg/chartutil"
"sigs.k8s.io/yaml"

"github.com/kubesphere/ksbuilder/pkg/extension"
"github.com/kubesphere/ksbuilder/pkg/api"
)

type packageOptions struct {
Expand Down Expand Up @@ -48,15 +48,12 @@ func (o *packageOptions) packageCmd(_ *cobra.Command, args []string) error {
return err
}

metadata, err := extension.LoadMetadata(p)
metadata, err := api.LoadMetadata(p)
if err != nil {
return err
}
chartYaml, err := metadata.ToChartYaml()
if err != nil {
return err
}
chartMetadata, err := yaml.Marshal(chartYaml)

chartMetadata, err := yaml.Marshal(metadata.ToChartYaml())
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
"kubesphere.io/client-go/kubesphere/scheme"

"github.com/kubesphere/ksbuilder/pkg/api"
"github.com/kubesphere/ksbuilder/pkg/extension"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func publishExtensionCmd() *cobra.Command {
func (o *publishOptions) publish(_ *cobra.Command, args []string) error {
// load extension
fmt.Printf("publish extension %s\n", args[0])
var ext *extension.Extension
var ext *api.Extension
var err error
if strings.HasPrefix(args[0], "oci://") {
ext, err = extension.LoadFromHelm(args[0])
Expand Down
15 changes: 6 additions & 9 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"helm.sh/helm/v3/pkg/chartutil"
"sigs.k8s.io/yaml"

"github.com/kubesphere/ksbuilder/pkg/api"
"github.com/kubesphere/ksbuilder/pkg/cloud"
"github.com/kubesphere/ksbuilder/pkg/extension"
)
Expand Down Expand Up @@ -47,12 +48,12 @@ func (o *pushOptions) push(_ *cobra.Command, args []string) error {
return err
}

metadata, err := extension.LoadMetadata(tempDir, extension.WithEncodeIcon(false))
metadata, err := api.LoadMetadata(tempDir, api.WithEncodeIcon(false))
if err != nil {
return err
}
// upload images to cloud
if extension.IsLocalFile(metadata.Icon) {
if api.IsLocalFile(metadata.Icon) {
resp, err := client.UploadFiles(metadata.Name, metadata.Version, tempDir, metadata.Icon)
if err != nil {
return err
Expand All @@ -62,7 +63,7 @@ func (o *pushOptions) push(_ *cobra.Command, args []string) error {
screenshots := make([]string, 0)
localScreenshots := make([]string, 0)
for _, p := range metadata.Screenshots {
if extension.IsLocalFile(p) {
if api.IsLocalFile(p) {
localScreenshots = append(localScreenshots, p)
} else {
screenshots = append(screenshots, p)
Expand All @@ -89,15 +90,11 @@ func (o *pushOptions) push(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
if err = os.WriteFile(tempDir+"/"+extension.MetadataFilename, data, 0644); err != nil {
if err = os.WriteFile(tempDir+"/"+api.MetadataFilename, data, 0644); err != nil {
return err
}

chartYaml, err := metadata.ToChartYaml()
if err != nil {
return err
}
chartMetadata, err := yaml.Marshal(chartYaml)
chartMetadata, err := yaml.Marshal(metadata.ToChartYaml())
if err != nil {
return err
}
Expand Down
117 changes: 117 additions & 0 deletions pkg/api/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package api

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1alpha1 "kubesphere.io/api/core/v1alpha1"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

const (
KubeSphereSystem = "kubesphere-system"
ConfigMapDataKey = "chart.tgz"
KubeSphereManaged = "kubesphere.io/managed"
)

type Extension struct {
Metadata *Metadata
// ChartURL valid when the chart source online.
ChartURL string
// ChartData valid when the chart source local.
ChartData []byte
}

func (ext *Extension) ToKubernetesResources() []runtimeclient.Object {
var resources = []runtimeclient.Object{
&corev1alpha1.Extension{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubesphere.io/v1alpha1",
Kind: "Extension",
},
ObjectMeta: metav1.ObjectMeta{
Name: ext.Metadata.Name,
Labels: map[string]string{
corev1alpha1.CategoryLabel: ext.Metadata.Category,
KubeSphereManaged: "true",
},
},
Spec: corev1alpha1.ExtensionSpec{
ExtensionInfo: corev1alpha1.ExtensionInfo{
Description: ext.Metadata.Description,
DisplayName: ext.Metadata.DisplayName,
Icon: ext.Metadata.Icon,
Provider: ext.Metadata.Provider,
Created: metav1.Now(),
},
},
Status: corev1alpha1.ExtensionStatus{
RecommendedVersion: ext.Metadata.Version,
},
}}
extensionVersion := &corev1alpha1.ExtensionVersion{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubesphere.io/v1alpha1",
Kind: "ExtensionVersion",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", ext.Metadata.Name, ext.Metadata.Version),
Labels: map[string]string{
corev1alpha1.ExtensionReferenceLabel: ext.Metadata.Name,
corev1alpha1.CategoryLabel: ext.Metadata.Category,
},
Annotations: ext.Metadata.Annotations,
},
Spec: corev1alpha1.ExtensionVersionSpec{
InstallationMode: ext.Metadata.InstallationMode,
ExtensionInfo: corev1alpha1.ExtensionInfo{
Description: ext.Metadata.Description,
DisplayName: ext.Metadata.DisplayName,
Icon: ext.Metadata.Icon,
Provider: ext.Metadata.Provider,
Created: metav1.Now(),
},
Docs: ext.Metadata.Docs,
Namespace: ext.Metadata.Namespace,
Home: ext.Metadata.Home,
Keywords: ext.Metadata.Keywords,
KSVersion: ext.Metadata.KSVersion,
KubeVersion: ext.Metadata.KubeVersion,
Sources: ext.Metadata.Sources,
Version: ext.Metadata.Version,
Category: ext.Metadata.Category,
Screenshots: ext.Metadata.Screenshots,
ExternalDependencies: ext.Metadata.ExternalDependencies,
},
}
if ext.ChartURL != "" {
extensionVersion.Spec.ChartURL = ext.ChartURL
resources = append(resources, extensionVersion)
} else {
configmap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("extension-%s-%s-chart", ext.Metadata.Name, ext.Metadata.Version),
Namespace: KubeSphereSystem,
},
BinaryData: map[string][]byte{
ConfigMapDataKey: ext.ChartData,
},
}
extensionVersion.Spec.ChartDataRef = &corev1alpha1.ConfigMapKeyRef{
Namespace: configmap.Namespace,
ConfigMapKeySelector: corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: configmap.Name,
},
Key: ConfigMapDataKey,
},
}
resources = append(resources, extensionVersion, configmap)
}
return resources
}
Loading

0 comments on commit c5e55e9

Please sign in to comment.