github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/caas/kubernetes/provider/precheck.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package provider
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/juju/environs"
    11  	"github.com/juju/juju/environs/context"
    12  	"github.com/juju/utils/keyvalues"
    13  )
    14  
    15  // PrecheckInstance performs a preflight check on the specified
    16  // series and constraints, ensuring that they are possibly valid for
    17  // creating an instance in this model.
    18  //
    19  // PrecheckInstance is best effort, and not guaranteed to eliminate
    20  // all invalid parameters. If PrecheckInstance returns nil, it is not
    21  // guaranteed that the constraints are valid; if a non-nil error is
    22  // returned, then the constraints are definitely invalid.
    23  func (k *kubernetesClient) PrecheckInstance(ctx context.ProviderCallContext, params environs.PrecheckInstanceParams) error {
    24  	// Ensure there are no unsupported constraints.
    25  	// Clouds generally don't enforce this but we will
    26  	// for Kubernetes deployments.
    27  	validator, err := k.ConstraintsValidator(ctx)
    28  	if err != nil {
    29  		return errors.Trace(err)
    30  	}
    31  	unsupported, err := validator.Validate(params.Constraints)
    32  	if err != nil {
    33  		return errors.NotValidf("constraints %q", params.Constraints.String())
    34  	}
    35  	if len(unsupported) > 0 {
    36  		return errors.NotSupportedf("constraints %v", strings.Join(unsupported, ","))
    37  	}
    38  
    39  	if params.Series != "kubernetes" {
    40  		return errors.NotValidf("series %q", params.Series)
    41  	}
    42  
    43  	if params.Placement == "" {
    44  		return nil
    45  	}
    46  
    47  	// Check placement is valid.
    48  	// TODO(caas) - check for valid node labels?
    49  	// Placement is a comma separated list of key-value pairs (node labels).
    50  	_, err = keyvalues.Parse(strings.Split(params.Placement, ","), false)
    51  	if err != nil {
    52  		return errors.NotValidf("placement directive %q", params.Placement)
    53  	}
    54  	return nil
    55  }