Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Refactor pkg/providers #252

Merged
merged 5 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion cmd/ignite-spawn/ignite-spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
"github.com/weaveworks/ignite/pkg/container/prometheus"
"github.com/weaveworks/ignite/pkg/dmlegacy"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/providers/spawn"
patchutil "github.com/weaveworks/ignite/pkg/util/patch"
)

func main() {
// Populate the providers
cmdutil.CheckErr(providers.Populate(providers.Providers))
cmdutil.CheckErr(providers.Populate(spawn.Providers))
RunIgniteSpawn()
}

Expand Down
11 changes: 5 additions & 6 deletions cmd/ignite/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/weaveworks/ignite/cmd/ignite/cmd/cmdutil"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/providers/ignite"
)

func NewCmdDaemon(out io.Writer) *cobra.Command {
Expand All @@ -19,7 +20,7 @@ func NewCmdDaemon(out io.Writer) *cobra.Command {
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
// Initialize the daemon providers (e.g. ManifestStorage)
cmdutil.CheckErr(providers.Populate(providers.DaemonProviders))
cmdutil.CheckErr(providers.Populate(ignite.DaemonProviders))

// Wait for Ctrl + C
var endWaiter sync.WaitGroup
Expand All @@ -35,11 +36,9 @@ func NewCmdDaemon(out io.Writer) *cobra.Command {

endWaiter.Wait()

// Close the SyncStorage's watcher threads
if providers.SyncStorage != nil {
fmt.Println("Closing...")
providers.SyncStorage.Close()
}
// Close the Storage's watcher threads
fmt.Println("Closing...")
providers.Storage.Close()
},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/ignite/ignite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/weaveworks/ignite/cmd/ignite/cmd"
"github.com/weaveworks/ignite/cmd/ignite/cmd/cmdutil"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/providers/ignite"
)

func main() {
Expand All @@ -17,7 +18,7 @@ func main() {
// Run runs the main cobra command of this application
func Run() error {
// Populate the providers
cmdutil.CheckErr(providers.Populate(providers.Providers))
cmdutil.CheckErr(providers.Populate(ignite.Providers))

c := cmd.NewIgniteCommand(os.Stdin, os.Stdout, os.Stderr)
return c.Execute()
Expand Down
3 changes: 2 additions & 1 deletion hack/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"github.com/spf13/cobra/doc"
"github.com/weaveworks/ignite/cmd/ignite/cmd"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/providers/ignite"
)

func main() {
if err := providers.Populate(providers.Providers); err != nil {
if err := providers.Populate(ignite.Providers); err != nil {
log.Fatal(err)
}
ignite := cmd.NewIgniteCommand(os.Stdin, os.Stdout, os.Stderr)
Expand Down
3 changes: 2 additions & 1 deletion pkg/network/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/containernetworking/cni/libcni"
cnitypes "github.com/containernetworking/cni/pkg/types"
log "github.com/sirupsen/logrus"
"github.com/weaveworks/ignite/pkg/network"
"github.com/weaveworks/ignite/pkg/runtime"
)

Expand All @@ -33,7 +34,7 @@ type cniNetwork struct {
CNIConfig libcni.CNI
}

func GetCNINetworkPlugin(runtime runtime.Interface) (NetworkPlugin, error) {
func GetCNINetworkPlugin(runtime runtime.Interface) (network.Plugin, error) {
binDirs := []string{CNIBinDir}
plugin := &cniNetworkPlugin{
runtime: runtime,
Expand Down
15 changes: 0 additions & 15 deletions pkg/network/cni/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,3 @@ const (
}]
}`
)

// NetworkPlugin describes a network plugin for docker
type NetworkPlugin interface {
// Name returns the network plugin's name.
Name() string

// SetupContainerNetwork sets up the networking for a Docker container using CNI
SetupContainerNetwork(containerID string) error

// RemoveContainerNetwork is the method called before a container using the CNI network can be deleted
RemoveContainerNetwork(containerID string) error

// Status returns error if the network plugin is in error state
Status() error
}
16 changes: 16 additions & 0 deletions pkg/network/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package network

// Plugin describes a generic network plugin
type Plugin interface {
// Name returns the network plugin's name.
Name() string

// SetupContainerNetwork sets up the networking for a container
SetupContainerNetwork(containerID string) error

// RemoveContainerNetwork is the method called before a container using the network plugin can be deleted
RemoveContainerNetwork(containerID string) error

// Status returns error if the network plugin is in error state
Status() error
}
11 changes: 0 additions & 11 deletions pkg/providers/client.go

This file was deleted.

13 changes: 13 additions & 0 deletions pkg/providers/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client

import (
log "github.com/sirupsen/logrus"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/providers"
)

func SetClient() (err error) {
log.Trace("Initializing the Client provider...")
providers.Client = client.NewClient(providers.Storage)
return
}
13 changes: 13 additions & 0 deletions pkg/providers/cni/cni.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cni

import (
log "github.com/sirupsen/logrus"
"github.com/weaveworks/ignite/pkg/network/cni"
"github.com/weaveworks/ignite/pkg/providers"
)

func SetCNINetworkPlugin() (err error) {
log.Trace("Initializing the CNI provider...")
providers.NetworkPlugin, err = cni.GetCNINetworkPlugin(providers.Runtime)
return
}
13 changes: 13 additions & 0 deletions pkg/providers/docker/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package docker

import (
log "github.com/sirupsen/logrus"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/runtime/docker"
)

func SetDockerRuntime() (err error) {
log.Trace("Initializing the Docker provider...")
providers.Runtime, err = docker.GetDockerClient()
return
}
25 changes: 25 additions & 0 deletions pkg/providers/ignite/providers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ignite

import (
"github.com/weaveworks/ignite/pkg/providers"
clientprovider "github.com/weaveworks/ignite/pkg/providers/client"
cniprovider "github.com/weaveworks/ignite/pkg/providers/cni"
dockerprovider "github.com/weaveworks/ignite/pkg/providers/docker"
storageprovider "github.com/weaveworks/ignite/pkg/providers/storage"
)

// NOTE: Provider initialization is order-dependent!
// E.g. the network plugin depends on the runtime.
var Providers = []providers.ProviderInitFunc{
dockerprovider.SetDockerRuntime, // Use the Docker runtime
cniprovider.SetCNINetworkPlugin, // Use the CNI Network plugin
storageprovider.SetGenericStorage, // Use a generic storage implementation backed by a cache
clientprovider.SetClient, // Set the globally available client
}

// `ignite daemon` overwrites/re-initializes the Storage and Client providers
// TODO: Break this into its own binary
var DaemonProviders = []providers.ProviderInitFunc{
storageprovider.SetManifestStorage, // Use the ManifestStorage implementation
clientprovider.SetClient, // Set the globally available client
}
13 changes: 0 additions & 13 deletions pkg/providers/network.go

This file was deleted.

40 changes: 24 additions & 16 deletions pkg/providers/providers.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package providers

// NOTE: Provider initialization is order-dependent!
// E.g. the network plugin depends on the runtime.
var Providers = []func() error{
SetDockerRuntime, // Use the Docker runtime
SetCNINetworkPlugin, // Use the CNI Network plugin
SetCachedStorage, // Use a generic storage implementation backed by a cache
SetClient, // Set the globally available client
}
import (
log "github.com/sirupsen/logrus"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/network"
"github.com/weaveworks/ignite/pkg/runtime"
"github.com/weaveworks/ignite/pkg/storage"
)

// `ignite daemon` overwrites/re-initializes the Storage and Client providers
var DaemonProviders = []func() error{
SetManifestStorage, // Use the ManifestStorage implementation
SetClient, // Set the globally available client
}
// NetworkPlugin provides the default network plugin implementation
var NetworkPlugin network.Plugin

// Populate initializes all providers
func Populate(providers []func() error) error {
// Runtime provides the default container runtime
var Runtime runtime.Interface

// Client is the default client that can be easily used
var Client *client.Client

for _, init := range providers {
// Storage is the default storage implementation
var Storage storage.Storage

type ProviderInitFunc func() error

// Populate initializes all providers
func Populate(providers []ProviderInitFunc) error {
log.Trace("Populating providers...")
for i, init := range providers {
log.Tracef("Provider %d...", i)
if err := init(); err != nil {
return err
}
Expand Down
14 changes: 0 additions & 14 deletions pkg/providers/runtime.go

This file was deleted.

14 changes: 14 additions & 0 deletions pkg/providers/spawn/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package spawn

import (
"github.com/weaveworks/ignite/pkg/providers"
clientprovider "github.com/weaveworks/ignite/pkg/providers/client"
storageprovider "github.com/weaveworks/ignite/pkg/providers/storage"
)

// NOTE: Provider initialization is order-dependent!
// E.g. the network plugin depends on the runtime.
var Providers = []providers.ProviderInitFunc{
storageprovider.SetGenericStorage, // Use a generic storage implementation backed by a cache
clientprovider.SetClient, // Set the globally available client
}
41 changes: 0 additions & 41 deletions pkg/providers/storage.go

This file was deleted.

29 changes: 29 additions & 0 deletions pkg/providers/storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package providers

import (
log "github.com/sirupsen/logrus"
"github.com/weaveworks/ignite/pkg/apis/ignite/scheme"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/storage"
"github.com/weaveworks/ignite/pkg/storage/cache"
"github.com/weaveworks/ignite/pkg/storage/manifest"
)

func SetGenericStorage() error {
log.Trace("Initializing the GenericStorage provider...")
providers.Storage = cache.NewCache(
storage.NewGenericStorage(
storage.NewDefaultRawStorage(constants.DATA_DIR), scheme.Serializer))
return nil
}

func SetManifestStorage() error {
log.Trace("Initializing the ManifestStorage provider...")
ms, err := manifest.NewManifestStorage("/etc/firecracker/manifests")
if err != nil {
return err
}
providers.Storage = cache.NewCache(ms)
return nil
}
4 changes: 4 additions & 0 deletions pkg/storage/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (c *cache) RawStorage() storage.RawStorage {
return c.storage.RawStorage()
}

func (c *cache) Close() error {
return c.storage.Close()
}

func (c *cache) Flush() error {
// Load the entire cache
allObjects, err := c.index.loadAll()
Expand Down
Loading