Refactor provider init (#360)

* Refactor provider init

This moves provider init out of vkubelet setup, instead preferring to
initialize vkubelet with a provider.

* Split API server configuration from setup.

This makes sure that configuration (which is done primarily through env
vars) is separate from actually standing up the servers.

This also makes sure to abort daemon initialization if the API servers
are not able to start.
This commit is contained in:
Brian Goff
2018-09-26 13:18:02 -07:00
committed by Robbie Zhang
parent 6b97713af3
commit 083f6dee05
21 changed files with 518 additions and 293 deletions

63
providers/provider.go Normal file
View File

@@ -0,0 +1,63 @@
package providers
import (
"context"
"io"
"time"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/remotecommand"
stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
)
// Provider contains the methods required to implement a virtual-kubelet provider.
type Provider interface {
// CreatePod takes a Kubernetes Pod and deploys it within the provider.
CreatePod(ctx context.Context, pod *v1.Pod) error
// UpdatePod takes a Kubernetes Pod and updates it within the provider.
UpdatePod(ctx context.Context, pod *v1.Pod) error
// DeletePod takes a Kubernetes Pod and deletes it from the provider.
DeletePod(ctx context.Context, pod *v1.Pod) error
// GetPod retrieves a pod by name from the provider (can be cached).
GetPod(ctx context.Context, namespace, name string) (*v1.Pod, error)
// GetContainerLogs retrieves the logs of a container by name from the provider.
GetContainerLogs(ctx context.Context, namespace, podName, containerName string, tail int) (string, error)
// ExecInContainer executes a command in a container in the pod, copying data
// between in/out/err and the container's stdin/stdout/stderr.
ExecInContainer(name string, uid types.UID, container string, cmd []string, in io.Reader, out, err io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize, timeout time.Duration) error
// GetPodStatus retrieves the status of a pod by name from the provider.
GetPodStatus(ctx context.Context, namespace, name string) (*v1.PodStatus, error)
// GetPods retrieves a list of all pods running on the provider (can be cached).
GetPods(context.Context) ([]*v1.Pod, error)
// Capacity returns a resource list with the capacity constraints of the provider.
Capacity(context.Context) v1.ResourceList
// NodeConditions returns a list of conditions (Ready, OutOfDisk, etc), which is
// polled periodically to update the node status within Kubernetes.
NodeConditions(context.Context) []v1.NodeCondition
// NodeAddresses returns a list of addresses for the node status
// within Kubernetes.
NodeAddresses(context.Context) []v1.NodeAddress
// NodeDaemonEndpoints returns NodeDaemonEndpoints for the node status
// within Kubernetes.
NodeDaemonEndpoints(context.Context) *v1.NodeDaemonEndpoints
// OperatingSystem returns the operating system the provider is for.
OperatingSystem() string
}
// PodMetricsProvider is an optional interface that providers can implement to expose pod stats
type PodMetricsProvider interface {
GetStatsSummary(context.Context) (*stats.Summary, error)
}

View File

@@ -0,0 +1,23 @@
// +build !no_alicooud_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/alicloud"
)
func init() {
register("alicloud", aliCloudInit)
}
func aliCloudInit(cfg InitConfig) (providers.Provider, error) {
return alicloud.NewECIProvider(
cfg.ConfigPath,
cfg.ResourceManager,
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,16 @@
// +build !no_aws_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/aws"
)
func init() {
register("aws", initAWS)
}
func initAWS(cfg InitConfig) (providers.Provider, error) {
return aws.NewFargateProvider(cfg.ConfigPath, cfg.ResourceManager, cfg.NodeName, cfg.OperatingSystem, cfg.InternalIP, cfg.DaemonPort)
}

View File

@@ -0,0 +1,23 @@
// +build !no_azure_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/azure"
)
func init() {
register("azure", initAzure)
}
func initAzure(cfg InitConfig) (providers.Provider, error) {
return azure.NewACIProvider(
cfg.ConfigPath,
cfg.ResourceManager,
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,23 @@
// +build !no_azurebatch_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/azurebatch"
)
func init() {
register("azurebatch", initAzureBatch)
}
func initAzureBatch(cfg InitConfig) (providers.Provider, error) {
return azurebatch.NewBatchProvider(
cfg.ConfigPath,
cfg.ResourceManager,
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,22 @@
// +build linux,!no_cri_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/cri"
)
func init() {
register("cri", criInit)
}
func criInit(cfg InitConfig) (providers.Provider, error) {
return cri.NewCRIProvider(
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.ResourceManager,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,23 @@
// +build !no_huawei_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/huawei"
)
func init() {
register("huawei", initHuawei)
}
func initHuawei(cfg InitConfig) (providers.Provider, error) {
return huawei.NewCCIProvider(
cfg.ConfigPath,
cfg.ResourceManager,
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,16 @@
// +build !no_hyper_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/hypersh"
)
func init() {
register("hyper", initHyper)
}
func initHyper(cfg InitConfig) (providers.Provider, error) {
return hypersh.NewHyperProvider(cfg.ConfigPath, cfg.ResourceManager, cfg.NodeName, cfg.OperatingSystem)
}

View File

@@ -0,0 +1,22 @@
// +build !no_mock_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/mock"
)
func init() {
register("mock", initMock)
}
func initMock(cfg InitConfig) (providers.Provider, error) {
return mock.NewMockProvider(
cfg.ConfigPath,
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,22 @@
// +build !no_sfmesh_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/sfmesh"
)
func init() {
register("sfmesh", sfmeshInit)
}
func sfmeshInit(cfg InitConfig) (providers.Provider, error) {
return sfmesh.NewSFMeshProvider(
cfg.ResourceManager,
cfg.NodeName,
cfg.OperatingSystem,
cfg.InternalIP,
cfg.DaemonPort,
)
}

View File

@@ -0,0 +1,16 @@
// +build linux,!no_vic_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/vic"
)
func init() {
register("vic", initVic)
}
func initVic(cfg InitConfig) (providers.Provider, error) {
return vic.NewVicProvider(cfg.ConfigPath, cfg.ResourceManager, cfg.NodeName, cfg.OperatingSystem)
}

View File

@@ -0,0 +1,16 @@
// +build !no_web_provider
package register
import (
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/virtual-kubelet/virtual-kubelet/providers/web"
)
func init() {
register("web", initWeb)
}
func initWeb(cfg InitConfig) (providers.Provider, error) {
return web.NewBrokerProvider(cfg.NodeName, cfg.OperatingSystem, cfg.DaemonPort)
}

View File

@@ -0,0 +1,35 @@
package register
import (
"github.com/cpuguy83/strongerrors"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/manager"
"github.com/virtual-kubelet/virtual-kubelet/providers"
)
var providerInits = make(map[string]initFunc)
// InitConfig is the config passed to initialize a registered provider.
type InitConfig struct {
ConfigPath string
NodeName string
OperatingSystem string
InternalIP string
DaemonPort int32
ResourceManager *manager.ResourceManager
}
type initFunc func(InitConfig) (providers.Provider, error)
// GetProvider gets the provider specified by the given name
func GetProvider(name string, cfg InitConfig) (providers.Provider, error) {
f, ok := providerInits[name]
if !ok {
return nil, strongerrors.NotFound(errors.Errorf("provider not found: %s", name))
}
return f(cfg)
}
func register(name string, f initFunc) {
providerInits[name] = f
}