Skip to content

Commit

Permalink
Make HostCloudRegion optional for non bootstrapping process on cmd level
Browse files Browse the repository at this point in the history
  • Loading branch information
ycliuhw committed May 24, 2019
1 parent e101e1f commit b91dfb2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
5 changes: 4 additions & 1 deletion caas/kubernetes/provider/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ func (c *controllerStack) getControllerSvcSpec(cloudType string) (*controllerSer

func (c *controllerStack) createControllerService() error {
svcName := c.resourceNameService
cloudType, _ := cloud.SplitHostCloudRegion(c.pcfg.Bootstrap.ControllerCloud.HostCloudRegion)
cloudType, _, err := cloud.SplitHostCloudRegion(c.pcfg.Bootstrap.ControllerCloud.HostCloudRegion)
if err != nil {
return errors.Trace(err)
}
controllerSvcSpec, err := c.getControllerSvcSpec(cloudType)
if err != nil {
return errors.Trace(err)
Expand Down
25 changes: 7 additions & 18 deletions caas/kubernetes/provider/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"reflect"
"strings"

"github.com/juju/errors"
"github.com/juju/utils"
Expand Down Expand Up @@ -41,6 +40,7 @@ type KubeCloudParams struct {
type KubeCloudStorageParams struct {
WorkloadStorage string
HostCloudRegion string
IsBootstrap bool
MetadataChecker caas.ClusterMetadataChecker
GetClusterMetadataFunc GetClusterMetadataFunc
}
Expand Down Expand Up @@ -115,22 +115,19 @@ func UpdateKubeCloudWithStorage(k8sCloud *cloud.Cloud, storageParams KubeCloudSt
clusterMetadata.Regions.SortedValues()[0],
)
}
if storageParams.HostCloudRegion == "" {
return fail(ClusterQueryError{})
}
_, region, err := ParseCloudRegion(storageParams.HostCloudRegion)
if err != nil {
return fail(errors.Annotatef(err, "validating cloud region %q", storageParams.HostCloudRegion))

cloudType, region, err := cloud.SplitHostCloudRegion(storageParams.HostCloudRegion)
if err != nil && storageParams.IsBootstrap {
// a valid host cloud region is required for bootstrapping to k8s.
return fail(errors.New("host cloud region is required for bootstrapping to k8s"))
}
k8sCloud.HostCloudRegion = storageParams.HostCloudRegion
k8sCloud.Regions = []cloud.Region{{
Name: region,
}}

// If the user has not specified storage, check that the cluster has Juju's opinionated defaults.
cloudType, _ := cloud.SplitHostCloudRegion(storageParams.HostCloudRegion)
err = storageParams.MetadataChecker.CheckDefaultWorkloadStorage(cloudType, clusterMetadata.NominatedStorageClass)

if storageParams.WorkloadStorage == "" {
if errors.IsNotFound(err) {
return fail(UnknownClusterError{CloudName: cloudType})
Expand Down Expand Up @@ -207,15 +204,6 @@ func UpdateKubeCloudWithStorage(k8sCloud *cloud.Cloud, storageParams KubeCloudSt
return storageMsg, nil
}

// ParseCloudRegion breaks apart a clusters cloud region.
func ParseCloudRegion(cloudRegion string) (string, string, error) {
fields := strings.SplitN(cloudRegion, "/", 2)
if len(fields) != 2 || fields[0] == "" || fields[1] == "" {
return "", "", errors.NotValidf("cloud region %q", cloudRegion)
}
return fields[0], fields[1], nil
}

// BaseKubeCloudOpenParams provides a basic OpenParams for a cluster
func BaseKubeCloudOpenParams(cloud cloud.Cloud, credential cloud.Credential) (environs.OpenParams, error) {
// To get a k8s client, we need a config with minimal information.
Expand Down Expand Up @@ -276,6 +264,7 @@ func (p kubernetesEnvironProvider) FinalizeCloud(ctx environs.FinalizeCloudConte
return cloud.Cloud{}, errors.Trace(err)
}
storageUpdateParams := KubeCloudStorageParams{
IsBootstrap: true,
MetadataChecker: broker,
GetClusterMetadataFunc: func(broker caas.ClusterMetadataChecker) (*caas.ClusterMetadata, error) {
clusterMetadata, err := broker.GetClusterMetadata("")
Expand Down
11 changes: 6 additions & 5 deletions caas/kubernetes/provider/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,22 @@ func (k *kubernetesClient) GetClusterMetadata(storageClass string) (*caas.Cluste
}

// We may have the workload storage but still need to look for operator storage.
preferredOperatorStorage, havePreferredOperatorStorage := jujuPreferredOperatorStorage[result.Cloud]
storageClasses, err := k.client().StorageV1().StorageClasses().List(v1.ListOptions{})
if err != nil {
return nil, errors.Annotate(err, "listing storage classes")
}

if discoverCDKStoragePreferences(storageClasses.Items, &result) {
// This is a CDK cluster with preferred storage class defined.
return &result, nil
}

var possibleWorkloadStorage, possibleOperatorStorage []*caas.StorageProvisioner
preferredOperatorStorage, hasPreferredOperatorStorage := jujuPreferredOperatorStorage[result.Cloud]
for _, sc := range storageClasses.Items {
caasSC := caasStorageProvisioner(sc)
isDefaultSc := isDefaultStorageClass(sc)
if result.OperatorStorageClass == nil && havePreferredOperatorStorage {
if result.OperatorStorageClass == nil && hasPreferredOperatorStorage {
if err := storageClassMatches(preferredOperatorStorage, caasSC); err == nil {
if isDefaultSc {
// Prefer operator storage from the default storage class.
Expand Down Expand Up @@ -193,10 +194,10 @@ func (k *kubernetesClient) listHostCloudRegions() (string, set.Strings, error) {
}

// CheckDefaultWorkloadStorage implements ClusterMetadataChecker.
func (k *kubernetesClient) CheckDefaultWorkloadStorage(cluster string, storageProvisioner *caas.StorageProvisioner) error {
preferredStorage, ok := jujuPreferredWorkloadStorage[cluster]
func (k *kubernetesClient) CheckDefaultWorkloadStorage(cloudType string, storageProvisioner *caas.StorageProvisioner) error {
preferredStorage, ok := jujuPreferredWorkloadStorage[cloudType]
if !ok {
return errors.NotFoundf("cluster %q", cluster)
return errors.NotFoundf("preferred workload storage for cloudType %q", cloudType)
}
return storageClassMatches(preferredStorage, storageProvisioner)
}
Expand Down
10 changes: 5 additions & 5 deletions cloud/clouds.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ type Cloud struct {
}

// SplitHostCloudRegion splits host cloud region to cloudType and region.
func SplitHostCloudRegion(hostCloudRegion string) (string, string) {
r := strings.Split(hostCloudRegion, "/")
if len(r) >= 2 {
return r[0], r[1]
func SplitHostCloudRegion(hostCloudRegion string) (string, string, error) {
fields := strings.SplitN(hostCloudRegion, "/", 2)
if len(fields) != 2 || fields[0] == "" || fields[1] == "" {
return "", "", errors.NotValidf("cloud region %q", hostCloudRegion)
}
return r[0], ""
return fields[0], fields[1], nil
}

// BuildHostCloudRegion combines cloudType with region to host cloud region.
Expand Down
6 changes: 4 additions & 2 deletions cmd/juju/caas/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func (c *AddCAASCommand) Run(ctx *cmd.Context) error {
storageParams := provider.KubeCloudStorageParams{
WorkloadStorage: c.workloadStorage,
HostCloudRegion: c.hostCloudRegion,
IsBootstrap: c.Local,
MetadataChecker: broker,
GetClusterMetadataFunc: c.getClusterMetadataFunc(ctx),
}
Expand Down Expand Up @@ -471,7 +472,7 @@ func (c *AddCAASCommand) newK8sClusterBroker(cloud jujucloud.Cloud, credential j
func (c *AddCAASCommand) validateCloudRegion(cloudRegion string) (_ string, err error) {
defer errors.DeferredAnnotatef(&err, "validating cloud region %q", cloudRegion)

cloudNameOrType, region, err := provider.ParseCloudRegion(cloudRegion)
cloudNameOrType, region, err := jujucloud.SplitHostCloudRegion(cloudRegion)
if err != nil {
return "", errors.Annotate(err, "parsing cloud region")
}
Expand Down Expand Up @@ -514,8 +515,9 @@ func (c *AddCAASCommand) getClusterMetadataFunc(ctx *cmd.Context) provider.GetCl
clusterMetadata, err := broker.GetClusterMetadata(c.workloadStorage)
if err != nil {
errChan <- err
} else {
result <- clusterMetadata
}
result <- clusterMetadata
}()

timeout := 30 * time.Second
Expand Down

0 comments on commit b91dfb2

Please sign in to comment.