Skip to content

Commit

Permalink
Implement resource tagging to deploy groups of resources easily. (#30)
Browse files Browse the repository at this point in the history
* Pre-testing implementation of tagged resources.

* Fix incorrect command attaching tags flags.

* Clean up

* Fix incorrectly named flag

* Fix indenting

* drop outdated comment
  • Loading branch information
Eagerod authored Oct 3, 2020
1 parent 5c8e675 commit 2bcabc6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
35 changes: 32 additions & 3 deletions cmd/hope/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ import (
"github.com/Eagerod/hope/pkg/kubeutil"
)

var deployCmdTagSlice *[]string

func initDeployCmdFlags() {
deployCmdTagSlice = deployCmd.Flags().StringArrayP("tag", "t", []string{}, "deploy resources with this tag")
}

var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy a Kubernetes yaml file",
Short: "Deploy Kubernetes resources defined in the hope file",
RunE: func(cmd *cobra.Command, args []string) error {
resources, err := getResources()
if err != nil {
return err
}

// TODO: Re-evaluate; maybe just deploy everything in order defined.
if len(args) != 0 && len(*deployCmdTagSlice) != 0 {
return errors.New("Cannot deploy tags and named resources together.")
}

masters := viper.GetStringSlice("masters")
kubectl, err := getKubectlFromAnyMaster(log.WithFields(log.Fields{}), masters)
if err != nil {
Expand All @@ -39,8 +50,26 @@ var deployCmd = &cobra.Command{
resourcesToDeploy := []Resource{}

if len(args) == 0 {
log.Debug("Received no arguments for deployment. Deploying all resources.")
resourcesToDeploy = *resources
if len(*deployCmdTagSlice) != 0 {
tagMap := map[string]bool{}
for _, tag := range *deployCmdTagSlice {
tagMap[tag] = true
}

for _, resource := range *resources {
for _, tag := range resource.Tags {
if _, ok := tagMap[tag]; ok {
resourcesToDeploy = append(resourcesToDeploy, resource)
continue
}
}
}

log.Debug("Deploying these resources: \n\t", strings.Join(args, "\n\t"), "\nFrom provided tags.")
} else {
log.Debug("Received no arguments for deployment. Deploying all resources.")
resourcesToDeploy = *resources
}
} else {
log.Debug("Deploying these resources: \n\t", strings.Join(args, "\n\t"), "\nIn the order given.")

Expand Down
1 change: 1 addition & 0 deletions cmd/hope/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Execute() {
rootCmd.AddCommand(sshCmd)
rootCmd.AddCommand(tokenCmd)

initDeployCmdFlags()
initHostnameCmdFlags()
initKubeconfigCmdFlags()
initResetCmd()
Expand Down
1 change: 1 addition & 0 deletions cmd/hope/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Resource struct {
Build BuildSpec
Job string
Exec ExecSpec
Tags []string
}

// TODO: Allow jobs to define max retry parameters, or accept them on the
Expand Down
12 changes: 10 additions & 2 deletions hope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ resources:
# It will always be preferred that urls appearing here are to stable yaml
# files available on the Internet, rather than anything loaded from the
# local machine.
# Local paths can be used for testing, but ultimately, whatever ends up being
# deployed should be done so using GitHub tags/releases.
# Local paths can be used for testing, but ultimately, whatever ends up
# being deployed should be done so using GitHub tags/releases.
# Every resource can provide a set of tags that will allow for the
# deployment of multiple resources without naming every resource
# explicitly.
- name: calico
file: https://docs.projectcalico.org/manifests/calico.yaml
tags: [network]
# Inline definitions are also supported.
# These values have to be provided as yaml strings under the `inline` key.
# Values passed in through `inline` will be fed through `envsubst` before
Expand Down Expand Up @@ -52,6 +56,7 @@ resources:
namespace: metallb-system
parameters:
- METALLB_SYSTEM_MEMBERLIST_SECRET_KEY
tags: [network]
# Build and push a docker image to the registry.
# Doesn't include a kubectl command at all, so that can be done in a step
# after a step like this appears.
Expand All @@ -64,6 +69,7 @@ resources:
build:
path: some-dir-with-dockerfile
tag: registry.internal.aleemhaji.com/example-repo:latest
tags: [app1]
# When a spec comes with an initialization procedure, a job type can be used.
# These will wait until the job with the specified name is completed.
# If the job fails, the deployment stops so that other resources that may
Expand All @@ -73,6 +79,7 @@ resources:
# set to be deleted by any mechanism.
- name: wait-for-some-kind-of-job
job: init-the-database
tags: [database]
# If something needs to be executed against pods of an existing set of pods,
# the exec resource will run a script against a running instance.
# A timeout can optionally be provided to wait for pods to start.
Expand All @@ -84,6 +91,7 @@ resources:
- mysql
- -e
- select 1;
tags: [database]
# Jobs contains a collection of specifications of templated jobs that can be
# run on demand in the cluster.
# These jobs shouldn't be associated to the deployment of any particular
Expand Down

0 comments on commit 2bcabc6

Please sign in to comment.