cloud

package
v0.3.5-hr Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2018 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// KindDockerCloud ...
	KindDockerCloud = "docker"
	// KindK8SCloud ...
	KindK8SCloud = "kubernetes"
)
View Source
const (
	// ResourceCPU in cores. (500m = .5 cores)
	ResourceCPU = "cpu"
	// ResourceMemory in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024)
	ResourceMemory = "memory"
	// ResourceRequestsCPU CPU request, in cores. (500m = .5 cores)
	ResourceRequestsCPU = "requests.cpu"
	// ResourceRequestsMemory Memory request, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024)
	ResourceRequestsMemory = "requests.memory"
	// ResourceLimitsCPU CPU limit, in cores. (500m = .5 cores)
	ResourceLimitsCPU = "limits.cpu"
	// ResourceLimitsMemory Memory limit, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024)
	ResourceLimitsMemory = "limits.memory"
)
View Source
const (

	// WorkerEventID is special
	WorkerEventID = "WORKER_EVENTID"

	CycloneServer      = "CYCLONE_SERVER"
	ConsoleWebEndpoint = "CONSOLE_WEB_ENDPOINT"

	RegistryLocation = "REGISTRY_LOCATION"
	RegistryUsername = "REGISTRY_USERNAME"
	RegistryPassword = "REGISTRY_PASSWORD"

	WorkerImage = "WORKER_IMAGE"

	GithubClient = "GITHUB_CLIENT"
	GithubSecret = "GITHUB_SECRET"

	GitlabURL    = "GITLAB_URL"
	GitlabClient = "GITLAB_CLIENT"
	GitlabSecret = "GITLAB_SECRET"

	LogServer = "LOG_SERVER"

	LimitMemory = "LIMIT_MEMORY"
	LimitCPU    = "LIMIT_CPU"

	WorkingDir = "/root/code"
)

env

View Source
const (
	// WorkerTimeout ...
	WorkerTimeout = 2 * time.Hour
)

Variables

View Source
var (
	// Debug ...
	Debug = false
	// CloudFactory ...
	CloudFactory = register.NewRegister()
)
View Source
var (
	// ZeroQuantity ...
	ZeroQuantity = NewDecimalQuantity(0)
	// ZeroQuota ...
	ZeroQuota = Quota{

		ResourceLimitsCPU:    ZeroQuantity,
		ResourceLimitsMemory: ZeroQuantity,
	}

	// DefaultLimitCPU 500m = 0.5 core = 500 * 100 * 100
	DefaultLimitCPU = MustParseCPU(0.5)
	// DefaultLimitMemory 500Mi = 500MiB = 500 * 1024 * 1024
	DefaultLimitMemory = NewBinaryQuantity(500 * 1024 * 1024)
	// DefaultQuota ...
	DefaultQuota = Quota{
		ResourceLimitsCPU:    DefaultLimitCPU,
		ResourceLimitsMemory: DefaultLimitMemory,
	}
)
View Source
var (
	// DefaultCloudPingTimeout ...
	DefaultCloudPingTimeout = time.Duration(5 * time.Second)
)
View Source
var (
	// ErrNoEnoughResource occurs when clouds have no enough resource to provison as worker
	ErrNoEnoughResource = errors.New("worker required resources are out of quota limit")
)

Functions

func BytesSize

func BytesSize(size float64) string

BytesSize returns a human-readable size in bytes, kibibytes, mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").

func CustomSize

func CustomSize(format string, size float64, base float64, _map []string) string

CustomSize returns a human-readable approximation of a size using custom format.

func IsAllCloudsBusyErr

func IsAllCloudsBusyErr(errs error) bool

IsAllCloudsBusyErr check whether all cloud is too busy to provision a worker

func RegisterConstructor

func RegisterConstructor(kind string, cc Constructor)

RegisterConstructor ...

Types

type Cloud

type Cloud interface {

	// Name returns cloud name
	Name() string

	// Kind returns cloud type, such as kubernetes
	Kind() string

	// Ping returns nil if cloud is accessible
	Ping() error

	// Resource returns the limit and used quotas of the cloud
	Resource() (*Resource, error)

	// CanProvision returns true if the cloud can provision a worker meetting the quota
	CanProvision(need Quota) (bool, error)

	// Provision returns a worker if the cloud can provison,
	// but worker is not running. you should call worker.Do() to do the work
	// Provision should call ConProvision firstly
	Provision(id string, opts WorkerOptions) (Worker, error)

	// LoadWorker rebuilds a worker from worker info
	LoadWorker(WorkerInfo) (Worker, error)

	//
	GetOptions() Options
}

Cloud defines a cloud provider which provision workers for cyclone

func NewDockerCloud

func NewDockerCloud(opts Options) (Cloud, error)

NewDockerCloud returns a new Cloud from CloudOption

func NewK8SCloud

func NewK8SCloud(opts Options) (Cloud, error)

NewK8SCloud ...

func NewK8SCloudInCluster

func NewK8SCloudInCluster(opts Options) (Cloud, error)

NewK8SCloudInCluster returns a cloud object which uses the service account kubernetes gives to pods

type Constructor

type Constructor func(Options) (Cloud, error)

Constructor ...

func GetConstructor

func GetConstructor(kind string) Constructor

GetConstructor ...

type Controller

type Controller struct {
	Clouds map[string]Cloud
	// contains filtered or unexported fields
}

Controller ... TODO cloud controller sync all cloud infomation

func NewController

func NewController() *Controller

NewController creates a new CloudController

func (*Controller) AddClouds

func (cc *Controller) AddClouds(opts ...Options) error

AddClouds creates clouds from CloudOptions and cache it

func (*Controller) DeleteCloud

func (cc *Controller) DeleteCloud(name string)

DeleteCloud returns a cloud by cloud name

func (*Controller) GetCloud

func (cc *Controller) GetCloud(name string) (Cloud, bool)

GetCloud returns a cloud by cloud name

func (*Controller) LoadWorker

func (cc *Controller) LoadWorker(info WorkerInfo) (Worker, error)

LoadWorker ...

func (*Controller) Provision

func (cc *Controller) Provision(id string, opts WorkerOptions) (Worker, error)

Provision asks all Clouds in CloudController to provision a worker you should compute needed quota by yourself,

func (*Controller) Resources

func (cc *Controller) Resources() (map[string]*Resource, error)

Resources returns all clouds quotas

type DockerCloud

type DockerCloud struct {
	// contains filtered or unexported fields
}

DockerCloud is a docker cloud

func (*DockerCloud) CanProvision

func (cloud *DockerCloud) CanProvision(need Quota) (bool, error)

CanProvision returns true if the cloud can provision a worker meetting the quota

func (*DockerCloud) Client

func (cloud *DockerCloud) Client() *apiclient.Client

Client returns docker client

func (*DockerCloud) GetOptions

func (cloud *DockerCloud) GetOptions() Options

GetOptions ...

func (*DockerCloud) Kind

func (cloud *DockerCloud) Kind() string

Kind returns cloud type.

func (*DockerCloud) LoadWorker

func (cloud *DockerCloud) LoadWorker(info WorkerInfo) (Worker, error)

LoadWorker rebuilds a worker from worker info

func (*DockerCloud) Name

func (cloud *DockerCloud) Name() string

Name returns cloud name.

func (*DockerCloud) Ping

func (cloud *DockerCloud) Ping() error

Ping returns nil if cloud is accessible

func (*DockerCloud) Provision

func (cloud *DockerCloud) Provision(id string, wopts WorkerOptions) (Worker, error)

Provision returns a worker if the cloud can provison

func (*DockerCloud) Resource

func (cloud *DockerCloud) Resource() (*Resource, error)

Resource returns the limit and used quotas of the cloud

type DockerWorker

type DockerWorker struct {
	// contains filtered or unexported fields
}

DockerWorker ...

func (*DockerWorker) Do

func (worker *DockerWorker) Do() error

Do starts the worker and do the work

func (*DockerWorker) GetWorkerInfo

func (worker *DockerWorker) GetWorkerInfo() WorkerInfo

GetWorkerInfo returns worker's infomation

func (*DockerWorker) IsTimeout

func (worker *DockerWorker) IsTimeout() (bool, time.Duration)

IsTimeout returns true if worker is timeout and returns the time left until it is due

func (*DockerWorker) Terminate

func (worker *DockerWorker) Terminate() error

Terminate terminates the worker and destroy it

type ErrCloudProvision

type ErrCloudProvision struct {
	// contains filtered or unexported fields
}

ErrCloudProvision contains all clouds provision errors

func NewErrCloudProvision

func NewErrCloudProvision() *ErrCloudProvision

NewErrCloudProvision return a new CloudProvisionErr

func (*ErrCloudProvision) Add

func (cpe *ErrCloudProvision) Add(name string, err error)

Add adds an error to CloudProvisionErr

func (*ErrCloudProvision) Err

func (cpe *ErrCloudProvision) Err() error

Err returns nil if CloudProvisionErr contains 0 error

func (*ErrCloudProvision) Error

func (cpe *ErrCloudProvision) Error() string

type K8SCloud

type K8SCloud struct {
	// contains filtered or unexported fields
}

K8SCloud ...

func (*K8SCloud) CanProvision

func (cloud *K8SCloud) CanProvision(quota Quota) (bool, error)

CanProvision returns true if the cloud can provision a worker meetting the quota

func (*K8SCloud) Client

func (cloud *K8SCloud) Client() *kubernetes.Clientset

Client returns k8s clientset

func (*K8SCloud) GetOptions

func (cloud *K8SCloud) GetOptions() Options

GetOptions ...

func (*K8SCloud) Kind

func (cloud *K8SCloud) Kind() string

Kind returns cloud type.

func (*K8SCloud) LoadWorker

func (cloud *K8SCloud) LoadWorker(info WorkerInfo) (Worker, error)

LoadWorker rebuilds a worker from worker info

func (*K8SCloud) Name

func (cloud *K8SCloud) Name() string

Name returns k8s cloud name

func (*K8SCloud) Ping

func (cloud *K8SCloud) Ping() error

Ping returns nil if cloud is accessible

func (*K8SCloud) Provision

func (cloud *K8SCloud) Provision(id string, wopts WorkerOptions) (Worker, error)

Provision returns a worker if the cloud can provison

func (*K8SCloud) Resource

func (cloud *K8SCloud) Resource() (*Resource, error)

Resource returns the limit and used quotas of the cloud

type K8SPodWorker

type K8SPodWorker struct {
	*K8SCloud
	// contains filtered or unexported fields
}

K8SPodWorker ...

func (*K8SPodWorker) Do

func (worker *K8SPodWorker) Do() error

Do starts the worker and do the work

func (*K8SPodWorker) GetWorkerInfo

func (worker *K8SPodWorker) GetWorkerInfo() WorkerInfo

GetWorkerInfo returns worker's infomation

func (*K8SPodWorker) IsTimeout

func (worker *K8SPodWorker) IsTimeout() (bool, time.Duration)

IsTimeout returns true if worker is timeout and returns the time left until it is due

func (*K8SPodWorker) Terminate

func (worker *K8SPodWorker) Terminate() error

Terminate terminates the worker and destroy it

type Options

type Options struct {
	// mongodb id
	ID string `bson:"_id,omitempty" json:"-"`

	// common options
	Kind     string `json:"kind,omitempty" bson:"kind,omitempty"`
	Name     string `json:"name,omitempty" bson:"name,omitempty"`
	Host     string `json:"host,omitempty" bson:"host,omitempty"`
	Insecure bool   `json:"insecure,omitempty" bson:"insecure,omitempty"`

	// docker cloud
	DockerCertPath string `json:"dockerCertPath,omitempty" bson:"dockerCertPath,omitempty"`

	// k8s cloud
	K8SInCluster   bool   `json:"k8sInCluster,omitempty" bson:"k8sInCluster,omitempty"`
	K8SNamespace   string `json:"k8sNamespace,omitempty" bson:"k8sNamespace,omitempty"`
	K8SBearerToken string `json:"k8sBearerToken,omitempty" bson:"k8sBearerToken,omitempty"`
}

Options is options for all kinds of clouds

type Quantity

type Quantity struct {
	resource.Quantity
}

Quantity warps resource.Quantity to implement cli.Generic interface

func MustParseCPU

func MustParseCPU(value float64) *Quantity

MustParseCPU turns the given float(in cores, such as 1.5 cores) into a quantity or panics; for tests or others cases where you know the value is valid.

func MustParseMemory

func MustParseMemory(value float64) *Quantity

MustParseMemory turns the given float(in Bytes, such as 500*1024*1024 bytes) into a quantity or panics; for tests or others cases where you know the value is valid.

func NewBinaryQuantity

func NewBinaryQuantity(value int) *Quantity

NewBinaryQuantity creates a new Quantity with resource.BinarySI Format

func NewDecimalQuantity

func NewDecimalQuantity(value int) *Quantity

NewDecimalQuantity creates a new Quantity with resource.DecimalSI Format

func NewQuantity

func NewQuantity(value int64, format resource.Format) *Quantity

NewQuantity creates a new Quantity

func NewQuantityFor

func NewQuantityFor(q resource.Quantity) *Quantity

NewQuantityFor reates a new Quantity from resource.Quantity

func (Quantity) DeepCopy

func (q Quantity) DeepCopy() Quantity

DeepCopy returns a deep-copy of the Quantity value. Note that the method receiver is a value, so we can mutate it in-place and return it.

func (*Quantity) Set

func (q *Quantity) Set(value string) error

Set implements cli.Generic interface

func (Quantity) String

func (q Quantity) String() string

type Quota

type Quota map[string]*Quantity

Quota ...

func (Quota) Add

func (q Quota) Add(y Quota)

Add adds the provide y Quota to the current value.

func (Quota) DeepCopy

func (q Quota) DeepCopy() Quota

DeepCopy returns a deep-copy of the Quota value.

func (Quota) Enough

func (q Quota) Enough(y Quota, z Quota) bool

Enough returns true if the Quota is greater than y plus z.

func (Quota) IsZero

func (q Quota) IsZero() bool

IsZero returns true if the all Quantities in Quota are equal to zero.

func (Quota) SetDefault

func (q Quota) SetDefault()

SetDefault fills quota with default quantity

func (Quota) Sub

func (q Quota) Sub(y Quota)

Sub subtracts the provided y Quota from the current value in place.

func (Quota) ToDockerQuota

func (q Quota) ToDockerQuota() container.Resources

ToDockerQuota converts Quota to docker resource quota type

func (Quota) ToK8SQuota

func (q Quota) ToK8SQuota() apiv1.ResourceRequirements

ToK8SQuota converts Quota to k8s resource quota type

type Resource

type Resource struct {
	Limit Quota `json:"limit,omitempty" bson:"limit,omitempty"`
	Used  Quota `json:"used,omitempty" bson:"used,omitempty"`
}

Resource describes cloud resource include limit and used quota

func NewResource

func NewResource() *Resource

NewResource returns a new Resource

func (*Resource) Add

func (r *Resource) Add(y *Resource)

Add adds the provided Resource y to current Resource

type Worker

type Worker interface {
	// Do starts the worker and does the work
	Do() error

	// GetWorkerInfo returns worker's infomation
	GetWorkerInfo() WorkerInfo

	// IsTimeout returns true if worker is timeout
	// and returns the time left until it is due
	IsTimeout() (bool, time.Duration)

	// Terminate terminates the worker and destroy it
	Terminate() error
}

Worker is the truly excutor to deal with build jobs

type WorkerEnvs

type WorkerEnvs struct {
	// for worker env
	CycloneServer      string
	ConsoleWebEndpoint string

	// Registry
	RegistryLocation string `json:"registryLocation,omitempty"`
	RegistryUsername string `json:"registryUsername,omitempty"`
	RegistryPassword string `json:"registryPassword,omitempty"`

	// github
	GithubClient string
	GithubSecret string

	// gitlab
	GitlabURL    string
	GitlabClient string
	GitlabSecret string

	LogServer string

	WorkerImage string
}

WorkerEnvs ...

func NewWorkerEnvs

func NewWorkerEnvs() *WorkerEnvs

NewWorkerEnvs creates a new WorkerEnvs

func (*WorkerEnvs) AddFlags

func (env *WorkerEnvs) AddFlags(app *cli.App)

AddFlags adds flags for a specific APIServer to the specified cli.app

type WorkerInfo

type WorkerInfo struct {
	CloudName string `json:"cloudName,omitempty" bson:"cloudName,omitempty"`
	CloudKind string `json:"cloudKind,omitempty" bson:"cloudKind,omitempty"`

	Name       string    `json:"name,omitempty" bson:"name,omitempty"`
	CreateTime time.Time `json:"createTime,omitempty" bson:"createTime,omitempty"`
	DueTime    time.Time `json:"dueTime,omitempty" bson:"dueTime,omitempty"`

	// for k8s
	Namespace string `json:"namespace,omitempty" bson:"namespace,omitempty"`
	PodName   string `json:"podID,omitempty" bson:"podName,omitempty"`

	// for docker
	ContainerID string `json:"containerID,omitempty" bson:"containerID,omitempty"`
}

WorkerInfo ...

type WorkerOptions

type WorkerOptions struct {
	WorkerEnvs *WorkerEnvs

	Quota Quota

	// Namespace represents the k8s namespace where to create worker, only works for k8s cloud provider.
	Namespace string

	// CacheVolume represents the volume to cache dependency for worker.
	CacheVolume string

	// BuildTool represents the tool used for build.
	BuildTool string
}

WorkerOptions contains the options for workers creation

func NewWorkerOptions

func NewWorkerOptions() *WorkerOptions

NewWorkerOptions creates a new WorkerOptions with default value

func (*WorkerOptions) AddFlags

func (opts *WorkerOptions) AddFlags(app *cli.App)

AddFlags adds flags for a specific APIServer to the specified cli.app

func (WorkerOptions) DeepCopy

func (opts WorkerOptions) DeepCopy() WorkerOptions

DeepCopy returns a deep-copy of the WorkerOptions value. Note that the method receiver is a value, so we can mutate it in-place and return it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL