Skip to content

Commit

Permalink
Merge pull request kubernetes#257 from seans3/generic-secret
Browse files Browse the repository at this point in the history
Kinflate add generic secret command.
  • Loading branch information
k8s-ci-robot authored Feb 6, 2018
2 parents 9b8afdf + f9f329b commit c82e0c3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
55 changes: 48 additions & 7 deletions pkg/kinflate/commands/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ import (
"fmt"
"io"

manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
"k8s.io/kubectl/pkg/kinflate/configmapandsecret"
"k8s.io/kubectl/pkg/kinflate/constants"
kutil "k8s.io/kubectl/pkg/kinflate/util"
"k8s.io/kubectl/pkg/kinflate/util/fs"

"github.com/spf13/cobra"
)

func newCmdAddSecretGeneric(errOut io.Writer) *cobra.Command {
func newCmdAddSecretGeneric(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
var config dataConfig
cmd := &cobra.Command{
Use: "generic NAME [--type=string] [--from-file=[key=]source] [--from-literal=key1=value1]",
Expand All @@ -45,14 +51,20 @@ func newCmdAddSecretGeneric(errOut io.Writer) *cobra.Command {
return err
}

if len(args) != 1 {
return fmt.Errorf("error: exactly one NAME is required, got %d", len(args))
loader := kutil.ManifestLoader{FS: fsys}
m, err := loader.Read(constants.KubeManifestFileName)
if err != nil {
return err
}
config.Name = args[0]

// TODO(apelisse,droot): Do something with that config.
// Add the generic secret to the manifest.
err = addGenericSecret(m, config)
if err != nil {
return err
}

return nil
// Write out the manifest with added secret.
return loader.Write(constants.KubeManifestFileName, m)
},
}

Expand All @@ -63,6 +75,35 @@ func newCmdAddSecretGeneric(errOut io.Writer) *cobra.Command {
return cmd
}

func addGenericSecret(m *manifest.Manifest, config dataConfig) error {
gs := getOrCreateGenericSecret(m, config.Name)

err := mergeData(&gs.DataSources, config)
if err != nil {
return err
}

// Validate manifest's generic secret by creating a generic secret.
_, _, err = configmapandsecret.MakeGenericSecretAndGenerateName(*gs)
if err != nil {
return err
}

return nil
}

func getOrCreateGenericSecret(m *manifest.Manifest, name string) *manifest.GenericSecret {
for i, s := range m.GenericSecrets {
if name == s.Name {
return &m.GenericSecrets[i]
}
}
// generic secret not found, create new one and add it to the manifest.
gs := manifest.GenericSecret{Name: name}
m.GenericSecrets = append(m.GenericSecrets, gs)
return &m.GenericSecrets[len(m.GenericSecrets)-1]
}

type addTLSSecret struct {
// Name of secret (required)
Name string
Expand Down Expand Up @@ -129,7 +170,7 @@ func NewCmdAddSecret(errOut io.Writer) *cobra.Command {
kinflate secret tls my-tls-secret --cert=cert/path.cert --key=key/path.key
`,
}
cmd.AddCommand(newCmdAddSecretGeneric(errOut))
cmd.AddCommand(newCmdAddSecretGeneric(errOut, fs.MakeRealFS()))
cmd.AddCommand(newCmdAddSecretTLS(errOut))

return cmd
Expand Down
37 changes: 37 additions & 0 deletions pkg/kinflate/commands/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,47 @@ package commands

import (
"testing"

manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
)

func TestNewAddSecretIsNotNil(t *testing.T) {
if NewCmdAddSecret(nil) == nil {
t.Fatal("NewCmdAddSecret shouldn't be nil")
}
}

func TestGetOrCreateGenericSecret(t *testing.T) {
gsName := "test-generic-secret"

manifest := &manifest.Manifest{
NamePrefix: "test-name-prefix",
}

if len(manifest.GenericSecrets) != 0 {
t.Fatal("Initial manifest should not have any genericsecrets")
}
gs := getOrCreateGenericSecret(manifest, gsName)

if gs == nil {
t.Fatalf("GenericSecret should always be non-nil")
}

if len(manifest.GenericSecrets) != 1 {
t.Fatalf("Manifest should have newly created generic secret")
}

if &manifest.GenericSecrets[len(manifest.GenericSecrets)-1] != gs {
t.Fatalf("Pointer address for newly inserted generic secret should be same")
}

existingGS := getOrCreateGenericSecret(manifest, gsName)

if existingGS != gs {
t.Fatalf("should have returned an existing generic secret with name: %v", gsName)
}

if len(manifest.GenericSecrets) != 1 {
t.Fatalf("Should not insert generic secret for an existing name: %v", gsName)
}
}

0 comments on commit c82e0c3

Please sign in to comment.