From 46bbbb64106d4ea07d6492f8daf4ebf5b3885ecb Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Tue, 10 Dec 2024 09:29:15 +0100 Subject: [PATCH] remove test and template commands --- cmd/command/plural/plural.go | 25 ------- pkg/api/client.go | 1 - pkg/api/installations.go | 20 ------ pkg/common/helm.go | 52 --------------- pkg/common/template.go | 105 ------------------------------ pkg/console/agent.go | 4 +- pkg/helm/helm.go | 26 -------- pkg/scaffold/template/template.go | 102 ----------------------------- 8 files changed, 1 insertion(+), 334 deletions(-) delete mode 100644 pkg/common/helm.go delete mode 100644 pkg/common/template.go delete mode 100644 pkg/scaffold/template/template.go diff --git a/cmd/command/plural/plural.go b/cmd/command/plural/plural.go index 16b6791d2..4698661aa 100644 --- a/cmd/command/plural/plural.go +++ b/cmd/command/plural/plural.go @@ -84,31 +84,6 @@ func (p *Plural) getCommands() []cli.Command { Action: common.LatestVersion(common.HandleServe), Category: "Workspace", }, - { - Name: "test", - Usage: "validate a values templace", - Action: common.LatestVersion(common.TestTemplate), - Category: "Publishing", - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "templateType", - Usage: "Determines the template type. Go template by default", - }, - }, - }, - { - Name: "template", - Aliases: []string{"tpl"}, - Usage: "templates a helm chart to be uploaded to plural", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "values", - Usage: "the values file", - }, - }, - Action: common.LatestVersion(common.HandleHelmTemplate), - Category: "Publishing", - }, } } diff --git a/pkg/api/client.go b/pkg/api/client.go index 79fa7f58d..0e0823483 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -43,7 +43,6 @@ type Client interface { CreateDomain(name string) error CreateInstallation(id string) (string, error) GetInstallation(name string) (*Installation, error) - GetInstallations() ([]*Installation, error) OIDCProvider(id string, attributes *OidcProviderAttributes) error CreateKeyBackup(attrs KeyBackupAttributes) error GetKeyBackup(name string) (*KeyBackup, error) diff --git a/pkg/api/installations.go b/pkg/api/installations.go index e8cce82e1..354143210 100644 --- a/pkg/api/installations.go +++ b/pkg/api/installations.go @@ -26,11 +26,6 @@ func (client *client) GetInstallation(name string) (*Installation, error) { } -func (client *client) DeleteInstallation(id string) error { - _, err := client.pluralClient.DeleteInstallation(client.ctx, id) - return err -} - func (client *client) CreateInstallation(id string) (string, error) { resp, err := client.pluralClient.CreateInstallation(client.ctx, id) if err != nil { @@ -110,21 +105,6 @@ func convertInstallation(installation *gqlclient.InstallationFragment) *Installa return i } -func (client *client) GetInstallations() ([]*Installation, error) { - result := make([]*Installation, 0) - - resp, err := client.pluralClient.GetInstallations(client.ctx) - if err != nil { - return result, err - } - - for _, edge := range resp.Installations.Edges { - result = append(result, convertInstallation(edge.Node)) - } - - return result, err -} - func (client *client) OIDCProvider(id string, attributes *OidcProviderAttributes) error { bindings := make([]*gqlclient.BindingAttributes, 0) for _, bind := range attributes.Bindings { diff --git a/pkg/common/helm.go b/pkg/common/helm.go deleted file mode 100644 index 459fd8d74..000000000 --- a/pkg/common/helm.go +++ /dev/null @@ -1,52 +0,0 @@ -package common - -import ( - "fmt" - "os" - - "github.com/pluralsh/plural-cli/pkg/helm" - scftmpl "github.com/pluralsh/plural-cli/pkg/scaffold/template" - "github.com/urfave/cli" - "sigs.k8s.io/yaml" -) - -func HandleHelmTemplate(c *cli.Context) error { - path := c.String("values") - f, err := scftmpl.TmpValuesFile(path) - if err != nil { - return err - } - - defer func(name string) { - _ = os.Remove(name) - }(f.Name()) - - name := "default" - namespace := "default" - actionConfig, err := helm.GetActionConfig(namespace) - if err != nil { - return err - } - values, err := getValues(f.Name()) - if err != nil { - return err - } - res, err := helm.Template(actionConfig, name, namespace, c.Args().Get(0), false, false, values) - if err != nil { - return err - } - fmt.Println(string(res)) - return nil -} - -func getValues(path string) (map[string]interface{}, error) { - values := make(map[string]interface{}) - valsContent, err := os.ReadFile(path) - if err != nil { - return nil, err - } - if err := yaml.Unmarshal(valsContent, &values); err != nil { - return nil, err - } - return values, nil -} diff --git a/pkg/common/template.go b/pkg/common/template.go deleted file mode 100644 index 7952e374c..000000000 --- a/pkg/common/template.go +++ /dev/null @@ -1,105 +0,0 @@ -package common - -import ( - "bytes" - "io" - "os" - - "github.com/pluralsh/gqlclient" - - "github.com/pluralsh/plural-cli/pkg/api" - "github.com/pluralsh/plural-cli/pkg/config" - lua "github.com/pluralsh/plural-cli/pkg/scaffold/template" - "github.com/pluralsh/plural-cli/pkg/template" - "github.com/urfave/cli" - "gopkg.in/yaml.v2" -) - -func TestTemplate(c *cli.Context) error { - conf := config.Read() - client := api.NewClient() - installations, _ := client.GetInstallations() - repoName := c.Args().Get(0) - templateTypeFlag := c.String("templateType") - templateType := gqlclient.TemplateTypeGotemplate - testTemplate, err := io.ReadAll(os.Stdin) - if err != nil { - return err - } - if templateTypeFlag != "" { - templateType = gqlclient.TemplateType(templateTypeFlag) - } - - for _, installation := range installations { - if installation.Repository.Name != repoName { - continue - } - - var output []byte - vals := genDefaultValues(conf, installation) - - if templateType == gqlclient.TemplateTypeLua { - output, err = luaTmpValues(string(testTemplate), vals) - if err != nil { - return err - } - } else { - output, err = goTmpValues(string(testTemplate), vals) - if err != nil { - return err - } - } - if _, err := os.Stdout.Write(output); err != nil { - return err - } - } - - return nil -} - -func genDefaultValues(conf config.Config, installation *api.Installation) map[string]interface{} { - return map[string]interface{}{ - "Values": installation.Context, - "License": installation.LicenseKey, - "Region": "region", - "Project": "example", - "Cluster": "cluster", - "Provider": "provider", - "Config": conf, - "Context": map[string]interface{}{}, - } -} - -func goTmpValues(valuesTmpl string, defaultValues map[string]interface{}) ([]byte, error) { - var buf bytes.Buffer - buf.Grow(5 * 1024) - tmpl, err := template.MakeTemplate(valuesTmpl) - if err != nil { - return nil, err - } - if err = tmpl.Execute(&buf, defaultValues); err != nil { - return nil, err - } - - return buf.Bytes(), nil -} - -func luaTmpValues(valuesTmpl string, defaultValues map[string]interface{}) ([]byte, error) { - output, err := lua.ExecuteLua(defaultValues, valuesTmpl) - if err != nil { - return nil, err - } - - return yaml.Marshal(output) -} - -type GrafanaDashboard struct { - Title string - Panels []struct { - Title string - Targets []struct { - Expr string - LegendFormat string - } - } -} diff --git a/pkg/console/agent.go b/pkg/console/agent.go index 2fd137371..62e4eccd3 100644 --- a/pkg/console/agent.go +++ b/pkg/console/agent.go @@ -45,9 +45,7 @@ func IsAlreadyAgentInstalled(k8sClient *kubernetes.Clientset) (bool, error) { func InstallAgent(url, token, namespace, version string, values map[string]interface{}) error { settings := cli.New() vals := map[string]interface{}{ - "secrets": map[string]string{ - "deployToken": token, - }, + "secrets": map[string]string{"deployToken": token}, "consoleUrl": url, } vals = algorithms.Merge(vals, values) diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 01c9543a3..5c950d39b 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -1,7 +1,6 @@ package helm import ( - "bytes" "context" "fmt" "log" @@ -14,7 +13,6 @@ import ( "github.com/pluralsh/plural-cli/pkg/utils" "gopkg.in/yaml.v2" "helm.sh/helm/v3/pkg/action" - "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/helmpath" @@ -48,30 +46,6 @@ func GetActionConfig(namespace string) (*action.Configuration, error) { return actionConfig, nil } -func Template(conf *action.Configuration, name, namespace, path string, isUpgrade, validate bool, values map[string]interface{}) ([]byte, error) { - // load chart from the path - chart, err := loader.Load(path) - if err != nil { - return nil, err - } - - client := action.NewInstall(conf) - client.DryRun = true - client.ReleaseName = name - client.Replace = true // Skip the name check - client.ClientOnly = !validate - client.IsUpgrade = isUpgrade - client.Namespace = namespace - client.IncludeCRDs = false - rel, err := client.Run(chart, values) - if err != nil { - return nil, err - } - var manifests bytes.Buffer - fmt.Fprintln(&manifests, strings.TrimSpace(rel.Manifest)) - return manifests.Bytes(), nil -} - func AddRepo(repoName, repoUrl string) error { repoFile := getEnvVar("HELM_REPOSITORY_CONFIG", helmpath.ConfigPath("repositories.yaml")) err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm) diff --git a/pkg/scaffold/template/template.go b/pkg/scaffold/template/template.go deleted file mode 100644 index 019b61619..000000000 --- a/pkg/scaffold/template/template.go +++ /dev/null @@ -1,102 +0,0 @@ -package template - -import ( - "bytes" - "fmt" - "os" - "strings" - - "github.com/pluralsh/plural-cli/pkg/config" - "github.com/pluralsh/plural-cli/pkg/output" - "github.com/pluralsh/plural-cli/pkg/template" - "github.com/pluralsh/plural-cli/pkg/utils" - "github.com/pluralsh/plural-cli/pkg/wkspace" - "gopkg.in/yaml.v2" -) - -func TmpValuesFile(path string) (f *os.File, err error) { - conf := config.Read() - if strings.HasSuffix(path, "lua") { - return luaTmpValuesFile(path, &conf) - } - - return goTmpValuesFile(path, &conf) - -} - -func luaTmpValuesFile(path string, conf *config.Config) (f *os.File, err error) { - valuesTmpl, err := utils.ReadFile(path) - if err != nil { - return - } - f, err = os.CreateTemp("", "values.yaml") - if err != nil { - return - } - defer func(f *os.File) { - _ = f.Close() - }(f) - - vals := genDefaultValues(conf) - - output, err := ExecuteLua(vals, valuesTmpl) - if err != nil { - return nil, err - } - - io, err := yaml.Marshal(output) - if err != nil { - return nil, err - } - - fmt.Println(string(io)) - _, err = f.Write(io) - if err != nil { - return nil, err - } - return -} - -func goTmpValuesFile(path string, conf *config.Config) (f *os.File, err error) { - valuesTmpl, err := utils.ReadFile(path) - if err != nil { - return - } - tmpl, err := template.MakeTemplate(valuesTmpl) - if err != nil { - return - } - - vals := genDefaultValues(conf) - var buf bytes.Buffer - - if err = tmpl.Execute(&buf, vals); err != nil { - return - } - - f, err = os.CreateTemp("", "values.yaml") - if err != nil { - return - } - defer func(f *os.File) { - _ = f.Close() - }(f) - - fmt.Println(buf.String()) - err = wkspace.FormatValues(f, buf.String(), output.New()) - return -} - -func genDefaultValues(conf *config.Config) map[string]interface{} { - return map[string]interface{}{ - "Values": map[string]interface{}{}, - "Configuration": map[string]map[string]interface{}{}, - "License": "example-license", - "Region": "region", - "Project": "example", - "Cluster": "cluster", - "Provider": "provider", - "Config": conf, - "Context": map[string]interface{}{}, - } -}