Skip to content
Permalink
Browse files

Validate Application Cluster Names In shipperctl admin clusters apply (

…#64)

* - Moved validation of `shipperctl admin clusters apply` higher up in the
process, to prevent the command leaving the configuration in a
half-state because of a validation error
- Added an extra line of validation to validate that cluster names are
DNS-1123 subdomain compliant.

* Added headers to examples in the docs, and made the error message link
to the relevant example.

* Updated dependencies.

* Fixed a linting error

* - Made the title for the new section in the doc clearer, and also
  changed the link in the CLI output
- Used `%q` in the error output to have Go automatically quote the
  error message, instead of doing it by hand
- Changed `ValidateConfiguration` to `ValidateApplicationClusters`
  which is more "true" ;-)
  • Loading branch information...
parhamdoustdar authored and icanhazbroccoli committed Apr 2, 2019
1 parent 5a8ddb1 commit ff70e7e794bebe73f8409f3533810b2be8b82dff
Showing with 29 additions and 5 deletions.
  1. +1 −0 Gopkg.lock
  2. +19 −5 cmd/shipperctl/cmd/apply.go
  3. 0 docs/build.sh
  4. +9 −0 docs/operations/shipperctl.rst

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -9,6 +9,7 @@ import (
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/validation"

"github.com/bookingcom/shipper/cmd/shipperctl/config"
"github.com/bookingcom/shipper/cmd/shipperctl/configurator"
@@ -63,6 +64,10 @@ func runApplyClustersCommand(cmd *cobra.Command, args []string) error {
return err
}

if err := validateApplicationClusters(clustersConfiguration); err != nil {
return err
}

for _, managementCluster := range clustersConfiguration.ManagementClusters {
cmd.Printf("Setting up management cluster %s:\n", managementCluster.Name)
if err := setupManagementCluster(managementCluster, cmd); err != nil {
@@ -94,6 +99,20 @@ func runApplyClustersCommand(cmd *cobra.Command, args []string) error {
return nil
}

func validateApplicationClusters(configuration *config.ClustersConfiguration) error {
for _, cluster := range configuration.ApplicationClusters {
if msgs := validation.IsDNS1123Subdomain(cluster.Name); len(msgs) > 0 {
return fmt.Errorf("%q is not a valid cluster name in Kubernetes. If this is the name of a context in your Kubernetes configuration, check out the relevant example at https://docs.shipper-k8s.io/en/latest/operations/shipperctl.html#using-google-kubernetes-engine-gke-context-names", cluster.Name)
}

if cluster.Region == "" {
return fmt.Errorf("you must specify region for cluster %s", cluster.Name)
}
}

return nil
}

func setupManagementCluster(managementCluster *config.ClusterConfiguration, cmd *cobra.Command) error {
configurator, err := configurator.NewClusterConfigurator(managementCluster, kubeConfigFile)
if err != nil {
@@ -367,17 +386,12 @@ func copySecretFromApplicationToManagementCluster(cmd *cobra.Command, applicatio

func createApplicationClusterObjectOnManagementCluster(cmd *cobra.Command, managementClusterConfigurator *configurator.Cluster, applicationCluster *config.ClusterConfiguration, host string) error {
cmd.Printf("Creating or updating the cluster object for cluster %s on the management cluster... ", applicationCluster.Name)
// Doing a priliminary validation
if applicationCluster.Region == "" {
return fmt.Errorf("must specify region for cluster %s", applicationCluster.Name)
}

// Initialize the map of capabilities if it's null so that we
// don't fire an error when creating it
if applicationCluster.Capabilities == nil {
applicationCluster.Capabilities = []string{}
}

if err := managementClusterConfigurator.CreateOrUpdateClusterWithConfig(applicationCluster, host); err != nil {
return err
}
0 docs/build.sh 100644 → 100755
No changes.
@@ -52,6 +52,9 @@ For each item in the list of **management** or **application** clusters, you can
Examples
^^^^^^^^

Minimal Configuration
~~~~~~~~~~~~~~~~~~~~~

Here is a minimal configuration to set up a local *minikube* instance:

.. code-block:: yaml
@@ -64,6 +67,9 @@ Here is a minimal configuration to set up a local *minikube* instance:
This way, setting up an environment to run Shipper in *Docker For Desktop*, for example, is as easy as creating a list of ``managementClusters`` and a list of ``applicationClusters``, and specifying ``docker-for-desktop`` as the name.

Specifying Cluster Fields
~~~~~~~~~~~~~~~~~~~~~~~~~

Here is something more interesting: having 2 application clusters, and marking one of them as unschedulable:

.. code-block:: yaml
@@ -78,6 +84,9 @@ Here is something more interesting: having 2 application clusters, and marking o
scheduler:
unschedulable: true
Using Google Kubernetes Engine (GKE) Context Names
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you're running on GKE, your cluster context names are likely to have underscores in them, like this: ``gke_ACCOUNT_ZONE_CLUSTERNAME``. ``shipperctl``'s usage of the context name as the name of the Cluster object will break, because Kubernetes objects are not allowed to have underscores in their names. To solve this, specify ``context`` explicitly in ``clusters.yaml``, like so:

.. code-block:: yaml

0 comments on commit ff70e7e

Please sign in to comment.
You can’t perform that action at this time.