Skip to content

Commit

Permalink
Make Deploy facade checks k8s cluster version and compare with the mi…
Browse files Browse the repository at this point in the history
…n-version in metadata.yaml;
  • Loading branch information
ycliuhw committed Sep 13, 2019
1 parent 1c80589 commit 2e58799
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 59 deletions.
5 changes: 3 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@

[[constraint]]
name = "gopkg.in/juju/charm.v6"
revision = "cf3ec8f263e4b35e70f76f8b660284ac26517958"
# revision = "cf3ec8f263e4b35e70f76f8b660284ac26517958"
source = "github.com/ycliuhw/charm"
revision = "c976888d3eca2099091d3dfb0f759995176abc29"

[[constraint]]
revision = "2adcece4e962a51e0793b8562560cf9da874026f"
Expand Down
4 changes: 2 additions & 2 deletions api/caasunitprovisioner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *Client) WatchPodSpec(application string) (watcher.NotifyWatcher, error)
type DeploymentInfo struct {
DeploymentType string
ServiceType string
Demonset bool
Daemonset bool
}

// ProvisioningInfo holds unit provisioning info.
Expand Down Expand Up @@ -189,7 +189,7 @@ func (c *Client) ProvisioningInfo(appName string) (*ProvisioningInfo, error) {
info.DeploymentInfo = DeploymentInfo{
DeploymentType: result.DeploymentInfo.DeploymentType,
ServiceType: result.DeploymentInfo.ServiceType,
Demonset: result.DeploymentInfo.Demonset,
Daemonset: result.DeploymentInfo.Daemonset,
}
}

Expand Down
9 changes: 2 additions & 7 deletions api/charms/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ func convertCharmMeta(meta *params.CharmMeta) (*charm.Meta, error) {
if meta == nil {
return nil, nil
}
minJujuVersion, err := version.Parse(meta.MinJujuVersion)
if err != nil {
return nil, errors.Trace(err)
}
minK8sVersion, err := version.Parse(meta.MinK8sVersion)
minVersion, err := version.Parse(meta.MinJujuVersion)
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -123,8 +119,7 @@ func convertCharmMeta(meta *params.CharmMeta) (*charm.Meta, error) {
PayloadClasses: convertCharmPayloadClassMap(meta.PayloadClasses),
Resources: resources,
Terms: meta.Terms,
MinJujuVersion: minJujuVersion,
MinK8sVersion: minK8sVersion,
MinJujuVersion: minVersion,
}
return result, nil
}
Expand Down
22 changes: 0 additions & 22 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,6 @@ func (c *Client) validateCharmVersions(ch charm.Charm) error {
return minVersionError(minver, agentver)
}
}

minver = ch.Meta().MinK8sVersion
if minver != version.Zero {
cloudVersion, err := c.cloudVersion()
if err != nil {
return errors.Trace(err)
}

if minver.Compare(cloudVersion) > 0 {
return minVersionError(minver, cloudVersion)
}
}
return nil
}

Expand Down Expand Up @@ -624,16 +612,6 @@ func (c *Client) AgentVersion() (version.Number, error) {
return result.Version, nil
}

// ???????????????? K8sVersion or generic cloud version???????????
// cloudVersion reports the version number of the provider cloud version.
func (c *Client) cloudVersion() (version.Number, error) {
var result params.AgentVersionResult
if err := c.facade.FacadeCall("CloudVersion", nil, &result); err != nil {
return version.Number{}, err
}
return result.Version, nil
}

// websocketDial is called instead of dialer.Dial so we can override it in
// tests.
var websocketDial = websocketDialWithErrors
Expand Down
37 changes: 26 additions & 11 deletions apiserver/facades/client/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type APIBase struct {

storagePoolManager poolmanager.PoolManager
registry storage.ProviderRegistry
storageValidator caas.StorageValidator
caasBroker caas.Broker
deployApplicationFunc func(ApplicationDeployer, DeployApplicationParams) (Application, error)
}

Expand Down Expand Up @@ -193,14 +193,14 @@ func newFacadeBase(ctx facade.Context) (*APIBase, error) {
var (
storagePoolManager poolmanager.PoolManager
registry storage.ProviderRegistry
storageValidator caas.Broker
caasBroker caas.Broker
)
if facadeModel.Type() == state.ModelTypeCAAS {
storageValidator, err = stateenvirons.GetNewCAASBrokerFunc(caas.New)(ctx.State())
caasBroker, err = stateenvirons.GetNewCAASBrokerFunc(caas.New)(ctx.State())
if err != nil {
return nil, errors.Annotate(err, "getting caas client")
}
registry = stateenvirons.NewStorageProviderRegistry(storageValidator)
registry = stateenvirons.NewStorageProviderRegistry(caasBroker)
storagePoolManager = poolmanager.New(state.NewStateSettings(ctx.State()), registry)
}

Expand All @@ -217,7 +217,7 @@ func newFacadeBase(ctx facade.Context) (*APIBase, error) {
storagePoolManager,
registry,
resources,
storageValidator,
caasBroker,
)
}

Expand All @@ -233,7 +233,7 @@ func NewAPIBase(
storagePoolManager poolmanager.PoolManager,
registry storage.ProviderRegistry,
resources facade.Resources,
storageValidator caas.StorageValidator,
caasBroker caas.Broker,
) (*APIBase, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
Expand All @@ -250,7 +250,7 @@ func NewAPIBase(
storagePoolManager: storagePoolManager,
registry: registry,
resources: resources,
storageValidator: storageValidator,
caasBroker: caasBroker,
}, nil
}

Expand Down Expand Up @@ -366,7 +366,16 @@ func (api *APIBase) Deploy(args params.ApplicationsDeploy) (params.ErrorResults,
}

for i, arg := range args.Applications {
err := deployApplication(api.backend, api.model, api.stateCharm, arg, api.deployApplicationFunc, api.storagePoolManager, api.registry, api.storageValidator)
err := deployApplication(
api.backend,
api.model,
api.stateCharm,
arg,
api.deployApplicationFunc,
api.storagePoolManager,
api.registry,
api.caasBroker,
)
result.Results[i].Error = common.ServerError(err)

if err != nil && len(arg.Resources) != 0 {
Expand Down Expand Up @@ -480,7 +489,7 @@ func deployApplication(
deployApplicationFunc func(ApplicationDeployer, DeployApplicationParams) (Application, error),
storagePoolManager poolmanager.PoolManager,
registry storage.ProviderRegistry,
storageValidator caas.StorageValidator,
caasBroker caas.Broker,
) error {
curl, err := charm.ParseURL(args.CharmURL)
if err != nil {
Expand All @@ -491,6 +500,7 @@ func deployApplication(
}

modelType := model.Type()
var caasVersion *version.Number
if modelType != state.ModelTypeIAAS {
if len(args.AttachStorage) > 0 {
return errors.Errorf(
Expand Down Expand Up @@ -528,7 +538,7 @@ func deployApplication(
return errors.Errorf(
"the %q storage pool requires a provider type of %q, not %q", poolName, k8s.K8s_ProviderType, sp.Provider)
}
if err := storageValidator.ValidateStorageClass(sp.Attributes); err != nil {
if err := caasBroker.ValidateStorageClass(sp.Attributes); err != nil {
return errors.Trace(err)
}

Expand All @@ -542,6 +552,11 @@ func deployApplication(
return errors.Annotatef(err, "getting workload storage params for %q", args.ApplicationName)
}
}

caasVersion, err = caasBroker.Version()
if err != nil {
return errors.Trace(err)
}
}

// This check is done early so that errors deeper in the call-stack do not
Expand All @@ -556,7 +571,7 @@ func deployApplication(
return errors.Trace(err)
}

if err := checkMinVersion(ch); err != nil {
if err := checkMinVersion(ch, caasVersion); err != nil {
return errors.Trace(err)
}

Expand Down
18 changes: 16 additions & 2 deletions apiserver/facades/client/application/charmstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func AddCharmWithAuthorizationAndRepo(st State, args params.AddCharmWithAuthoriz
return errors.Trace(err)
}

if err := checkMinVersion(downloadedCharm); err != nil {
if err := checkMinVersion(downloadedCharm, nil); err != nil {
return errors.Trace(err)
}

Expand Down Expand Up @@ -248,11 +248,25 @@ func openCSClient(csAPIURL string, args params.AddCharmWithAuthorization) (*cscl
return csClient, nil
}

func checkMinVersion(ch charm.Charm) error {
func checkMinVersion(ch charm.Charm, caasVersion *version.Number) (err error) {
// check Juju min version.
minver := ch.Meta().MinJujuVersion
if minver != version.Zero && minver.Compare(jujuversion.Current) > 0 {
return minVersionError(minver, jujuversion.Current)
}
if caasVersion == nil {
return nil
}
// check caas min version.
minver, err = version.Parse(ch.Meta().Deployment.MinVersion)
if err != nil {
return errors.Trace(err)
}
logger.Criticalf("MinVersion %#v, caasVersion %#v", minver, caasVersion)
if minver != version.Zero && minver.Compare(*caasVersion) > 0 {
// TODO: return a new error!!
return minVersionError(minver, *caasVersion)
}
return nil
}

Expand Down
1 change: 0 additions & 1 deletion apiserver/facades/client/charms/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ func convertCharmMeta(meta *charm.Meta) *params.CharmMeta {
Resources: convertCharmResourceMetaMap(meta.Resources),
Terms: meta.Terms,
MinJujuVersion: meta.MinJujuVersion.String(),
MinK8sVersion: meta.MinK8sVersion.String(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (f *Facade) provisioningInfo(model Model, tagString string) (*params.Kubern
info.DeploymentInfo = &params.KubernetesDeploymentInfo{
DeploymentType: string(deployInfo.DeploymentType),
ServiceType: string(deployInfo.ServiceType),
Demonset: deployInfo.Demonset,
Daemonset: deployInfo.Daemonset,
}
}
return info, nil
Expand Down
6 changes: 5 additions & 1 deletion apiserver/facades/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7259,6 +7259,9 @@
"KubernetesDeploymentInfo": {
"type": "object",
"properties": {
"daemonset": {
"type": "boolean"
},
"deployment-type": {
"type": "string"
},
Expand All @@ -7269,7 +7272,8 @@
"additionalProperties": false,
"required": [
"deployment-type",
"service-type"
"service-type",
"daemonset"
]
},
"KubernetesDeviceParams": {
Expand Down
1 change: 0 additions & 1 deletion apiserver/params/charms.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ type CharmMeta struct {
Resources map[string]CharmResourceMeta `json:"resources,omitempty"`
Terms []string `json:"terms,omitempty"`
MinJujuVersion string `json:"min-juju-version,omitempty"`
MinK8sVersion string `json:"min-k8s-version,omitempty"`
}

// CharmInfo holds all the charm data that the client needs.
Expand Down
2 changes: 1 addition & 1 deletion apiserver/params/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type KubernetesDeploymentInfo struct {
DeploymentType string `json:"deployment-type"`
ServiceType string `json:"service-type"`
Demonset bool `json:"demonset"`
Daemonset bool `json:"daemonset"`
}

// KubernetesProvisioningInfo holds unit provisioning info.
Expand Down
21 changes: 17 additions & 4 deletions caas/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ const (
// ServiceType defines a service type.
type ServiceType string

// IsOmit indicates if a service is required.
func (st ServiceType) IsOmit() bool {
return st == ServiceOmit
}

const (
ServiceCluster ServiceType = "cluster" // Todo(caas): deprecate this later.
ServiceClusterIP ServiceType = "clusterip"
ServiceCluster ServiceType = "cluster"
ServiceLoadBalancer ServiceType = "loadbalancer"
ServiceExternal ServiceType = "external"
ServiceOmit ServiceType = "omit"
Expand All @@ -93,8 +97,8 @@ const (
type DeploymentParams struct {
DeploymentType DeploymentType
ServiceType ServiceType
// TODO(caas): implement demonset support.
Demonset bool
// TODO(caas): implement daemonset support.
Daemonset bool
}

// ServiceParams defines parameters used to create a service.
Expand Down Expand Up @@ -194,6 +198,9 @@ type Broker interface {

// Upgrader provides the API to perform upgrades.
Upgrader

// ClusterVersionGetter provides methods to get cluster version information.
ClusterVersionGetter
}

// Upgrader provides the API to perform upgrades.
Expand All @@ -208,6 +215,12 @@ type StorageValidator interface {
ValidateStorageClass(config map[string]interface{}) error
}

// ClusterVersionGetter provides methods to get cluster version information.
type ClusterVersionGetter interface {
// Version returns cluster version information.
Version() (*version.Number, error)
}

// ServiceGetterSetter provides the API to get/set service.
type ServiceGetterSetter interface {
// EnsureService creates or updates a service for pods with the given params.
Expand Down
Loading

0 comments on commit 2e58799

Please sign in to comment.