Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Add distributed claim store through Kubernetes' secrets
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
  • Loading branch information
ulyssessouza committed Feb 3, 2020
1 parent 6d698be commit 780f3d7
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 45 deletions.
14 changes: 10 additions & 4 deletions internal/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (

"github.com/deislabs/cnab-go/action"
"github.com/deislabs/cnab-go/bundle"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/app/internal"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/inspect"
"github.com/docker/app/internal/packager"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
"github.com/docker/app/internal/store"
)

const inspectExample = `- $ docker app inspect my-running-app
Expand Down Expand Up @@ -43,7 +45,11 @@ func inspectCmd(dockerCli command.Cli, installerContext *cliopts.InstallerContex
}

func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOptions, installerContext *cliopts.InstallerContextOptions) error {
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext())
orchestrator, err := store.GetOrchestrator(dockerCli)
if err != nil {
return err

Check warning on line 50 in internal/commands/inspect.go

View check run for this annotation

Codecov / codecov/patch

internal/commands/inspect.go#L50

Added line #L50 was not covered by tests
}
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext(), orchestrator)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
"time"

"github.com/deislabs/cnab-go/action"
"github.com/docker/app/internal"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/store"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
Expand All @@ -22,6 +18,11 @@ import (
"github.com/docker/go/canonical/json"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/docker/app/internal"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/store"
)

var (
Expand Down Expand Up @@ -71,8 +72,12 @@ func runList(dockerCli command.Cli, opts listOptions, installerContext *cliopts.
if err != nil {
return err
}
orchestrator, err := store.GetOrchestrator(dockerCli)
if err != nil {
return err

Check warning on line 77 in internal/commands/list.go

View check run for this annotation

Codecov / codecov/patch

internal/commands/list.go#L77

Added line #L77 was not covered by tests
}
targetContext := dockerCli.CurrentContext()
installationStore, err := appstore.InstallationStore(targetContext)
installationStore, err := appstore.InstallationStore(targetContext, orchestrator)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion internal/commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func removeCmd(dockerCli command.Cli, installerContext *cliopts.InstallerContext
Example: `$ docker app rm myrunningapp`,
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext())
orchestrator, err := store.GetOrchestrator(dockerCli)
if err != nil {
return err

Check warning on line 39 in internal/commands/remove.go

View check run for this annotation

Codecov / codecov/patch

internal/commands/remove.go#L39

Added line #L39 was not covered by tests
}
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext(), orchestrator)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"os"

"github.com/deislabs/cnab-go/claim"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/docker/app/internal"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/commands/build"
"github.com/docker/app/internal/commands/image"
"github.com/docker/app/internal/store"
appstore "github.com/docker/app/internal/store"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type mainOptions struct {
Expand Down Expand Up @@ -105,12 +106,12 @@ func muteDockerCli(dockerCli command.Cli) func() {
}
}

func prepareStores(targetContext string) (store.ImageStore, store.InstallationStore, store.CredentialStore, error) {
func prepareStores(targetContext string, orchestrator command.Orchestrator) (store.ImageStore, store.InstallationStore, store.CredentialStore, error) {
appstore, err := store.NewApplicationStore(config.Dir())
if err != nil {
return nil, nil, nil, err
}
installationStore, err := appstore.InstallationStore(targetContext)
installationStore, err := appstore.InstallationStore(targetContext, orchestrator)
if err != nil {
return nil, nil, nil, err
}
Expand Down
24 changes: 13 additions & 11 deletions internal/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import (
"fmt"
"os"

"github.com/docker/app/internal/packager"

"github.com/docker/app/internal/image"

"github.com/deislabs/cnab-go/driver"
"github.com/docker/app/internal/cliopts"

"github.com/deislabs/cnab-go/action"
"github.com/deislabs/cnab-go/credentials"
bdl "github.com/docker/app/internal/bundle"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/store"
"github.com/deislabs/cnab-go/driver"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

bdl "github.com/docker/app/internal/bundle"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/image"
"github.com/docker/app/internal/packager"
"github.com/docker/app/internal/store"
)

type runOptions struct {
Expand Down Expand Up @@ -103,7 +101,11 @@ func runBundle(dockerCli command.Cli, bndl *image.AppImage, opts runOptions, ins
if err := packager.CheckAppVersion(dockerCli.Err(), bndl.Bundle); err != nil {
return err
}
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext())
orchestrator, err := store.GetOrchestrator(dockerCli)
if err != nil {
return err

Check warning on line 106 in internal/commands/run.go

View check run for this annotation

Codecov / codecov/patch

internal/commands/run.go#L106

Added line #L106 was not covered by tests
}
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext(), orchestrator)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions internal/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"fmt"
"os"

"github.com/deislabs/cnab-go/driver"

"github.com/deislabs/cnab-go/action"
"github.com/deislabs/cnab-go/credentials"
"github.com/deislabs/cnab-go/driver"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/app/internal/bundle"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/packager"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
"github.com/docker/app/internal/store"
)

type updateOptions struct {
Expand Down Expand Up @@ -41,7 +42,11 @@ func updateCmd(dockerCli command.Cli, installerContext *cliopts.InstallerContext
}

func runUpdate(dockerCli command.Cli, installationName string, opts updateOptions, installerContext *cliopts.InstallerContextOptions) error {
imageStore, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext())
orchestrator, err := store.GetOrchestrator(dockerCli)
if err != nil {
return err

Check warning on line 47 in internal/commands/update.go

View check run for this annotation

Codecov / codecov/patch

internal/commands/update.go#L47

Added line #L47 was not covered by tests
}
imageStore, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext(), orchestrator)
if err != nil {
return err
}
Expand Down
31 changes: 25 additions & 6 deletions internal/store/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"os"
"path/filepath"

"github.com/deislabs/cnab-go/utils/crud"
cnabCrud "github.com/deislabs/cnab-go/utils/crud"
"github.com/docker/cli/cli/command"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"

appCrud "github.com/docker/app/internal/store/crud"
)

const (
Expand Down Expand Up @@ -51,12 +54,28 @@ func NewApplicationStore(configDir string) (*ApplicationStore, error) {
}

// InstallationStore initializes and returns a context based installation store
func (a ApplicationStore) InstallationStore(context string) (InstallationStore, error) {
path := filepath.Join(a.path, InstallationStoreDirectory, makeDigestedDirectory(context))
if err := os.MkdirAll(path, 0755); err != nil {
return nil, errors.Wrapf(err, "failed to create installation store directory for context %q", context)
func (a ApplicationStore) InstallationStore(context string, orchestrator command.Orchestrator) (InstallationStore, error) {
switch {
// FIXME What if orchestrator.HasKubernetes() and still want to use local store?
case orchestrator.HasKubernetes():

Check warning on line 60 in internal/store/app.go

View check run for this annotation

Codecov / codecov/patch

internal/store/app.go#L60

Added line #L60 was not covered by tests
// FIXME Get this namespace, labelKey and labelValue dynamically through cli opts
k8sStore, err := appCrud.NewKubernetesSecretsStore(
appCrud.DefaultKubernetesNamespace,
appCrud.LabelKV{
appCrud.DefaultSecretLabelKey,
appCrud.DefaultSecretLabelValue,
})
if err != nil {
return nil, err

Check warning on line 69 in internal/store/app.go

View check run for this annotation

Codecov / codecov/patch

internal/store/app.go#L62-L69

Added lines #L62 - L69 were not covered by tests
}
return &installationStore{store: k8sStore}, nil

Check warning on line 71 in internal/store/app.go

View check run for this annotation

Codecov / codecov/patch

internal/store/app.go#L71

Added line #L71 was not covered by tests
default:
path := filepath.Join(a.path, InstallationStoreDirectory, makeDigestedDirectory(context))
if err := os.MkdirAll(path, 0755); err != nil {
return nil, errors.Wrapf(err, "failed to create installation store directory for context %q", context)

Check warning on line 75 in internal/store/app.go

View check run for this annotation

Codecov / codecov/patch

internal/store/app.go#L75

Added line #L75 was not covered by tests
}
return &installationStore{store: cnabCrud.NewFileSystemStore(path, "json")}, nil
}
return &installationStore{store: crud.NewFileSystemStore(path, "json")}, nil
}

// CredentialStore initializes and returns a context based credential store
Expand Down
3 changes: 2 additions & 1 deletion internal/store/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package store
import (
"testing"

"github.com/docker/cli/cli/command"
"gotest.tools/assert"
"gotest.tools/fs"
)
Expand All @@ -17,7 +18,7 @@ func TestNewApplicationStoreInitializesDirectories(t *testing.T) {
assert.Equal(t, appstore.path, dockerConfigDir.Join("app"))

// an installation store is created per context
_, err = appstore.InstallationStore("my-context")
_, err = appstore.InstallationStore("my-context", command.OrchestratorSwarm)
assert.NilError(t, err)

// a credential store is created per context
Expand Down
Loading

0 comments on commit 780f3d7

Please sign in to comment.