-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands to list feature and editor charts
Signed-off-by: Tamal Saha <[email protected]>
- Loading branch information
Showing
989 changed files
with
80,091 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmds | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"kmodules.xyz/resource-metadata/hub/resourceeditors" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
func NewCmdListEditorCharts() *cobra.Command { | ||
var ( | ||
apiGroups []string | ||
outDir string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "list-editor-charts", | ||
Short: "List all editor charts", | ||
DisableFlagsInUseLine: true, | ||
DisableAutoGenTag: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
images, err := ListEditorCharts(sets.New[string](apiGroups...)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := yaml.Marshal(images) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
filename := filepath.Join(outDir, "editor-charts.yaml") | ||
err = os.WriteFile(filename, data, 0o644) | ||
return err | ||
}, | ||
} | ||
|
||
cmd.Flags().StringSliceVar(&apiGroups, "apiGroup", nil, "API Group to be included in the output") | ||
cmd.Flags().StringVar(&outDir, "output-dir", "", "Output directory") | ||
_ = cobra.MarkFlagRequired(cmd.Flags(), "output-dir") | ||
|
||
return cmd | ||
} | ||
|
||
func ListEditorCharts(groups sets.Set[string]) ([]string, error) { | ||
images := sets.New[string]() | ||
|
||
for _, ed := range resourceeditors.List() { | ||
if !groups.Has(ed.Spec.Resource.Group) { | ||
continue | ||
} | ||
if ed.Spec.UI == nil { | ||
continue | ||
} | ||
if ed.Spec.UI.Options != nil { | ||
images.Insert(fmt.Sprintf("ghcr.io/appscode-charts/%s:%s", ed.Spec.UI.Options.Name, ed.Spec.UI.Options.Version)) | ||
} | ||
if ed.Spec.UI.Editor != nil { | ||
images.Insert(fmt.Sprintf("ghcr.io/appscode-charts/%s:%s", ed.Spec.UI.Editor.Name, ed.Spec.UI.Editor.Version)) | ||
} | ||
for _, action := range ed.Spec.UI.Actions { | ||
for _, item := range action.Items { | ||
if item.Editor != nil { | ||
images.Insert(fmt.Sprintf("ghcr.io/appscode-charts/%s:%s", item.Editor.Name, item.Editor.Version)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return sets.List(images), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmds | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"kmodules.xyz/client-go/tools/parser" | ||
|
||
"github.com/spf13/cobra" | ||
shell "gomodules.xyz/go-sh" | ||
"gopkg.in/yaml.v2" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
func NewCmdListFeatureCharts() *cobra.Command { | ||
var ( | ||
rootDir string | ||
outDir string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "list-feature-charts", | ||
Short: "List all feature charts", | ||
DisableFlagsInUseLine: true, | ||
DisableAutoGenTag: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
images, err := ListUICharts(rootDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := yaml.Marshal(images) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
filename := filepath.Join(outDir, "feature-charts.yaml") | ||
err = os.WriteFile(filename, data, 0o644) | ||
return err | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&rootDir, "root-dir", "", "Root directory") | ||
cmd.Flags().StringVar(&outDir, "output-dir", "", "Output directory") | ||
_ = cobra.MarkFlagRequired(cmd.Flags(), "output-dir") | ||
|
||
return cmd | ||
} | ||
|
||
type Skeleton struct { | ||
Spec struct { | ||
Chart struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
} `json:"chart"` | ||
} `json:"spec"` | ||
} | ||
|
||
type ChartInfo struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
AppVersion string `json:"app_version"` | ||
Description string `json:"description"` | ||
} | ||
|
||
func ListUICharts(rootDir string) ([]string, error) { | ||
sh := shell.NewSession() | ||
sh.SetDir("/tmp") | ||
sh.ShowCMD = true | ||
|
||
images := sets.New[string]() | ||
var out []byte | ||
var err error | ||
|
||
if rootDir == "" { | ||
// helm search repo opscenter-features -o json | ||
out, err = sh.Command("helm", "search", "repo", "opscenter-features", "-o", "json").Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var list []ChartInfo | ||
err = json.Unmarshal(out, &list) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(list) == 0 { | ||
return nil, errors.New("helm chart opscenter-features not found") | ||
} | ||
version := list[0].Version | ||
|
||
out, err = sh.Command("helm", "template", "oci://ghcr.io/appscode-charts/opscenter-features", "--version="+version).Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
out, err = sh.SetDir(rootDir).Command("helm", "template", "opscenter-features").Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
helmout, err := parser.ListResources(out) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, ri := range helmout { | ||
if ri.Object.GetKind() != "FeatureSet" && ri.Object.GetKind() != "Feature" { | ||
continue | ||
} | ||
|
||
chartName, found, err := unstructured.NestedString(ri.Object.UnstructuredContent(), "spec", "chart", "name") | ||
if err != nil { | ||
return nil, err | ||
} else if !found { | ||
continue | ||
} | ||
chartVersion, found, err := unstructured.NestedString(ri.Object.UnstructuredContent(), "spec", "chart", "version") | ||
if err != nil { | ||
return nil, err | ||
} else if !found { | ||
continue | ||
} | ||
|
||
images.Insert(fmt.Sprintf("ghcr.io/appscode-charts/%s:%s", chartName, chartVersion)) | ||
} | ||
|
||
return sets.List(images), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.