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 trust-zone helm values command #19

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions cmd/cofidectl/cmd/trustzone/helm/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2024 Cofide Limited.
// SPDX-License-Identifier: Apache-2.0

package helm

import (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

cmdcontext "github.com/cofide/cofidectl/cmd/cofidectl/cmd/context"
"github.com/cofide/cofidectl/internal/pkg/provider/helm"
"github.com/cofide/cofidectl/pkg/plugin"
)

type HelmCommand struct {
cmdCtx *cmdcontext.CommandContext
}

func NewHelmCommand(cmdCtx *cmdcontext.CommandContext) *HelmCommand {
return &HelmCommand{
cmdCtx: cmdCtx,
}
}

var helmRootCmdDesc = `
This command consists of multiple sub-commands to administer Cofide trust zone Helm configuration.
`

func (c *HelmCommand) GetRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "helm values [ARGS]",
Short: "Manage trust zone Helm configuration",
Long: helmRootCmdDesc,
Args: cobra.NoArgs,
}

cmd.AddCommand(
c.GetValuesCommand(),
)

return cmd
}

var helmValuesCmdDesc = `
This command will generate Helm values for a trust zone in the Cofide configuration state.
`

type valuesOpts struct {
outputPath string
}

func (c *HelmCommand) GetValuesCommand() *cobra.Command {
opts := valuesOpts{}
cmd := &cobra.Command{
Use: "values [ARGS]",
Short: "Generate Helm values for a trust zone",
Long: helmValuesCmdDesc,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
if err != nil {
return err
}

values, err := c.getValues(ds, args[0])
if err != nil {
return err
}

var writer io.Writer
if opts.outputPath == "-" {
writer = os.Stdout
} else {
f, err := os.Create(opts.outputPath)
if err != nil {
return err
}
defer f.Close()
writer = f
}
if err := writeValues(values, writer); err != nil {
return err
}
if opts.outputPath != "-" {
fmt.Printf("Wrote Helm values to %s\n", opts.outputPath)
}
return nil
},
}

f := cmd.Flags()
f.StringVar(&opts.outputPath, "output-file", "values.yaml", "Path of a file to write YAML values to, or - for stdout")

return cmd
}

// getValues returns the Helm values for a trust zone.
func (c *HelmCommand) getValues(ds plugin.DataSource, tzName string) (map[string]interface{}, error) {
trustZone, err := ds.GetTrustZone(tzName)
if err != nil {
return nil, err
}

generator := helm.NewHelmValuesGenerator(trustZone, ds)
values, err := generator.GenerateValues()
if err != nil {
return nil, err
}
return values, nil
}

// writeValues writes values in YAML format to the specified writer.
func writeValues(values map[string]interface{}, writer io.Writer) error {
encoder := yaml.NewEncoder(writer)
defer encoder.Close()
return encoder.Encode(values)
}
8 changes: 6 additions & 2 deletions cmd/cofidectl/cmd/trustzone/trustzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"strconv"

cmdcontext "github.com/cofide/cofidectl/cmd/cofidectl/cmd/context"
"github.com/cofide/cofidectl/cmd/cofidectl/cmd/trustzone/helm"
"github.com/manifoldco/promptui"

trust_provider_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/trust_provider/v1alpha1"
trust_zone_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/trust_zone/v1alpha1"
kubeutil "github.com/cofide/cofidectl/internal/pkg/kube"
"github.com/cofide/cofidectl/internal/pkg/provider/helm"
helmprovider "github.com/cofide/cofidectl/internal/pkg/provider/helm"
"github.com/cofide/cofidectl/internal/pkg/spire"
cofidectl_plugin "github.com/cofide/cofidectl/pkg/plugin"
"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -47,10 +48,13 @@ func (c *TrustZoneCommand) GetRootCommand() *cobra.Command {
Args: cobra.NoArgs,
}

helmCmd := helm.NewHelmCommand(c.cmdCtx)

cmd.AddCommand(
c.GetListCommand(),
c.GetAddCommand(),
c.GetStatusCommand(),
helmCmd.GetRootCommand(),
)

return cmd
Expand Down Expand Up @@ -204,7 +208,7 @@ func (c *TrustZoneCommand) status(ctx context.Context, source cofidectl_plugin.D
return err
}

prov, err := helm.NewHelmSPIREProvider(ctx, trustZone, nil, nil)
prov, err := helmprovider.NewHelmSPIREProvider(ctx, trustZone, nil, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.35.1
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.16.1
)

Expand Down Expand Up @@ -161,7 +162,6 @@ require (
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.0 // indirect
k8s.io/api v0.31.1
k8s.io/apiextensions-apiserver v0.31.0 // indirect
Expand Down
Loading