Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commands to list feature and editor charts #9

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ require (
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
k8s.io/component-base v0.30.2
k8s.io/klog/v2 v2.130.1
kmodules.xyz/client-go v0.30.15
kmodules.xyz/resource-metadata v0.18.14-0.20240920103441-9faf0d663661
kubedb.dev/apimachinery v0.47.0
kubeops.dev/scanner v0.0.19
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
Expand All @@ -49,15 +53,18 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand All @@ -69,12 +76,15 @@ require (
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vbatts/tar-split v0.11.3 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
Expand All @@ -92,12 +102,13 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.30.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240703190633-0aa61b46e8c2 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
kmodules.xyz/custom-resources v0.30.0 // indirect
kmodules.xyz/go-containerregistry v0.0.12 // indirect
sigs.k8s.io/controller-runtime v0.18.4 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
x-helm.dev/apimachinery v0.0.16 // indirect
)
60 changes: 57 additions & 3 deletions go.sum

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions pkg/cmds/list_editor_charts.go
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
}
148 changes: 148 additions & 0 deletions pkg/cmds/list_feature_charts.go
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
}
2 changes: 2 additions & 0 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func NewRootCmd() *cobra.Command {

rootCmd.AddCommand(NewCmdParseImage())
rootCmd.AddCommand(NewCmdListImages())
rootCmd.AddCommand(NewCmdListEditorCharts())
rootCmd.AddCommand(NewCmdListFeatureCharts())
rootCmd.AddCommand(NewCmdGenerateScripts())
rootCmd.AddCommand(NewCmdCompletion())
rootCmd.AddCommand(v.NewCmdVersion())
Expand Down
18 changes: 18 additions & 0 deletions vendor/github.com/Masterminds/goutils/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/Masterminds/goutils/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading