Skip to content

Commit

Permalink
Add command for printing a starter template
Browse files Browse the repository at this point in the history
  • Loading branch information
kroonprins committed Jan 22, 2023
1 parent 9b4aeea commit 9ddb0b4
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ Examples:
kube-create-secret create -f template.yaml
kube-create-secret re-create -f secret.yaml
kube-create-secret show -f secret.yaml
kube-create-secret new


Available Commands:
create Create a secret from a SecretTemplate definition.
re-create Re-create a secret from a Secret that was previously created with kube-create-secret.
show Show the template for a Secret that was previously created with kube-create-secret.
new Print starter template.
```

- [Examples](#examples)
Expand Down
6 changes: 4 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/kroonprins/kube-create-secret/cmd/create"
"github.com/kroonprins/kube-create-secret/cmd/new"
re_create "github.com/kroonprins/kube-create-secret/cmd/re-create"
"github.com/kroonprins/kube-create-secret/cmd/show"
"github.com/spf13/cobra"
Expand All @@ -21,7 +22,8 @@ func newRootCmd() *cobra.Command {
Long: `Utility for creating kubernetes secrets.`,
Example: " kube-create-secret create -f template.yaml\n" +
" kube-create-secret re-create -f secret.yaml\n" +
" kube-create-secret show -f secret.yaml\n",
" kube-create-secret show -f secret.yaml\n" +
" kube-create-secret new\n",
}

versionCmd := &cobra.Command{
Expand All @@ -34,7 +36,7 @@ func newRootCmd() *cobra.Command {
},
}

rootCmd.AddCommand(versionCmd, create.Cmd, re_create.Cmd, show.Cmd)
rootCmd.AddCommand(versionCmd, create.Cmd, re_create.Cmd, show.Cmd, new.Cmd)

return rootCmd
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/new/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package new

import (
goflag "flag"

"github.com/bitnami-labs/sealed-secrets/pkg/pflagenv"
"github.com/kroonprins/kube-create-secret/cmd/constants"
"github.com/kroonprins/kube-create-secret/pkg/core"
"github.com/kroonprins/kube-create-secret/pkg/output/marshal"
"github.com/kroonprins/kube-create-secret/pkg/output/write"
"github.com/kroonprins/kube-create-secret/pkg/types"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag/v2"
)

var (
config = *core.NewConfig()
templateType types.StarterTemplateType
)

func init() {
fs := Cmd.PersistentFlags()
fs.VarP(enumflag.NewSlice(&config.OutputFormats, "output", types.FormatIds, enumflag.EnumCaseInsensitive), "output", "o", "Output format. One of: (json, yaml). If not specified the format of the input is used.")
fs.VarP(enumflag.New(&templateType, "type", types.StarterTemplateTypes, enumflag.EnumCaseInsensitive), "type", "t", "Template type. One of: (data, stringData, tls).")

fs.AddGoFlagSet(goflag.CommandLine)
pflagenv.SetFlagsFromEnv(constants.FLAGENV_PREFIX, fs)
}

var Cmd = &cobra.Command{
Use: "new",
Aliases: []string{"n"},
Short: "Print starter template.",
Long: `Print starter template.`,
Example: " kube-create-secret new\n" +
" kube-create-secret new -t tls -o json\n",
RunE: func(cmd *cobra.Command, args []string) error {
core.Marshallers = []core.Marshaller{
marshal.NewYamlMarshaller(),
marshal.NewJsonMarshaller(),
}
core.OutputWriters = []core.OutputWriter{
write.NewStdOutWriter(),
}

return core.StarterTemplate(config, templateType)
},
}
15 changes: 15 additions & 0 deletions pkg/core/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"

"github.com/kroonprins/kube-create-secret/pkg/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -74,6 +75,20 @@ func ShowTemplate(config Config) error {
return write(config, secretTemplates)
}

func StarterTemplate(config Config, templateType types.StarterTemplateType) error {
secretTemplate, err := NewStarterTemplate(config, templateType)
if err != nil {
return err
}
debug(secretTemplate, "Secret template")

if len(config.OutputFormats) == 0 {
config.OutputFormats = []types.Format{types.YAML}
}

return write(config, []types.SecretTemplate{*secretTemplate})
}

func debug(object interface{}, objectType string) {
if klog.V(1).Enabled() {
marshalled, err := json.Marshal(object)
Expand Down
91 changes: 91 additions & 0 deletions pkg/core/startertemplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package core

import (
"fmt"

"github.com/kroonprins/kube-create-secret/pkg/constants"
"github.com/kroonprins/kube-create-secret/pkg/types"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func NewStarterTemplate(config Config, templateType types.StarterTemplateType) (*types.SecretTemplate, error) {
if templateType == types.DATA {
return &types.SecretTemplate{
TypeMeta: metav1.TypeMeta{
Kind: constants.SECRET_TEMPLATE_KIND,
APIVersion: constants.SECRET_TEMPLATE_API_VERSION,
},
ObjectMeta: metav1.ObjectMeta{
Name: "[insert template name]",
},
Spec: types.SecretTemplateSpec{
Secret: corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "[insert secret name]",
Namespace: "[insert namespace] (optional)",
},
Type: corev1.SecretTypeOpaque,
},
Data: map[string]string{
"[insert key]": "ref+[insert provider]://[insert provider config]",
},
},
}, nil
} else if templateType == types.STRINGDATA {
return &types.SecretTemplate{
TypeMeta: metav1.TypeMeta{
Kind: constants.SECRET_TEMPLATE_KIND,
APIVersion: constants.SECRET_TEMPLATE_API_VERSION,
},
ObjectMeta: metav1.ObjectMeta{
Name: "[insert template name]",
},
Spec: types.SecretTemplateSpec{
Secret: corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "[insert secret name]",
Namespace: "[insert namespace] (optional)",
},
Type: corev1.SecretTypeOpaque,
},
StringData: map[string]string{
"[insert key]": "ref+[insert provider]://[insert provider config]",
},
},
}, nil
} else if templateType == types.TLS {
return &types.SecretTemplate{
TypeMeta: metav1.TypeMeta{
Kind: constants.SECRET_TEMPLATE_KIND,
APIVersion: constants.SECRET_TEMPLATE_API_VERSION,
},
ObjectMeta: metav1.ObjectMeta{
Name: "[insert template name]",
},
Spec: types.SecretTemplateSpec{
Secret: corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "[insert secret name]",
Namespace: "[insert namespace] (optional)",
},
Type: corev1.SecretTypeTLS,
},
Tls: &types.Tls{
Pkcs12: "ref+[insert provider]://[insert provider config]",
Password: "ref+[insert provider]://[insert provider config] (optional)",
Name: "[insert name] (optional)",
KeyConfig: &types.TlsKeyConfig{
Name: "[insert name] (optional)",
},
CrtConfig: &types.TlsCrtConfig{
Name: "[insert name (optional)]",
ChainDelimiter: "[insert delimiter (optional)]",
},
},
},
}, nil
}
return nil, fmt.Errorf("unhandled template type %s", types.StarterTemplateTypes[templateType][0])
}
14 changes: 14 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ var FormatIds = map[Format][]string{
SEALED_SECRET: {"sealed-secret"},
}

type StarterTemplateType int

const (
DATA StarterTemplateType = iota
STRINGDATA
TLS
)

var StarterTemplateTypes = map[StarterTemplateType][]string{
DATA: {"data"},
STRINGDATA: {"stringData"},
TLS: {"tls"},
}

type SecretTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down

0 comments on commit 9ddb0b4

Please sign in to comment.