diff --git a/.gitignore b/.gitignore index e2227a57b..c61a717ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,6 @@ -*~ -.*.swp -*.pyc -.project -nohup.out -/.coverage -/bin -/vendor/* -/rootfs/manager/bin/manager -/rootfs/manager/bin/kubectl -/rootfs/manager/bin/v1.* -/rootfs/resourcifier/bin/resourcifier -/rootfs/resourcifier/bin/kubectl -/rootfs/resourcifier/bin/v1.* -/rootfs/expandybird/bin/expandybird -/rootfs/expandybird/opt/expansion -.DS_Store -/log/ -/scripts/env.sh +.coverage/ +bin/ +rootfs/helm +rootfs/tiller +vendor/ +_proto/*.pb.go diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d1a245afe..00336c131 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,56 +15,12 @@ Follow either of the two links above to access the appropriate CLA and instructi ***NOTE***: Only original source code from you and other people that have signed the CLA can be accepted into the main repository. -## Development Lifecycle - -The project uses a combination of milestones and priority labels on GitHub issues to help development flow smoothly. While exceptions may be required on occasion, the team observes the following guidelines: - -* At appropriate intervals, the Helm team creates a milestone and assigns - issues to it. This represents the team's priorities and intent. -* PRs/Issues related to the current milestone are prioritized over other PRs. -* PRs/Issues that fix a broken master build (or meet other P0 criteria) are - prioritized over other PRs. - ## How to Contribute A Patch -### Overview - -1. Submit an issue describing your proposed change to the repo in question. -1. A collaborator will respond to your issue. -1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). +1. If you haven't already done so, sign a Contributor License Agreement (see details above). 1. Fork the desired repo, develop and test your code changes. 1. Submit a pull request. -### Feature Proposals - -Before adding a feature or making a major change to the code, open an -issue marked as a _proposal_ and explain your idea. For complex changes, -you may be asked to produce a design document. - -### Single Issue - -When fixing or implementing a GitHub issue, resist the temptation to refactor nearby code or to fix that potential bug you noticed. Instead, open a new pull request just for that change. - -Keeping concerns separated allows pull requests to be tested, reviewed, and merged more quickly. - -Squash and rebase the commit or commits in your pull request into logical units of work with `git`. Include tests and documentation changes in the same commit, so that a revert would remove all traces of the feature or fix. - -If a PR completely resolves an existing issue, this should be noted. In the PR description–not in the commit itself–include a line such as "Closes #1234". The issue referenced will then be closed when your PR is merged. If it otherwise relates to an existing issue, that should be noted in the comment as well. - -### Include Tests & Documentation - -If you change or add functionality, your changes should include the necessary tests to prove that it works. While working on local code changes, always run the tests. Any change that could affect a user's experience also needs a change or addition to the relevant documentation. - -Pull requests that do not include sufficient tests or documentation will be rejected. - -***NOTE***: Please note that we are currently using Go version 1.6, and tests will fail if you run them on any other version of Go. - -### Coding Standards - -Go code should always be run through `gofmt` on the default settings. Lines of code may be up to 99 characters long. Documentation strings and tests are required for all public methods. Use of third-party go packages should be minimal, but when doing so, vendor code using [Glide](http://glide.sh/). - -Python code should conform to [PEP8](https://www.python.org/dev/peps/pep-0008/). - ### Merge Approval Helm collaborators may add "LGTM" (Looks Good To Me) or an equivalent comment to indicate that a PR is acceptable. Any change requires at least one LGTM. No pull requests can be merged until at least one Helm collaborator signs off with an LGTM. diff --git a/MAINTAINERS.md b/MAINTAINERS.md deleted file mode 100644 index 235618051..000000000 --- a/MAINTAINERS.md +++ /dev/null @@ -1,61 +0,0 @@ -# Helm Maintainers - -This document explains the leadership structure of the Kubernetes Helm project, and list the current project maintainers. - -## What is a Maintainer? - -(Unabashedly stolen from the [Docker](https://github.com/docker/docker/blob/master/MAINTAINERS) project) - -There are different types of maintainers, with different responsibilities, but -all maintainers have 3 things in common: - -1. They share responsibility in the project's success. -2. They have made a long-term, recurring time investment to improve the project. -3. They spend that time doing whatever needs to be done, not necessarily what -is the most interesting or fun. - -## Types of Maintainers - -The Helm project includes two types of official maintainers: maintainers and core maintainers. - -### Helm Maintainers - -Helm maintainers are developers who have commit access to the Helm repository. -The duties of a maintainer include: - -* Classify and respond to GitHub issues and review pull requests -* Perform code reviews -* Shape the Helm roadmap and lead efforts to accomplish roadmap milestones -* Participate actively in feature development and bug fixing -* Answer questions and help users -* Participate in planning meetings - -### Helm Core Maintainers - -In addition to the duties of a Maintainer, Helm Core Maintainers also: - -* Coordinate planning meetings -* Triage GitHub issues for milestone planning -* Escalate emergency issues (broken builds, security flaws) outside of - the normal planning process - -The current core maintainers of Helm: - -* Jack Greenfield - [@jackgr](https://github.com/jackgr) -* Matt Butcher - [@technosophos](https://github.com/technosophos) - -## Project Planning - -The Helm team holds regular planning meetings to set the project direction, milestones, and relative prioritization of issues. Planning meetings are coordinated via the #Helm room in the [Kubernetes Slack](http://slack.kubernetes.io/). - -In order to solicit feedback from the community, planning meetings are run in public whenever possible. - -## Becoming a Maintainer - -Generally, potential maintainers are selected by the existing core maintainers based in part on the following criteria: - -* Sustained contributions to the project over a period of time (usually months) -* A willingness to help users on GitHub and in the [#Helm Slack room](http://slack.kubernetes.io/) -* A friendly attitude - -The Helm core maintainers must unanimously agree before inviting a community member to join as a maintainer, although in many cases the candidate has already been acting in the capacity of a maintainer for some time, and has been consulted on issues, pull requests, etc. \ No newline at end of file diff --git a/Makefile b/Makefile index aba820c6b..fd5e64151 100644 --- a/Makefile +++ b/Makefile @@ -1,118 +1,73 @@ -# Copyright 2015 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -GO_DIRS ?= $(shell glide nv -x ) -GO_PKGS ?= $(shell glide nv) - -ROOTFS := rootfs -CLIENT := cmd/helm - -.PHONY: info -info: - $(MAKE) -C $(ROOTFS) $@ - -.PHONY: gocheck -ifndef GOPATH - $(error No GOPATH set) -endif - -.PHONY: build -build: gocheck - @scripts/build-go.sh - -.PHONY: build-static -build-static: gocheck - @BUILD_TYPE=STATIC scripts/build-go.sh - -.PHONY: build-cross -build-cross: gocheck - @BUILD_TYPE=CROSS scripts/build-go.sh +DOCKER_REGISTRY ?= gcr.io +IMAGE_PREFIX ?= deis-sandbox +SHORT_NAME ?= tiller + +# go option +GO ?= go +GOARCH ?= $(shell go env GOARCH) +GOOS ?= $(shell go env GOOS) +PKG := $(shell glide novendor) +TAGS := +TESTS := . +TESTFLAGS := +LDFLAGS := +GOFLAGS := +BINDIR := ./bin +BINARIES := helm tiller + +include versioning.mk .PHONY: all all: build -.PHONY: clean -clean: - $(MAKE) -C $(ROOTFS) $@ - go clean -v $(GO_PKGS) - rm -rf bin +.PHONY: build +build: GOFLAGS += -a -installsuffix cgo +build: + @for i in $(BINARIES); do \ + CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build -o $(BINDIR)/$$i $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./cmd/$$i || exit 1; \ + done + +.PHONY: check-docker +check-docker: + @if [ -z $$(which docker) ]; then \ + echo "Missing \`docker\` client which is required for development"; \ + exit 2; \ + fi + +.PHONY: docker-binary +docker-binary: GOOS = linux +docker-binary: GOARCH = amd64 +docker-binary: BINDIR = ./rootfs +docker-binary: build + +.PHONY: docker-build +docker-build: check-docker docker-binary + docker build --rm -t ${IMAGE} rootfs + docker tag -f ${IMAGE} ${MUTABLE_IMAGE} .PHONY: test -test: build test-style test-unit test-flake8 - -.PHONY: quicktest -quicktest: test-style - go test $(GO_PKGS) - -.PHONY: push -push: push-server push-client - -.PHONY: push-server -push-server: build-static - $(MAKE) -C $(ROOTFS) push - -.PHONY: push-client -push-client: gocheck - @BUILD_TYPE=CROSS scripts/build-go.sh $(CLIENT) - $(MAKE) -C $(CLIENT) push - -.PHONY: container -container: build-static - $(MAKE) -C $(ROOTFS) $@ +test: build +test: TESTFLAGS += -race -v +test: test-style +test: test-unit .PHONY: test-unit test-unit: - @echo Running tests... - go test -race -v $(GO_PKGS) - -.PHONY: test-flake8 -test-flake8: - @echo Running flake8... - flake8 expansion - @echo ---------------- + $(GO) test $(GOFLAGS) -run $(TESTS) $(PKG) $(TESTFLAGS) .PHONY: test-style test-style: @scripts/validate-go.sh -HAS_GLIDE := $(shell command -v glide;) -HAS_GOLINT := $(shell command -v golint;) -HAS_GOVET := $(shell command -v go tool vet;) -HAS_GOX := $(shell command -v gox;) -HAS_PIP := $(shell command -v pip;) -HAS_FLAKE8 := $(shell command -v flake8;) +.PHONY: clean +clean: + @rm -rf $(BINDIR) + +.PHONY: coverage +coverage: + @scripts/coverage.sh .PHONY: bootstrap bootstrap: - @echo Installing deps -ifndef HAS_PIP - $(error Please install the latest version of Python pip) -endif -ifndef HAS_GLIDE - go get -u github.com/Masterminds/glide -endif -ifndef HAS_GOLINT - go get -u github.com/golang/lint/golint -endif -ifndef HAS_GOVET - go get -u golang.org/x/tools/cmd/vet -endif -ifndef HAS_GOX - go get -u github.com/mitchellh/gox -endif -ifndef HAS_FLAKE8 - pip install flake8 -endif glide install - pip install --user -r expansion/requirements.txt + diff --git a/README.md b/README.md index 31cd2761d..81872159e 100644 --- a/README.md +++ b/README.md @@ -1,162 +1,38 @@ -# Helm +# Kubernetes Helm -[![Circle CI](https://circleci.com/gh/kubernetes/helm.svg?style=svg)](https://circleci.com/gh/kubernetes/helm) [![Go Report Card](http://goreportcard.com/badge/kubernetes/helm)](http://goreportcard.com/report/kubernetes/helm) +Helm is a tool for managing Kubernetes charts. Charts are packages of +pre-configured Kubernetes resources. -Helm makes it easy to create, describe, update and -delete Kubernetes resources using declarative configuration. A configuration is -just a `YAML` file that configures Kubernetes resources or supplies parameters -to templates. +## Install -Helm Manager runs server side, in your Kubernetes cluster, so it can tell you what templates -you've instantiated there, what resources they created, and even how the resources -are organized. So, for example, you can ask questions like: +Helm is in its early stages of development. At this time there are no +releases. -* What Redis instances are running in this cluster? -* What Redis master and slave services are part of this Redis instance? -* What pods are part of this Redis slave? +To install Helm from source, follow this process: -The official Helm repository of charts is available in the -[kubernetes/charts](https://github.com/kubernetes/charts) repository. +Make sure you have the prerequisites: +- Go 1.6 +- A running Kubernetes cluster +- `kubectl` properly configured to talk to your cluster +- Glide 0.10 or greater -Please hang out with us in [the Slack chat room](https://kubernetes.slack.com/messages/helm/). +1. Clone (or otherwise download) this repository +2. Run `make boostrap build` -## Installing Helm +You will now have two binaries built: -Note: if you're exploring or using the project, you'll probably want to pull -[the latest release](https://github.com/kubernetes/helm/releases/latest), -since there may be undiscovered or unresolved issues at HEAD. +- `bin/helm` is the client +- `bin/tiller` is the server -From a Linux or Mac OS X client: +You can locally run Tiller, or you build a Docker image (`make +docker-build`) and then deploy it (`helm init -i IMAGE_NAME`). -``` -$ git clone https://github.com/kubernetes/helm.git -$ cd helm -$ make build -$ bin/helm server install -``` +The [documentation](docs) folder contains more information about the +architecture and usage of Helm/Tiller. -That's it. You can now use `kubectl` to see Helm running in your cluster like this: +## The History of the Project -``` -$ kubectl get pod,rc,service --namespace=helm -NAME READY STATUS RESTARTS AGE -expandybird-rc-e0whp 1/1 Running 0 35m -expandybird-rc-zdp8w 1/1 Running 0 35m -manager-rc-bl4i4 1/1 Running 0 35m -resourcifier-rc-21clg 1/1 Running 0 35m -resourcifier-rc-i2zhi 1/1 Running 0 35m -NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE -expandybird-service 10.0.0.248 8081/TCP 35m -manager-service 10.0.0.49 8080/TCP 35m -resourcifier-service 10.0.0.184 8082/TCP 35m -NAME DESIRED CURRENT AGE -expandybird-rc 2 2 35m -manager-rc 1 1 35m -resourcifier-rc 2 2 35m -``` - -If you see expandybird, manager and resourcifier services, as well as expandybird, manager and resourcifier replication controllers with pods that are READY, then Helm is up and running! - -## Using Helm - -Run a Kubernetes proxy to allow the Helm client to connect to the remote cluster: - -``` -kubectl proxy --port=8001 & -``` - -Configure the HELM_HOST environment variable to let the local Helm client talk to the Helm manager service running in your remote Kubernetes cluster using the proxy. - -``` -export HELM_HOST=http://localhost:8001/api/v1/proxy/namespaces/helm/services/manager-service:manager -``` - -## Installing Charts - -To quickly deploy a chart, you can use the Helm command line tool. - -Currently here is the step by step guide. - -First add a respository of Charts used for testing: - -``` -$ bin/helm repo add kubernetes-charts-testing gs://kubernetes-charts-testing -``` - -Then deploy a Chart from this repository. For example to start a Redis cluster: - -``` -$ bin/helm deploy --name test --properties "workers=2" gs://kubernetes-charts-testing/redis-2.0.0.tgz -``` -The command above will create a helm "deployment" called `test` using the `redis-2.0.0.tgz` chart stored in the google storage bucket `kubernetes-charts-testing`. - -`$ bin/helm deployment describe test` will allow you to see the status of the resources you just created using the redis-2.0.0.tgz chart. You can also use kubectl to see the the same resources. It'll look like this: - -``` -$ kubectl get pods,svc,rc -NAME READY STATUS RESTARTS AGE -barfoo-barfoo 5/5 Running 0 45m -redis-master-rc-8wrqt 1/1 Running 0 41m -redis-slave-rc-6ptx6 1/1 Running 0 41m -redis-slave-rc-yc12q 1/1 Running 0 41m -NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE -kubernetes 10.0.0.1 443/TCP 45m -redis-master 10.0.0.67 6379/TCP 41m -redis-slave 10.0.0.168 6379/TCP 41m -NAME DESIRED CURRENT AGE -redis-master-rc 1 1 41m -redis-slave-rc 2 2 41m -``` - -To connect to your Redis master with a local `redis-cli` just use `kubectl port-forward` in a similar manner to: - -``` -$ kubectl port-forward redis-master-rc-8wrqt 6379:639 & -$ redis-cli -127.0.0.1:6379> info -... -role:master -connected_slaves:2 -slave0:ip=172.17.0.10,port=6379,state=online,offset=925,lag=0 -slave1:ip=172.17.0.11,port=6379,state=online,offset=925,lag=1 -``` - -Once you are done, you can delete your deployment with - -``` -$ bin/helm deployment list -test -$ bin/helm deployment rm test -```` - -## Uninstalling Helm from Kubernetes - -You can uninstall Helm entirely using the following command: - -``` -$ bin/helm server uninstall -``` - -This command will remove everything in the Helm namespace being used. - -## Design of Helm - -There is a more detailed [design document](docs/design/design.md) available. - -## Status of the Project - -This project is still under active development, so you might run into issues. If -you do, please don't be shy about letting us know, or better yet, contribute a -fix or feature. - -## Contributing -Your contributions are welcome. - -We use the same [workflow](https://github.com/kubernetes/kubernetes/blob/master/docs/devel/development.md#git-setup), -[License](LICENSE) and [Contributor License Agreement](CONTRIBUTING.md) as the main Kubernetes repository. - -## Relationship to Google Cloud Platform's Deployment Manager and Deis's Helm -Kubernetes Helm represent a merge of Google's Deployment Manager (DM) and the original Helm from Deis. -Kubernetes Helm uses many of the same concepts and languages as -[Google Cloud Deployment Manager](https://cloud.google.com/deployment-manager/overview), -but creates resources in Kubernetes clusters, not in Google Cloud Platform projects. It also brings several concepts from the original Helm such as Charts. +Kubernetes Helm is the merged result of [Helm +Classic](https://github.com/helm/helm) and the Kubernetes port of GCS Deployment +Manager. The project was jointly started by Google and Deis, though it +is now part of the CNCF. diff --git a/_proto/Makefile b/_proto/Makefile new file mode 100644 index 000000000..756934a0b --- /dev/null +++ b/_proto/Makefile @@ -0,0 +1,37 @@ +space := $(empty) $(empty) +comma := , +empty := + +import_path = github.com/kubernetes/helm/pkg/proto/hapi + +dst = ../pkg/proto +target = go +plugins = grpc + +chart_ias = $(subst $(space),$(comma),$(addsuffix =$(import_path)/$(chart_pkg),$(addprefix M,$(chart_pbs)))) +chart_pbs = $(wildcard hapi/chart/*.proto) +chart_pkg = chart + +release_ias = $(subst $(space),$(comma),$(addsuffix =$(import_path)/$(release_pkg),$(addprefix M,$(release_pbs)))) +release_pbs = $(wildcard hapi/release/*.proto) +release_pkg = release + +services_ias = $(subst $(space),$(comma),$(addsuffix =$(import_path)/$(services_pkg),$(addprefix M,$(services_pbs)))) +services_pbs = $(wildcard hapi/services/*.proto) +services_pkg = services + +google_deps = Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp,Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any + +all: chart release services + +chart: + protoc --$(target)_out=plugins=$(plugins),$(google_deps),$(chart_ias):$(dst) $(chart_pbs) + +release: + protoc --$(target)_out=plugins=$(plugins),$(google_deps),$(chart_ias):$(dst) $(release_pbs) + +services: + protoc --$(target)_out=plugins=$(plugins),$(google_deps),$(chart_ias),$(release_ias):$(dst) $(services_pbs) + +clean: + @rm -rf $(dst)/hapi 2>/dev/null diff --git a/_proto/hapi/chart/chart.proto b/_proto/hapi/chart/chart.proto new file mode 100644 index 000000000..90e4938cc --- /dev/null +++ b/_proto/hapi/chart/chart.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +package hapi.chart; + +import "hapi/chart/config.proto"; +import "hapi/chart/metadata.proto"; +import "hapi/chart/template.proto"; + +option go_package = "chart"; + +// +// Chart: +// A chart is a helm package that contains metadata, a default config, zero or more +// optionally parameterizable templates, and zero or more charts (dependencies). +// +message Chart { + // Contents of the Chartfile. + hapi.chart.Metadata metadata = 1; + + // Templates for this chart. + repeated hapi.chart.Template templates = 2; + + // Charts that this chart depends on. + repeated Chart dependencies = 3; + + // Default config for this template. + hapi.chart.Config values = 4; + +} diff --git a/_proto/hapi/chart/config.proto b/_proto/hapi/chart/config.proto new file mode 100644 index 000000000..0829632ac --- /dev/null +++ b/_proto/hapi/chart/config.proto @@ -0,0 +1,25 @@ +syntax = "proto3"; + +package hapi.chart; + +option go_package = "chart"; + +// +// Config: +// +// A config supplies values to the parametrizable templates of a chart. +// +message Config { + string raw = 1; + + map values = 2; +} + +// +// Value: +// +// TODO +// +message Value { + string value = 1; +} diff --git a/_proto/hapi/chart/metadata.proto b/_proto/hapi/chart/metadata.proto new file mode 100644 index 000000000..08e03985d --- /dev/null +++ b/_proto/hapi/chart/metadata.proto @@ -0,0 +1,49 @@ +syntax = "proto3"; + +package hapi.chart; + +option go_package = "chart"; + +// +// Maintainer: +// +// A descriptor of the Chart maintainer(s). +// +message Maintainer { + // Name is a user name or organization name + string name = 1; + + // Email is an optional email address to contact the named maintainer + string email = 2; +} + +// +// Metadata: +// +// Metadata for a Chart file. This models the structure +// of a Chart.yaml file. +// +// Spec: https://github.com/kubernetes/helm/blob/master/docs/design/chart_format.md#the-chart-file +// +message Metadata { + // The name of the chart + string name = 1; + + // The URL to a relecant project page, git repo, or contact person + string home = 2; + + // Source is the URL to the source code of this chart + repeated string sources = 3; + + // A SemVer 2 conformant version string of the chart + string version = 4; + + // A one-sentence description of the chart + string description = 5; + + // A list of string keywords + repeated string keywords = 6; + + // A list of name and URL/email address combinations for the maintainer(s) + repeated Maintainer maintainers = 7; +} diff --git a/_proto/hapi/chart/template.proto b/_proto/hapi/chart/template.proto new file mode 100644 index 000000000..3e68113c2 --- /dev/null +++ b/_proto/hapi/chart/template.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package hapi.chart; + +option go_package = "chart"; + +// Template represents a template as a name/value pair. +// +// By convention, name is a relative path within the scope of the chart's +// base directory. +message Template { + // Name is the path-like name of the template. + string name = 1; + + // Data is the template as byte data. + bytes data = 2; +} diff --git a/_proto/hapi/release/info.proto b/_proto/hapi/release/info.proto new file mode 100644 index 000000000..382f4acfc --- /dev/null +++ b/_proto/hapi/release/info.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; + +package hapi.release; + +import "google/protobuf/timestamp.proto"; +import "hapi/release/status.proto"; + +option go_package = "release"; + +// +// Info: +// +// +message Info { + Status status = 1; + + google.protobuf.Timestamp first_deployed = 2; + + google.protobuf.Timestamp last_deployed = 3; + + // Deleted tracks when this object was deleted. + google.protobuf.Timestamp deleted = 4; +} diff --git a/_proto/hapi/release/release.proto b/_proto/hapi/release/release.proto new file mode 100644 index 000000000..52ba2cd44 --- /dev/null +++ b/_proto/hapi/release/release.proto @@ -0,0 +1,33 @@ +syntax = "proto3"; + +package hapi.release; + +import "hapi/release/info.proto"; +import "hapi/chart/config.proto"; +import "hapi/chart/chart.proto"; + +option go_package = "release"; + +// +// Release: +// +// A release describes a deployment of a chart, together with the chart +// and the variables used to deploy that chart. +// +message Release { + // Name is the name of the release + string name = 1; + + // Info provides information about a release + hapi.release.Info info = 2; + + // Chart is the chart that was released. + hapi.chart.Chart chart = 3; + + // Config is the set of extra Values added to the chart. + // These values override the default values inside of the chart. + hapi.chart.Config config = 4; + + // Manifest is the string representation of the rendered template. + string manifest = 5; +} diff --git a/_proto/hapi/release/status.proto b/_proto/hapi/release/status.proto new file mode 100644 index 000000000..9ec021005 --- /dev/null +++ b/_proto/hapi/release/status.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +package hapi.release; + +import "google/protobuf/any.proto"; + +option go_package = "release"; + +// +// Status: +// +// +message Status { + enum Code { + UNKNOWN = 0; + + DEPLOYED = 1; + + DELETED = 2; + + SUPERSEDED = 3; + } + + Code code = 1; + + google.protobuf.Any details = 2; +} diff --git a/_proto/hapi/services/probe.proto b/_proto/hapi/services/probe.proto new file mode 100644 index 000000000..062b37bdb --- /dev/null +++ b/_proto/hapi/services/probe.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package hapi.services.probe; + +option go_package = "services"; + +service ProbeService { + rpc Ready(ReadyRequest) returns (ReadyResponse) { + } +} + +message ReadyRequest { +} + +message ReadyResponse { +} diff --git a/_proto/hapi/services/tiller.proto b/_proto/hapi/services/tiller.proto new file mode 100644 index 000000000..cd5adb958 --- /dev/null +++ b/_proto/hapi/services/tiller.proto @@ -0,0 +1,181 @@ +syntax = "proto3"; + +package hapi.services.tiller; + +import "hapi/chart/chart.proto"; +import "hapi/chart/config.proto"; +import "hapi/release/release.proto"; +import "hapi/release/info.proto"; + +option go_package = "services"; + +// +// ReleaseService: +// +// The service that a helm application uses to mutate, +// query, and manage releases. +// +// Release: A named installation composed of a chart and +// config. At any given time a release has one +// chart and one config. +// +// Config: A config is a TOML file that supplies values +// to the parametrizable templates of a chart. +// +// Chart: A chart is a helm package that contains +// metadata, a default config, zero or more +// optionally parameterizable templates, and +// zero or more charts (dependencies). +// +// +service ReleaseService { + // + // Retrieve release history. TODO: Allow filtering the set of releases by + // release status. By default, ListAllReleases returns the releases who + // current status is "Active". + // + rpc ListReleases(ListReleasesRequest) returns (stream ListReleasesResponse) { + } + + // + // Retrieve status information for the specified release. + // + rpc GetReleaseStatus(GetReleaseStatusRequest) returns (GetReleaseStatusResponse) { + } + + // + // Retrieve the release content (chart + value) for the specifed release. + // + rpc GetReleaseContent(GetReleaseContentRequest) returns (GetReleaseContentResponse) { + } + + // + // Update release content. + // + rpc UpdateRelease(UpdateReleaseRequest) returns (UpdateReleaseResponse) { + } + + // + // Request release install. + // + rpc InstallRelease(InstallReleaseRequest) returns (InstallReleaseResponse) { + } + + // + // Request release deletion. + // + rpc UninstallRelease(UninstallReleaseRequest) returns (UninstallReleaseResponse) { + } +} + +// +// ListReleasesRequest: +// +// TODO +// +message ListReleasesRequest { + // The maximum number of releases to be returned + int64 limit = 1; + + // The zero-based offset at which the returned release list begins + int64 offset = 2; +} + +// +// ListReleasesResponse: +// +// TODO +// +message ListReleasesResponse { + // The expected total number of releases to be returned + int64 count = 1; + + // The zero-based offset at which the list is positioned + int64 offset = 2; + + // The total number of queryable releases + int64 total = 3; + + // The resulting releases + repeated hapi.release.Release releases = 4; +} + +// GetReleaseStatusRequest is a request to get the status of a release. +message GetReleaseStatusRequest { + // Name is the name of the release + string name = 1; +} + +// GetReleaseStatusResponse is the response indicating the status of the named release. +message GetReleaseStatusResponse { + // Name is the name of the release. + string name = 1; + + // Info contains information about the release. + hapi.release.Info info = 2; +} + +// GetReleaseContentRequest is a request to get the contents of a release. +message GetReleaseContentRequest { + // The name of the release + string name = 1; +} + +// GetReleaseContentResponse is a response containing the contents of a release. +message GetReleaseContentResponse { + // The release content + hapi.release.Release release = 1; +} + +// +// UpdateReleaseRequest: +// +// TODO +// +message UpdateReleaseRequest { +} + +// +// UpdateReleaseResponse: +// +// TODO +// +message UpdateReleaseResponse { +} + +// +// InstallReleaseRequest: +// +// TODO +// +message InstallReleaseRequest { + // Chart is the protobuf representation of a chart. + hapi.chart.Chart chart = 1; + // Values is a string containing (unparsed) TOML values. + hapi.chart.Config values = 2; + // DryRun, if true, will run through the release logic, but neither create + // a release object nor deploy to Kubernetes. The release object returned + // in the response will be fake. + bool dry_run = 3; +} + +// +// InstallReleaseResponse: +// +// TODO +// +message InstallReleaseResponse { + hapi.release.Release release = 1; +} + +// UninstallReleaseRequest represents a request to uninstall a named release. +message UninstallReleaseRequest { + // Name is the name of the release to delete. + string name = 1; +} + +// UninstallReleaseResponse represents a successful response to an uninstall request. +message UninstallReleaseResponse { + // Release is the release that was marked deleted. + hapi.release.Release release = 1; +} diff --git a/circle.yml b/circle.yml index c66114bea..07825d6e6 100644 --- a/circle.yml +++ b/circle.yml @@ -25,8 +25,6 @@ dependencies: - tar -vxz -C $HOME/bin --strip=1 -f glide-$GLIDE_VERSION-linux-amd64.tar.gz - export PATH="$HOME/bin:$PATH" GLIDE_HOME="$HOME/.glide" - cd $GOPATH/src/$IMPORT_PATH - - sudo pip install -r expansion/requirements.txt - - sudo pip install flake8 test: override: diff --git a/cmd/doc.go b/cmd/doc.go deleted file mode 100644 index 8a962fc29..000000000 --- a/cmd/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package cmd contains the executables for Helm. -package cmd diff --git a/cmd/expandybird/expander/expander.go b/cmd/expandybird/expander/expander.go deleted file mode 100644 index bb987163b..000000000 --- a/cmd/expandybird/expander/expander.go +++ /dev/null @@ -1,131 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package expander - -import ( - "bytes" - "encoding/json" - "fmt" - "github.com/ghodss/yaml" - "log" - "os/exec" - - "github.com/kubernetes/helm/pkg/expansion" -) - -type expander struct { - ExpansionBinary string -} - -// NewExpander returns an ExpandyBird expander. -func NewExpander(binary string) expansion.Expander { - return &expander{binary} -} - -type expandyBirdConfigOutput struct { - Resources []interface{} `yaml:"resources,omitempty"` -} - -type expandyBirdOutput struct { - Config *expandyBirdConfigOutput `yaml:"config,omitempty"` - Layout interface{} `yaml:"layout,omitempty"` -} - -// ExpandChart passes the given configuration to the expander and returns the -// expanded configuration as a string on success. -func (e *expander) ExpandChart(request *expansion.ServiceRequest) (*expansion.ServiceResponse, error) { - - if err := expansion.ValidateRequest(request); err != nil { - return nil, err - } - - request, err := expansion.ValidateProperties(request) - if err != nil { - return nil, err - } - - chartInv := request.ChartInvocation - chartFile := request.Chart.Chartfile - chartMembers := request.Chart.Members - - if e.ExpansionBinary == "" { - message := fmt.Sprintf("expansion binary cannot be empty") - return nil, fmt.Errorf("%s: %s", chartInv.Name, message) - } - - entrypointIndex := -1 - for i, f := range chartMembers { - if f.Path == chartFile.Expander.Entrypoint { - entrypointIndex = i - } - } - if entrypointIndex == -1 { - message := fmt.Sprintf("The entrypoint in the chart.yaml cannot be found: %s", chartFile.Expander.Entrypoint) - return nil, fmt.Errorf("%s: %s", chartInv.Name, message) - } - - // Those are automatically increasing buffers, so writing arbitrary large - // data here won't block the child process. - var stdout bytes.Buffer - var stderr bytes.Buffer - - // Now we convert the new chart representation into the form that classic ExpandyBird takes. - - chartInvJSON, err := json.Marshal(chartInv) - if err != nil { - return nil, fmt.Errorf("error marshalling chart invocation %s: %s", chartInv.Name, err) - } - content := "{ \"resources\": [" + string(chartInvJSON) + "] }" - - cmd := &exec.Cmd{ - Path: e.ExpansionBinary, - // Note, that binary name still has to be passed argv[0]. - Args: []string{e.ExpansionBinary, content}, - Stdout: &stdout, - Stderr: &stderr, - } - - for i, f := range chartMembers { - name := f.Path - path := f.Path - if i == entrypointIndex { - // This is how expandyBird identifies the entrypoint. - name = chartInv.Type - } - cmd.Args = append(cmd.Args, name, path, string(f.Content)) - } - - if err := cmd.Start(); err != nil { - log.Printf("error starting expansion process: %s", err) - return nil, err - } - - cmd.Wait() - - log.Printf("Expansion process: pid: %d SysTime: %v UserTime: %v", cmd.ProcessState.Pid(), - cmd.ProcessState.SystemTime(), cmd.ProcessState.UserTime()) - if stderr.String() != "" { - return nil, fmt.Errorf("%s: %s", chartInv.Name, stderr.String()) - } - - output := &expandyBirdOutput{} - if err := yaml.Unmarshal(stdout.Bytes(), output); err != nil { - return nil, fmt.Errorf("cannot unmarshal expansion result (%s):\n%s", err, output) - } - - return &expansion.ServiceResponse{Resources: output.Config.Resources}, nil -} diff --git a/cmd/expandybird/expander/expander_test.go b/cmd/expandybird/expander/expander_test.go deleted file mode 100644 index df13a78c9..000000000 --- a/cmd/expandybird/expander/expander_test.go +++ /dev/null @@ -1,832 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package expander - -import ( - "fmt" - "path/filepath" - "reflect" - "runtime" - "strings" - "testing" - - "github.com/kubernetes/helm/pkg/chart" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/expansion" -) - -var expanderName = "../../../expansion/expansion.py" - -// content provides an easy way to provide file content verbatim in tests. -func content(lines []string) []byte { - return []byte(strings.Join(lines, "\n") + "\n") -} - -func getChartNameFromPC(pc uintptr) string { - rf := runtime.FuncForPC(pc) - fn := rf.Name() - bn := filepath.Base(fn) - split := strings.Split(bn, ".") - if len(split) > 1 { - split = split[1:] - } - - cn := fmt.Sprintf("%s-1.2.3.tgz", split[0]) - return cn -} - -func getChartURLFromPC(pc uintptr) string { - cn := getChartNameFromPC(pc) - cu := fmt.Sprintf("gs://kubernetes-charts-testing/%s", cn) - return cu -} - -func getTestChartName(t *testing.T) string { - pc, _, _, _ := runtime.Caller(1) - cu := getChartURLFromPC(pc) - cl, err := chart.Parse(cu) - if err != nil { - t.Fatalf("cannot parse chart reference %s: %s", cu, err) - } - - return cl.Name -} - -func getTestChartURL() string { - pc, _, _, _ := runtime.Caller(1) - cu := getChartURLFromPC(pc) - return cu -} - -func testExpansion(t *testing.T, req *expansion.ServiceRequest, - expResponse *expansion.ServiceResponse, expError string) { - backend := NewExpander(expanderName) - response, err := backend.ExpandChart(req) - if err != nil { - message := err.Error() - if expResponse != nil || !strings.Contains(message, expError) { - t.Fatalf("unexpected error: %v\n", err) - } - } else { - if expResponse == nil { - t.Fatalf("expected error did not occur: %s\n", expError) - } - if !reflect.DeepEqual(response, expResponse) { - message := fmt.Sprintf( - "want:\n%s\nhave:\n%s\n", expResponse, response) - t.Fatalf("output mismatch:\n%s\n", message) - } - } -} - -var pyExpander = &chart.Expander{ - Name: "ExpandyBird", - Entrypoint: "templates/main.py", -} - -var jinjaExpander = &chart.Expander{ - Name: "ExpandyBird", - Entrypoint: "templates/main.jinja", -} - -func TestEmptyJinja(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{"resources:"}), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{}, - }, - "", // Error - ) -} - -func TestEmptyPython(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: pyExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.py", - Content: content([]string{ - "def GenerateConfig(ctx):", - " return 'resources:'", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{}, - }, - "", // Error - ) -} - -func TestSimpleJinja(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "- name: foo", - " type: bar", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "type": "bar", - }, - }, - }, - "", // Error - ) -} - -func TestSimplePython(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: pyExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.py", - Content: content([]string{ - "def GenerateConfig(ctx):", - " return '''resources:", - "- name: foo", - " type: bar", - "'''", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "type": "bar", - }, - }, - }, - "", // Error - ) -} - -func TestPropertiesJinja(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - Properties: map[string]interface{}{ - "prop1": 3.0, - "prop2": "foo", - }, - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "- name: foo", - " type: {{ properties.prop2 }}", - " properties:", - " something: {{ properties.prop1 }}", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "properties": map[string]interface{}{ - "something": 3.0, - }, - "type": "foo", - }, - }, - }, - "", // Error - ) -} - -func TestPropertiesPython(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - Properties: map[string]interface{}{ - "prop1": 3.0, - "prop2": "foo", - }, - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: pyExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.py", - Content: content([]string{ - "def GenerateConfig(ctx):", - " return '''resources:", - "- name: foo", - " type: %(prop2)s", - " properties:", - " something: %(prop1)s", - "''' % ctx.properties", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "properties": map[string]interface{}{ - "something": 3.0, - }, - "type": "foo", - }, - }, - }, - "", // Error - ) -} - -func TestMultiFileJinja(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{"{% include 'templates/secondary.jinja' %}"}), - }, - { - Path: "templates/secondary.jinja", - Content: content([]string{ - "resources:", - "- name: foo", - " type: bar", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "type": "bar", - }, - }, - }, - "", // Error - ) -} - -var schemaContent = content([]string{ - `{`, - ` "required": ["prop1", "prop2"],`, - ` "additionalProperties": false,`, - ` "properties": {`, - ` "prop1": {`, - ` "description": "Nice description.",`, - ` "type": "integer"`, - ` },`, - ` "prop2": {`, - ` "description": "Nice description.",`, - ` "type": "string"`, - ` }`, - ` }`, - `}`, -}) - -func TestSchema(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - Properties: map[string]interface{}{ - "prop1": 3.0, - "prop2": "foo", - }, - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - Schema: "Schema.yaml", - }, - Members: []*chart.Member{ - { - Path: "Schema.yaml", - Content: schemaContent, - }, - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "- name: foo", - " type: {{ properties.prop2 }}", - " properties:", - " something: {{ properties.prop1 }}", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "properties": map[string]interface{}{ - "something": 3.0, - }, - "type": "foo", - }, - }, - }, - "", // Error - ) -} - -func TestSchemaFail(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - Properties: map[string]interface{}{ - "prop1": 3.0, - "prop3": "foo", - }, - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - Schema: "Schema.yaml", - }, - Members: []*chart.Member{ - { - Path: "Schema.yaml", - Content: schemaContent, - }, - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "- name: foo", - " type: {{ properties.prop2 }}", - " properties:", - " something: {{ properties.prop1 }}", - }), - }, - }, - }, - }, - nil, // Response. - `"prop2" property is missing and required`, - ) -} - -func TestMultiFileJinjaMissing(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{"{% include 'templates/secondary.jinja' %}"}), - }, - }, - }, - }, - nil, // Response - "TemplateNotFound: templates/secondary.jinja", - ) -} - -func TestMultiFilePython(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: pyExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.py", - Content: content([]string{ - "from templates import second", - "import templates.third", - "def GenerateConfig(ctx):", - " t2 = second.Gen()", - " t3 = templates.third.Gen()", - " return t2", - }), - }, - { - Path: "templates/second.py", - Content: content([]string{ - "def Gen():", - " return '''resources:", - "- name: foo", - " type: bar", - "'''", - }), - }, - { - Path: "templates/third.py", - Content: content([]string{ - "def Gen():", - " return '''resources:", - "- name: foo", - " type: bar", - "'''", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "type": "bar", - }, - }, - }, - "", // Error - ) -} - -func TestMultiFilePythonMissing(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: pyExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.py", - Content: content([]string{ - "from templates import second", - }), - }, - }, - }, - }, - nil, // Response - "cannot import name second", // Error - ) -} - -func TestWrongChartName(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: "WrongName", - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{"resources:"}), - }, - }, - }, - }, - nil, // Response - "does not match provided chart", - ) -} - -func TestEntrypointNotFound(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{}, - }, - }, - nil, // Response - "The entrypoint in the chart.yaml cannot be found", - ) -} - -func TestMalformedResource(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "fail", - }), - }, - }, - }, - }, - nil, // Response - "could not found expected ':'", // [sic] - ) -} - -func TestResourceNoName(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "- type: bar", - }), - }, - }, - }, - }, - nil, // Response. - "Resource does not have a name", - ) -} - -func TestResourceNoType(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: getTestChartURL(), - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: getTestChartName(t), - Expander: jinjaExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.jinja", - Content: content([]string{ - "resources:", - "- name: foo", - }), - }, - }, - }, - }, - nil, // Response. - "Resource does not have type defined", - ) -} - -func TestReplicatedService(t *testing.T) { - replicatedService, err := chart.LoadDir("../../../examples/charts/replicatedservice") - if err != nil { - t.Fatal(err) - } - replicatedServiceContent, err := replicatedService.LoadContent() - if err != nil { - t.Fatal(err) - } - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: "gs://kubernetes-charts-testing/replicatedservice-1.2.3.tgz", - Properties: map[string]interface{}{ - "image": "myimage", - "container_port": 1234, - "replicas": 3, - }, - }, - Chart: replicatedServiceContent, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "test_invocation-rc", - "properties": map[string]interface{}{ - "apiVersion": "v1", - "kind": "ReplicationController", - "metadata": map[string]interface{}{ - "labels": map[string]interface{}{ - "name": "test_invocation-rc", - }, - "name": "test_invocation-rc", - "namespace": "default", - }, - "spec": map[string]interface{}{ - "replicas": 3.0, - "selector": map[string]interface{}{ - "name": "test_invocation", - }, - "template": map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": map[string]interface{}{ - "name": "test_invocation", - }, - }, - "spec": map[string]interface{}{ - "containers": []interface{}{ - map[string]interface{}{ - "env": []interface{}{}, - "image": "myimage", - "name": "test_invocation", - "ports": []interface{}{ - map[string]interface{}{ - "containerPort": 1234.0, - "name": "test_invocation", - }, - }, - }, - }, - }, - }, - }, - }, - "type": "ReplicationController", - }, - map[string]interface{}{ - "name": "test_invocation-service", - "properties": map[string]interface{}{ - "apiVersion": "v1", - "kind": "Service", - "metadata": map[string]interface{}{ - "labels": map[string]interface{}{ - "name": "test_invocation-service", - }, - "name": "test_invocation-service", - "namespace": "default", - }, - "spec": map[string]interface{}{ - "ports": []interface{}{ - map[string]interface{}{ - "name": "test_invocation", - "port": 1234.0, - "targetPort": 1234.0, - }, - }, - "selector": map[string]interface{}{ - "name": "test_invocation", - }, - }, - }, - "type": "Service", - }, - }, - }, - "", // Error. - ) -} diff --git a/cmd/expandybird/main.go b/cmd/expandybird/main.go deleted file mode 100644 index 7160eb459..000000000 --- a/cmd/expandybird/main.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "github.com/kubernetes/helm/cmd/expandybird/expander" - "github.com/kubernetes/helm/pkg/expansion" - "github.com/kubernetes/helm/pkg/version" - - "flag" - "log" -) - -// interface that we are going to listen on -var address = flag.String("address", "", "Interface to listen on") - -// port that we are going to listen on -var port = flag.Int("port", 8080, "Port to listen on") - -// path to expansion binary -var expansionBinary = flag.String("expansion_binary", "../../../expansion/expansion.py", - "The path to the expansion binary that will be used to expand the template.") - -func main() { - flag.Parse() - backend := expander.NewExpander(*expansionBinary) - service := expansion.NewService(*address, *port, backend) - log.Printf("Version: %s", version.Version) - log.Printf("Listening on http://%s:%d/expand", *address, *port) - log.Fatal(service.ListenAndServe()) -} diff --git a/cmd/expandybird/test/ExpectedOutput.yaml b/cmd/expandybird/test/ExpectedOutput.yaml deleted file mode 100644 index 02bf868b8..000000000 --- a/cmd/expandybird/test/ExpectedOutput.yaml +++ /dev/null @@ -1,77 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -config: - resources: - - name: expandybird-service - properties: - apiVersion: v1 - kind: Service - metadata: - labels: - app: expandybird - name: expandybird-service - namespace: default - spec: - ports: - - name: expandybird - port: 8080 - targetPort: 8080 - selector: - app: expandybird - type: LoadBalancer - type: Service - - name: expandybird-rc - properties: - apiVersion: v1 - kind: ReplicationController - metadata: - labels: - app: expandybird - name: expandybird-rc - namespace: default - spec: - replicas: 3 - selector: - app: expandybird - template: - metadata: - labels: - app: expandybird - spec: - containers: - - env: [] - image: gcr.io/kubernetes-helm/expandybird - name: expandybird - ports: - - containerPort: 8080 - name: expandybird - type: ReplicationController -layout: - resources: - - name: expandybird - properties: - container_port: 8080 - external_service: true - image: gcr.io/kubernetes-helm/expandybird - labels: - app: expandybird - replicas: 3 - service_port: 8080 - target_port: 8080 - resources: - - name: expandybird-service - type: Service - - name: expandybird-rc - type: ReplicationController - type: replicatedservice.py \ No newline at end of file diff --git a/cmd/expandybird/test/InvalidFileName.yaml b/cmd/expandybird/test/InvalidFileName.yaml deleted file mode 100644 index fa29b84d2..000000000 --- a/cmd/expandybird/test/InvalidFileName.yaml +++ /dev/null @@ -1,22 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: invalidfilename.py -resources: -- name: expandybird - type: replicatedservice.py - properties: - service_port: 8080 - target_port: 8080 - image: gcr.io/kubernetes-helm/expandybird diff --git a/cmd/expandybird/test/InvalidProperty.yaml b/cmd/expandybird/test/InvalidProperty.yaml deleted file mode 100644 index aab9b6341..000000000 --- a/cmd/expandybird/test/InvalidProperty.yaml +++ /dev/null @@ -1,22 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: replicatedservice.py -resources: -- name: expandybird - type: replicatedservice.py - properties: - service_port: 8080 - target_port: 8080 - invalidproperty: gcr.io/kubernetes-helm/expandybird diff --git a/cmd/expandybird/test/InvalidTypeName.yaml b/cmd/expandybird/test/InvalidTypeName.yaml deleted file mode 100644 index 67b7050f0..000000000 --- a/cmd/expandybird/test/InvalidTypeName.yaml +++ /dev/null @@ -1,22 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: replicatedservice.py -resources: -- name: expandybird - type: invalidtypename.py - properties: - service_port: 8080 - target_port: 8080 - image: gcr.io/kubernetes-helm/expandybird diff --git a/cmd/expandybird/test/MalformedContent.yaml b/cmd/expandybird/test/MalformedContent.yaml deleted file mode 100644 index c96ae41d2..000000000 --- a/cmd/expandybird/test/MalformedContent.yaml +++ /dev/null @@ -1,20 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: replicatedservice.py -resources: -- name: expandybird - type: replicatedservice.py - thisisnotalist: somevalue - shouldnotbehere: anothervalue diff --git a/cmd/expandybird/test/MissingImports.yaml b/cmd/expandybird/test/MissingImports.yaml deleted file mode 100644 index 91c11fe33..000000000 --- a/cmd/expandybird/test/MissingImports.yaml +++ /dev/null @@ -1,21 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -resources: -- name: expandybird - type: replicatedservice.py - properties: - service_port: 8080 - target_port: 8080 - image: gcr.io/kubernetes-helm/expandybird diff --git a/cmd/expandybird/test/MissingResourceName.yaml b/cmd/expandybird/test/MissingResourceName.yaml deleted file mode 100644 index 00f7e7d61..000000000 --- a/cmd/expandybird/test/MissingResourceName.yaml +++ /dev/null @@ -1,21 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: replicatedservice.py -resources: -- type: replicatedservice.py - properties: - service_port: 8080 - target_port: 8080 - image: gcr.io/kubernetes-helm/expandybird diff --git a/cmd/expandybird/test/MissingTypeName.yaml b/cmd/expandybird/test/MissingTypeName.yaml deleted file mode 100644 index 4a867f86c..000000000 --- a/cmd/expandybird/test/MissingTypeName.yaml +++ /dev/null @@ -1,21 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: replicatedservice.py -resources: -- name: expandybird - properties: - service_port: 8080 - target_port: 8080 - image: gcr.io/kubernetes-helm/expandybird diff --git a/cmd/expandybird/test/TestArchive.tar b/cmd/expandybird/test/TestArchive.tar deleted file mode 100644 index 9cb986b97..000000000 Binary files a/cmd/expandybird/test/TestArchive.tar and /dev/null differ diff --git a/cmd/expandybird/test/ValidContent.yaml b/cmd/expandybird/test/ValidContent.yaml deleted file mode 100644 index 5798efec3..000000000 --- a/cmd/expandybird/test/ValidContent.yaml +++ /dev/null @@ -1,27 +0,0 @@ -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -imports: -- path: replicatedservice.py -resources: -- name: expandybird - type: replicatedservice.py - properties: - service_port: 8080 - target_port: 8080 - container_port: 8080 - external_service: true - replicas: 3 - image: gcr.io/kubernetes-helm/expandybird - labels: - app: expandybird \ No newline at end of file diff --git a/cmd/expandybird/test/replicatedservice.py b/cmd/expandybird/test/replicatedservice.py deleted file mode 100644 index 231fb2640..000000000 --- a/cmd/expandybird/test/replicatedservice.py +++ /dev/null @@ -1,182 +0,0 @@ - -###################################################################### -# Copyright 2015 The Kubernetes Authors All rights reserved. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -###################################################################### - -"""Defines a ReplicatedService type by creating both a Service and an RC. - -This module creates a typical abstraction for running a service in a -Kubernetes cluster, namely a replication controller and a service packaged -together into a single unit. -""" - -import yaml - -SERVICE_TYPE_COLLECTION = 'Service' -RC_TYPE_COLLECTION = 'ReplicationController' - - -def GenerateConfig(context): - """Generates a Replication Controller and a matching Service. - - Args: - context: Template context. See schema for context properties. - - Returns: - A Container Manifest as a YAML string. - """ - # YAML config that we're going to create for both RC & Service - config = {'resources': []} - - name = context.env['name'] - container_name = context.properties.get('container_name', name) - namespace = context.properties.get('namespace', 'default') - - # Define things that the Service cares about - service_name = context.properties.get('service_name', name + '-service') - service_type = SERVICE_TYPE_COLLECTION - - # Define things that the Replication Controller (rc) cares about - rc_name = name + '-rc' - rc_type = RC_TYPE_COLLECTION - - service = { - 'name': service_name, - 'type': service_type, - 'properties': { - 'apiVersion': 'v1', - 'kind': 'Service', - 'metadata': { - 'name': service_name, - 'namespace': namespace, - 'labels': GenerateLabels(context, service_name), - }, - 'spec': { - 'ports': [GenerateServicePorts(context, container_name)], - 'selector': GenerateLabels(context, name) - } - } - } - set_up_external_lb = context.properties.get('external_service', None) - if set_up_external_lb: - service['properties']['spec']['type'] = 'LoadBalancer' - config['resources'].append(service) - - rc = { - 'name': rc_name, - 'type': rc_type, - 'properties': { - 'apiVersion': 'v1', - 'kind': 'ReplicationController', - 'metadata': { - 'name': rc_name, - 'namespace': namespace, - 'labels': GenerateLabels(context, rc_name), - }, - 'spec': { - 'replicas': context.properties['replicas'], - 'selector': GenerateLabels(context, name), - 'template': { - 'metadata': { - 'labels': GenerateLabels(context, name), - }, - 'spec': { - 'containers': [ - { - 'env': GenerateEnv(context), - 'name': container_name, - 'image': context.properties['image'], - 'ports': [ - { - 'name': container_name, - 'containerPort': context.properties['container_port'], - } - ] - } - ] - } - } - } - } - } - - config['resources'].append(rc) - return yaml.dump(config) - - -def GenerateLabels(context, name): - """Generates labels either from the context.properties['labels'] or - generates a default label 'app':name - - We make a deep copy of the context.properties['labels'] section to avoid - linking in the yaml document, which I believe reduces readability of the - expanded template. If no labels are given, generate a default 'app':name. - - Args: - context: Template context, which can contain the following properties: - labels - Labels to generate - - Returns: - A dict containing labels in a name:value format - """ - tmp_labels = context.properties.get('labels', None) - ret_labels = {'app': name} - if isinstance(tmp_labels, dict): - for key, value in tmp_labels.iteritems(): - ret_labels[key] = value - return ret_labels - - -def GenerateServicePorts(context, name): - """Generates a ports section for a service. - - Args: - context: Template context, which can contain the following properties: - service_port - Port to use for the service - target_port - Target port for the service - protocol - Protocol to use. - - Returns: - A dict containing a port definition - """ - service_port = context.properties.get('service_port', None) - target_port = context.properties.get('target_port', None) - protocol = context.properties.get('protocol') - - ports = {} - if name: - ports['name'] = name - if service_port: - ports['port'] = service_port - if target_port: - ports['targetPort'] = target_port - if protocol: - ports['protocol'] = protocol - - return ports - -def GenerateEnv(context): - """Generates environmental variables for a pod. - - Args: - context: Template context, which can contain the following properties: - env - Environment variables to set. - - Returns: - A list containing env variables in dict format {name: 'name', value: 'value'} - """ - env = [] - tmp_env = context.properties.get('env', []) - for entry in tmp_env: - if isinstance(entry, dict): - env.append({'name': entry.get('name'), 'value': entry.get('value')}) - return env diff --git a/cmd/expandybird/test/schemas/bad.jinja.schema b/cmd/expandybird/test/schemas/bad.jinja.schema deleted file mode 100644 index 825f8dcf7..000000000 --- a/cmd/expandybird/test/schemas/bad.jinja.schema +++ /dev/null @@ -1,9 +0,0 @@ -info: - title: Schema with a lots of errors in it - -imports: - -properties: - exclusiveMin: - type: integer - exclusiveMinimum: 0 diff --git a/cmd/expandybird/test/schemas/default_ref.jinja.schema b/cmd/expandybird/test/schemas/default_ref.jinja.schema deleted file mode 100644 index 51f83d2c8..000000000 --- a/cmd/expandybird/test/schemas/default_ref.jinja.schema +++ /dev/null @@ -1,14 +0,0 @@ -info: - title: Schema with a property that has a referenced default value - -imports: - -properties: - number: - $ref: '#/level/mult' - -level: - mult: - type: integer - multipleOf: 1 - default: 1 diff --git a/cmd/expandybird/test/schemas/defaults.jinja.schema b/cmd/expandybird/test/schemas/defaults.jinja.schema deleted file mode 100644 index bcb7ee34e..000000000 --- a/cmd/expandybird/test/schemas/defaults.jinja.schema +++ /dev/null @@ -1,12 +0,0 @@ -info: - title: Schema with properties that have default values - -imports: - -properties: - one: - type: integer - default: 1 - alpha: - type: string - default: alpha diff --git a/cmd/expandybird/test/schemas/defaults.py.schema b/cmd/expandybird/test/schemas/defaults.py.schema deleted file mode 100644 index bcb7ee34e..000000000 --- a/cmd/expandybird/test/schemas/defaults.py.schema +++ /dev/null @@ -1,12 +0,0 @@ -info: - title: Schema with properties that have default values - -imports: - -properties: - one: - type: integer - default: 1 - alpha: - type: string - default: alpha diff --git a/cmd/expandybird/test/schemas/invalid_default.jinja.schema b/cmd/expandybird/test/schemas/invalid_default.jinja.schema deleted file mode 100644 index e60d11148..000000000 --- a/cmd/expandybird/test/schemas/invalid_default.jinja.schema +++ /dev/null @@ -1,11 +0,0 @@ -info: - title: Schema with a required integer property that has a default string value - -imports: - -required: - - number -properties: - number: - type: integer - default: string diff --git a/cmd/expandybird/test/schemas/invalid_reference.py.schema b/cmd/expandybird/test/schemas/invalid_reference.py.schema deleted file mode 100644 index 7c3fa3e10..000000000 --- a/cmd/expandybird/test/schemas/invalid_reference.py.schema +++ /dev/null @@ -1,10 +0,0 @@ -info: - title: Schema with references to something that doesnt exist - -imports: - -properties: - odd: - type: integer - not: - $ref: '#/wheeeeeee' diff --git a/cmd/expandybird/test/schemas/invalid_reference_schema.py.schema b/cmd/expandybird/test/schemas/invalid_reference_schema.py.schema deleted file mode 100644 index 6c824568c..000000000 --- a/cmd/expandybird/test/schemas/invalid_reference_schema.py.schema +++ /dev/null @@ -1,8 +0,0 @@ -info: - title: Schema with references to something that doesnt exist - -imports: - -properties: - odd: - $ref: '#/wheeeeeee' diff --git a/cmd/expandybird/test/schemas/metadata.py.schema b/cmd/expandybird/test/schemas/metadata.py.schema deleted file mode 100644 index 3d6e1e346..000000000 --- a/cmd/expandybird/test/schemas/metadata.py.schema +++ /dev/null @@ -1,20 +0,0 @@ -info: - title: Schema with properties that have extra metadata - -imports: - -properties: - one: - type: integer - default: 1 - metadata: - gcloud: is great! - compute: is awesome - alpha: - type: string - default: alpha - metadata: - - you - - can - - do - - anything diff --git a/cmd/expandybird/test/schemas/missing_quote.py.schema b/cmd/expandybird/test/schemas/missing_quote.py.schema deleted file mode 100644 index ddd4b5bfd..000000000 --- a/cmd/expandybird/test/schemas/missing_quote.py.schema +++ /dev/null @@ -1,11 +0,0 @@ -info: - title: Schema with references - -imports: - -properties: - number: - $ref: #/number - -number: - type: integer diff --git a/cmd/expandybird/test/schemas/nested_defaults.py.schema b/cmd/expandybird/test/schemas/nested_defaults.py.schema deleted file mode 100644 index b5288c91b..000000000 --- a/cmd/expandybird/test/schemas/nested_defaults.py.schema +++ /dev/null @@ -1,33 +0,0 @@ -info: - title: VM with Disks - author: Kubernetes - description: Creates a single vm, then attaches disks to it. - -required: -- zone - -properties: - zone: - type: string - description: GCP zone - default: us-central1-a - disks: - type: array - items: - type: object - required: - - name - properties: - name: - type: string - description: Suffix for this disk - sizeGb: - type: integer - default: 100 - diskType: - type: string - enum: - - pd-standard - - pd-ssd - default: pd-standard - additionalProperties: false diff --git a/cmd/expandybird/test/schemas/numbers.py.schema b/cmd/expandybird/test/schemas/numbers.py.schema deleted file mode 100644 index eff245182..000000000 --- a/cmd/expandybird/test/schemas/numbers.py.schema +++ /dev/null @@ -1,27 +0,0 @@ -info: - title: Schema with a lots of number properties and restrictions - -imports: - -properties: - minimum0: - type: integer - minimum: 0 - exclusiveMin0: - type: integer - minimum: 0 - exclusiveMinimum: true - maximum10: - type: integer - maximum: 10 - exclusiveMax10: - type: integer - maximum: 10 - exclusiveMaximum: true - even: - type: integer - multipleOf: 2 - odd: - type: integer - not: - multipleOf: 2 diff --git a/cmd/expandybird/test/schemas/ref_nested_defaults.py.schema b/cmd/expandybird/test/schemas/ref_nested_defaults.py.schema deleted file mode 100644 index 80813b73d..000000000 --- a/cmd/expandybird/test/schemas/ref_nested_defaults.py.schema +++ /dev/null @@ -1,36 +0,0 @@ -info: - title: VM with Disks - author: Kubernetes - description: Creates a single vm, then attaches disks to it. - -required: -- zone - -properties: - zone: - type: string - description: GCP zone - default: us-central1-a - disks: - type: array - items: - $ref: '#/disk' - -disk: - type: object - required: - - name - properties: - name: - type: string - description: Suffix for this disk - sizeGb: - type: integer - default: 100 - diskType: - type: string - enum: - - pd-standard - - pd-ssd - default: pd-standard - additionalProperties: false diff --git a/cmd/expandybird/test/schemas/reference.jinja.schema b/cmd/expandybird/test/schemas/reference.jinja.schema deleted file mode 100644 index e90251c39..000000000 --- a/cmd/expandybird/test/schemas/reference.jinja.schema +++ /dev/null @@ -1,14 +0,0 @@ -info: - title: Schema with references - -imports: - -properties: - odd: - type: integer - not: - $ref: '#/even' - - -even: - multipleOf: 2 diff --git a/cmd/expandybird/test/schemas/req_default_ref.py.schema b/cmd/expandybird/test/schemas/req_default_ref.py.schema deleted file mode 100644 index 08b1da3e9..000000000 --- a/cmd/expandybird/test/schemas/req_default_ref.py.schema +++ /dev/null @@ -1,14 +0,0 @@ -info: - title: Schema with a required property that has a referenced default value - -imports: - -required: - - number -properties: - number: - $ref: '#/default_val' - -default_val: - type: integer - default: my_name diff --git a/cmd/expandybird/test/schemas/required.jinja.schema b/cmd/expandybird/test/schemas/required.jinja.schema deleted file mode 100644 index 94c8e39f8..000000000 --- a/cmd/expandybird/test/schemas/required.jinja.schema +++ /dev/null @@ -1,10 +0,0 @@ -info: - title: Schema with a required property - -imports: - -required: - - name -properties: - name: - type: string diff --git a/cmd/expandybird/test/schemas/required_default.jinja.schema b/cmd/expandybird/test/schemas/required_default.jinja.schema deleted file mode 100644 index d739e2c20..000000000 --- a/cmd/expandybird/test/schemas/required_default.jinja.schema +++ /dev/null @@ -1,11 +0,0 @@ -info: - title: Schema with a required property that has a default value - -imports: - -required: - - name -properties: - name: - type: string - default: my_name diff --git a/cmd/expandybird/test/templates/description_text.txt b/cmd/expandybird/test/templates/description_text.txt deleted file mode 100644 index 33e5dea2e..000000000 --- a/cmd/expandybird/test/templates/description_text.txt +++ /dev/null @@ -1 +0,0 @@ -"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." diff --git a/cmd/expandybird/test/templates/duplicate_names.yaml b/cmd/expandybird/test/templates/duplicate_names.yaml deleted file mode 100644 index f7386f186..000000000 --- a/cmd/expandybird/test/templates/duplicate_names.yaml +++ /dev/null @@ -1,9 +0,0 @@ -resources: -- type: compute.v1.instance - name: my_instance - properties: - zone: test-zone-a -- type: compute.v1.instance - name: my_instance - properties: - zone: test-zone-b diff --git a/cmd/expandybird/test/templates/duplicate_names_B.jinja b/cmd/expandybird/test/templates/duplicate_names_B.jinja deleted file mode 100644 index 8ae8a7923..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_B.jinja +++ /dev/null @@ -1,5 +0,0 @@ -resources: -- type: compute.v1.instance - name: B - properties: - zone: test-zone-b diff --git a/cmd/expandybird/test/templates/duplicate_names_C.jinja b/cmd/expandybird/test/templates/duplicate_names_C.jinja deleted file mode 100644 index 3aff269fe..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_C.jinja +++ /dev/null @@ -1,5 +0,0 @@ -resources: -- type: compute.v1.instance - name: C - properties: - zone: test-zone-c diff --git a/cmd/expandybird/test/templates/duplicate_names_in_subtemplates.jinja b/cmd/expandybird/test/templates/duplicate_names_in_subtemplates.jinja deleted file mode 100644 index f7386f186..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_in_subtemplates.jinja +++ /dev/null @@ -1,9 +0,0 @@ -resources: -- type: compute.v1.instance - name: my_instance - properties: - zone: test-zone-a -- type: compute.v1.instance - name: my_instance - properties: - zone: test-zone-b diff --git a/cmd/expandybird/test/templates/duplicate_names_in_subtemplates.yaml b/cmd/expandybird/test/templates/duplicate_names_in_subtemplates.yaml deleted file mode 100644 index 06e8a263c..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_in_subtemplates.yaml +++ /dev/null @@ -1,5 +0,0 @@ -imports: ["duplicate_names_in_subtemplates.jinja"] - -resources: -- name: subtemplate - type: duplicate_names_in_subtemplates.jinja diff --git a/cmd/expandybird/test/templates/duplicate_names_mixed_level.yaml b/cmd/expandybird/test/templates/duplicate_names_mixed_level.yaml deleted file mode 100644 index 7cb82e5e7..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_mixed_level.yaml +++ /dev/null @@ -1,7 +0,0 @@ -imports: ["duplicate_names_B.jinja", "duplicate_names_C.jinja"] - -resources: -- name: A - type: duplicate_names_B.jinja -- name: B - type: duplicate_names_C.jinja diff --git a/cmd/expandybird/test/templates/duplicate_names_mixed_level_result.yaml b/cmd/expandybird/test/templates/duplicate_names_mixed_level_result.yaml deleted file mode 100644 index de34fa47a..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_mixed_level_result.yaml +++ /dev/null @@ -1,22 +0,0 @@ -config: - resources: - - name: B - properties: - zone: test-zone-b - type: compute.v1.instance - - name: C - properties: - zone: test-zone-c - type: compute.v1.instance -layout: - resources: - - name: A - resources: - - name: B - type: compute.v1.instance - type: duplicate_names_B.jinja - - name: B - resources: - - name: C - type: compute.v1.instance - type: duplicate_names_C.jinja diff --git a/cmd/expandybird/test/templates/duplicate_names_parent_child.yaml b/cmd/expandybird/test/templates/duplicate_names_parent_child.yaml deleted file mode 100644 index 4c3ae6bf1..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_parent_child.yaml +++ /dev/null @@ -1,7 +0,0 @@ -imports: ["duplicate_names_B.jinja"] - -resources: -- name: A - type: duplicate_names_B.jinja -- name: B - type: compute.v1.instance diff --git a/cmd/expandybird/test/templates/duplicate_names_parent_child_result.yaml b/cmd/expandybird/test/templates/duplicate_names_parent_child_result.yaml deleted file mode 100644 index 8384a72ee..000000000 --- a/cmd/expandybird/test/templates/duplicate_names_parent_child_result.yaml +++ /dev/null @@ -1,17 +0,0 @@ -config: - resources: - - name: B - properties: - zone: test-zone-b - type: compute.v1.instance - - name: B - type: compute.v1.instance -layout: - resources: - - name: A - resources: - - name: B - type: compute.v1.instance - type: duplicate_names_B.jinja - - name: B - type: compute.v1.instance diff --git a/cmd/expandybird/test/templates/helper.jinja b/cmd/expandybird/test/templates/helper.jinja deleted file mode 100644 index d174617f4..000000000 --- a/cmd/expandybird/test/templates/helper.jinja +++ /dev/null @@ -1,5 +0,0 @@ -resources: -- name: helper - type: bar - properties: - test: {{ properties["foobar"] }} diff --git a/cmd/expandybird/test/templates/helper.jinja.schema b/cmd/expandybird/test/templates/helper.jinja.schema deleted file mode 100644 index 7bbdf5b5e..000000000 --- a/cmd/expandybird/test/templates/helper.jinja.schema +++ /dev/null @@ -1,4 +0,0 @@ -properties: - foobar: - type: string - default: Use this schema diff --git a/cmd/expandybird/test/templates/helpers/common.jinja b/cmd/expandybird/test/templates/helpers/common.jinja deleted file mode 100644 index 056435742..000000000 --- a/cmd/expandybird/test/templates/helpers/common.jinja +++ /dev/null @@ -1,3 +0,0 @@ -{%- macro GenerateMachineName(prefix='', suffix='') -%} - {{ prefix + "-" + suffix }} -{%- endmacro %} diff --git a/cmd/expandybird/test/templates/helpers/common.py b/cmd/expandybird/test/templates/helpers/common.py deleted file mode 100644 index a553e65dd..000000000 --- a/cmd/expandybird/test/templates/helpers/common.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Dummy helper methods invoked in other constructors.""" - - -def GenerateMachineName(prefix, suffix): - """Generates name of a VM.""" - return prefix + "-" + suffix diff --git a/cmd/expandybird/test/templates/helpers/extra/__init__.py b/cmd/expandybird/test/templates/helpers/extra/__init__.py deleted file mode 100644 index 27e204d9a..000000000 --- a/cmd/expandybird/test/templates/helpers/extra/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Package marker file.""" diff --git a/cmd/expandybird/test/templates/helpers/extra/common2.py b/cmd/expandybird/test/templates/helpers/extra/common2.py deleted file mode 100644 index 469354b81..000000000 --- a/cmd/expandybird/test/templates/helpers/extra/common2.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Dummy helper methods invoked in other constructors.""" - - -def GenerateMachineSize(): - """Generates size of a VM.""" - return "big" diff --git a/cmd/expandybird/test/templates/invalid_config.yaml b/cmd/expandybird/test/templates/invalid_config.yaml deleted file mode 100644 index d2e04dfb4..000000000 --- a/cmd/expandybird/test/templates/invalid_config.yaml +++ /dev/null @@ -1,2 +0,0 @@ -resources: -- name: foo properties: bar: baz diff --git a/cmd/expandybird/test/templates/jinja_defaults.jinja b/cmd/expandybird/test/templates/jinja_defaults.jinja deleted file mode 100644 index 74837f2a2..000000000 --- a/cmd/expandybird/test/templates/jinja_defaults.jinja +++ /dev/null @@ -1,16 +0,0 @@ -resources: -- type: compute.v1.instance - name: vm-created-by-cloud-config-{{ properties["deployment"] }} - properties: - zone: {{ properties["zone"] }} - machineType: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/zones/{{ properties["zone"] }}/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-{{ properties["deployment"] }} - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/global/networks/default diff --git a/cmd/expandybird/test/templates/jinja_defaults.jinja.schema b/cmd/expandybird/test/templates/jinja_defaults.jinja.schema deleted file mode 100644 index 7cf4ae108..000000000 --- a/cmd/expandybird/test/templates/jinja_defaults.jinja.schema +++ /dev/null @@ -1,18 +0,0 @@ -info: - title: Schema for a basic jinja template that includes default values - -imports: - -properties: - foo: - description: blah - type: string - zone: - type: string - default: test-zone - project: - type: string - default: test-project - deployment: - type: string - default: test-deployment diff --git a/cmd/expandybird/test/templates/jinja_defaults.yaml b/cmd/expandybird/test/templates/jinja_defaults.yaml deleted file mode 100644 index 1b3ee64f4..000000000 --- a/cmd/expandybird/test/templates/jinja_defaults.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: -- path: "jinja_defaults.jinja" -- path: "jinja_defaults.jinja.schema" - -resources: -- name: jinja_defaults_name - type: jinja_defaults.jinja - properties: - foo: bar diff --git a/cmd/expandybird/test/templates/jinja_defaults_result.yaml b/cmd/expandybird/test/templates/jinja_defaults_result.yaml deleted file mode 100644 index 60c03b408..000000000 --- a/cmd/expandybird/test/templates/jinja_defaults_result.yaml +++ /dev/null @@ -1,29 +0,0 @@ -config: - resources: - - name: vm-created-by-cloud-config-test-deployment - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - zone: test-zone - type: compute.v1.instance -layout: - resources: - - name: jinja_defaults_name - properties: - deployment: test-deployment - foo: bar - project: test-project - zone: test-zone - resources: - - name: vm-created-by-cloud-config-test-deployment - type: compute.v1.instance - type: jinja_defaults.jinja diff --git a/cmd/expandybird/test/templates/jinja_missing_required.jinja b/cmd/expandybird/test/templates/jinja_missing_required.jinja deleted file mode 100644 index 2247bf2e1..000000000 --- a/cmd/expandybird/test/templates/jinja_missing_required.jinja +++ /dev/null @@ -1,4 +0,0 @@ -Nothing here because this file should never be called. -The validation will fail before this file is used. - -{{% Invalid diff --git a/cmd/expandybird/test/templates/jinja_missing_required.jinja.schema b/cmd/expandybird/test/templates/jinja_missing_required.jinja.schema deleted file mode 100644 index 387b64dde..000000000 --- a/cmd/expandybird/test/templates/jinja_missing_required.jinja.schema +++ /dev/null @@ -1,11 +0,0 @@ -info: - title: Schema with a required property - -imports: - -required: - - important -properties: - important: - type: string - diff --git a/cmd/expandybird/test/templates/jinja_missing_required.yaml b/cmd/expandybird/test/templates/jinja_missing_required.yaml deleted file mode 100644 index 412bccf8e..000000000 --- a/cmd/expandybird/test/templates/jinja_missing_required.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: -- path: "jinja_missing_required.jinja" -- path: "jinja_missing_required.jinja.schema" - -resources: -- name: jinja_missing_required_resource_name - type: jinja_missing_required.jinja - properties: - less-important: an optional property diff --git a/cmd/expandybird/test/templates/jinja_multiple_errors.jinja b/cmd/expandybird/test/templates/jinja_multiple_errors.jinja deleted file mode 100644 index 2247bf2e1..000000000 --- a/cmd/expandybird/test/templates/jinja_multiple_errors.jinja +++ /dev/null @@ -1,4 +0,0 @@ -Nothing here because this file should never be called. -The validation will fail before this file is used. - -{{% Invalid diff --git a/cmd/expandybird/test/templates/jinja_multiple_errors.jinja.schema b/cmd/expandybird/test/templates/jinja_multiple_errors.jinja.schema deleted file mode 100644 index 5d73e125d..000000000 --- a/cmd/expandybird/test/templates/jinja_multiple_errors.jinja.schema +++ /dev/null @@ -1,22 +0,0 @@ -info: - title: Schema with several rules - -imports: - -properties: - number: - type: integer - short-string: - type: string - maxLength: 10 - odd: - type: integer - not: - multipleOf: 2 - abc: - type: string - enum: - - a - - b - - c - diff --git a/cmd/expandybird/test/templates/jinja_multiple_errors.yaml b/cmd/expandybird/test/templates/jinja_multiple_errors.yaml deleted file mode 100644 index 0fc663628..000000000 --- a/cmd/expandybird/test/templates/jinja_multiple_errors.yaml +++ /dev/null @@ -1,12 +0,0 @@ -imports: -- path: "jinja_multiple_errors.jinja" -- path: "jinja_multiple_errors.jinja.schema" - -resources: -- name: jinja_multiple_errors - type: jinja_multiple_errors.jinja - properties: - number: a string - short-string: longer than 10 chars - odd: 6 - abc: d diff --git a/cmd/expandybird/test/templates/jinja_noparams.jinja b/cmd/expandybird/test/templates/jinja_noparams.jinja deleted file mode 100644 index 9b0ec9476..000000000 --- a/cmd/expandybird/test/templates/jinja_noparams.jinja +++ /dev/null @@ -1,19 +0,0 @@ -resources: -{% for name in ['name1', 'name2'] %} -- type: compute.v1.instance - name: {{ name }} - properties: - zone: test-zone - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default -{% endfor %} - diff --git a/cmd/expandybird/test/templates/jinja_noparams.yaml b/cmd/expandybird/test/templates/jinja_noparams.yaml deleted file mode 100644 index 7debc85e2..000000000 --- a/cmd/expandybird/test/templates/jinja_noparams.yaml +++ /dev/null @@ -1,6 +0,0 @@ -imports: ["jinja_noparams.jinja"] - -resources: -- name: jinja_noparams_name - type: jinja_noparams.jinja - diff --git a/cmd/expandybird/test/templates/jinja_noparams_result.yaml b/cmd/expandybird/test/templates/jinja_noparams_result.yaml deleted file mode 100644 index 5a5f0e52c..000000000 --- a/cmd/expandybird/test/templates/jinja_noparams_result.yaml +++ /dev/null @@ -1,41 +0,0 @@ -config: - resources: - - name: name1 - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - zone: test-zone - type: compute.v1.instance - - name: name2 - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - zone: test-zone - type: compute.v1.instance -layout: - resources: - - name: jinja_noparams_name - resources: - - name: name1 - type: compute.v1.instance - - name: name2 - type: compute.v1.instance - type: jinja_noparams.jinja diff --git a/cmd/expandybird/test/templates/jinja_template.jinja b/cmd/expandybird/test/templates/jinja_template.jinja deleted file mode 100644 index 5febf21bd..000000000 --- a/cmd/expandybird/test/templates/jinja_template.jinja +++ /dev/null @@ -1,18 +0,0 @@ -resources: -- type: compute.v1.instance - name: vm-created-by-cloud-config-{{ properties["deployment"] }} - properties: - zone: {{ properties["zone"] }} - machineType: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/zones/{{ properties["zone"] }}/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-{{ properties["deployment"] }} - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/global/networks/default - - diff --git a/cmd/expandybird/test/templates/jinja_template.yaml b/cmd/expandybird/test/templates/jinja_template.yaml deleted file mode 100644 index bc2b2b0db..000000000 --- a/cmd/expandybird/test/templates/jinja_template.yaml +++ /dev/null @@ -1,10 +0,0 @@ -imports: ["jinja_template.jinja"] - -resources: -- name: jinja_template_name - type: jinja_template.jinja - properties: - zone: test-zone - project: test-project - deployment: test-deployment - diff --git a/cmd/expandybird/test/templates/jinja_template_result.yaml b/cmd/expandybird/test/templates/jinja_template_result.yaml deleted file mode 100644 index 10a36fa56..000000000 --- a/cmd/expandybird/test/templates/jinja_template_result.yaml +++ /dev/null @@ -1,28 +0,0 @@ -config: - resources: - - name: vm-created-by-cloud-config-test-deployment - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - zone: test-zone - type: compute.v1.instance -layout: - resources: - - name: jinja_template_name - properties: - deployment: test-deployment - project: test-project - zone: test-zone - resources: - - name: vm-created-by-cloud-config-test-deployment - type: compute.v1.instance - type: jinja_template.jinja diff --git a/cmd/expandybird/test/templates/jinja_template_with_env.jinja b/cmd/expandybird/test/templates/jinja_template_with_env.jinja deleted file mode 100644 index 545824e5e..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_env.jinja +++ /dev/null @@ -1,18 +0,0 @@ -resources: -- type: compute.v1.instance - name: vm-created-by-cloud-config-{{ env["deployment"] }} - properties: - zone: {{ properties["zone"] }} - machineType: https://www.googleapis.com/compute/v1/projects/{{ env["project"] }}/zones/{{ properties["zone"] }}/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-{{ env["deployment"] }}-{{ env["name"] }}-{{ env["type"] }} - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/{{ env["project"] }}/global/networks/default - - diff --git a/cmd/expandybird/test/templates/jinja_template_with_env.yaml b/cmd/expandybird/test/templates/jinja_template_with_env.yaml deleted file mode 100644 index 58388635c..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_env.yaml +++ /dev/null @@ -1,8 +0,0 @@ -imports: ["jinja_template_with_env.jinja"] - -resources: -- name: jinja_template_with_env_name - type: jinja_template_with_env.jinja - properties: - zone: test-zone - diff --git a/cmd/expandybird/test/templates/jinja_template_with_env_result.yaml b/cmd/expandybird/test/templates/jinja_template_with_env_result.yaml deleted file mode 100644 index a69f89ab1..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_env_result.yaml +++ /dev/null @@ -1,26 +0,0 @@ -config: - resources: - - name: vm-created-by-cloud-config-test-deployment - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment-jinja_template_with_env_name-jinja_template_with_env.jinja - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - zone: test-zone - type: compute.v1.instance -layout: - resources: - - name: jinja_template_with_env_name - properties: - zone: test-zone - resources: - - name: vm-created-by-cloud-config-test-deployment - type: compute.v1.instance - type: jinja_template_with_env.jinja diff --git a/cmd/expandybird/test/templates/jinja_template_with_import.jinja b/cmd/expandybird/test/templates/jinja_template_with_import.jinja deleted file mode 100644 index b5c125726..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_import.jinja +++ /dev/null @@ -1,6 +0,0 @@ -{% import 'helpers/common.jinja' as common %} -resources: -- name: {{ common.GenerateMachineName("myFrontend", "prod") }} - type: compute.v1.instance - properties: - machineSize: big diff --git a/cmd/expandybird/test/templates/jinja_template_with_import.yaml b/cmd/expandybird/test/templates/jinja_template_with_import.yaml deleted file mode 100644 index d4ec9f327..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_import.yaml +++ /dev/null @@ -1,5 +0,0 @@ -imports: ["jinja_template_with_import.jinja", "helpers/common.jinja"] - -resources: -- name: jinja_template_with_import_name - type: jinja_template_with_import.jinja diff --git a/cmd/expandybird/test/templates/jinja_template_with_import_result.yaml b/cmd/expandybird/test/templates/jinja_template_with_import_result.yaml deleted file mode 100644 index 3a3c3b2e5..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_import_result.yaml +++ /dev/null @@ -1,13 +0,0 @@ -config: - resources: - - name: myFrontend-prod - properties: - machineSize: big - type: compute.v1.instance -layout: - resources: - - name: jinja_template_with_import_name - resources: - - name: myFrontend-prod - type: compute.v1.instance - type: jinja_template_with_import.jinja diff --git a/cmd/expandybird/test/templates/jinja_template_with_inlinedfile.jinja b/cmd/expandybird/test/templates/jinja_template_with_inlinedfile.jinja deleted file mode 100644 index 01d7642e4..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_inlinedfile.jinja +++ /dev/null @@ -1,7 +0,0 @@ -{% import 'helpers/common.jinja' as common %} -resources: -- name: {{ common.GenerateMachineName("myFrontend", "prod") }} - type: compute.v1.instance - properties: - description: {{ imports[properties["description-file"]] }} - machineSize: big diff --git a/cmd/expandybird/test/templates/jinja_template_with_inlinedfile.yaml b/cmd/expandybird/test/templates/jinja_template_with_inlinedfile.yaml deleted file mode 100644 index e8bec0891..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_inlinedfile.yaml +++ /dev/null @@ -1,7 +0,0 @@ -imports: ["jinja_template_with_inlinedfile.jinja", "helpers/common.jinja", "description_text.txt"] - -resources: -- name: jinja_template_with_inlinedfile_name - type: jinja_template_with_inlinedfile.jinja - properties: - description-file: description_text.txt diff --git a/cmd/expandybird/test/templates/jinja_template_with_inlinedfile_result.yaml b/cmd/expandybird/test/templates/jinja_template_with_inlinedfile_result.yaml deleted file mode 100644 index 6f4bf9eee..000000000 --- a/cmd/expandybird/test/templates/jinja_template_with_inlinedfile_result.yaml +++ /dev/null @@ -1,21 +0,0 @@ -config: - resources: - - name: myFrontend-prod - properties: - description: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do - eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim - veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo - consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse - cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat - non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - machineSize: big - type: compute.v1.instance -layout: - resources: - - name: jinja_template_with_inlinedfile_name - properties: - description-file: description_text.txt - resources: - - name: myFrontend-prod - type: compute.v1.instance - type: jinja_template_with_inlinedfile.jinja diff --git a/cmd/expandybird/test/templates/jinja_unresolved.jinja b/cmd/expandybird/test/templates/jinja_unresolved.jinja deleted file mode 100644 index 6ad1ed1cd..000000000 --- a/cmd/expandybird/test/templates/jinja_unresolved.jinja +++ /dev/null @@ -1,18 +0,0 @@ -resources: -- type: compute.v1.instance - name: vm-created-by-cloud-config-{{ porcelain["deployment"] }} - properties: - zone: {{ properties["zone"] }} - machineType: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/zones/{{ properties["zone"] }}/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-{{ properties["deployment"] }} - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/global/networks/default - - diff --git a/cmd/expandybird/test/templates/jinja_unresolved.yaml b/cmd/expandybird/test/templates/jinja_unresolved.yaml deleted file mode 100644 index 8bc31af11..000000000 --- a/cmd/expandybird/test/templates/jinja_unresolved.yaml +++ /dev/null @@ -1,10 +0,0 @@ -imports: ["jinja_unresolved.jinja"] - -resources: -- name: jinja_template_name - type: jinja_unresolved.jinja - properties: - zone: test-zone - project: test-project - deployment: test-deployment - diff --git a/cmd/expandybird/test/templates/no_properties.py b/cmd/expandybird/test/templates/no_properties.py deleted file mode 100644 index 66cd164c5..000000000 --- a/cmd/expandybird/test/templates/no_properties.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Return empty resources block.""" - - -def GenerateConfig(_): - return """resources:""" diff --git a/cmd/expandybird/test/templates/no_properties.yaml b/cmd/expandybird/test/templates/no_properties.yaml deleted file mode 100644 index 1c7d29795..000000000 --- a/cmd/expandybird/test/templates/no_properties.yaml +++ /dev/null @@ -1,6 +0,0 @@ -imports: -- path: "no_properties.py" - -resources: -- name: test-resource - type: no_properties.py diff --git a/cmd/expandybird/test/templates/no_properties_result.yaml b/cmd/expandybird/test/templates/no_properties_result.yaml deleted file mode 100644 index 41ccb5602..000000000 --- a/cmd/expandybird/test/templates/no_properties_result.yaml +++ /dev/null @@ -1,6 +0,0 @@ -config: - resources: [] -layout: - resources: - - name: test-resource - type: no_properties.py diff --git a/cmd/expandybird/test/templates/no_resources.py b/cmd/expandybird/test/templates/no_resources.py deleted file mode 100644 index c387ebca0..000000000 --- a/cmd/expandybird/test/templates/no_resources.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Does nothing.""" - - -def GenerateConfig(_): - """Returns empty string.""" - return '' diff --git a/cmd/expandybird/test/templates/no_resources.yaml b/cmd/expandybird/test/templates/no_resources.yaml deleted file mode 100644 index d9257d8ea..000000000 --- a/cmd/expandybird/test/templates/no_resources.yaml +++ /dev/null @@ -1,6 +0,0 @@ -imports: -- path: "no_resources.py" - -resources: -- name: test-resource - type: no_resources.py diff --git a/cmd/expandybird/test/templates/python_and_jinja_template.jinja b/cmd/expandybird/test/templates/python_and_jinja_template.jinja deleted file mode 100644 index 8a670b476..000000000 --- a/cmd/expandybird/test/templates/python_and_jinja_template.jinja +++ /dev/null @@ -1,17 +0,0 @@ -resources: -- type: compute.v1.instance - name: vm-created-by-cloud-config-{{ properties["deployment"] }} - properties: - zone: {{ properties["zone"] }} - machineType: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/zones/{{ properties["zone"] }}/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-{{ properties["deployment"] }} - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/{{ properties["project"] }}/global/networks/default - diff --git a/cmd/expandybird/test/templates/python_and_jinja_template.py b/cmd/expandybird/test/templates/python_and_jinja_template.py deleted file mode 100644 index 86562c9d0..000000000 --- a/cmd/expandybird/test/templates/python_and_jinja_template.py +++ /dev/null @@ -1,35 +0,0 @@ -#% description: Creates a VM running a Salt master daemon in a Docker container. -#% parameters: -#% - name: masterAddress -#% type: string -#% description: Name of the Salt master VM. -#% required: true -#% - name: project -#% type: string -#% description: Name of the Cloud project. -#% required: true -#% - name: zone -#% type: string -#% description: Zone to create the resources in. -#% required: true - -"""Generates config for a VM running a SaltStack master. - -Just for fun this template is in Python, while the others in this -directory are in Jinja2. -""" - - -def GenerateConfig(evaluation_context): - return """ -resources: -- name: python_and_jinja_template_jinja_name - type: python_and_jinja_template.jinja - properties: - zone: %(zone)s - project: %(project)s - deployment: %(master)s - -""" % {"master": evaluation_context.properties["masterAddress"], - "project": evaluation_context.properties["project"], - "zone": evaluation_context.properties["zone"]} diff --git a/cmd/expandybird/test/templates/python_and_jinja_template.yaml b/cmd/expandybird/test/templates/python_and_jinja_template.yaml deleted file mode 100644 index 46daafc27..000000000 --- a/cmd/expandybird/test/templates/python_and_jinja_template.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: ["python_and_jinja_template.jinja", "python_and_jinja_template.py"] - -resources: -- name: python_and_jinja_template_name - type: python_and_jinja_template.py - properties: - masterAddress: master-address - project: my-project - zone: my-zone diff --git a/cmd/expandybird/test/templates/python_and_jinja_template_result.yaml b/cmd/expandybird/test/templates/python_and_jinja_template_result.yaml deleted file mode 100644 index 3d23dcfbf..000000000 --- a/cmd/expandybird/test/templates/python_and_jinja_template_result.yaml +++ /dev/null @@ -1,35 +0,0 @@ -config: - resources: - - name: vm-created-by-cloud-config-master-address - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-master-address - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/my-project/zones/my-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - zone: my-zone - type: compute.v1.instance -layout: - resources: - - name: python_and_jinja_template_name - properties: - masterAddress: master-address - project: my-project - zone: my-zone - resources: - - name: python_and_jinja_template_jinja_name - properties: - deployment: master-address - project: my-project - zone: my-zone - resources: - - name: vm-created-by-cloud-config-master-address - type: compute.v1.instance - type: python_and_jinja_template.jinja - type: python_and_jinja_template.py diff --git a/cmd/expandybird/test/templates/python_bad_schema.py b/cmd/expandybird/test/templates/python_bad_schema.py deleted file mode 100644 index 0ebda1f6c..000000000 --- a/cmd/expandybird/test/templates/python_bad_schema.py +++ /dev/null @@ -1,3 +0,0 @@ -"""Throws an exception.""" - -raise Exception diff --git a/cmd/expandybird/test/templates/python_bad_schema.py.schema b/cmd/expandybird/test/templates/python_bad_schema.py.schema deleted file mode 100644 index cf5d5d6d0..000000000 --- a/cmd/expandybird/test/templates/python_bad_schema.py.schema +++ /dev/null @@ -1,19 +0,0 @@ -info: - title: Schema with several errors - -imports: - -properties: - bad-type: - type: int - missing-cond: - type: string - exclusiveMaximum: 10 - odd-string: - type: string - not: - multipleOf: 2 - bad-enum: - type: string - enum: not a list - diff --git a/cmd/expandybird/test/templates/python_bad_schema.yaml b/cmd/expandybird/test/templates/python_bad_schema.yaml deleted file mode 100644 index d52024b6f..000000000 --- a/cmd/expandybird/test/templates/python_bad_schema.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: -- path: "python_bad_schema.py" -- path: "python_bad_schema.py.schema" - -resources: -- name: python_bad_schema - type: python_bad_schema.py - properties: - innocent: true diff --git a/cmd/expandybird/test/templates/python_noparams.py b/cmd/expandybird/test/templates/python_noparams.py deleted file mode 100644 index 2542d71bb..000000000 --- a/cmd/expandybird/test/templates/python_noparams.py +++ /dev/null @@ -1,12 +0,0 @@ -"""Constructs a VM.""" - - -def GenerateConfig(_): - """Generates config of a VM.""" - return """ -resources: -- name: myBackend - type: compute.v1.instance - properties: - machineSize: big -""" diff --git a/cmd/expandybird/test/templates/python_noparams.yaml b/cmd/expandybird/test/templates/python_noparams.yaml deleted file mode 100644 index b7abeaf55..000000000 --- a/cmd/expandybird/test/templates/python_noparams.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: ["python_noparams.py"] - -resources: -- name: myFrontend - type: compute.v1.instance - properties: - machineSize: big -- name: python_noparams_name - type: python_noparams.py diff --git a/cmd/expandybird/test/templates/python_noparams_result.yaml b/cmd/expandybird/test/templates/python_noparams_result.yaml deleted file mode 100644 index 944d9018a..000000000 --- a/cmd/expandybird/test/templates/python_noparams_result.yaml +++ /dev/null @@ -1,19 +0,0 @@ -config: - resources: - - name: myFrontend - properties: - machineSize: big - type: compute.v1.instance - - name: myBackend - properties: - machineSize: big - type: compute.v1.instance -layout: - resources: - - name: myFrontend - type: compute.v1.instance - - name: python_noparams_name - resources: - - name: myBackend - type: compute.v1.instance - type: python_noparams.py diff --git a/cmd/expandybird/test/templates/python_schema.py b/cmd/expandybird/test/templates/python_schema.py deleted file mode 100644 index 2d935f7ad..000000000 --- a/cmd/expandybird/test/templates/python_schema.py +++ /dev/null @@ -1,57 +0,0 @@ -#% description: Creates a VM running a Salt master daemon in a Docker container. -#% parameters: -#% - name: masterAddress -#% type: string -#% description: Name of the Salt master VM. -#% required: true -#% - name: project -#% type: string -#% description: Name of the Cloud project. -#% required: true -#% - name: zone -#% type: string -#% description: Zone to create the resources in. -#% required: true - -"""Generates config for a VM running a SaltStack master. - -Just for fun this template is in Python, while the others in this -directory are in Jinja2. -""" - - -def GenerateConfig(evaluation_context): - return """ -resources: -- type: compute.v1.firewall - name: %(master)s-firewall - properties: - network: https://www.googleapis.com/compute/v1/projects/%(project)s/global/networks/default - sourceRanges: [ "0.0.0.0/0" ] - allowed: - - IPProtocol: tcp - ports: [ "4505", "4506" ] -- type: compute.v1.instance - name: %(master)s - properties: - zone: %(zone)s - machineType: https://www.googleapis.com/compute/v1/projects/%(project)s/zones/%(zone)s/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/%(project)s/global/networks/default - accessConfigs: - - name: External NAT - type: ONE_TO_ONE_NAT - metadata: - items: - - key: startup-script - value: startup-script-value -""" % {"master": evaluation_context.properties["masterAddress"], - "project": evaluation_context.env["project"], - "zone": evaluation_context.properties["zone"]} diff --git a/cmd/expandybird/test/templates/python_schema.py.schema b/cmd/expandybird/test/templates/python_schema.py.schema deleted file mode 100644 index cb960bcfd..000000000 --- a/cmd/expandybird/test/templates/python_schema.py.schema +++ /dev/null @@ -1,14 +0,0 @@ -info: - title: A simple python template that has a schema. - -imports: - -properties: - masterAddress: - type: string - default: slave-address - description: masterAddress - zone: - type: string - default: not-test-zone - description: zone diff --git a/cmd/expandybird/test/templates/python_schema.yaml b/cmd/expandybird/test/templates/python_schema.yaml deleted file mode 100644 index 94f94d4e5..000000000 --- a/cmd/expandybird/test/templates/python_schema.yaml +++ /dev/null @@ -1,10 +0,0 @@ -imports: -- path: "python_schema.py" -- path: "python_schema.py.schema" - -resources: -- name: python_schema - type: python_schema.py - properties: - masterAddress: master-address - zone: my-zone diff --git a/cmd/expandybird/test/templates/python_schema_result.yaml b/cmd/expandybird/test/templates/python_schema_result.yaml deleted file mode 100644 index 2b75d97c2..000000000 --- a/cmd/expandybird/test/templates/python_schema_result.yaml +++ /dev/null @@ -1,46 +0,0 @@ -config: - resources: - - name: master-address-firewall - properties: - allowed: - - IPProtocol: tcp - ports: - - '4505' - - '4506' - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - sourceRanges: - - 0.0.0.0/0 - type: compute.v1.firewall - - name: master-address - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/my-project/zones/my-zone/machineTypes/f1-micro - metadata: - items: - - key: startup-script - value: startup-script-value - networkInterfaces: - - accessConfigs: - - name: External NAT - type: ONE_TO_ONE_NAT - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - zone: my-zone - type: compute.v1.instance -layout: - resources: - - name: python_schema - properties: - masterAddress: master-address - zone: my-zone - resources: - - name: master-address-firewall - type: compute.v1.firewall - - name: master-address - type: compute.v1.instance - type: python_schema.py diff --git a/cmd/expandybird/test/templates/python_template.py b/cmd/expandybird/test/templates/python_template.py deleted file mode 100644 index 57ff7fe73..000000000 --- a/cmd/expandybird/test/templates/python_template.py +++ /dev/null @@ -1,57 +0,0 @@ -#% description: Creates a VM running a Salt master daemon in a Docker container. -#% parameters: -#% - name: masterAddress -#% type: string -#% description: Name of the Salt master VM. -#% required: true -#% - name: project -#% type: string -#% description: Name of the Cloud project. -#% required: true -#% - name: zone -#% type: string -#% description: Zone to create the resources in. -#% required: true - -"""Generates config for a VM running a SaltStack master. - -Just for fun this template is in Python, while the others in this -directory are in Jinja2. -""" - - -def GenerateConfig(evaluation_context): - return """ -resources: -- type: compute.v1.firewall - name: %(master)s-firewall - properties: - network: https://www.googleapis.com/compute/v1/projects/%(project)s/global/networks/default - sourceRanges: [ "0.0.0.0/0" ] - allowed: - - IPProtocol: tcp - ports: [ "4505", "4506" ] -- type: compute.v1.instance - name: %(master)s - properties: - zone: %(zone)s - machineType: https://www.googleapis.com/compute/v1/projects/%(project)s/zones/%(zone)s/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/%(project)s/global/networks/default - accessConfigs: - - name: External NAT - type: ONE_TO_ONE_NAT - metadata: - items: - - key: startup-script - value: startup-script-value -""" % {"master": evaluation_context.properties["masterAddress"], - "project": evaluation_context.properties["project"], - "zone": evaluation_context.properties["zone"]} diff --git a/cmd/expandybird/test/templates/python_template.yaml b/cmd/expandybird/test/templates/python_template.yaml deleted file mode 100644 index 08ae8b8bb..000000000 --- a/cmd/expandybird/test/templates/python_template.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: ["python_template.py"] - -resources: -- name: python_template_name - type: python_template.py - properties: - masterAddress: master-address - project: my-project - zone: my-zone diff --git a/cmd/expandybird/test/templates/python_template_result.yaml b/cmd/expandybird/test/templates/python_template_result.yaml deleted file mode 100644 index 1b82f3fed..000000000 --- a/cmd/expandybird/test/templates/python_template_result.yaml +++ /dev/null @@ -1,47 +0,0 @@ -config: - resources: - - name: master-address-firewall - properties: - allowed: - - IPProtocol: tcp - ports: - - '4505' - - '4506' - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - sourceRanges: - - 0.0.0.0/0 - type: compute.v1.firewall - - name: master-address - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/my-project/zones/my-zone/machineTypes/f1-micro - metadata: - items: - - key: startup-script - value: startup-script-value - networkInterfaces: - - accessConfigs: - - name: External NAT - type: ONE_TO_ONE_NAT - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - zone: my-zone - type: compute.v1.instance -layout: - resources: - - name: python_template_name - properties: - masterAddress: master-address - project: my-project - zone: my-zone - resources: - - name: master-address-firewall - type: compute.v1.firewall - - name: master-address - type: compute.v1.instance - type: python_template.py diff --git a/cmd/expandybird/test/templates/python_template_with_env.py b/cmd/expandybird/test/templates/python_template_with_env.py deleted file mode 100644 index 2d935f7ad..000000000 --- a/cmd/expandybird/test/templates/python_template_with_env.py +++ /dev/null @@ -1,57 +0,0 @@ -#% description: Creates a VM running a Salt master daemon in a Docker container. -#% parameters: -#% - name: masterAddress -#% type: string -#% description: Name of the Salt master VM. -#% required: true -#% - name: project -#% type: string -#% description: Name of the Cloud project. -#% required: true -#% - name: zone -#% type: string -#% description: Zone to create the resources in. -#% required: true - -"""Generates config for a VM running a SaltStack master. - -Just for fun this template is in Python, while the others in this -directory are in Jinja2. -""" - - -def GenerateConfig(evaluation_context): - return """ -resources: -- type: compute.v1.firewall - name: %(master)s-firewall - properties: - network: https://www.googleapis.com/compute/v1/projects/%(project)s/global/networks/default - sourceRanges: [ "0.0.0.0/0" ] - allowed: - - IPProtocol: tcp - ports: [ "4505", "4506" ] -- type: compute.v1.instance - name: %(master)s - properties: - zone: %(zone)s - machineType: https://www.googleapis.com/compute/v1/projects/%(project)s/zones/%(zone)s/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/%(project)s/global/networks/default - accessConfigs: - - name: External NAT - type: ONE_TO_ONE_NAT - metadata: - items: - - key: startup-script - value: startup-script-value -""" % {"master": evaluation_context.properties["masterAddress"], - "project": evaluation_context.env["project"], - "zone": evaluation_context.properties["zone"]} diff --git a/cmd/expandybird/test/templates/python_template_with_env.yaml b/cmd/expandybird/test/templates/python_template_with_env.yaml deleted file mode 100644 index d3bbc26c7..000000000 --- a/cmd/expandybird/test/templates/python_template_with_env.yaml +++ /dev/null @@ -1,8 +0,0 @@ -imports: ["python_template_with_env.py"] - -resources: -- name: python_template_with_env_name - type: python_template_with_env.py - properties: - masterAddress: master-address - zone: my-zone diff --git a/cmd/expandybird/test/templates/python_template_with_env_result.yaml b/cmd/expandybird/test/templates/python_template_with_env_result.yaml deleted file mode 100644 index 027732c8f..000000000 --- a/cmd/expandybird/test/templates/python_template_with_env_result.yaml +++ /dev/null @@ -1,46 +0,0 @@ -config: - resources: - - name: master-address-firewall - properties: - allowed: - - IPProtocol: tcp - ports: - - '4505' - - '4506' - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - sourceRanges: - - 0.0.0.0/0 - type: compute.v1.firewall - - name: master-address - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/my-project/zones/my-zone/machineTypes/f1-micro - metadata: - items: - - key: startup-script - value: startup-script-value - networkInterfaces: - - accessConfigs: - - name: External NAT - type: ONE_TO_ONE_NAT - network: https://www.googleapis.com/compute/v1/projects/my-project/global/networks/default - zone: my-zone - type: compute.v1.instance -layout: - resources: - - name: python_template_with_env_name - properties: - masterAddress: master-address - zone: my-zone - resources: - - name: master-address-firewall - type: compute.v1.firewall - - name: master-address - type: compute.v1.instance - type: python_template_with_env.py diff --git a/cmd/expandybird/test/templates/python_template_with_import.py b/cmd/expandybird/test/templates/python_template_with_import.py deleted file mode 100644 index ba362233a..000000000 --- a/cmd/expandybird/test/templates/python_template_with_import.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Constructs a VM.""" -import json - -import helpers.common -import helpers.extra.common2 - - -def GenerateConfig(_): - """Generates config of a VM.""" - return """ -resources: -- name: %s - type: compute.v1.instance - properties: - machineSize: %s -""" % (helpers.common.GenerateMachineName( - json.dumps('myFrontend').strip('"'), 'prod'), - helpers.extra.common2.GenerateMachineSize()) diff --git a/cmd/expandybird/test/templates/python_template_with_import.yaml b/cmd/expandybird/test/templates/python_template_with_import.yaml deleted file mode 100644 index 73cce18f1..000000000 --- a/cmd/expandybird/test/templates/python_template_with_import.yaml +++ /dev/null @@ -1,5 +0,0 @@ -imports: ["python_template_with_import.py", "helpers/common.py", "helpers/common2.py", "helpers/__init__.py"] - -resources: -- name: python_template_with_import_name - type: python_template_with_import.py diff --git a/cmd/expandybird/test/templates/python_template_with_import_result.yaml b/cmd/expandybird/test/templates/python_template_with_import_result.yaml deleted file mode 100644 index d8e283308..000000000 --- a/cmd/expandybird/test/templates/python_template_with_import_result.yaml +++ /dev/null @@ -1,13 +0,0 @@ -config: - resources: - - name: myFrontend-prod - properties: - machineSize: big - type: compute.v1.instance -layout: - resources: - - name: python_template_with_import_name - resources: - - name: myFrontend-prod - type: compute.v1.instance - type: python_template_with_import.py diff --git a/cmd/expandybird/test/templates/python_template_with_inlinedfile.py b/cmd/expandybird/test/templates/python_template_with_inlinedfile.py deleted file mode 100644 index 2ea1fb8ed..000000000 --- a/cmd/expandybird/test/templates/python_template_with_inlinedfile.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Constructs a VM.""" - -# Verify that both ways of hierarchical imports work. -from helpers import common -import helpers.extra.common2 - - -def GenerateConfig(evaluation_context): - """Generates config of a VM.""" - return """ -resources: -- name: %s - type: compute.v1.instance - properties: - description: %s - machineSize: %s -""" % (common.GenerateMachineName("myFrontend", "prod"), - evaluation_context.imports[ - evaluation_context.properties["description-file"]], - helpers.extra.common2.GenerateMachineSize()) diff --git a/cmd/expandybird/test/templates/python_template_with_inlinedfile.yaml b/cmd/expandybird/test/templates/python_template_with_inlinedfile.yaml deleted file mode 100644 index 8c1d8c38c..000000000 --- a/cmd/expandybird/test/templates/python_template_with_inlinedfile.yaml +++ /dev/null @@ -1,7 +0,0 @@ -imports: ["python_template_with_inlinedfile.py", "helpers/common.py", "helpers/common2.py", "helpers/__init__.py", "description_text.txt"] - -resources: -- name: python_template_with_inlinedfile_name - type: python_template_with_inlinedfile.py - properties: - description-file: description_text.txt diff --git a/cmd/expandybird/test/templates/python_template_with_inlinedfile_result.yaml b/cmd/expandybird/test/templates/python_template_with_inlinedfile_result.yaml deleted file mode 100644 index 92706a0fd..000000000 --- a/cmd/expandybird/test/templates/python_template_with_inlinedfile_result.yaml +++ /dev/null @@ -1,21 +0,0 @@ -config: - resources: - - name: myFrontend-prod - properties: - description: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do - eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim - veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo - consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse - cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat - non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - machineSize: big - type: compute.v1.instance -layout: - resources: - - name: python_template_with_inlinedfile_name - properties: - description-file: description_text.txt - resources: - - name: myFrontend-prod - type: compute.v1.instance - type: python_template_with_inlinedfile.py diff --git a/cmd/expandybird/test/templates/python_with_exception.py b/cmd/expandybird/test/templates/python_with_exception.py deleted file mode 100644 index bf982a82f..000000000 --- a/cmd/expandybird/test/templates/python_with_exception.py +++ /dev/null @@ -1,7 +0,0 @@ -"""A python script that raise exceptions. - -""" - - -def GenerateConfig(unused_context): - raise NameError('No file found') diff --git a/cmd/expandybird/test/templates/python_with_exception.yaml b/cmd/expandybird/test/templates/python_with_exception.yaml deleted file mode 100644 index fadc23970..000000000 --- a/cmd/expandybird/test/templates/python_with_exception.yaml +++ /dev/null @@ -1,9 +0,0 @@ -imports: ["python_with_exception.py"] - -resources: -- name: python_with_exception_name - type: python_with_exception.py - properties: - masterAddress: master-address - project: my-project - zone: my-zone diff --git a/cmd/expandybird/test/templates/simple.yaml b/cmd/expandybird/test/templates/simple.yaml deleted file mode 100644 index 5065fa564..000000000 --- a/cmd/expandybird/test/templates/simple.yaml +++ /dev/null @@ -1,18 +0,0 @@ -resources: -- type: compute.v1.instance - name: vm-created-by-cloud-config-{{ params["deployment"] }} - properties: - zone: test-zone - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - disks: - - deviceName: boot - type: PERSISTENT - boot: true - autoDelete: true - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - - diff --git a/cmd/expandybird/test/templates/simple_result.yaml b/cmd/expandybird/test/templates/simple_result.yaml deleted file mode 100644 index 4353c7194..000000000 --- a/cmd/expandybird/test/templates/simple_result.yaml +++ /dev/null @@ -1,21 +0,0 @@ -config: - resources: - - name: vm-created-by-cloud-config-{{ params["deployment"] }} - properties: - disks: - - autoDelete: true - boot: true - deviceName: boot - initializeParams: - diskName: disk-created-by-cloud-config-test-deployment - sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140619 - type: PERSISTENT - machineType: https://www.googleapis.com/compute/v1/projects/test-project/zones/test-zone/machineTypes/f1-micro - networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/test-project/global/networks/default - zone: test-zone - type: compute.v1.instance -layout: - resources: - - name: vm-created-by-cloud-config-{{ params["deployment"] }} - type: compute.v1.instance diff --git a/cmd/expandybird/test/templates/use_helper.jinja b/cmd/expandybird/test/templates/use_helper.jinja deleted file mode 100644 index c748a8ae4..000000000 --- a/cmd/expandybird/test/templates/use_helper.jinja +++ /dev/null @@ -1,7 +0,0 @@ -resources: -- name: use-helper - type: foo - properties: - test: {{ properties['barfoo'] }} -- name: use-helper-helper - type: helper.jinja diff --git a/cmd/expandybird/test/templates/use_helper.jinja.schema b/cmd/expandybird/test/templates/use_helper.jinja.schema deleted file mode 100644 index 69986603d..000000000 --- a/cmd/expandybird/test/templates/use_helper.jinja.schema +++ /dev/null @@ -1,4 +0,0 @@ -properties: - barfoo: - type: string - default: Use this schema also diff --git a/cmd/expandybird/test/templates/use_helper.yaml b/cmd/expandybird/test/templates/use_helper.yaml deleted file mode 100644 index 818b0d284..000000000 --- a/cmd/expandybird/test/templates/use_helper.yaml +++ /dev/null @@ -1,3 +0,0 @@ -resources: -- name: use-helper - type: use_helper.jinja diff --git a/cmd/expandybird/test/templates/use_helper_result.yaml b/cmd/expandybird/test/templates/use_helper_result.yaml deleted file mode 100644 index 51c17afc8..000000000 --- a/cmd/expandybird/test/templates/use_helper_result.yaml +++ /dev/null @@ -1,26 +0,0 @@ -config: - resources: - - name: use-helper - properties: - test: Use this schema also - type: foo - - name: helper - properties: - test: Use this schema - type: bar -layout: - resources: - - name: use-helper - properties: - barfoo: Use this schema also - resources: - - name: use-helper - type: foo - - name: use-helper-helper - properties: - foobar: Use this schema - resources: - - name: helper - type: bar - type: helper.jinja - type: use_helper.jinja diff --git a/cmd/goexpander/expander/expander.go b/cmd/goexpander/expander/expander.go deleted file mode 100644 index ee7c513f3..000000000 --- a/cmd/goexpander/expander/expander.go +++ /dev/null @@ -1,146 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package expander - -import ( - "bytes" - "fmt" - "io" - "strings" - "text/template" - - "github.com/Masterminds/sprig" - "github.com/cloudfoundry-incubator/candiedyaml" - "github.com/ghodss/yaml" - "github.com/kubernetes/helm/pkg/expansion" -) - -// parseYAMLStream takes an encoded YAML stream and turns it into a slice of JSON-marshalable -// objects, one for each document in the stream. -func parseYAMLStream(in io.Reader) ([]interface{}, error) { - // Use candiedyaml because it's the only one that supports streams. - decoder := candiedyaml.NewDecoder(in) - var document interface{} - stream := []interface{}{} - for { - err := decoder.Decode(&document) - if err != nil { - if strings.Contains(err.Error(), "Expected document start at line") { - return stream, nil - } - return nil, err - } - // Now it's held in document but we have to do a bit of a dance to get it in a form that can - // be marshaled as JSON for our API response. The fundamental problem is that YAML is a - // superset of JSON in that it can represent non-string keys, and full IEEE floating point - // values (NaN etc). JSON only allows string keys and its definition of a number is based - // around a sequence of digits. - - // Kubernetes does not make use of these features, as it uses YAML as just "pretty JSON". - // Consequently this does not affect Helm either. However, both candiedyaml and go-yaml - // return types that are too wide for JSON marshalling (i.e. map[interface{}]interface{} - // instead of map[string]interface{}), so we have to do some explicit conversion. Luckily, - // ghodss/yaml has code to help with this, since decoding from YAML to JSON-marshalable - // values is exactly the problem that it was designed to solve. - - // 1) Marshal it back to YAML string. - yamlBytes, err := candiedyaml.Marshal(document) - if err != nil { - return nil, err - } - - // 2) Use ghodss/yaml to unmarshal that string into JSON-compatible data structures. - var jsonObj interface{} - if err := yaml.Unmarshal(yamlBytes, &jsonObj); err != nil { - return nil, err - } - - // Now it's suitable for embedding in an API response. - stream = append(stream, jsonObj) - } -} - -type expander struct { -} - -// NewExpander returns an Go Templating expander. -func NewExpander() expansion.Expander { - return &expander{} -} - -// ExpandChart resolves the given files to a sequence of JSON-marshalable values. -func (e *expander) ExpandChart(request *expansion.ServiceRequest) (*expansion.ServiceResponse, error) { - - err := expansion.ValidateRequest(request) - if err != nil { - return nil, err - } - - request, err = expansion.ValidateProperties(request) - if err != nil { - return nil, err - } - - chartInv := request.ChartInvocation - chartMembers := request.Chart.Members - - resources := []interface{}{} - for _, file := range chartMembers { - name := file.Path - content := file.Content - tmpl := template.New(name).Funcs(sprig.HermeticTxtFuncMap()) - - for _, otherFile := range chartMembers { - otherName := otherFile.Path - otherContent := otherFile.Content - if name == otherName { - continue - } - _, err := tmpl.Parse(string(otherContent)) - if err != nil { - return nil, err - } - } - - // Have to put something in that resolves non-empty or Go templates get confused. - _, err := tmpl.Parse("# Content begins now") - if err != nil { - return nil, err - } - - tmpl, err = tmpl.Parse(string(content)) - if err != nil { - return nil, err - } - - generated := bytes.NewBuffer(nil) - if err := tmpl.ExecuteTemplate(generated, name, chartInv.Properties); err != nil { - return nil, err - } - - stream, err := parseYAMLStream(generated) - if err != nil { - return nil, fmt.Errorf("%s\nContent:\n%s", err.Error(), generated) - } - - for _, doc := range stream { - resources = append(resources, doc) - } - } - - return &expansion.ServiceResponse{Resources: resources}, nil -} diff --git a/cmd/goexpander/expander/expander_test.go b/cmd/goexpander/expander/expander_test.go deleted file mode 100644 index 95ce37c9d..000000000 --- a/cmd/goexpander/expander/expander_test.go +++ /dev/null @@ -1,262 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package expander - -import ( - "fmt" - "reflect" - "strings" - "testing" - - "github.com/kubernetes/helm/pkg/chart" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/expansion" -) - -// content provides an easy way to provide file content verbatim in tests. -func content(lines []string) []byte { - return []byte(strings.Join(lines, "\n") + "\n") -} - -func testExpansion(t *testing.T, req *expansion.ServiceRequest, - expResponse *expansion.ServiceResponse, expError string) { - backend := NewExpander() - response, err := backend.ExpandChart(req) - if err != nil { - message := err.Error() - if expResponse != nil || !strings.Contains(message, expError) { - t.Fatalf("unexpected error: %s\n", message) - } - } else { - if expResponse == nil { - t.Fatalf("expected error did not occur: %s\n", expError) - } - if !reflect.DeepEqual(response, expResponse) { - message := fmt.Sprintf( - "want:\n%s\nhave:\n%s\n", expResponse, response) - t.Fatalf("output mismatch:\n%s\n", message) - } - } -} - -var goExpander = &chart.Expander{ - Name: "GoTemplating", - Entrypoint: "templates/main.py", -} - -func TestEmpty(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: "gs://kubernetes-charts-testing/Test-1.2.3.tgz", - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: "Test", - Expander: goExpander, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{}, - }, - "", // Error - ) -} - -func TestSingle(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: "gs://kubernetes-charts-testing/Test-1.2.3.tgz", - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: "Test", - Expander: goExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.yaml", - Content: content([]string{ - "name: foo", - "type: bar", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "type": "bar", - }, - }, - }, - "", // Error - ) -} - -func TestProperties(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: "gs://kubernetes-charts-testing/Test-1.2.3.tgz", - Properties: map[string]interface{}{ - "prop1": 3.0, - "prop2": "foo", - }, - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: "Test", - Expander: goExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/main.yaml", - Content: content([]string{ - "name: foo", - "type: {{ .prop2 }}", - "properties:", - " something: {{ .prop1 }}", - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "foo", - "properties": map[string]interface{}{ - "something": 3.0, - }, - "type": "foo", - }, - }, - }, - "", // Error - ) -} - -func TestComplex(t *testing.T) { - testExpansion( - t, - &expansion.ServiceRequest{ - ChartInvocation: &common.Resource{ - Name: "test_invocation", - Type: "gs://kubernetes-charts-testing/Test-1.2.3.tgz", - Properties: map[string]interface{}{ - "DatabaseName": "mydb", - "NumRepicas": 3, - }, - }, - Chart: &chart.Content{ - Chartfile: &chart.Chartfile{ - Name: "Test", - Expander: goExpander, - }, - Members: []*chart.Member{ - { - Path: "templates/bar.tmpl", - Content: content([]string{ - `{{ template "banana" . }}`, - }), - }, - { - Path: "templates/base.tmpl", - Content: content([]string{ - `{{ define "apple" }}`, - `name: Abby`, - `kind: Apple`, - `dbname: {{default "whatdb" .DatabaseName}}`, - `{{ end }}`, - ``, - `{{ define "banana" }}`, - `name: Bobby`, - `kind: Banana`, - `dbname: {{default "whatdb" .DatabaseName}}`, - `{{ end }}`, - }), - }, - { - Path: "templates/foo.tmpl", - Content: content([]string{ - `---`, - `foo:`, - ` bar: baz`, - `---`, - `{{ template "apple" . }}`, - `---`, - `{{ template "apple" . }}`, - `...`, - }), - }, - { - Path: "templates/docs.txt", - Content: content([]string{ - `{{/*`, - `File contains only a comment.`, - `Suitable for documentation within templates/`, - `*/}}`, - }), - }, - { - Path: "templates/docs2.txt", - Content: content([]string{ - `# File contains only a comment.`, - `# Suitable for documentation within templates/`, - }), - }, - }, - }, - }, - &expansion.ServiceResponse{ - Resources: []interface{}{ - map[string]interface{}{ - "name": "Bobby", - "kind": "Banana", - "dbname": "mydb", - }, - map[string]interface{}{ - "foo": map[string]interface{}{ - "bar": "baz", - }, - }, - map[string]interface{}{ - "name": "Abby", - "kind": "Apple", - "dbname": "mydb", - }, - map[string]interface{}{ - "name": "Abby", - "kind": "Apple", - "dbname": "mydb", - }, - }, - }, - "", // Error - ) -} diff --git a/cmd/goexpander/main.go b/cmd/goexpander/main.go deleted file mode 100644 index ded71a35c..000000000 --- a/cmd/goexpander/main.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "github.com/kubernetes/helm/cmd/goexpander/expander" - "github.com/kubernetes/helm/pkg/expansion" - "github.com/kubernetes/helm/pkg/version" - - "flag" - "log" -) - -// interface that we are going to listen on -var address = flag.String("address", "", "Interface to listen on") - -// port that we are going to listen on -var port = flag.Int("port", 8080, "Port to listen on") - -func main() { - flag.Parse() - backend := expander.NewExpander() - service := expansion.NewService(*address, *port, backend) - log.Printf("Version: %s", version.Version) - log.Printf("Listening on http://%s:%d/expand", *address, *port) - log.Fatal(service.ListenAndServe()) -} diff --git a/cmd/helm/Makefile b/cmd/helm/Makefile deleted file mode 100644 index 30cfef5a5..000000000 --- a/cmd/helm/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright 2015 The Kubernetes Authors All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -SHELL := /bin/bash - -GOLANG_CROSSPLATFORMS := darwin/386 darwin/amd64 freebsd/386 freebsd/amd64 freebsd/arm linux/386 linux/amd64 linux/arm windows/386 windows/amd64 - -ROOT_DIR := $(abspath ./../..) -BIN_DIR := $(ROOT_DIR)/bin - -DEFAULT_BUCKET := gs://get-helm -STORAGE_BUCKET ?= $(DEFAULT_BUCKET) - -DEFAULT_TAG := git-$(shell git rev-parse --short HEAD) -TAG ?= $(DEFAULT_TAG) - -all: push - -push: - for platform in ${GOLANG_CROSSPLATFORMS}; do \ - echo $$platform; \ - PLATFORM=$${platform%/*} && ARCH=$${platform##*/} && \ - BINARY=$${PLATFORM}-$${ARCH} && \ - ZIP=${TAG}-helm-$${BINARY}.zip && \ - zip -j $${ZIP} ${BIN_DIR}/$${BINARY}/helm* && \ - gsutil cp $${ZIP} ${STORAGE_BUCKET} && \ - rm $${ZIP} ; \ - done diff --git a/cmd/helm/chart.go b/cmd/helm/chart.go deleted file mode 100644 index 91d4e7e85..000000000 --- a/cmd/helm/chart.go +++ /dev/null @@ -1,113 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/chart" - "github.com/kubernetes/helm/pkg/format" -) - -func init() { - addCommands(chartCommands()) -} - -const chartDesc = `A Chart is a package that can be installed and managed by Helm. - - The 'helm chart' subcommands provide tools for working with Helm charts. To - get started creating your own chart, use 'helm chart create NAME'. - - For details, use 'helm chart CMD -h'. -` - -func chartCommands() cli.Command { - return cli.Command{ - // Names following form prescribed here: http://is.gd/QUSEOF - Name: "chart", - Usage: "Perform chart-centered operations.", - Description: chartDesc, - Subcommands: []cli.Command{ - { - Name: "create", - Usage: "Create a new chart directory and set up base files and directories.", - ArgsUsage: "CHARTNAME", - Action: func(c *cli.Context) { run(c, createChart) }, - }, - { - Name: "package", - Aliases: []string{"pack"}, - Usage: "Given a chart directory, package it into a release.", - ArgsUsage: "PATH", - Action: func(c *cli.Context) { run(c, pack) }, - }, - }, - } -} - -func createChart(c *cli.Context) error { - args := c.Args() - if len(args) < 1 { - return errors.New("'helm create' requires a chart name as an argument") - } - - dir, name := filepath.Split(args[0]) - - cf := &chart.Chartfile{ - Name: name, - Description: "Created by Helm", - Version: "0.1.0", - } - - _, err := chart.Create(cf, dir) - return err - -} - -func pack(cxt *cli.Context) error { - args := cxt.Args() - if len(args) < 1 { - return errors.New("'helm package' requires a path to a chart directory as an argument") - } - - dir := args[0] - if fi, err := os.Stat(dir); err != nil { - return fmt.Errorf("Could not find directory %s: %s", dir, err) - } else if !fi.IsDir() { - return fmt.Errorf("Not a directory: %s", dir) - } - - fname, err := packDir(dir) - if err != nil { - return err - } - format.Msg(fname) - return nil -} - -func packDir(dir string) (string, error) { - c, err := chart.LoadDir(dir) - if err != nil { - return "", fmt.Errorf("Failed to load %s: %s", dir, err) - } - - return chart.Save(c, ".") -} diff --git a/cmd/helm/chart_upload.go b/cmd/helm/chart_upload.go deleted file mode 100644 index 6077675d3..000000000 --- a/cmd/helm/chart_upload.go +++ /dev/null @@ -1,115 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "fmt" - "os" - "regexp" - "strings" - - "github.com/aokoli/goutils" - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/chart" - "github.com/kubernetes/helm/pkg/format" -) - -func uploadChart(c *cli.Context) error { - args := c.Args() - if len(args) < 1 { - format.Err("First argument, filename, is required. Try 'helm deploy --help'") - os.Exit(1) - } - - cname := c.String("name") - fname := args[0] - - if fname == "" { - return errors.New("A filename must be specified. For a tar archive, this is the name of the root template in the archive.") - } - - _, err := doUpload(fname, cname, c) - return err -} -func doUpload(filename, cname string, cxt *cli.Context) (string, error) { - - fi, err := os.Stat(filename) - if err != nil { - return "", err - } - - if fi.IsDir() { - format.Info("Chart is directory") - c, err := chart.LoadDir(filename) - if err != nil { - return "", err - } - if cname == "" { - cname = genName(c.Chartfile().Name) - } - - // TODO: Is it better to generate the file in temp dir like this, or - // just put it in the CWD? - //tdir, err := ioutil.TempDir("", "helm-") - //if err != nil { - //format.Warn("Could not create temporary directory. Using .") - //tdir = "." - //} else { - //defer os.RemoveAll(tdir) - //} - tdir := "." - tfile, err := chart.Save(c, tdir) - if err != nil { - return "", err - } - filename = tfile - } else if cname == "" { - n, _, e := parseTarName(filename) - if e != nil { - return "", e - } - cname = n - } - - // TODO: Add a version build metadata on the chart. - - if cxt.Bool("dry-run") { - format.Info("Prepared deploy %q using file %q", cname, filename) - return "", nil - } - - c := NewClient(cxt) - return c.PostChart(filename, cname) -} - -func genName(pname string) string { - s, _ := goutils.RandomAlphaNumeric(8) - return fmt.Sprintf("%s-%s", pname, s) -} - -func parseTarName(name string) (string, string, error) { - tnregexp := regexp.MustCompile(chart.TarNameRegex) - if strings.HasSuffix(name, ".tgz") { - name = strings.TrimSuffix(name, ".tgz") - } - v := tnregexp.FindStringSubmatch(name) - if v == nil { - return name, "", fmt.Errorf("invalid name %s", name) - } - return v[1], v[2], nil -} diff --git a/cmd/helm/create.go b/cmd/helm/create.go new file mode 100644 index 000000000..1cf1e949c --- /dev/null +++ b/cmd/helm/create.go @@ -0,0 +1,63 @@ +package main + +import ( + "errors" + "path/filepath" + + "github.com/kubernetes/helm/pkg/chart" + "github.com/spf13/cobra" +) + +const createDesc = ` +This command creates a chart directory along with the common files and +directories used in a chart. + +For example, 'helm create foo' will create a directory structure that looks +something like this: + + foo/ + |- Chart.yaml # Information about your chart + | + |- values.toml # The default values for your templates + | + |- charts/ # Charts that this chart depends on + | + |- templates/ # The template files + +'helm create' takes a path for an argument. If directories in the given path +do not exist, Helm will attempt to create them as it goes. If the given +destination exists and there are files in that directory, conflicting files +will be overwritten, but other files will be left alone. +` + +func init() { + RootCommand.AddCommand(createCmd) +} + +var createCmd = &cobra.Command{ + Use: "create [PATH]", + Short: "Create a new chart at the location specified.", + Long: createDesc, + RunE: runCreate, +} + +func runCreate(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("the name of the new chart is required") + } + cname := args[0] + cmd.Printf("Creating %s\n", cname) + + chartname := filepath.Base(cname) + cfile := chart.Chartfile{ + Name: chartname, + Description: "A Helm chart for Kubernetes", + Version: "0.1.0", + } + + if _, err := chart.Create(&cfile, filepath.Dir(cname)); err != nil { + return err + } + + return nil +} diff --git a/cmd/helm/deploy.go b/cmd/helm/deploy.go deleted file mode 100644 index 5db207bc6..000000000 --- a/cmd/helm/deploy.go +++ /dev/null @@ -1,111 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "fmt" - "io/ioutil" - "os" - - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/common" - "gopkg.in/yaml.v2" -) - -func init() { - addCommands(deployCmd()) -} - -func deployCmd() cli.Command { - return cli.Command{ - Name: "deploy", - Usage: "Deploy a chart into the cluster.", - ArgsUsage: "[CHART]", - Action: func(c *cli.Context) { run(c, deploy) }, - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "config,c", - Usage: "The configuration YAML file for this deployment.", - }, - cli.StringFlag{ - Name: "name,n", - Usage: "Name of deployment, used for deploy and update commands (defaults to template name)", - }, - // TODO: I think there is a Generic flag type that we can implement parsing with. - cli.StringFlag{ - Name: "properties,p", - Usage: "A comma-separated list of key=value pairs: 'foo=bar,foo2=baz'.", - }, - }, - } -} - -func deploy(c *cli.Context) error { - - res := &common.Resource{ - // By default - Properties: map[string]interface{}{}, - } - - if c.String("config") != "" { - // If there is a configuration file, use it. - err := loadConfig(c.String("config"), &res.Properties) - if err != nil { - return err - } - } - - args := c.Args() - if len(args) == 0 { - return fmt.Errorf("Need chart name on commandline") - } - res.Type = args[0] - - if name := c.String("name"); len(name) > 0 { - res.Name = name - } else { - return fmt.Errorf("Need deployed name on commandline") - } - - if props, err := parseProperties(c.String("properties")); err != nil { - return err - } else if len(props) > 0 { - // Coalesce the properties into the first props. We have no way of - // knowing which resource the properties are supposed to be part - // of. - for n, v := range props { - res.Properties[n] = v - } - } - - return NewClient(c).PostDeployment(res) -} - -// isLocalChart returns true if the given path can be statted. -func isLocalChart(path string) bool { - _, err := os.Stat(path) - return err == nil -} - -// loadConfig loads chart arguments into c -func loadConfig(filename string, dest *map[string]interface{}) error { - data, err := ioutil.ReadFile(filename) - if err != nil { - return err - } - return yaml.Unmarshal(data, dest) -} diff --git a/cmd/helm/deployment.go b/cmd/helm/deployment.go deleted file mode 100644 index c17b68023..000000000 --- a/cmd/helm/deployment.go +++ /dev/null @@ -1,187 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "os" - "regexp" - "text/template" - - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/format" -) - -var ( - errMissingDeploymentArg = errors.New("First argument, deployment name, is required. Try 'helm get --help'") - errTooManyArgs = errors.New("Too many arguments provided. Try 'helm dep describe [DEPLOYMENT]'") -) - -const deploymentDesc = `A deployment is an instance of a Chart running in the cluster. - - Deployments have a name, a chart, and possibly a set of properites. The deployment - commands provide tools for managing deployments. - - To deploy a new chart, use the top-level 'helm deploy' command. From there, - the 'helm deployment' commands may be used to work with the deployed - application. - - For more help, use 'helm deployment CMD -h'.` - -const defaultShowFormat = `Name: {{.Name}} -Status: {{.State.Status}} -{{with .State.Errors}}Errors: -{{range .}} {{.}}{{end}} -{{end}}` - -const defaultShowResourceFormat = `Name: {{.Name}} -Type: {{.Type}} -Status: {{.State.Status}} -{{with .State.Errors}}Errors: -{{range .}} {{.}}{{end}} -{{end}}` - -func init() { - addCommands(deploymentCommands()) -} - -func deploymentCommands() cli.Command { - return cli.Command{ - // Names following form prescribed here: http://is.gd/QUSEOF - Name: "deployment", - Aliases: []string{"dep"}, - Usage: "Perform deployment-centered operations.", - Description: deploymentDesc, - Subcommands: []cli.Command{ - { - Name: "remove", - Aliases: []string{"rm"}, - Usage: "Deletes the named deployment(s).", - ArgsUsage: "DEPLOYMENT [DEPLOYMENT [...]]", - Action: func(c *cli.Context) { run(c, deleteDeployment) }, - }, - { - Name: "describe", - Usage: "Describes the kubernetes resources for the named deployment(s).", - ArgsUsage: "DEPLOYMENT", - Action: func(c *cli.Context) { run(c, describeDeployment) }, - }, - { - Name: "show", - Aliases: []string{"info"}, - Usage: "Provide details about this deployment.", - ArgsUsage: "", - Action: func(c *cli.Context) { run(c, showDeployment) }, - }, - { - Name: "list", - Aliases: []string{"ls"}, - Usage: "list all deployments, or filter by an optional regular expression.", - ArgsUsage: "REGEXP", - Action: func(c *cli.Context) { run(c, listDeployments) }, - }, - }, - } -} - -func listDeployments(c *cli.Context) error { - list, err := NewClient(c).ListDeployments() - if err != nil { - return err - } - args := c.Args() - if len(args) >= 1 { - pattern := args[0] - r, err := regexp.Compile(pattern) - if err != nil { - return err - } - - newlist := []string{} - for _, i := range list { - if r.MatchString(i) { - newlist = append(newlist, i) - } - } - list = newlist - } - - if len(list) == 0 { - return errors.New("no deployments found") - } - - format.List(list) - return nil -} - -func deleteDeployment(c *cli.Context) error { - args := c.Args() - if len(args) < 1 { - return errMissingDeploymentArg - } - for _, name := range args { - deployment, err := NewClient(c).DeleteDeployment(name) - if err != nil { - return err - } - format.Info("Deleted %q at %s", name, deployment.DeletedAt) - } - return nil -} - -func describeDeployment(c *cli.Context) error { - args := c.Args() - if len(args) < 1 { - return errMissingDeploymentArg - } - if len(args) > 1 { - return errTooManyArgs - } - name := args[0] - manifest, err := NewClient(c).DescribeDeployment(name) - if err != nil { - return err - } - - if manifest.ExpandedConfig == nil { - return errors.New("No ExpandedConfig found for: " + name) - } - - for _, resource := range manifest.ExpandedConfig.Resources { - tmpl := template.Must(template.New("showresource").Parse(defaultShowResourceFormat)) - err = tmpl.Execute(os.Stdout, resource) - if err != nil { - return err - } - - } - return nil -} - -func showDeployment(c *cli.Context) error { - args := c.Args() - if len(args) < 1 { - return errMissingDeploymentArg - } - name := args[0] - deployment, err := NewClient(c).GetDeployment(name) - if err != nil { - return err - } - tmpl := template.Must(template.New("show").Parse(defaultShowFormat)) - return tmpl.Execute(os.Stdout, deployment) -} diff --git a/cmd/helm/deployment_test.go b/cmd/helm/deployment_test.go deleted file mode 100644 index 581cbc6aa..000000000 --- a/cmd/helm/deployment_test.go +++ /dev/null @@ -1,107 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "encoding/json" - "net/http" - "testing" - - "github.com/kubernetes/helm/pkg/common" -) - -type pathAndResponse struct { - path string - resp interface{} -} - -func TestDeployment(t *testing.T) { - var deploymentTestCases = []struct { - args []string - resp []pathAndResponse - expected string - }{ - { - []string{"deployment", "show", "guestbook.yaml"}, - []pathAndResponse{{"/deployments/", &common.Deployment{ - Name: "guestbook.yaml", - State: &common.DeploymentState{Status: common.CreatedStatus}, - }}}, - "Name: guestbook.yaml\nStatus: Created\n", - }, - { - []string{"deployment", "show", "guestbook.yaml"}, - []pathAndResponse{{"/deployments/", &common.Deployment{ - Name: "guestbook.yaml", - State: &common.DeploymentState{ - Status: common.FailedStatus, - Errors: []string{"error message"}, - }, - }}}, - "Name: guestbook.yaml\nStatus: Failed\nErrors:\n error message\n", - }, - { - []string{"deployment", "list"}, - []pathAndResponse{{"/deployments/", []string{"guestbook.yaml"}}}, - "guestbook.yaml\n", - }, - { - []string{"deployment", "describe", "guestbook.yaml"}, - []pathAndResponse{{ - "/deployments/guestbook.yaml", - &common.Deployment{Name: "guestbook.yaml", - State: &common.DeploymentState{Status: common.CreatedStatus}, - LatestManifest: "manifestxyz", - }}, - {"/deployments/guestbook.yaml/manifests/manifestxyz", &common.Manifest{ - Deployment: "guestbook.yaml", - Name: "manifestxyz", - ExpandedConfig: &common.Configuration{ - Resources: []*common.Resource{ - {Name: "fe-rc", Type: "ReplicationController", State: &common.ResourceState{Status: common.Created}}, - {Name: "fe", Type: "Service", State: &common.ResourceState{Status: common.Created}}, - {Name: "be-rc", Type: "ReplicationController", State: &common.ResourceState{Status: common.Created}}, - {Name: "be", Type: "Service", State: &common.ResourceState{Status: common.Created}}, - }, - }, - }}}, - "Name: fe-rc\nType: ReplicationController\nStatus: Created\n" + - "Name: fe\nType: Service\nStatus: Created\n" + - "Name: be-rc\nType: ReplicationController\nStatus: Created\n" + - "Name: be\nType: Service\nStatus: Created\n", - }, - } - - for _, tc := range deploymentTestCases { - th := testHelm(t) - for _, pathAndResponse := range tc.resp { - var response = pathAndResponse.resp - th.mux.HandleFunc(pathAndResponse.path, func(w http.ResponseWriter, r *http.Request) { - data, err := json.Marshal(response) - th.must(err) - w.Write(data) - }) - } - - th.run(tc.args...) - - if tc.expected != th.output { - t.Errorf("Expected %v got %v", tc.expected, th.output) - } - th.cleanup() - } -} diff --git a/cmd/helm/doctor.go b/cmd/helm/doctor.go deleted file mode 100644 index 5fb8c1e43..000000000 --- a/cmd/helm/doctor.go +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/client" - "github.com/kubernetes/helm/pkg/format" - "github.com/kubernetes/helm/pkg/kubectl" -) - -func init() { - addCommands(doctorCmd()) -} - -func doctorCmd() cli.Command { - return cli.Command{ - Name: "doctor", - Usage: "Run a series of checks for necessary prerequisites.", - ArgsUsage: "", - Action: func(c *cli.Context) { run(c, doctor) }, - } -} - -func doctor(c *cli.Context) error { - var runner kubectl.Runner - runner = &kubectl.RealRunner{} - if client.IsInstalled(runner) { - format.Success("You have everything you need. Go forth my friend!") - } else { - format.Warning("Looks like you don't have the helm server-side components installed.\nRun: `helm server install`") - } - - return nil -} diff --git a/cmd/helm/fetch.go b/cmd/helm/fetch.go new file mode 100644 index 000000000..c14c4f103 --- /dev/null +++ b/cmd/helm/fetch.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "os" + + "github.com/spf13/cobra" +) + +func init() { + RootCommand.AddCommand(fetchCmd) +} + +var fetchCmd = &cobra.Command{ + Use: "fetch", + Short: "Download a chart from a repository and unpack it in local directory.", + Long: "", + RunE: fetch, +} + +func fetch(cmd *cobra.Command, args []string) error { + // parse args + // get download url + // call download url + out, err := os.Create("nginx-2.0.0.tgz") + if err != nil { + return err + } + defer out.Close() + resp, err := http.Get("http://localhost:8879/charts/nginx-2.0.0.tgz") + fmt.Println("after req") + // unpack file + if err != nil { + return err + } + + defer resp.Body.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + return nil +} diff --git a/cmd/helm/get.go b/cmd/helm/get.go new file mode 100644 index 000000000..c431543bd --- /dev/null +++ b/cmd/helm/get.go @@ -0,0 +1,53 @@ +package main + +import ( + "errors" + "fmt" + + "github.com/kubernetes/helm/pkg/helm" + "github.com/spf13/cobra" +) + +var getHelp = ` +This command shows the details of a named release. + +It can be used to get extended information about the release, including: + + - The values used to generate the release + - The chart used to generate the release + - The generated manifest file + +By default, this prints a human readable collection of information about the +chart, the supplied values, and the generated manifest file. +` + +var errReleaseRequired = errors.New("release name is required") + +var getCommand = &cobra.Command{ + Use: "get [flags] RELEASE_NAME", + Short: "Download a named release", + Long: getHelp, + RunE: getCmd, +} + +func init() { + RootCommand.AddCommand(getCommand) +} + +func getCmd(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errReleaseRequired + } + + res, err := helm.GetReleaseContent(args[0]) + if err != nil { + return err + } + + fmt.Printf("Chart/Version: %s %s\n", res.Release.Chart.Metadata.Name, res.Release.Chart.Metadata.Version) + fmt.Println("Config:") + fmt.Println(res.Release.Config) + fmt.Println("\nManifest:") + fmt.Println(res.Release.Manifest) + return nil +} diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 94b529671..43a032c00 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -1,119 +1,46 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - package main import ( "os" - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/client" - "github.com/kubernetes/helm/pkg/format" - "github.com/kubernetes/helm/pkg/version" + "github.com/spf13/cobra" ) -const desc = `Helm: the package and deployment manager for Kubernetes +var stdout = os.Stdout +var helmHome string - Helm is a tool for packaging, deploying, and managing Kubernetes - applications. It has a client component (this tool) and several in-cluster - components. +var globalUsage = `The Kubernetes package manager - Before you can use Helm to manage applications, you must install the - in-cluster components into the target Kubernetes cluster: +To begin working with Helm, run the 'helm init' command: - $ helm server install +$ helm init - Once the in-cluster portion is running, you can use 'helm deploy' to - deploy a new application: +This will install Tiller to your running Kubernetes cluster. +It will also set up any necessary local configuration. - $ helm deploy -n NAME CHART +Commond actions from this point on include: - For more information on Helm commands, you can use the following tools: +- helm search: search for charts +- helm fetch: download a chart to your local directory to view +- helm install: upload the chart to Kubernetes +- helm list: list releases of charts - $ helm help # top-level help - $ helm CMD --help # help for a particular command or set of commands +ENVIRONMENT: +$HELM_HOME: Set an alternative location for Helm files. + By default, these are stored in ~/.helm ` -var commands []cli.Command +// RootCommand is the top-level command for Helm. +var RootCommand = &cobra.Command{ + Use: "helm", + Short: "The Helm package manager for Kubernetes.", + Long: globalUsage, +} func init() { - addCommands(cmds()...) + RootCommand.PersistentFlags().StringVar(&helmHome, "home", "$HOME/.helm", "location of you Helm files [$HELM_HOME]") } -// debug indicates whether the process is in debug mode. -// -// This is set at app start-up time, based on the presence of the --debug -// flag. -var debug bool - func main() { - app := cli.NewApp() - app.Name = "helm" - app.Version = version.Version - app.Usage = desc - app.Commands = commands - - // TODO: make better - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "host,u", - Usage: "The URL of the DM server", - EnvVar: "HELM_HOST", - Value: "https://localhost:8000/", - }, - cli.StringFlag{ - Name: "kubectl", - Usage: "The path to the kubectl binary", - EnvVar: "KUBECTL", - }, - cli.IntFlag{ - Name: "timeout", - Usage: "Time in seconds to wait for response", - Value: 10, - }, - cli.BoolFlag{ - Name: "debug", - Usage: "Enable verbose debugging output", - }, - } - app.Before = func(c *cli.Context) error { - debug = c.GlobalBool("debug") - return nil - } - app.Run(os.Args) -} - -func cmds() []cli.Command { - return []cli.Command{} -} - -func addCommands(cmds ...cli.Command) { - commands = append(commands, cmds...) -} - -func run(c *cli.Context, f func(c *cli.Context) error) { - if err := f(c); err != nil { - format.Err(err) - os.Exit(1) - } -} - -// NewClient creates a new client instance preconfigured for CLI usage. -func NewClient(c *cli.Context) *client.Client { - host := c.GlobalString("host") - timeout := c.GlobalInt("timeout") - return client.NewClient(host).SetDebug(debug).SetTimeout(timeout) + RootCommand.Execute() } diff --git a/cmd/helm/helm_test.go b/cmd/helm/helm_test.go deleted file mode 100644 index 481be8a85..000000000 --- a/cmd/helm/helm_test.go +++ /dev/null @@ -1,118 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "io/ioutil" - "net/http" - "net/http/httptest" - "os" - "testing" - - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/format" -) - -type testHelmData struct { - t *testing.T - mux *http.ServeMux - server *httptest.Server - app *cli.App - output string -} - -func testHelm(t *testing.T) *testHelmData { - th := &testHelmData{t: t} - - th.app = cli.NewApp() - th.app.Commands = commands - - th.app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "host,u", - Value: "https://localhost:8000/", - }, - cli.IntFlag{ - Name: "timeout", - Value: 10, - }, - cli.BoolFlag{ - Name: "debug", - }, - } - th.app.Before = func(c *cli.Context) error { - debug = c.GlobalBool("debug") - return nil - } - - th.mux = http.NewServeMux() - th.server = httptest.NewServer(th.mux) - - return th -} - -func (th *testHelmData) cleanup() { - th.server.Close() -} - -func (th *testHelmData) URL() string { - return th.server.URL -} - -// must gives a fatal error if err is not nil. -func (th *testHelmData) must(err error) { - if err != nil { - th.t.Fatal(err) - } -} - -// check gives a test non-fatal error if err is not nil. -func (th *testHelmData) check(err error) { - if err != nil { - th.t.Error(err) - } -} - -func (th *testHelmData) run(args ...string) { - th.output = "" - args = append([]string{"helm", "--host", th.URL()}, args...) - th.output = captureOutput(func() { - th.app.Run(args) - }) -} - -// captureOutput redirect all log/std streams, capture and replace -func captureOutput(fn func()) string { - osStdout, osStderr := os.Stdout, os.Stderr - logStdout, logStderr := format.Stdout, format.Stderr - defer func() { - os.Stdout, os.Stderr = osStdout, osStderr - format.Stdout, format.Stderr = logStdout, logStderr - }() - - r, w, _ := os.Pipe() - - os.Stdout, os.Stderr = w, w - format.Stdout, format.Stderr = w, w - - fn() - - // read test output and restore previous stdout - w.Close() - b, _ := ioutil.ReadAll(r) - return string(b) -} diff --git a/cmd/helm/home.go b/cmd/helm/home.go new file mode 100644 index 000000000..0f3d4480a --- /dev/null +++ b/cmd/helm/home.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +var longHomeHelp = ` +This command displays the location of HELM_HOME. This is where +any helm configuration files live. +` + +var homeCommand = &cobra.Command{ + Use: "home", + Short: "Displays the location of HELM_HOME", + Long: longHomeHelp, + Run: home, +} + +func init() { + RootCommand.AddCommand(homeCommand) +} + +func home(cmd *cobra.Command, args []string) { + cmd.Printf(homePath() + "\n") +} diff --git a/cmd/helm/init.go b/cmd/helm/init.go new file mode 100644 index 000000000..3fc777c10 --- /dev/null +++ b/cmd/helm/init.go @@ -0,0 +1,120 @@ +package main + +import ( + "errors" + "fmt" + "io/ioutil" + "os" + + "github.com/kubernetes/helm/pkg/client" + "github.com/kubernetes/helm/pkg/kubectl" + "github.com/spf13/cobra" +) + +const initDesc = ` +This command installs Tiller (the helm server side component) onto your +Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.helm/) +` + +var ( + tillerImg string + defaultRepo = map[string]string{"default-name": "default-url"} +) + +func init() { + initCmd.Flags().StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image") + RootCommand.AddCommand(initCmd) +} + +var initCmd = &cobra.Command{ + Use: "init", + Short: "Initialize Helm on both client and server.", + Long: installDesc, + RunE: runInit, +} + +// runInit initializes local config and installs tiller to Kubernetes Cluster +func runInit(cmd *cobra.Command, args []string) error { + if len(args) != 0 { + return errors.New("This command does not accept arguments. \n") + } + + if err := ensureHome(); err != nil { + return err + } + + if err := installTiller(); err != nil { + return err + } + + fmt.Println("Happy Helming!") + return nil +} + +func installTiller() error { + // TODO: take value of global flag kubectl and pass that in + runner := buildKubectlRunner("") + + i := client.NewInstaller() + i.Tiller["Image"] = tillerImg + out, err := i.Install(runner) + + if err != nil { + return fmt.Errorf("error installing %s %s", string(out), err) + } + fmt.Println("\nTiller (the helm server side component) has been installed into your Kubernetes Cluster.") + + return nil +} + +func buildKubectlRunner(kubectlPath string) kubectl.Runner { + if kubectlPath != "" { + kubectl.Path = kubectlPath + } + return &kubectl.RealRunner{} +} + +// ensureHome checks to see if $HELM_HOME exists +// +// If $HELM_HOME does not exist, this function will create it. +func ensureHome() error { + configDirectories := []string{homePath(), cacheDirectory(), localRepoDirectory()} + + for _, p := range configDirectories { + if fi, err := os.Stat(p); err != nil { + fmt.Printf("Creating %s \n", p) + if err := os.MkdirAll(p, 0755); err != nil { + return fmt.Errorf("Could not create %s: %s", p, err) + } + } else if !fi.IsDir() { + return fmt.Errorf("%s must be a directory", p) + } + } + + repoFile := repositoriesFile() + if fi, err := os.Stat(repoFile); err != nil { + fmt.Printf("Creating %s \n", repoFile) + if err := ioutil.WriteFile(repoFile, []byte("local: localhost:8879/charts\n"), 0644); err != nil { + return err + } + } else if fi.IsDir() { + return fmt.Errorf("%s must be a file, not a directory", repoFile) + } + + localRepoCacheFile := localRepoDirectory(localRepoCacheFilePath) + if fi, err := os.Stat(localRepoCacheFile); err != nil { + fmt.Printf("Creating %s \n", localRepoCacheFile) + _, err := os.Create(localRepoCacheFile) + if err != nil { + return err + } + + //TODO: take this out and replace with helm update functionality + os.Symlink(localRepoCacheFile, cacheDirectory("local-cache.yaml")) + } else if fi.IsDir() { + return fmt.Errorf("%s must be a file, not a directory", localRepoCacheFile) + } + + fmt.Printf("$HELM_HOME has been configured at %s.\n", helmHome) + return nil +} diff --git a/cmd/helm/init_test.go b/cmd/helm/init_test.go new file mode 100644 index 000000000..6c5bebbe9 --- /dev/null +++ b/cmd/helm/init_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "io/ioutil" + "os" + "testing" +) + +func TestEnsureHome(t *testing.T) { + home := createTmpHome() + helmHome = home + if err := ensureHome(); err != nil { + t.Errorf("%s", err) + } + + expectedDirs := []string{homePath(), cacheDirectory(), localRepoDirectory()} + for _, dir := range expectedDirs { + if fi, err := os.Stat(dir); err != nil { + t.Errorf("%s", err) + } else if !fi.IsDir() { + t.Errorf("%s is not a directory", fi) + } + } + + if fi, err := os.Stat(repositoriesFile()); err != nil { + t.Errorf("%s", err) + } else if fi.IsDir() { + t.Errorf("%s should not be a directory", fi) + } + + if fi, err := os.Stat(localRepoDirectory(localRepoCacheFilePath)); err != nil { + t.Errorf("%s", err) + } else if fi.IsDir() { + t.Errorf("%s should not be a directory", fi) + } +} + +func createTmpHome() string { + tmpHome, _ := ioutil.TempDir("", "helm_home") + defer os.Remove(tmpHome) + return tmpHome +} diff --git a/cmd/helm/install.go b/cmd/helm/install.go new file mode 100644 index 000000000..8b515522c --- /dev/null +++ b/cmd/helm/install.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/cobra" + + "github.com/kubernetes/helm/pkg/chart" + "github.com/kubernetes/helm/pkg/helm" +) + +const installDesc = ` +This command installs a chart archive. +` + +func init() { + RootCommand.Flags() + RootCommand.AddCommand(installCmd) +} + +var installCmd = &cobra.Command{ + Use: "install [CHART]", + Short: "install a chart archive.", + Long: installDesc, + RunE: runInstall, +} + +func runInstall(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("This command needs at least one argument, the name of the chart.") + } + + ch, err := loadChart(args[0]) + if err != nil { + return err + } + + res, err := helm.InstallRelease(ch) + if err != nil { + return err + } + + fmt.Printf("release.name: %s\n", res.Release.Name) + fmt.Printf("release.chart: %s\n", res.Release.Chart.Metadata.Name) + fmt.Printf("release.status: %s\n", res.Release.Info.Status.Code) + + return nil +} + +func loadChart(path string) (*chart.Chart, error) { + path, err := filepath.Abs(path) + if err != nil { + return nil, err + } + + if fi, err := os.Stat(path); err != nil { + return nil, err + } else if fi.IsDir() { + return chart.LoadDir(path) + } + + return chart.Load(path) +} diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go new file mode 100644 index 000000000..04eb25c6c --- /dev/null +++ b/cmd/helm/lint.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + + "github.com/kubernetes/helm/pkg/lint" + "github.com/spf13/cobra" +) + +var longLintHelp = ` +This command takes a path to a chart and runs a series of tests to verify that +the chart is well-formed. + +If the linter encounters things that will cause the chart to fail installation, +it will emit [ERROR] messages. If it encounters issues that break with convention +or recommendation, it will emit [WARNING] messages. +` + +var lintCommand = &cobra.Command{ + Use: "lint [flags] PATH", + Short: "Examines a chart for possible issues", + Long: longLintHelp, + Run: lintCmd, +} + +func init() { + RootCommand.AddCommand(lintCommand) +} + +func lintCmd(cmd *cobra.Command, args []string) { + path := "." + if len(args) > 0 { + path = args[0] + } + issues := lint.All(path) + for _, i := range issues { + fmt.Printf("%s\n", i) + } +} diff --git a/cmd/helm/package.go b/cmd/helm/package.go new file mode 100644 index 000000000..c64784ef4 --- /dev/null +++ b/cmd/helm/package.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/kubernetes/helm/pkg/chart" + "github.com/kubernetes/helm/pkg/repo" + "github.com/spf13/cobra" +) + +const packageDesc = ` +This command packages a chart into a versioned chart archive file. If a path +is given, this will look at that path for a chart (which must contain a +Chart.yaml file) and then package that directory. + +If no path is given, this will look in the present working directory for a +Chart.yaml file, and (if found) build the current directory into a chart. + +Versioned chart archives are used by Helm package repositories. +` + +var save bool + +func init() { + packageCmd.Flags().BoolVar(&save, "save", true, "save packaged chart to local chart repository") + RootCommand.AddCommand(packageCmd) +} + +var packageCmd = &cobra.Command{ + Use: "package [CHART_PATH]", + Short: "Package a chart directory into a chart archive.", + Long: packageDesc, + RunE: runPackage, +} + +func runPackage(cmd *cobra.Command, args []string) error { + path := "." + + if len(args) > 0 { + path = args[0] + } else { + return fmt.Errorf("This command needs at least one argument, the path to the chart.") + } + + path, err := filepath.Abs(path) + if err != nil { + return err + } + + ch, err := chart.LoadDir(path) + if err != nil { + return err + } + + // Save to $HELM_HOME/local directory. + if save { + if err := repo.AddChartToLocalRepo(ch, localRepoDirectory()); err != nil { + return err + } + } + + // Save to the current working directory. + cwd, err := os.Getwd() + if err != nil { + return err + } + name, err := chart.Save(ch, cwd) + if err == nil { + cmd.Printf("Saved %s to current directory\n", name) + } + return err +} diff --git a/cmd/helm/properties.go b/cmd/helm/properties.go deleted file mode 100644 index df8797f78..000000000 --- a/cmd/helm/properties.go +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "strconv" - "strings" -) - -// TODO: The concept of property here is really simple. We could definitely get -// better about the values we allow. Also, we need some validation on the names. - -var errInvalidProperty = errors.New("property is not in name=value format") - -// parseProperties is a utility for parsing a comma-separated key=value string. -func parseProperties(kvstr string) (map[string]interface{}, error) { - properties := map[string]interface{}{} - - if len(kvstr) == 0 { - return properties, nil - } - - pairs := strings.Split(kvstr, ",") - for _, p := range pairs { - // Allow for "k=v, k=v" - p = strings.TrimSpace(p) - pair := strings.Split(p, "=") - if len(pair) < 2 { - return properties, errInvalidProperty - } - - // If the value looks int-like, convert it. - if i, err := strconv.Atoi(pair[1]); err == nil { - properties[pair[0]] = i - } else { - properties[pair[0]] = pair[1] - } - } - - return properties, nil -} diff --git a/cmd/helm/remove.go b/cmd/helm/remove.go new file mode 100644 index 000000000..900c1f06d --- /dev/null +++ b/cmd/helm/remove.go @@ -0,0 +1,52 @@ +package main + +import ( + "errors" + "fmt" + + "github.com/kubernetes/helm/pkg/helm" + "github.com/spf13/cobra" +) + +const removeDesc = ` +This command takes a release name, and then deletes the release from Kubernetes. +It removes all of the resources associated with the last release of the chart. + +Use the '--dry-run' flag to see which releases will be deleted without actually +deleting them. +` + +var removeDryRun bool + +var removeCommand = &cobra.Command{ + Use: "remove [flags] RELEASE_NAME", + Aliases: []string{"rm"}, + SuggestFor: []string{"delete", "del"}, + Short: "Given a release name, remove the release from Kubernetes", + Long: removeDesc, + RunE: rmRelease, +} + +func init() { + RootCommand.AddCommand(removeCommand) + removeCommand.Flags().BoolVar(&removeDryRun, "dry-run", false, "Simulate action, but don't actually do it.") +} + +func rmRelease(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("Command 'remove' requires a release name.") + } + + // TODO: Handle dry run use case. + if removeDryRun { + fmt.Printf("Deleting %s\n", args[0]) + return nil + } + + _, err := helm.UninstallRelease(args[0]) + if err != nil { + return err + } + + return nil +} diff --git a/cmd/helm/repo.go b/cmd/helm/repo.go new file mode 100644 index 000000000..97612cc78 --- /dev/null +++ b/cmd/helm/repo.go @@ -0,0 +1,99 @@ +package main + +import ( + "fmt" + "os" + + "github.com/gosuri/uitable" + "github.com/kubernetes/helm/pkg/repo" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +func init() { + repoCmd.AddCommand(repoAddCmd) + repoCmd.AddCommand(repoListCmd) + RootCommand.AddCommand(repoCmd) +} + +var repoCmd = &cobra.Command{ + Use: "repo add|remove|list [ARG]", + Short: "add, list, or remove chart repositories", +} + +var repoAddCmd = &cobra.Command{ + Use: "add [flags] [NAME] [URL]", + Short: "add a chart repository", + RunE: runRepoAdd, +} + +var repoListCmd = &cobra.Command{ + Use: "list [flags]", + Short: "list chart repositories", + RunE: runRepoList, +} + +func runRepoAdd(cmd *cobra.Command, args []string) error { + if len(args) != 2 { + return fmt.Errorf("This command needs two argument, a name for the chart repository and the url of the chart repository") + } + + err := insertRepoLine(args[0], args[1]) + if err != nil { + return err + } + fmt.Println(args[0] + " has been added to your repositories") + return nil +} + +func runRepoList(cmd *cobra.Command, args []string) error { + f, err := repo.LoadRepositoriesFile(repositoriesFile()) + if err != nil { + return err + } + if len(f.Repositories) == 0 { + fmt.Println("No repositories to show") + return nil + } + table := uitable.New() + table.MaxColWidth = 50 + table.AddRow("NAME", "URL") + for k, v := range f.Repositories { + table.AddRow(k, v) + } + fmt.Println(table) + return nil +} + +func insertRepoLine(name, url string) error { + err := checkUniqueName(name) + if err != nil { + return err + } + + b, _ := yaml.Marshal(map[string]string{name: url}) + f, err := os.OpenFile(repositoriesFile(), os.O_APPEND|os.O_WRONLY, 0666) + if err != nil { + return err + } + defer f.Close() + _, err = f.Write(b) + if err != nil { + return err + } + + return nil +} + +func checkUniqueName(name string) error { + file, err := repo.LoadRepositoriesFile(repositoriesFile()) + if err != nil { + return err + } + + _, ok := file.Repositories[name] + if ok { + return fmt.Errorf("The repository name you provided (%s) already exists. Please specifiy a different name.", name) + } + return nil +} diff --git a/cmd/helm/repo_test.go b/cmd/helm/repo_test.go new file mode 100644 index 000000000..6f29a20d7 --- /dev/null +++ b/cmd/helm/repo_test.go @@ -0,0 +1,35 @@ +package main + +import ( + "testing" + + "github.com/kubernetes/helm/pkg/repo" +) + +func TestRepoAdd(t *testing.T) { + home := createTmpHome() + helmHome = home + if err := ensureHome(); err != nil { + t.Errorf("%s", err) + } + + testName := "test-name" + testURL := "test-url" + if err := insertRepoLine(testName, testURL); err != nil { + t.Errorf("%s", err) + } + + f, err := repo.LoadRepositoriesFile(repositoriesFile()) + if err != nil { + t.Errorf("%s", err) + } + _, ok := f.Repositories[testName] + if !ok { + t.Errorf("%s was not successfully inserted into %s", testName, repositoriesFile()) + } + + if err := insertRepoLine(testName, testURL); err == nil { + t.Errorf("Duplicate repository name was added") + } + +} diff --git a/cmd/helm/repository.go b/cmd/helm/repository.go deleted file mode 100644 index f17c2fae2..000000000 --- a/cmd/helm/repository.go +++ /dev/null @@ -1,130 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "encoding/json" - "errors" - "path/filepath" - - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/format" - "github.com/kubernetes/helm/pkg/repo" -) - -func init() { - addCommands(repoCommands()) -} - -const chartRepoPath = "repositories" - -const repoDesc = `Helm repositories store Helm charts. - - The repository commands are used to manage which Helm repositories Helm may - use as a source for Charts. The repositories are accessed by in-cluster Helm - components. - - To list the repositories that your server knows about, use 'helm repo list'. - - For more details, use 'helm repo CMD -h'. -` - -const addRepoDesc = `The add repository command is used to add a name a repository url to your - chart repository list. The repository url must begin with a valid protocoal. At the moment, - we only support google cloud storage for chart repositories. - - A valid command might look like: - $ helm repo add charts gs://kubernetes-charts -` - -func repoCommands() cli.Command { - return cli.Command{ - Name: "repository", - Aliases: []string{"repo"}, - Usage: "Perform chart repository operations.", - Description: repoDesc, - Subcommands: []cli.Command{ - { - Name: "add", - Usage: "Add a chart repository to the remote manager.", - Description: addRepoDesc, - ArgsUsage: "[NAME] [REPOSITORY_URL]", - Action: func(c *cli.Context) { run(c, addRepo) }, - }, - { - Name: "list", - Usage: "List the chart repositories on the remote manager.", - ArgsUsage: "", - Action: func(c *cli.Context) { run(c, listRepos) }, - }, - { - Name: "remove", - Aliases: []string{"rm"}, - Usage: "Remove a chart repository from the remote manager.", - ArgsUsage: "REPOSITORY_NAME", - Action: func(c *cli.Context) { run(c, removeRepo) }, - }, - }, - } -} - -func addRepo(c *cli.Context) error { - args := c.Args() - if len(args) < 2 { - return errors.New("'helm repo add' requires a name and repository url as arguments") - } - name := args[0] - repoURL := args[1] - payload, _ := json.Marshal(repo.Repo{URL: repoURL, Name: name}) - msg := "" - if _, err := NewClient(c).Post(chartRepoPath, payload, &msg); err != nil { - return err - } - format.Info(name + " has been added to your chart repositories!") - return nil -} - -func listRepos(c *cli.Context) error { - dest := map[string]string{} - if _, err := NewClient(c).Get(chartRepoPath, &dest); err != nil { - return err - } - if len(dest) < 1 { - format.Info("Looks like you don't have any chart repositories.") - format.Info("Add a chart repository using the `helm repo add [REPOSITORY_URL]` command.") - } else { - format.Msg("Chart Repositories:\n") - for k, v := range dest { - //TODO: make formatting pretty - format.Msg(k + "\t" + v + "\n") - } - } - return nil -} - -func removeRepo(c *cli.Context) error { - args := c.Args() - if len(args) < 1 { - return errors.New("'helm repo remove' requires a repository name as an argument") - } - name := args[0] - if _, err := NewClient(c).Delete(filepath.Join(chartRepoPath, name), nil); err != nil { - return err - } - format.Msg(name + " has been removed.\n") - return nil -} diff --git a/cmd/helm/search.go b/cmd/helm/search.go new file mode 100644 index 000000000..9ef651387 --- /dev/null +++ b/cmd/helm/search.go @@ -0,0 +1,60 @@ +package main + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/kubernetes/helm/pkg/repo" + "github.com/spf13/cobra" +) + +func init() { + RootCommand.AddCommand(searchCmd) +} + +var searchCmd = &cobra.Command{ + Use: "search [CHART]", + Short: "Search for charts", + Long: "", //TODO: add search command description + RunE: search, +} + +func search(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("This command needs at least one argument") + } + + results, err := searchCacheForPattern(args[0]) + if err != nil { + return err + } + cmd.Println("Charts:") + for _, result := range results { + fmt.Println(result) + } + return nil +} + +func searchCacheForPattern(name string) ([]string, error) { + fileList := []string{} + filepath.Walk(cacheDirectory(), func(path string, f os.FileInfo, err error) error { + if !f.IsDir() { + fileList = append(fileList, path) + } + return nil + }) + matches := []string{} + for _, f := range fileList { + cache, _ := repo.LoadCacheFile(f) + repoName := filepath.Base(strings.TrimRight(f, "-cache.txt")) + for k := range cache.Entries { + if strings.Contains(k, name) { + matches = append(matches, repoName+"/"+k) + } + } + } + return matches, nil +} diff --git a/cmd/helm/serve.go b/cmd/helm/serve.go new file mode 100644 index 000000000..0673f13e5 --- /dev/null +++ b/cmd/helm/serve.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/kubernetes/helm/pkg/repo" + "github.com/spf13/cobra" +) + +var serveDesc = `This command starts a local chart repository server that serves the charts saved in your $HELM_HOME/local/ directory.` + +//TODO: add repoPath flag to be passed in in case you want +// to serve charts from a different local dir + +func init() { + RootCommand.AddCommand(serveCmd) +} + +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Start a local http web server", + Long: serveDesc, + Run: serve, +} + +func serve(cmd *cobra.Command, args []string) { + repo.StartLocalRepo(localRepoDirectory()) +} diff --git a/cmd/helm/server.go b/cmd/helm/server.go deleted file mode 100644 index ee902da40..000000000 --- a/cmd/helm/server.go +++ /dev/null @@ -1,195 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "fmt" - - "github.com/codegangsta/cli" - "github.com/kubernetes/helm/pkg/client" - "github.com/kubernetes/helm/pkg/format" - "github.com/kubernetes/helm/pkg/kubectl" -) - -// ErrAlreadyInstalled indicates that Helm Server is already installed. -var ErrAlreadyInstalled = errors.New("Already Installed") - -func init() { - addCommands(dmCmd()) -} - -func dmCmd() cli.Command { - return cli.Command{ - Name: "server", - Usage: "Manage Helm server-side components", - Description: `Server commands manage the in-cluster portion of Helm. - - Helm has several components that run inside of Kubernetes. Before Helm can - be used to install and manage packages, it must be installed into the - Kubernetes cluster in which packages will be installed. - - The 'helm server' commands rely upon a properly configured 'kubectl' to - communicate with the Kubernetes cluster. To verify that your 'kubectl' - client is pointed to the correct cluster, use 'kubectl cluster-info'. - - Use 'helm server install' to install the in-cluster portion of Helm. -`, - Subcommands: []cli.Command{ - { - Name: "install", - Usage: "Install Helm server components on Kubernetes.", - ArgsUsage: "", - Description: `Use kubectl to install Helm components in their own namespace on Kubernetes. - - Make sure your Kubernetes environment is pointed to the cluster on which you - wish to install.`, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Show what would be installed, but don't install anything.", - }, - cli.StringFlag{ - Name: "resourcifier-image", - Usage: "The full image name of the Docker image for resourcifier.", - EnvVar: "HELM_RESOURCIFIER_IMAGE", - }, - cli.StringFlag{ - Name: "expandybird-image", - Usage: "The full image name of the Docker image for expandybird.", - EnvVar: "HELM_EXPANDYBIRD_IMAGE", - }, - cli.StringFlag{ - Name: "manager-image", - Usage: "The full image name of the Docker image for manager.", - EnvVar: "HELM_MANAGER_IMAGE", - }, - }, - Action: func(c *cli.Context) { run(c, installServer) }, - }, - { - Name: "uninstall", - Usage: "Uninstall the Helm server-side from Kubernetes.", - ArgsUsage: "", - Description: ``, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Show what would be uninstalled, but don't remove anything.", - }, - }, - Action: func(c *cli.Context) { run(c, uninstallServer) }, - }, - { - Name: "status", - Usage: "Show status of Helm server-side components.", - ArgsUsage: "", - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Only display the underlying kubectl commands.", - }, - }, - Action: func(c *cli.Context) { run(c, statusServer) }, - }, - { - Name: "target", - Usage: "Displays information about the Kubernetes cluster.", - ArgsUsage: "", - Action: func(c *cli.Context) { run(c, targetServer) }, - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "dry-run", - Usage: "Only display the underlying kubectl commands.", - }, - }, - }, - }, - } -} - -func installServer(c *cli.Context) error { - resImg := c.String("resourcifier-image") - ebImg := c.String("expandybird-image") - manImg := c.String("manager-image") - - dryRun := c.Bool("dry-run") - kubectlPath := c.GlobalString("kubectl") - runner := buildKubectlRunner(kubectlPath, dryRun) - - i := client.NewInstaller() - i.Manager["Image"] = manImg - i.Resourcifier["Image"] = resImg - i.Expandybird["Image"] = ebImg - - out, err := i.Install(runner) - if err != nil { - return fmt.Errorf("error installing %s %s", string(out), err) - } - format.Msg(out) - return nil -} - -func uninstallServer(c *cli.Context) error { - dryRun := c.Bool("dry-run") - kubectlPath := c.GlobalString("kubectl") - runner := buildKubectlRunner(kubectlPath, dryRun) - - out, err := client.Uninstall(runner) - if err != nil { - return fmt.Errorf("error uninstalling: %s %s", out, err) - } - format.Msg(out) - return nil -} - -func statusServer(c *cli.Context) error { - dryRun := c.Bool("dry-run") - kubectlPath := c.GlobalString("kubectl") - runner := buildKubectlRunner(kubectlPath, dryRun) - - out, err := runner.GetByKind("pods", "", "helm") - if err != nil { - return err - } - format.Msg(string(out)) - return nil -} - -func targetServer(c *cli.Context) error { - dryRun := c.Bool("dry-run") - kubectlPath := c.GlobalString("kubectl") - runner := buildKubectlRunner(kubectlPath, dryRun) - - out, err := runner.ClusterInfo() - if err != nil { - return fmt.Errorf("%s (%s)", out, err) - } - format.Msg(string(out)) - return nil -} - -func buildKubectlRunner(kubectlPath string, dryRun bool) kubectl.Runner { - if dryRun { - return &kubectl.PrintRunner{} - } - // TODO: Refactor out kubectl.Path global - if kubectlPath != "" { - kubectl.Path = kubectlPath - } - return &kubectl.RealRunner{} -} diff --git a/cmd/helm/status.go b/cmd/helm/status.go new file mode 100644 index 000000000..5a381545d --- /dev/null +++ b/cmd/helm/status.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "time" + + "github.com/kubernetes/helm/pkg/helm" + "github.com/kubernetes/helm/pkg/timeconv" + "github.com/spf13/cobra" +) + +var statusHelp = ` +This command shows the status of a named release. +` + +var statusCommand = &cobra.Command{ + Use: "status [flags] RELEASE_NAME", + Short: "Displays the status of the named release", + Long: statusHelp, + RunE: status, +} + +func init() { + RootCommand.AddCommand(statusCommand) +} + +func status(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errReleaseRequired + } + + res, err := helm.GetReleaseStatus(args[0]) + if err != nil { + return err + } + + fmt.Printf("Last Deployed: %s\n", timeconv.Format(res.Info.LastDeployed, time.ANSIC)) + fmt.Printf("Status: %s\n", res.Info.Status.Code) + if res.Info.Status.Details != nil { + fmt.Printf("Details: %s\n", res.Info.Status.Details) + } + + return nil +} diff --git a/cmd/helm/structure.go b/cmd/helm/structure.go new file mode 100644 index 000000000..056be291d --- /dev/null +++ b/cmd/helm/structure.go @@ -0,0 +1,31 @@ +package main + +import ( + "os" + "path/filepath" +) + +const ( + repositoriesFilePath string = "repositories.yaml" + cachePath string = "cache" + localRepoPath string = "local" + localRepoCacheFilePath string = "cache.yaml" +) + +func homePath() string { + return os.ExpandEnv(helmHome) +} + +func cacheDirectory(paths ...string) string { + fragments := append([]string{homePath(), cachePath}, paths...) + return filepath.Join(fragments...) +} + +func localRepoDirectory(paths ...string) string { + fragments := append([]string{homePath(), localRepoPath}, paths...) + return filepath.Join(fragments...) +} + +func repositoriesFile() string { + return filepath.Join(homePath(), repositoriesFilePath) +} diff --git a/cmd/manager/chartrepos.go b/cmd/manager/chartrepos.go deleted file mode 100644 index 8a1ab1304..000000000 --- a/cmd/manager/chartrepos.go +++ /dev/null @@ -1,169 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "github.com/kubernetes/helm/cmd/manager/router" - "github.com/kubernetes/helm/pkg/httputil" - "github.com/kubernetes/helm/pkg/repo" - "github.com/kubernetes/helm/pkg/util" - - "encoding/json" - "net/http" - "net/url" - "regexp" -) - -func registerChartRepoRoutes(c *router.Context, h *router.Handler) { - h.Add("GET /repositories", listChartReposHandlerFunc) - h.Add("GET /repositories/*", getChartRepoHandlerFunc) - h.Add("GET /repositories/*/charts", listRepoChartsHandlerFunc) - h.Add("GET /repositories/*/charts/*", getRepoChartHandlerFunc) - h.Add("POST /repositories", addChartRepoHandlerFunc) - h.Add("DELETE /repositories/*", removeChartRepoHandlerFunc) -} - -func listChartReposHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list chart repositories" - repos, err := c.Manager.ListRepos() - if err != nil { - return err - } - - util.LogHandlerExitWithJSON(handler, w, repos, http.StatusOK) - return nil -} - -func addChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: add chart repository" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - cr := &repo.Repo{} - if err := httputil.Decode(w, r, cr); err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - if string(cr.Format) == "" { - cr.Format = repo.GCSRepoFormat - } - - if string(cr.Type) == "" { - cr.Type = repo.GCSRepoType - } - - if err := c.Manager.AddRepo(cr); err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - msg, _ := json.Marshal(cr.Name + " has been added to the list of chart repositories.") - util.LogHandlerExitWithJSON(handler, w, msg, http.StatusCreated) - return nil -} - -func removeChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: remove chart repository" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - name, err := pos(w, r, 2) - if err != nil { - return err - } - - err = c.Manager.RemoveRepo(name) - if err != nil { - return err - } - - msg, _ := json.Marshal(name + " has been removed from the list of chart repositories.") - util.LogHandlerExitWithJSON(handler, w, msg, http.StatusOK) - return nil -} - -func getChartRepoHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get repository" - util.LogHandlerEntry(handler, r) - repoURL, err := pos(w, r, 2) - if err != nil { - return err - } - - cr, err := c.Manager.GetRepo(repoURL) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, cr, http.StatusOK) - return nil -} - -func listRepoChartsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list repository charts" - util.LogHandlerEntry(handler, r) - repoURL, err := pos(w, r, 2) - if err != nil { - return err - } - - values, err := url.ParseQuery(r.URL.RawQuery) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - var regex *regexp.Regexp - regexString := values.Get("regex") - if regexString != "" { - regex, err = regexp.Compile(regexString) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - } - - repoCharts, err := c.Manager.ListRepoCharts(repoURL, regex) - if err != nil { - return err - } - - util.LogHandlerExitWithJSON(handler, w, repoCharts, http.StatusOK) - return nil -} - -func getRepoChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get repository charts" - util.LogHandlerEntry(handler, r) - repoURL, err := pos(w, r, 2) - if err != nil { - return err - } - - chartName, err := pos(w, r, 4) - if err != nil { - return err - } - - repoChart, err := c.Manager.GetChartForRepo(repoURL, chartName) - if err != nil { - return err - } - - util.LogHandlerExitWithJSON(handler, w, repoChart, http.StatusOK) - return nil -} diff --git a/cmd/manager/chartrepos_test.go b/cmd/manager/chartrepos_test.go deleted file mode 100644 index 4662bac6d..000000000 --- a/cmd/manager/chartrepos_test.go +++ /dev/null @@ -1,174 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "github.com/kubernetes/helm/pkg/repo" - - "bytes" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "testing" -) - -var ( - TestRepoBucket = "kubernetes-charts-testing" - TestRepoURL = "gs://" + TestRepoBucket - TestChartName = "frobnitz-0.0.1.tgz" - TestRepoType = string(repo.GCSRepoType) - TestRepoFormat = string(repo.GCSRepoFormat) - TestRepoCredentialName = "default" -) - -func TestListChartRepos(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /repositories", listChartReposHandlerFunc) - defer s.Close() - - URL := getTestURL(t, s.URL, "", "") - res, err := http.Get(URL) - if err != nil { - t.Fatalf("Failed GET: %s", err) - } - - if res.StatusCode != http.StatusOK { - t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) - } -} - -func TestGetChartRepo(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /repositories/*", getChartRepoHandlerFunc) - defer s.Close() - - URL := getTestURL(t, s.URL, TestRepoBucket, "") - res, err := http.Get(URL) - if err != nil { - t.Fatalf("Failed GET: %s", err) - } - - if res.StatusCode != http.StatusOK { - t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) - } -} - -func TestListRepoCharts(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /repositories/*/charts", listRepoChartsHandlerFunc) - defer s.Close() - - URL := getTestURL(t, s.URL, TestRepoBucket, "charts") - res, err := http.Get(URL) - if err != nil { - t.Fatalf("Failed GET: %s", err) - } - - if res.StatusCode != http.StatusOK { - t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) - } -} - -func TestGetRepoChart(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /repositories/*/charts/*", getRepoChartHandlerFunc) - defer s.Close() - - chartURL := fmt.Sprintf("charts/%s", TestChartName) - URL := getTestURL(t, s.URL, TestRepoBucket, chartURL) - res, err := http.Get(URL) - if err != nil { - t.Fatalf("Failed GET: %s", err) - } - - if res.StatusCode != http.StatusOK { - t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) - } -} - -func TestAddChartRepo(t *testing.T) { - c := stubContext() - s := httpHarness(c, "POST /repositories", addChartRepoHandlerFunc) - defer s.Close() - - URL := getTestURL(t, s.URL, "", "") - body := getTestRepo(t, URL) - res, err := http.Post(URL, "application/json", body) - if err != nil { - t.Fatalf("Failed POST: %s", err) - } - - if res.StatusCode != http.StatusCreated { - t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) - } -} - -func TestRemoveChartRepo(t *testing.T) { - c := stubContext() - s := httpHarness(c, "DELETE /repositories/*", removeChartRepoHandlerFunc) - defer s.Close() - - URL := getTestURL(t, s.URL, TestRepoBucket, "") - req, err := http.NewRequest("DELETE", URL, nil) - if err != nil { - t.Fatalf("Cannot create DELETE request: %s", err) - } - - res, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("Failed DELETE: %s", err) - } - - defer res.Body.Close() - if res.StatusCode != http.StatusOK { - t.Errorf("Expected status %d, got %d", http.StatusOK, res.StatusCode) - } -} - -func getTestRepo(t *testing.T, URL string) io.Reader { - tr, err := repo.NewRepo(URL, TestRepoCredentialName, TestRepoBucket, TestRepoFormat, TestRepoType) - if err != nil { - t.Fatalf("Cannot create test repository: %s", err) - } - - trb, err := json.Marshal(&tr) - if err != nil { - t.Fatalf("Cannot marshal test repository: %s", err) - } - - return bytes.NewReader(trb) -} - -func getTestURL(t *testing.T, baseURL, repoURL, chartURL string) string { - URL := fmt.Sprintf("%s/repositories", baseURL) - if repoURL != "" { - URL = fmt.Sprintf("%s/%s", URL, repoURL) - } - - if chartURL != "" { - URL = fmt.Sprintf("%s/%s", URL, chartURL) - } - - u, err := url.Parse(URL) - if err != nil { - t.Fatalf("cannot parse test URL %s: %s", URL, err) - } - - return u.String() -} diff --git a/cmd/manager/deployments.go b/cmd/manager/deployments.go deleted file mode 100644 index 7948d17f3..000000000 --- a/cmd/manager/deployments.go +++ /dev/null @@ -1,425 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "fmt" - "log" - "net/http" - "strings" - - "github.com/kubernetes/helm/cmd/manager/manager" - "github.com/kubernetes/helm/cmd/manager/repository" - "github.com/kubernetes/helm/cmd/manager/repository/persistent" - "github.com/kubernetes/helm/cmd/manager/repository/transient" - "github.com/kubernetes/helm/cmd/manager/router" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/httputil" - "github.com/kubernetes/helm/pkg/repo" - "github.com/kubernetes/helm/pkg/util" -) - -func registerDeploymentRoutes(c *router.Context, h *router.Handler) { - h.Add("GET /healthz", healthz) - h.Add("GET /deployments", listDeploymentsHandlerFunc) - h.Add("GET /deployments/*", getDeploymentHandlerFunc) - h.Add("POST /deployments", createDeploymentHandlerFunc) - h.Add("DELETE /deployments/*", deleteDeploymentHandlerFunc) - h.Add("PUT /deployments/*", putDeploymentHandlerFunc) - h.Add("GET /deployments/*/manifests", listManifestsHandlerFunc) - h.Add("GET /deployments/*/manifests/*", getManifestHandlerFunc) - h.Add("POST /expand", expandHandlerFunc) - h.Add("GET /charts", listChartsHandlerFunc) - h.Add("GET /charts/*/instances", listChartInstancesHandlerFunc) - h.Add("GET /charts/*/repository", getRepoForChartHandlerFunc) - h.Add("GET /charts/*/metadata", getMetadataForChartHandlerFunc) - h.Add("GET /charts/*", getChartHandlerFunc) - h.Add("POST /credentials/*", createCredentialHandlerFunc) - h.Add("GET /credentials/*", getCredentialHandlerFunc) -} - -func healthz(w http.ResponseWriter, r *http.Request, c *router.Context) error { - log.Println("manager: healthz checkpoint") - // TODO: This should check the availability of the repository, and fail if it - // cannot connect. - fmt.Fprintln(w, "OK") - return nil -} - -func setupDependencies(c *router.Context) error { - var credentialProvider repo.ICredentialProvider - if c.Config.CredentialFile != "" { - if c.Config.CredentialSecrets { - return errors.New("Both credentialFile and credentialSecrets are set") - } - var err error - credentialProvider, err = repo.NewFilebasedCredentialProvider(c.Config.CredentialFile) - if err != nil { - return fmt.Errorf("cannot create credential provider %s: %s", c.Config.CredentialFile, err) - } - } else if *credentialSecrets { - credentialProvider = repo.NewSecretsCredentialProvider() - } else { - credentialProvider = repo.NewInmemCredentialProvider() - } - c.CredentialProvider = credentialProvider - c.Manager = newManager(c) - - return nil -} - -func newManager(c *router.Context) manager.Manager { - cfg := c.Config - service := repo.NewInmemRepoService() - cp := c.CredentialProvider - rp := repo.NewRepoProvider(service, repo.NewGCSRepoProvider(cp), cp) - expander := manager.NewExpander(cfg.ExpanderPort, cfg.ExpanderURL, rp) - deployer := manager.NewDeployer(util.GetServiceURLOrDie(cfg.DeployerName, cfg.DeployerPort, cfg.DeployerURL)) - address := strings.TrimPrefix(util.GetServiceURLOrDie(cfg.MongoName, cfg.MongoPort, cfg.MongoAddress), "http://") - repository := createRepository(address) - return manager.NewManager(expander, deployer, repository, rp, service, c.CredentialProvider) -} - -func createRepository(address string) repository.Repository { - r, err := persistent.NewRepository(address) - if err != nil { - r = transient.NewRepository() - } - - return r -} - -func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list deployments" - util.LogHandlerEntry(handler, r) - l, err := c.Manager.ListDeployments() - if err != nil { - util.LogAndReturnError(handler, http.StatusInternalServerError, err, w) - return nil - } - var names []string - for _, d := range l { - names = append(names, d.Name) - } - - util.LogHandlerExitWithJSON(handler, w, names, http.StatusOK) - return nil -} - -func getDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get deployment" - util.LogHandlerEntry(handler, r) - name, err := pos(w, r, 2) - if err != nil { - return nil - } - - d, err := c.Manager.GetDeployment(name) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, d, http.StatusOK) - return nil -} - -func createDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: create deployment" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - depReq := getDeploymentRequest(w, r, handler) - if depReq != nil { - d, err := c.Manager.CreateDeployment(depReq) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, d, http.StatusCreated) - } - - return nil -} - -func deleteDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: delete deployment" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - name, err := pos(w, r, 2) - if err != nil { - return err - } - - d, err := c.Manager.DeleteDeployment(name, true) - if err != nil { - return err - } - - util.LogHandlerExitWithJSON(handler, w, d, http.StatusOK) - return nil -} - -func putDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: update deployment" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - name, err := pos(w, r, 2) - if err != nil { - return err - } - - depReq := getDeploymentRequest(w, r, handler) - if depReq != nil { - d, err := c.Manager.PutDeployment(name, depReq) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, d, http.StatusCreated) - } - - return nil -} - -// pos gets a path item by position. -// -// For example. the path "/foo/bar" has three positions: "" at 0, "foo" at -// 1, and "bar" at 2. -// -// For verb/path combos, position 0 will be the verb: "GET /foo/bar" will have -// "GET " at position 0. -func pos(w http.ResponseWriter, r *http.Request, i int) (string, error) { - parts := strings.Split(r.URL.Path, "/") - if len(parts) < i-1 { - return "", fmt.Errorf("No index for %d", i) - } - return parts[i], nil -} - -func getDeploymentRequest(w http.ResponseWriter, r *http.Request, handler string) *common.DeploymentRequest { - util.LogHandlerEntry(handler, r) - depReq := &common.DeploymentRequest{} - if err := httputil.Decode(w, r, depReq); err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - return depReq -} - -func listManifestsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list manifests" - util.LogHandlerEntry(handler, r) - deploymentName, err := pos(w, r, 2) - if err != nil { - return err - } - - m, err := c.Manager.ListManifests(deploymentName) - if err != nil { - return err - } - - var manifestNames []string - for _, manifest := range m { - manifestNames = append(manifestNames, manifest.Name) - } - - util.LogHandlerExitWithJSON(handler, w, manifestNames, http.StatusOK) - return nil -} - -func getManifestHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get manifest" - util.LogHandlerEntry(handler, r) - deploymentName, err := pos(w, r, 2) - if err != nil { - return err - } - - manifestName, err := pos(w, r, 4) - if err != nil { - return err - } - - m, err := c.Manager.GetManifest(deploymentName, manifestName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, m, http.StatusOK) - return nil -} - -func expandHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: expand config" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - depReq := getDeploymentRequest(w, r, handler) - if depReq != nil { - c, err := c.Manager.Expand(depReq) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, c, http.StatusCreated) - } - - return nil -} - -// Putting Type handlers here for now because deployments.go -// currently owns its own Manager backend and doesn't like to share. -func listChartsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list charts" - util.LogHandlerEntry(handler, r) - types, err := c.Manager.ListCharts() - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, types, http.StatusOK) - return nil -} - -func listChartInstancesHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: list chart instances" - util.LogHandlerEntry(handler, r) - chartName, err := pos(w, r, 2) - if err != nil { - return err - } - - instances, err := c.Manager.ListChartInstances(chartName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, instances, http.StatusOK) - return nil -} - -func getRepoForChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get repository for chart" - util.LogHandlerEntry(handler, r) - chartName, err := pos(w, r, 2) - if err != nil { - return err - } - - repository, err := c.Manager.GetRepoForChart(chartName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, repository, http.StatusOK) - return nil -} - -func getMetadataForChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get chart metadata" - util.LogHandlerEntry(handler, r) - chartName, err := pos(w, r, 2) - if err != nil { - return err - } - - metadata, err := c.Manager.GetMetadataForChart(chartName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, metadata, http.StatusOK) - return nil -} - -func getChartHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get chart" - util.LogHandlerEntry(handler, r) - chartName, err := pos(w, r, 2) - if err != nil { - return err - } - - ch, err := c.Manager.GetChart(chartName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, ch, http.StatusOK) - return nil -} - -func getCredential(w http.ResponseWriter, r *http.Request, handler string) *repo.Credential { - util.LogHandlerEntry(handler, r) - t := &repo.Credential{} - if err := httputil.Decode(w, r, t); err != nil { - httputil.BadRequest(w, r, err) - return nil - } - return t -} - -func createCredentialHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: create credential" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - credentialName, err := pos(w, r, 2) - if err != nil { - return err - } - - cr := getCredential(w, r, handler) - if cr != nil { - err = c.Manager.CreateCredential(credentialName, cr) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - } - - util.LogHandlerExitWithJSON(handler, w, c, http.StatusOK) - return nil -} - -func getCredentialHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error { - handler := "manager: get credential" - util.LogHandlerEntry(handler, r) - credentialName, err := pos(w, r, 2) - if err != nil { - return err - } - - cr, err := c.Manager.GetCredential(credentialName) - if err != nil { - httputil.BadRequest(w, r, err) - return nil - } - - util.LogHandlerExitWithJSON(handler, w, cr, http.StatusOK) - return nil -} diff --git a/cmd/manager/deployments_test.go b/cmd/manager/deployments_test.go deleted file mode 100644 index b3b9023c4..000000000 --- a/cmd/manager/deployments_test.go +++ /dev/null @@ -1,215 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "bytes" - "encoding/json" - "io/ioutil" - "net/http" - "strings" - "testing" - - "github.com/kubernetes/helm/pkg/common" -) - -func TestHealthz(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /", healthz) - defer s.Close() - - res, err := http.Get(s.URL) - if err != nil { - t.Fatalf("err on http get: %v", err) - } - body, err := ioutil.ReadAll(res.Body) - defer res.Body.Close() - - if err != nil { - t.Fatalf("Failed to GET healthz: %s", err) - } else if res.StatusCode != 200 { - t.Fatalf("Unexpected status: %d", res.StatusCode) - } - - expectedBody := "OK" - if bytes.Equal(body, []byte(expectedBody)) { - t.Fatalf("Expected response body: %s, Actual response body: %s", - expectedBody, string(body)) - } - - expectedContentType := "text/plain" - contentType := res.Header["Content-Type"][0] - if !strings.Contains(contentType, expectedContentType) { - t.Fatalf("Expected Content-Type to include %s", expectedContentType) - } -} - -func TestCreateDeployments(t *testing.T) { - c := stubContext() - depReq := &common.DeploymentRequest{Name: "foo"} - s := httpHarness(c, "POST /deployments", createDeploymentHandlerFunc) - defer s.Close() - - var b bytes.Buffer - if err := json.NewEncoder(&b).Encode(depReq); err != nil { - t.Fatal(err) - } - - res, err := http.Post(s.URL+"/deployments", "application/json", &b) - if err != nil { - t.Errorf("Failed POST: %s", err) - } else if res.StatusCode != http.StatusCreated { - t.Errorf("Expected status %d, got %d", http.StatusCreated, res.StatusCode) - } -} - -func TestListDeployments(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /deployments", listDeploymentsHandlerFunc) - defer s.Close() - - man := c.Manager.(*mockManager) - man.deployments = []*common.Deployment{ - {Name: "one", State: &common.DeploymentState{Status: common.CreatedStatus}}, - {Name: "two", State: &common.DeploymentState{Status: common.DeployedStatus}}, - } - - res, err := http.Get(s.URL + "/deployments") - if err != nil { - t.Errorf("Failed GET: %s", err) - } else if res.StatusCode != http.StatusOK { - t.Errorf("Unexpected status code: %d", res.StatusCode) - } - - var out []string - if err := json.NewDecoder(res.Body).Decode(&out); err != nil { - t.Errorf("Failed to parse results: %s", err) - return - } - if len(out) != 2 { - t.Errorf("Expected 2 names, got %d", len(out)) - } -} - -func TestGetDeployments(t *testing.T) { - c := stubContext() - s := httpHarness(c, "GET /deployments/*", getDeploymentHandlerFunc) - defer s.Close() - - man := c.Manager.(*mockManager) - man.deployments = []*common.Deployment{ - {Name: "portunes", State: &common.DeploymentState{Status: common.CreatedStatus}}, - } - - res, err := http.Get(s.URL + "/deployments/portunes") - if err != nil { - t.Errorf("Failed GET: %s", err) - } else if res.StatusCode != http.StatusOK { - t.Errorf("Unexpected status code: %d", res.StatusCode) - } - - var out common.Deployment - if err := json.NewDecoder(res.Body).Decode(&out); err != nil { - t.Errorf("Failed to parse results: %s", err) - return - } - - if out.Name != "portunes" { - t.Errorf("Unexpected name %q", out.Name) - } - - if out.State.Status != common.CreatedStatus { - t.Errorf("Unexpected status %v", out.State.Status) - } -} - -func TestDeleteDeployments(t *testing.T) { - c := stubContext() - s := httpHarness(c, "DELETE /deployments/*", deleteDeploymentHandlerFunc) - defer s.Close() - - man := c.Manager.(*mockManager) - man.deployments = []*common.Deployment{ - {Name: "portunes", State: &common.DeploymentState{Status: common.CreatedStatus}}, - } - - req, err := http.NewRequest("DELETE", s.URL+"/deployments/portunes", nil) - if err != nil { - t.Fatal("Failed to create delete request") - } - - res, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("Failed to execute delete request: %s", err) - } - - if res.StatusCode != 200 { - t.Errorf("Expected status code 200, got %d", res.StatusCode) - } - - var out common.Deployment - if err := json.NewDecoder(res.Body).Decode(&out); err != nil { - t.Errorf("Failed to parse results: %s", err) - return - } - - if out.Name != "portunes" { - t.Errorf("Unexpected name %q", out.Name) - } -} - -func TestPutDeployment(t *testing.T) { - c := stubContext() - s := httpHarness(c, "PUT /deployments/*", putDeploymentHandlerFunc) - defer s.Close() - - man := c.Manager.(*mockManager) - man.deployments = []*common.Deployment{ - {Name: "demeter", State: &common.DeploymentState{Status: common.CreatedStatus}}, - } - - depreq := &common.DeploymentRequest{Name: "demeter"} - depreq.Configuration = common.Configuration{Resources: []*common.Resource{}} - out, err := json.Marshal(depreq) - if err != nil { - t.Fatalf("Failed to marshal DeploymentRequest: %s", err) - } - - req, err := http.NewRequest("PUT", s.URL+"/deployments/demeter", bytes.NewBuffer(out)) - if err != nil { - t.Fatal("Failed to create PUT request") - } - - res, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("Failed to execute PUT request: %s", err) - } - - if res.StatusCode != 201 { - t.Errorf("Expected status code 201, got %d", res.StatusCode) - } - - d := &common.Deployment{} - if err := json.NewDecoder(res.Body).Decode(&d); err != nil { - t.Errorf("Failed to parse results: %s", err) - return - } - - if d.Name != "demeter" { - t.Errorf("Unexpected name %q", d.Name) - } -} diff --git a/cmd/manager/main.go b/cmd/manager/main.go deleted file mode 100644 index c6747d05d..000000000 --- a/cmd/manager/main.go +++ /dev/null @@ -1,88 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "flag" - "fmt" - "log" - "net/http" - "os" - - "github.com/kubernetes/helm/cmd/manager/router" - "github.com/kubernetes/helm/pkg/httputil" - "github.com/kubernetes/helm/pkg/version" -) - -var ( - port = flag.Int("port", 8080, "The port to listen on") - maxLength = flag.Int64("maxLength", 1024, "The maximum length (KB) of a template.") - expanderPort = flag.String("expanderPort", "8081", "The IP port of the default expander service.") - expanderURL = flag.String("expanderURL", "", "The URL for the default expander service.") - deployerName = flag.String("deployer", "resourcifier-service", "The DNS name of the deployer service.") - deployerPort = flag.String("deployerPort", "8082", "The IP port of the deployer service.") - deployerURL = flag.String("deployerURL", "", "The URL for the deployer service.") - credentialFile = flag.String("credentialFile", "", "Local file to use for credentials.") - credentialSecrets = flag.Bool("credentialSecrets", true, "Use secrets for credentials.") - mongoName = flag.String("mongoName", "mongodb", "The DNS name of the mongodb service.") - mongoPort = flag.String("mongoPort", "27017", "The port of the mongodb service.") - mongoAddress = flag.String("mongoAddress", "mongodb:27017", "The address of the mongodb service.") -) - -func main() { - // Set up dependencies - c := &router.Context{ - Config: parseFlags(), - } - - if err := setupDependencies(c); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } - - httputil.DefaultEncoder.MaxReadLen = c.Config.MaxTemplateLength - - // Set up routes - handler := router.NewHandler(c) - registerDeploymentRoutes(c, handler) - registerChartRepoRoutes(c, handler) - - // Now create a server. - log.Printf("Starting Manager %s on %s", version.Version, c.Config.Address) - if err := http.ListenAndServe(c.Config.Address, handler); err != nil { - log.Printf("Server exited with error %s", err) - os.Exit(1) - } -} - -func parseFlags() *router.Config { - flag.Parse() - return &router.Config{ - Address: fmt.Sprintf(":%d", *port), - MaxTemplateLength: *maxLength, - ExpanderPort: *expanderPort, - ExpanderURL: *expanderURL, - DeployerName: *deployerName, - DeployerPort: *deployerPort, - DeployerURL: *deployerURL, - CredentialFile: *credentialFile, - CredentialSecrets: *credentialSecrets, - MongoName: *mongoName, - MongoPort: *mongoPort, - MongoAddress: *mongoAddress, - } -} diff --git a/cmd/manager/manager/deployer.go b/cmd/manager/manager/deployer.go deleted file mode 100644 index 52179886b..000000000 --- a/cmd/manager/manager/deployer.go +++ /dev/null @@ -1,187 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "bytes" - "fmt" - "io" - "io/ioutil" - "log" - "net/http" - "net/url" - "strings" - "time" - - "github.com/ghodss/yaml" - "github.com/kubernetes/helm/pkg/common" -) - -// Deployer abstracts interactions with the expander and deployer services. -type Deployer interface { - GetConfiguration(cached *common.Configuration) (*common.Configuration, error) - CreateConfiguration(configuration *common.Configuration) (*common.Configuration, error) - DeleteConfiguration(configuration *common.Configuration) (*common.Configuration, error) - PutConfiguration(configuration *common.Configuration) (*common.Configuration, error) -} - -// NewDeployer returns a new initialized Deployer. -// TODO(vaikas): Add a flag for setting the timeout. -func NewDeployer(url string) Deployer { - return &deployer{url, 15} -} - -type deployer struct { - deployerURL string - timeout int -} - -func (d *deployer) getBaseURL() string { - return fmt.Sprintf("%s/configurations", d.deployerURL) -} - -type formatter func(err error) error - -// GetConfiguration reads and returns the actual configuration -// of the resources described by a cached configuration. -func (d *deployer) GetConfiguration(cached *common.Configuration) (*common.Configuration, error) { - errors := &Error{} - actual := &common.Configuration{} - for _, resource := range cached.Resources { - rtype := url.QueryEscape(resource.Type) - rname := url.QueryEscape(resource.Name) - url := fmt.Sprintf("%s/%s/%s", d.getBaseURL(), rtype, rname) - body, err := d.callService("GET", url, nil, func(e error) error { - return fmt.Errorf("cannot get configuration for resource (%s)", e) - }) - if err != nil { - log.Println(errors.appendError(err)) - continue - } - - if len(body) != 0 { - result := &common.Resource{Name: resource.Name, Type: resource.Type} - if err := yaml.Unmarshal(body, &result.Properties); err != nil { - return nil, fmt.Errorf("cannot get configuration for resource (%v)", err) - } - - actual.Resources = append(actual.Resources, result) - } - } - - if len(errors.errors) > 0 { - return nil, errors - } - - return actual, nil -} - -// CreateConfiguration deploys the set of resources described by a configuration and returns -// the Configuration with status for each resource filled in. -func (d *deployer) CreateConfiguration(configuration *common.Configuration) (*common.Configuration, error) { - return d.callServiceWithConfiguration("POST", "create", configuration) -} - -// DeleteConfiguration deletes the set of resources described by a configuration. -func (d *deployer) DeleteConfiguration(configuration *common.Configuration) (*common.Configuration, error) { - return d.callServiceWithConfiguration("DELETE", "delete", configuration) -} - -// PutConfiguration replaces the set of resources described by a configuration and returns -// the Configuration with status for each resource filled in. -func (d *deployer) PutConfiguration(configuration *common.Configuration) (*common.Configuration, error) { - return d.callServiceWithConfiguration("PUT", "replace", configuration) -} - -func (d *deployer) callServiceWithConfiguration(method, operation string, configuration *common.Configuration) (*common.Configuration, error) { - callback := func(e error) error { - return fmt.Errorf("cannot %s configuration: %s", operation, e) - } - - y, err := yaml.Marshal(configuration) - if err != nil { - return nil, callback(err) - } - - reader := ioutil.NopCloser(bytes.NewReader(y)) - resp, err := d.callService(method, d.getBaseURL(), reader, callback) - - if err != nil { - return nil, err - } - - result := &common.Configuration{} - if len(resp) != 0 { - if err := yaml.Unmarshal(resp, &result); err != nil { - return nil, fmt.Errorf("cannot unmarshal response: (%v)", err) - } - } - return result, nil -} - -func (d *deployer) callService(method, url string, reader io.Reader, callback formatter) ([]byte, error) { - request, err := http.NewRequest(method, url, reader) - if err != nil { - return nil, callback(err) - } - - if method != "GET" { - request.Header.Add("Content-Type", "application/json") - } - - timeout := time.Duration(time.Duration(d.timeout) * time.Second) - client := http.Client{Timeout: timeout} - response, err := client.Do(request) - if err != nil { - return nil, callback(err) - } - - defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) - if err != nil { - return nil, callback(err) - } - - if response.StatusCode < http.StatusOK || - response.StatusCode >= http.StatusMultipleChoices { - err := fmt.Errorf("resourcifier response:\n%s", body) - return nil, callback(err) - } - - return body, nil -} - -// Error is an error type that captures errors from the multiple calls to kubectl -// made for a single configuration. -type Error struct { - errors []error -} - -// Error returns the string value of an Error. -func (e *Error) Error() string { - errs := []string{} - for _, err := range e.errors { - errs = append(errs, err.Error()) - } - - return strings.Join(errs, "\n") -} - -func (e *Error) appendError(err error) error { - e.errors = append(e.errors, err) - return err -} diff --git a/cmd/manager/manager/deployer_test.go b/cmd/manager/manager/deployer_test.go deleted file mode 100644 index bab7fb463..000000000 --- a/cmd/manager/manager/deployer_test.go +++ /dev/null @@ -1,304 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "path" - "reflect" - "strings" - "testing" - - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/util" - - "github.com/ghodss/yaml" -) - -var validConfigurationTestCaseData = []byte(` -resources: - - name: test-controller-v1 - type: ReplicationController - properties: - kind: ReplicationController - apiVersion: v1 - metadata: - name: test-controller-v1 - namespace: dm - labels: - k8s-app: test - version: v1 - spec: - replicas: 1 - selector: - k8s-app: test - version: v1 - template: - metadata: - labels: - k8s-app: test - version: v1 - spec: - containers: - - name: test - image: deployer/test:latest - ports: - - name: test - containerPort: 8080 - protocol: TCP - - name: test - type: Service - properties: - apiVersion: v1 - kind: Service - metadata: - name: test - namespace: dm - labels: - k8s-app: test - version: v1 - spec: - type: LoadBalancer - selector: - k8s-app: test - version: v1 - ports: - - name: test - port: 8080 - targetPort: test - protocol: TCP -`) - -type DeployerTestCases struct { - TestCases []DeployerTestCase -} - -type DeployerTestCase struct { - Description string - Error string - Handler func(w http.ResponseWriter, r *http.Request) -} - -func TestGetConfiguration(t *testing.T) { - valid := getValidConfiguration(t) - tests := []DeployerTestCase{ - { - "expect success for GetConfiguration", - "", - func(w http.ResponseWriter, r *http.Request) { - // Get name from path, find in valid, and return its properties. - rtype := path.Base(path.Dir(r.URL.Path)) - rname := path.Base(r.URL.Path) - for _, resource := range valid.Resources { - if resource.Type == rtype && resource.Name == rname { - util.LogHandlerExitWithYAML("resourcifier: get configuration", w, resource.Properties, http.StatusOK) - return - } - } - - status := fmt.Sprintf("resource %s of type %s not found", rname, rtype) - http.Error(w, status, http.StatusInternalServerError) - }, - }, - { - "expect error for GetConfiguration", - "cannot get configuration", - deployerErrorHandler, - }, - } - - for _, dtc := range tests { - ts := httptest.NewServer(http.HandlerFunc(dtc.Handler)) - defer ts.Close() - - deployer := NewDeployer(ts.URL) - result, err := deployer.GetConfiguration(valid) - if err != nil { - message := err.Error() - if !strings.Contains(message, dtc.Error) { - t.Errorf("error in test case:%s:%s\n", dtc.Description, message) - } - } else { - if dtc.Error != "" { - t.Errorf("expected error:%s\ndid not occur in test case:%s\n", - dtc.Error, dtc.Description) - } - - if !reflect.DeepEqual(valid, result) { - t.Errorf("error in test case:%s:\nwant:%s\nhave:%s\n", - dtc.Description, util.ToYAMLOrError(valid), util.ToYAMLOrError(result)) - } - } - } -} - -func TestCreateConfiguration(t *testing.T) { - valid := getValidConfiguration(t) - tests := []DeployerTestCase{ - { - "expect success for CreateConfiguration", - "", - deployerSuccessHandler, - }, - { - "expect error for CreateConfiguration", - "cannot create configuration", - deployerErrorHandler, - }, - } - - for _, dtc := range tests { - ts := httptest.NewServer(http.HandlerFunc(dtc.Handler)) - defer ts.Close() - - deployer := NewDeployer(ts.URL) - _, err := deployer.CreateConfiguration(valid) - if err != nil { - message := err.Error() - if !strings.Contains(message, dtc.Error) { - t.Errorf("error in test case:%s:%s\n", dtc.Description, message) - } - } else { - if dtc.Error != "" { - t.Errorf("expected error:%s\ndid not occur in test case:%s\n", - dtc.Error, dtc.Description) - } - } - } -} - -func TestDeleteConfiguration(t *testing.T) { - valid := getValidConfiguration(t) - tests := []DeployerTestCase{ - { - "expect success for DeleteConfiguration", - "", - deployerSuccessHandler, - }, - { - "expect error for DeleteConfiguration", - "cannot delete configuration", - deployerErrorHandler, - }, - } - - for _, dtc := range tests { - ts := httptest.NewServer(http.HandlerFunc(dtc.Handler)) - defer ts.Close() - - deployer := NewDeployer(ts.URL) - _, err := deployer.DeleteConfiguration(valid) - if err != nil { - message := err.Error() - if !strings.Contains(message, dtc.Error) { - t.Errorf("error in test case:%s:%s\n", dtc.Description, message) - } - } else { - if dtc.Error != "" { - t.Errorf("expected error:%s\ndid not occur in test case:%s\n", - dtc.Error, dtc.Description) - } - } - } -} - -func TestPutConfiguration(t *testing.T) { - valid := getValidConfiguration(t) - tests := []DeployerTestCase{ - { - "expect success for PutConfiguration", - "", - deployerSuccessHandler, - }, - { - "expect error for PutConfiguration", - "cannot replace configuration", - deployerErrorHandler, - }, - } - - for _, dtc := range tests { - ts := httptest.NewServer(http.HandlerFunc(dtc.Handler)) - defer ts.Close() - - deployer := NewDeployer(ts.URL) - _, err := deployer.PutConfiguration(valid) - if err != nil { - message := err.Error() - if !strings.Contains(message, dtc.Error) { - t.Errorf("error in test case:%s:%s\n", dtc.Description, message) - } - } else { - if dtc.Error != "" { - t.Errorf("expected error:%s\ndid not occur in test case:%s\n", - dtc.Error, dtc.Description) - } - } - } -} - -func getValidConfiguration(t *testing.T) *common.Configuration { - valid := &common.Configuration{} - err := yaml.Unmarshal(validConfigurationTestCaseData, valid) - if err != nil { - t.Errorf("cannot unmarshal test case data:%s\n", err) - } - - return valid -} - -func deployerErrorHandler(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - http.Error(w, "something failed", http.StatusInternalServerError) -} - -func deployerSuccessHandler(w http.ResponseWriter, r *http.Request) { - valid := &common.Configuration{} - err := yaml.Unmarshal(validConfigurationTestCaseData, valid) - if err != nil { - status := fmt.Sprintf("cannot unmarshal test case data:%s", err) - http.Error(w, status, http.StatusInternalServerError) - return - } - - defer r.Body.Close() - body, err := ioutil.ReadAll(r.Body) - if err != nil { - status := fmt.Sprintf("cannot read request body:%s", err) - http.Error(w, status, http.StatusInternalServerError) - return - } - - result := &common.Configuration{} - if err := yaml.Unmarshal(body, result); err != nil { - status := fmt.Sprintf("cannot unmarshal request body:%s", err) - http.Error(w, status, http.StatusInternalServerError) - return - } - - if !reflect.DeepEqual(valid, result) { - status := fmt.Sprintf("error in http handler:\nwant:%s\nhave:%s\n", - util.ToYAMLOrError(valid), util.ToYAMLOrError(result)) - http.Error(w, status, http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusOK) -} diff --git a/cmd/manager/manager/expander.go b/cmd/manager/manager/expander.go deleted file mode 100644 index 4f4c081b4..000000000 --- a/cmd/manager/manager/expander.go +++ /dev/null @@ -1,225 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/expansion" - "github.com/kubernetes/helm/pkg/repo" - "github.com/kubernetes/helm/pkg/util" - - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "net/url" - "strings" -) - -// ExpandedConfiguration is the structure returned by the expansion service. -type ExpandedConfiguration struct { - Config *common.Configuration `json:"config"` - Layout *common.Layout `json:"layout"` -} - -// Expander abstracts interactions with the expander and deployer services. -type Expander interface { - ExpandConfiguration(conf *common.Configuration) (*ExpandedConfiguration, error) -} - -// NewExpander returns a new initialized Expander. -func NewExpander(port, URL string, rp repo.IRepoProvider) Expander { - if rp == nil { - rp = repo.NewRepoProvider(nil, nil, nil) - } - - return &expander{expanderPort: port, expanderURL: URL, repoProvider: rp} -} - -type expander struct { - repoProvider repo.IRepoProvider - expanderPort string - expanderURL string -} - -// ExpandConfiguration expands the supplied configuration and returns -// an expanded configuration. -func (e *expander) ExpandConfiguration(conf *common.Configuration) (*ExpandedConfiguration, error) { - expConf, err := e.expandConfiguration(conf) - if err != nil { - return nil, err - } - - return expConf, nil -} - -func (e *expander) expandConfiguration(conf *common.Configuration) (*ExpandedConfiguration, error) { - resources := []*common.Resource{} - layouts := []*common.LayoutResource{} - - // Iterate over all of the resources in the unexpanded configuration - for _, resource := range conf.Resources { - additions := []*common.Resource{resource} - layout := &common.LayoutResource{ - Resource: common.Resource{ - Name: resource.Name, Type: resource.Type, - }, - } - - // If the type is a chart reference - if repo.IsChartReference(resource.Type) { - // Fetch, decompress and unpack - cbr, _, err := e.repoProvider.GetChartByReference(resource.Type) - if err != nil { - return nil, err - } - - defer cbr.Close() - - // Load the charts contents into strings that we can pass to exapnsion - content, err := cbr.LoadContent() - if err != nil { - return nil, err - } - - expander := cbr.Chartfile().Expander - if expander != nil && expander.Name != "" { - // Build a request to the expansion service and call it to do the expansion - svcReq := &expansion.ServiceRequest{ - ChartInvocation: resource, - Chart: content, - } - - svcResp, err := e.callService(expander.Name, svcReq) - if err != nil { - return nil, err - } - - // Call ourselves recursively with the list of resources returned by expansion - expConf, err := e.expandConfiguration(svcResp) - if err != nil { - return nil, err - } - - // Append the reources returned by the recursion to the flat list of resources - additions = expConf.Config.Resources - - // This was not a primitive resource, so add its properties to the layout - // Then add the all of the layout resources returned by the recursion to the layout - layout.Resources = expConf.Layout.Resources - layout.Properties = resource.Properties - } else { - // Raise an error if a non template chart supplies properties - if resource.Properties != nil { - return nil, fmt.Errorf("properties provided for non template chart %s", resource.Type) - } - - additions = []*common.Resource{} - for _, member := range content.Members { - segments := strings.Split(member.Path, "/") - if len(segments) > 1 && segments[0] == "templates" { - if strings.HasSuffix(member.Path, "yaml") || strings.HasSuffix(member.Path, "json") { - resource, err := util.ParseKubernetesObject(member.Content) - if err != nil { - return nil, err - } - - resources = append(resources, resource) - } - } - } - } - } - - resources = append(resources, additions...) - layouts = append(layouts, layout) - } - - // All done with this level, so return the expanded configuration - result := &ExpandedConfiguration{ - Config: &common.Configuration{Resources: resources}, - Layout: &common.Layout{Resources: layouts}, - } - - return result, nil -} - -func (e *expander) callService(svcName string, svcReq *expansion.ServiceRequest) (*common.Configuration, error) { - svcURL, err := e.getServiceURL(svcName) - if err != nil { - return nil, err - } - - j, err := json.Marshal(svcReq) - if err != nil { - return nil, err - } - - reader := ioutil.NopCloser(bytes.NewReader(j)) - request, err := http.NewRequest("POST", svcURL, reader) - if err != nil { - return nil, err - } - - request.Header.Set("Content-Type", "application/json") - request.Header.Set("Accept", "*/*") - - response, err := http.DefaultClient.Do(request) - if err != nil { - e := fmt.Errorf("call failed (%s) with payload:\n%s\n", err, string(j)) - return nil, e - } - - defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) - if err != nil { - e := fmt.Errorf("error reading response: %s", err) - return nil, e - } - - if response.StatusCode != http.StatusOK { - err := fmt.Errorf("expandybird response:\n%s", body) - return nil, err - } - - svcResp := &common.Configuration{} - if err := json.Unmarshal(body, svcResp); err != nil { - e := fmt.Errorf("cannot unmarshal response body (%s):%s", err, body) - return nil, e - } - - return svcResp, nil -} - -func (e *expander) getServiceURL(svcName string) (string, error) { - if !strings.HasPrefix(svcName, "http:") && !strings.HasPrefix(svcName, "https:") { - var err error - svcName, err = util.GetServiceURL(svcName, e.expanderPort, e.expanderURL) - if err != nil { - return "", err - } - } - - u, err := url.Parse(svcName) - if err != nil { - return "", err - } - - u.Path = fmt.Sprintf("%s/expand", u.Path) - return u.String(), nil -} diff --git a/cmd/manager/manager/expander_test.go b/cmd/manager/manager/expander_test.go deleted file mode 100644 index 8ab757581..000000000 --- a/cmd/manager/manager/expander_test.go +++ /dev/null @@ -1,373 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "reflect" - "strings" - "testing" - - "github.com/ghodss/yaml" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/expansion" - "github.com/kubernetes/helm/pkg/repo" - "github.com/kubernetes/helm/pkg/util" -) - -var ( - TestRepoBucket = "kubernetes-charts-testing" - TestRepoURL = "gs://" + TestRepoBucket - TestChartName = "frobnitz" - TestChartVersion = "0.0.1" - TestArchiveName = TestChartName + "-" + TestChartVersion + ".tgz" - TestResourceType = TestRepoURL + "/" + TestArchiveName - TestRepoType = string(repo.GCSRepoType) - TestRepoFormat = string(repo.GCSRepoFormat) - TestRepoCredentialName = "default" - TestRepoName = TestRepoBucket -) - -var validResponseTestCaseData = []byte(` -resources: -- name: test-service - properties: - test-property: test-value - type: Service -- name: test-rc - properties: - test-property: test-value - type: ReplicationController -- name: test3-service - properties: - test-property: test-value - type: Service -- name: test3-rc - properties: - test-property: test-value - type: ReplicationController -- name: test4-service - properties: - test-property: test-value - type: Service -- name: test4-rc - properties: - test-property: test-value - type: ReplicationController -`) - -var validLayoutTestCaseData = []byte(` -resources: -- name: test_invocation - resources: - - name: test-service - type: Service - - name: test-rc - type: ReplicationController - - name: test3-service - type: Service - - name: test3-rc - type: ReplicationController - - name: test4-service - type: Service - - name: test4-rc - type: ReplicationController - type: gs://kubernetes-charts-testing/frobnitz-0.0.1.tgz -`) - -/* -[]byte(` -resources: -- name: test - properties: - test-property: test-value - resources: - - name: test-service - type: Service - - name: test-rc - type: ReplicationController - type: test-type.py -- name: test2 - properties: null - resources: - - name: test3 - properties: - test-property: test-value - resources: - - name: test3-service - type: Service - - name: test3-rc - type: ReplicationController - type: test-type.py - - name: test4 - properties: - test-property: test-value - resources: - - name: test4-service - type: Service - - name: test4-rc - type: ReplicationController - type: test-type.py - type: test2.jinja -`) - -var roundTripContent = ` -resources: -- name: test - type: test.py - properties: - test: test -` - -var roundTripExpanded = ` -resources: -- name: test2 - type: test2.py - properties: - test: test -` - -var roundTripLayout = ` -resources: -- name: test - type: test.py - properties: - test: test - resources: - - name: test2 - type: test2.py - properties: - test: test -` - -var roundTripExpanded2 = ` -resources: -- name: test3 - type: Service - properties: - test: test -` - -var roundTripLayout2 = ` -resources: -- name: test2 - type: test2.py - properties: - test: test - resources: - - name: test3 - type: Service - properties: - test: test -` - -var finalExpanded = ` -config: - resources: - - name: test3 - type: Service - properties: - test: test -layout: - resources: - - name: test - type: test.py - properties: - test: test - resources: - - name: test2 - type: test2.py - properties: - test: test - resources: - - name: test3 - type: Service - properties: - test: test -` - -var roundTripResponse = &ExpandedConfiguration{ - Config: roundTripExpanded, -} - -var roundTripResponse2 = &ExpandedConfiguration{ - Config: roundTripExpanded2, -} - -var roundTripResponses = []*ExpandedConfiguration{ - roundTripResponse, - roundTripResponse2, -} -*/ - -type ExpanderTestCase struct { - Description string - Error string - Handler func(w http.ResponseWriter, r *http.Request) - ValidResponse *ExpandedConfiguration -} - -func TestExpandTemplate(t *testing.T) { - // roundTripResponse := &ExpandedConfiguration{} - // if err := yaml.Unmarshal([]byte(finalExpanded), roundTripResponse); err != nil { - // panic(err) - // } - - tests := []ExpanderTestCase{ - { - "expect success for ExpandConfiguration", - "", - expanderSuccessHandler, - getValidExpandedConfiguration(), - }, - { - "expect error for ExpandConfiguration", - "simulated failure", - expanderErrorHandler, - nil, - }, - } - - for _, etc := range tests { - ts := httptest.NewServer(http.HandlerFunc(etc.Handler)) - defer ts.Close() - - expander := NewExpander("8081", ts.URL, getTestRepoProvider(t)) - resource := &common.Resource{ - Name: "test_invocation", - Type: TestResourceType, - } - - conf := &common.Configuration{ - Resources: []*common.Resource{ - resource, - }, - } - - actualResponse, err := expander.ExpandConfiguration(conf) - if err != nil { - message := err.Error() - if etc.Error == "" { - t.Errorf("unexpected error in test case %s: %s", etc.Description, err) - } - if !strings.Contains(message, etc.Error) { - t.Errorf("error in test case:%s:%s\n", etc.Description, message) - } - } else { - if etc.Error != "" { - t.Errorf("expected error:%s\ndid not occur in test case:%s\n", - etc.Error, etc.Description) - } - - expectedResponse := etc.ValidResponse - if !reflect.DeepEqual(expectedResponse, actualResponse) { - t.Errorf("error in test case:%s:\nwant:%s\nhave:%s\n", - etc.Description, util.ToYAMLOrError(expectedResponse), util.ToYAMLOrError(actualResponse)) - } - } - } -} - -func getValidServiceResponse() *common.Configuration { - conf := &common.Configuration{} - if err := yaml.Unmarshal(validResponseTestCaseData, conf); err != nil { - panic(fmt.Errorf("cannot unmarshal valid response: %s\n", err)) - } - - return conf -} - -func getValidExpandedConfiguration() *ExpandedConfiguration { - conf := getValidServiceResponse() - layout := &common.Layout{} - if err := yaml.Unmarshal(validLayoutTestCaseData, layout); err != nil { - panic(fmt.Errorf("cannot unmarshal valid response: %s\n", err)) - } - - return &ExpandedConfiguration{Config: conf, Layout: layout} - -} - -func expanderErrorHandler(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - http.Error(w, "simulated failure", http.StatusInternalServerError) -} - -/* -func roundTripHandler(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - handler := "expandybird: expand" - util.LogHandlerEntry(handler, r) - if len(roundTripResponses) < 1 { - http.Error(w, "Too many calls to round trip handler", http.StatusInternalServerError) - return - } - - util.LogHandlerExitWithJSON(handler, w, roundTripResponses[0], http.StatusOK) - roundTripResponses = roundTripResponses[1:] -} -*/ - -func expanderSuccessHandler(w http.ResponseWriter, r *http.Request) { - handler := "expandybird: expand" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - body, err := ioutil.ReadAll(r.Body) - if err != nil { - status := fmt.Sprintf("cannot read request body:%s", err) - http.Error(w, status, http.StatusInternalServerError) - return - } - - svcReq := &expansion.ServiceRequest{} - if err := json.Unmarshal(body, svcReq); err != nil { - status := fmt.Sprintf("cannot unmarshal request body:%s\n%s\n", err, body) - http.Error(w, status, http.StatusInternalServerError) - return - } - - /* - if !reflect.DeepEqual(validRequestTestCaseData, *svcReq) { - status := fmt.Sprintf("error in http handler:\nwant:%s\nhave:%s\n", - util.ToJSONOrError(validRequestTestCaseData), util.ToJSONOrError(template)) - http.Error(w, status, http.StatusInternalServerError) - return - } - */ - - svcResp := getValidServiceResponse() - util.LogHandlerExitWithJSON(handler, w, svcResp, http.StatusOK) -} - -func getTestRepoProvider(t *testing.T) repo.IRepoProvider { - rs := repo.NewInmemRepoService() - rp := repo.NewRepoProvider(rs, nil, nil) - tr, err := repo.NewRepo(TestRepoURL, TestRepoCredentialName, TestRepoName, TestRepoFormat, TestRepoType) - if err != nil { - t.Fatalf("cannot create test repository: %s", err) - } - - if err := rs.CreateRepo(tr); err != nil { - t.Fatalf("cannot initialize repository service: %s", err) - } - - return rp -} diff --git a/cmd/manager/manager/manager.go b/cmd/manager/manager/manager.go deleted file mode 100644 index 2b148ac60..000000000 --- a/cmd/manager/manager/manager.go +++ /dev/null @@ -1,447 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "fmt" - "log" - "regexp" - "time" - - "github.com/kubernetes/helm/cmd/manager/repository" - "github.com/kubernetes/helm/pkg/chart" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/repo" -) - -// Manager manages a persistent set of Deployments. -type Manager interface { - // Deployments - ListDeployments() ([]common.Deployment, error) - GetDeployment(name string) (*common.Deployment, error) - CreateDeployment(depReq *common.DeploymentRequest) (*common.Deployment, error) - DeleteDeployment(name string, forget bool) (*common.Deployment, error) - PutDeployment(name string, depReq *common.DeploymentRequest) (*common.Deployment, error) - - // Manifests - ListManifests(deploymentName string) (map[string]*common.Manifest, error) - GetManifest(deploymentName string, manifest string) (*common.Manifest, error) - Expand(t *common.DeploymentRequest) (*common.Manifest, error) - - // Charts - ListCharts() ([]string, error) - ListChartInstances(chartName string) ([]*common.ChartInstance, error) - GetRepoForChart(chartName string) (string, error) - GetMetadataForChart(chartName string) (*chart.Chartfile, error) - GetChart(chartName string) (*chart.Chart, error) - - // Repo Charts - ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) - GetChartForRepo(repoName, chartName string) (*chart.Chart, error) - - // Credentials - CreateCredential(name string, c *repo.Credential) error - GetCredential(name string) (*repo.Credential, error) - - // Chart Repositories - ListRepos() (map[string]string, error) - AddRepo(addition repo.IRepo) error - RemoveRepo(repoName string) error - GetRepo(repoName string) (repo.IRepo, error) -} - -type manager struct { - expander Expander - deployer Deployer - repository repository.Repository - repoProvider repo.IRepoProvider - service repo.IRepoService - //TODO: add chart repo service - credentialProvider repo.ICredentialProvider -} - -// NewManager returns a new initialized Manager. -func NewManager(expander Expander, - deployer Deployer, - repository repository.Repository, - repoProvider repo.IRepoProvider, - service repo.IRepoService, - credentialProvider repo.ICredentialProvider) Manager { - return &manager{expander, deployer, repository, repoProvider, service, credentialProvider} -} - -// ListDeployments returns the list of deployments -func (m *manager) ListDeployments() ([]common.Deployment, error) { - l, err := m.repository.ListDeployments() - if err != nil { - return nil, err - } - return l, nil -} - -// GetDeployment retrieves the configuration stored for a given deployment -// as well as the current configuration from the cluster. -func (m *manager) GetDeployment(name string) (*common.Deployment, error) { - d, err := m.repository.GetDeployment(name) - if err != nil { - return nil, err - } - - return d, nil -} - -// ListManifests retrieves the manifests for a given deployment -// of each of the deployments in the repository and returns the deployments. -func (m *manager) ListManifests(deploymentName string) (map[string]*common.Manifest, error) { - l, err := m.repository.ListManifests(deploymentName) - if err != nil { - return nil, err - } - - return l, nil -} - -// GetManifest retrieves the specified manifest for a given deployment -func (m *manager) GetManifest(deploymentName string, manifestName string) (*common.Manifest, error) { - d, err := m.repository.GetManifest(deploymentName, manifestName) - if err != nil { - return nil, err - } - - return d, nil -} - -// CreateDeployment expands the supplied configuration, creates the resulting -// resources in the cluster, creates a new deployment that tracks it, stores the -// deployment in the repository and returns the deployment. -func (m *manager) CreateDeployment(depReq *common.DeploymentRequest) (*common.Deployment, error) { - log.Printf("Creating deployment: %s", depReq.Name) - _, err := m.repository.CreateDeployment(depReq.Name) - if err != nil { - log.Printf("CreateDeployment failed %v", err) - return nil, err - } - - manifest, err := m.Expand(depReq) - if err != nil { - log.Printf("Manifest creation failed: %v", err) - m.repository.SetDeploymentState(depReq.Name, failState(err)) - return nil, err - } - - if err := m.repository.AddManifest(manifest); err != nil { - log.Printf("AddManifest failed %v", err) - m.repository.SetDeploymentState(depReq.Name, failState(err)) - return nil, err - } - - actualConfig, err := m.deployer.CreateConfiguration(manifest.ExpandedConfig) - if err != nil { - // Deployment failed, mark as failed - log.Printf("CreateConfiguration failed: %v", err) - m.repository.SetDeploymentState(depReq.Name, failState(err)) - - // If we failed before being able to create some of the resources, then - // return the failure as such. Otherwise, we're going to add the manifest - // and hence resource specific errors down below. - if actualConfig == nil { - return nil, err - } - } else { - // May be errors in the resources themselves. - errs := getResourceErrors(actualConfig) - if len(errs) > 0 { - e := fmt.Errorf("Found resource errors during deployment: %v", errs) - m.repository.SetDeploymentState(depReq.Name, failState(e)) - return nil, e - } - - m.repository.SetDeploymentState(depReq.Name, &common.DeploymentState{Status: common.DeployedStatus}) - } - - // Update the manifest with the actual state of the reified resources - manifest.ExpandedConfig = actualConfig - if err := m.repository.SetManifest(manifest); err != nil { - log.Printf("SetManifest failed %v", err) - m.repository.SetDeploymentState(depReq.Name, failState(err)) - return nil, err - } - - // Finally update the type instances for this deployment. - m.setChartInstances(depReq.Name, manifest.Name, manifest.Layout) - return m.repository.GetValidDeployment(depReq.Name) -} - -func (m *manager) setChartInstances(deploymentName string, manifestName string, layout *common.Layout) { - m.repository.ClearChartInstancesForDeployment(deploymentName) - - instances := make(map[string][]*common.ChartInstance) - for i, r := range layout.Resources { - addChartInstances(&instances, r, deploymentName, manifestName, fmt.Sprintf("$.resources[%d]", i)) - } - - m.repository.AddChartInstances(instances) -} - -func addChartInstances(instances *map[string][]*common.ChartInstance, r *common.LayoutResource, deploymentName string, manifestName string, jsonPath string) { - // Add this resource. - inst := &common.ChartInstance{ - Name: r.Name, - Type: r.Type, - Deployment: deploymentName, - Manifest: manifestName, - Path: jsonPath, - } - - (*instances)[r.Type] = append((*instances)[r.Type], inst) - - // Add all sub resources if they exist. - for i, sr := range r.Resources { - addChartInstances(instances, sr, deploymentName, manifestName, fmt.Sprintf("%s.resources[%d]", jsonPath, i)) - } -} - -// DeleteDeployment deletes the configuration for the deployment with -// the supplied identifier from the cluster.repository. If forget is true, then -// the deployment is removed from the repository. Otherwise, it is marked -// as deleted and retained. -func (m *manager) DeleteDeployment(name string, forget bool) (*common.Deployment, error) { - log.Printf("Deleting deployment: %s", name) - d, err := m.repository.GetValidDeployment(name) - if err != nil { - return nil, err - } - - // If there's a latest manifest, delete the underlying resources. - latest, err := m.repository.GetLatestManifest(name) - if err != nil { - m.repository.SetDeploymentState(name, failState(err)) - return nil, err - } - - if latest != nil { - log.Printf("Deleting resources from the latest manifest") - // Clear previous state. - for _, r := range latest.ExpandedConfig.Resources { - r.State = nil - } - - if _, err := m.deployer.DeleteConfiguration(latest.ExpandedConfig); err != nil { - log.Printf("Failed to delete resources from the latest manifest: %v", err) - m.repository.SetDeploymentState(name, failState(err)) - return nil, err - } - - // Create an empty manifest since resources have been deleted. - if !forget { - manifest := &common.Manifest{Deployment: name, Name: generateManifestName()} - if err := m.repository.AddManifest(manifest); err != nil { - log.Printf("Failed to add empty manifest") - return nil, err - } - } - } - - d, err = m.repository.DeleteDeployment(name, forget) - if err != nil { - return nil, err - } - - // Finally remove the type instances for this deployment. - m.repository.ClearChartInstancesForDeployment(name) - return d, nil -} - -// PutDeployment replaces the configuration of the deployment with -// the supplied identifier in the cluster, and returns the deployment. -func (m *manager) PutDeployment(name string, depReq *common.DeploymentRequest) (*common.Deployment, error) { - _, err := m.repository.GetValidDeployment(name) - if err != nil { - return nil, err - } - - manifest, err := m.Expand(depReq) - if err != nil { - log.Printf("Manifest creation failed: %v", err) - m.repository.SetDeploymentState(name, failState(err)) - return nil, err - } - - actualConfig, err := m.deployer.PutConfiguration(manifest.ExpandedConfig) - if err != nil { - m.repository.SetDeploymentState(name, failState(err)) - return nil, err - } - - manifest.ExpandedConfig = actualConfig - err = m.repository.AddManifest(manifest) - if err != nil { - m.repository.SetDeploymentState(name, failState(err)) - return nil, err - } - - // Finally update the type instances for this deployment. - m.setChartInstances(depReq.Name, manifest.Name, manifest.Layout) - return m.repository.GetValidDeployment(depReq.Name) -} - -func (m *manager) Expand(depReq *common.DeploymentRequest) (*common.Manifest, error) { - expConf, err := m.expander.ExpandConfiguration(&depReq.Configuration) - if err != nil { - log.Printf("Expansion failed %v", err) - return nil, err - } - - return &common.Manifest{ - Name: generateManifestName(), - Deployment: depReq.Name, - InputConfig: &depReq.Configuration, - ExpandedConfig: expConf.Config, - Layout: expConf.Layout, - }, nil -} - -func (m *manager) ListCharts() ([]string, error) { - return m.repository.ListCharts() -} - -func (m *manager) ListChartInstances(chartName string) ([]*common.ChartInstance, error) { - return m.repository.GetChartInstances(chartName) -} - -// GetRepoForChart returns the repository where the referenced chart resides. -func (m *manager) GetRepoForChart(reference string) (string, error) { - _, r, err := m.repoProvider.GetChartByReference(reference) - if err != nil { - return "", err - } - - return r.GetURL(), nil -} - -// GetMetadataForChart returns the metadata for the referenced chart. -func (m *manager) GetMetadataForChart(reference string) (*chart.Chartfile, error) { - c, _, err := m.repoProvider.GetChartByReference(reference) - if err != nil { - return nil, err - } - - return c.Chartfile(), nil -} - -// GetChart returns the referenced chart. -func (m *manager) GetChart(reference string) (*chart.Chart, error) { - c, _, err := m.repoProvider.GetChartByReference(reference) - if err != nil { - return nil, err - } - - return c, nil -} - -// ListRepos returns the list of available repository URLs -func (m *manager) ListRepos() (map[string]string, error) { - return m.service.ListRepos() -} - -// AddRepo adds a repository to the list -func (m *manager) AddRepo(addition repo.IRepo) error { - return m.service.CreateRepo(addition) -} - -// RemoveRepo removes a repository from the list by URL -func (m *manager) RemoveRepo(repoName string) error { - repoURL, err := m.service.GetRepoURLByName(repoName) - if err != nil { - return err - } - - return m.service.DeleteRepo(repoURL) -} - -// GetRepo returns the repository with the given name -func (m *manager) GetRepo(repoName string) (repo.IRepo, error) { - repoURL, err := m.service.GetRepoURLByName(repoName) - if err != nil { - return nil, err - } - - return m.service.GetRepoByURL(repoURL) -} - -func generateManifestName() string { - return fmt.Sprintf("manifest-%d", time.Now().UTC().UnixNano()) -} - -func failState(e error) *common.DeploymentState { - return &common.DeploymentState{ - Status: common.FailedStatus, - Errors: []string{e.Error()}, - } -} - -func getResourceErrors(c *common.Configuration) []string { - var errs []string - for _, r := range c.Resources { - if r.State.Status == common.Failed { - errs = append(errs, r.State.Errors...) - } - } - - return errs -} - -// ListRepoCharts lists charts in a given repository whose names -// conform to the supplied regular expression, or all charts, if the regular -// expression is nil. -func (m *manager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) { - repoURL, err := m.service.GetRepoURLByName(repoName) - if err != nil { - return nil, err - } - - r, err := m.repoProvider.GetRepoByURL(repoURL) - if err != nil { - return nil, err - } - - return r.ListCharts(regex) -} - -// GetChartForRepo returns a chart by name from a given repository. -func (m *manager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) { - repoURL, err := m.service.GetRepoURLByName(repoName) - if err != nil { - return nil, err - } - - r, err := m.repoProvider.GetRepoByURL(repoURL) - if err != nil { - return nil, err - } - - return r.GetChart(chartName) -} - -// CreateCredential creates a credential that can be used to authenticate to repository -func (m *manager) CreateCredential(name string, c *repo.Credential) error { - return m.credentialProvider.SetCredential(name, c) -} - -func (m *manager) GetCredential(name string) (*repo.Credential, error) { - return m.credentialProvider.GetCredential(name) -} diff --git a/cmd/manager/manager/manager_test.go b/cmd/manager/manager/manager_test.go deleted file mode 100644 index 150431630..000000000 --- a/cmd/manager/manager/manager_test.go +++ /dev/null @@ -1,551 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package manager - -import ( - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/repo" - - "errors" - "reflect" - "strings" - "testing" -) - -var layout = common.Layout{ - Resources: []*common.LayoutResource{{Resource: common.Resource{Name: "test", Type: "test"}}}, -} -var configuration = common.Configuration{ - Resources: []*common.Resource{{Name: "test", Type: "test"}}, -} -var resourcesWithSuccessState = common.Configuration{ - Resources: []*common.Resource{{Name: "test", Type: "test", State: &common.ResourceState{Status: common.Created}}}, -} -var resourcesWithFailureState = common.Configuration{ - Resources: []*common.Resource{{ - Name: "test", - Type: "test", - State: &common.ResourceState{ - Status: common.Failed, - Errors: []string{"test induced error"}, - }, - }}, -} -var deploymentRequest = common.DeploymentRequest{Name: "test", Configuration: configuration} - -var expandedConfig = ExpandedConfiguration{ - Config: &configuration, - Layout: &layout, -} - -var deploymentName = "deployment" - -var manifestName = "manifest-2" -var manifest = common.Manifest{Name: manifestName, ExpandedConfig: &configuration, Layout: &layout} -var manifestMap = map[string]*common.Manifest{manifest.Name: &manifest} - -var deployment = common.Deployment{ - Name: "test", -} - -var deploymentList = []common.Deployment{deployment, {Name: "test2"}} - -var typeInstMap = map[string][]string{"test": {"test"}} - -var errTest = errors.New("test error") - -type expanderStub struct{} - -func (expander *expanderStub) ExpandConfiguration(conf *common.Configuration) (*ExpandedConfiguration, error) { - if reflect.DeepEqual(conf, &configuration) { - return &expandedConfig, nil - } - - return nil, errTest -} - -type deployerStub struct { - FailCreate bool - Created []*common.Configuration - FailDelete bool - Deleted []*common.Configuration - FailCreateResource bool -} - -func (deployer *deployerStub) reset() { - deployer.FailCreate = false - deployer.Created = make([]*common.Configuration, 0) - deployer.FailDelete = false - deployer.Deleted = make([]*common.Configuration, 0) - deployer.FailCreateResource = false -} - -func newDeployerStub() *deployerStub { - ret := &deployerStub{} - return ret -} - -func (deployer *deployerStub) GetConfiguration(cached *common.Configuration) (*common.Configuration, error) { - return nil, nil -} - -func (deployer *deployerStub) CreateConfiguration(configuration *common.Configuration) (*common.Configuration, error) { - if deployer.FailCreate { - return nil, errTest - } - if deployer.FailCreateResource { - return &resourcesWithFailureState, errTest - } - - deployer.Created = append(deployer.Created, configuration) - return &resourcesWithSuccessState, nil -} - -func (deployer *deployerStub) DeleteConfiguration(configuration *common.Configuration) (*common.Configuration, error) { - if deployer.FailDelete { - return nil, errTest - } - deployer.Deleted = append(deployer.Deleted, configuration) - return nil, nil -} - -func (deployer *deployerStub) PutConfiguration(configuration *common.Configuration) (*common.Configuration, error) { - return nil, nil -} - -type repositoryStub struct { - FailListDeployments bool - Created []string - ManifestAdd map[string]*common.Manifest - ManifestSet map[string]*common.Manifest - Deleted []string - GetValid []string - ChartInstances map[string][]string - ChartInstancesCleared bool - GetChartInstancesCalled bool - ListTypesCalled bool - DeploymentStates []*common.DeploymentState -} - -func (repository *repositoryStub) reset() { - repository.FailListDeployments = false - repository.Created = make([]string, 0) - repository.ManifestAdd = make(map[string]*common.Manifest) - repository.ManifestSet = make(map[string]*common.Manifest) - repository.Deleted = make([]string, 0) - repository.GetValid = make([]string, 0) - repository.ChartInstances = make(map[string][]string) - repository.ChartInstancesCleared = false - repository.GetChartInstancesCalled = false - repository.ListTypesCalled = false - repository.DeploymentStates = []*common.DeploymentState{} -} - -func newRepositoryStub() *repositoryStub { - ret := &repositoryStub{} - return ret -} - -// Deployments. -func (repository *repositoryStub) ListDeployments() ([]common.Deployment, error) { - if repository.FailListDeployments { - return deploymentList, errTest - } - - return deploymentList, nil -} - -func (repository *repositoryStub) GetDeployment(d string) (*common.Deployment, error) { - if d == deploymentName { - return &deployment, nil - } - - return nil, errTest -} - -func (repository *repositoryStub) GetValidDeployment(d string) (*common.Deployment, error) { - repository.GetValid = append(repository.GetValid, d) - return &deployment, nil -} - -func (repository *repositoryStub) CreateDeployment(d string) (*common.Deployment, error) { - repository.Created = append(repository.Created, d) - return &deployment, nil -} - -func (repository *repositoryStub) DeleteDeployment(d string, forget bool) (*common.Deployment, error) { - repository.Deleted = append(repository.Deleted, d) - return &deployment, nil -} - -func (repository *repositoryStub) SetDeploymentState(name string, state *common.DeploymentState) error { - repository.DeploymentStates = append(repository.DeploymentStates, state) - return nil -} - -// Manifests. -func (repository *repositoryStub) AddManifest(manifest *common.Manifest) error { - repository.ManifestAdd[manifest.Deployment] = manifest - return nil -} - -func (repository *repositoryStub) SetManifest(manifest *common.Manifest) error { - repository.ManifestSet[manifest.Deployment] = manifest - return nil -} - -func (repository *repositoryStub) ListManifests(d string) (map[string]*common.Manifest, error) { - if d == deploymentName { - return manifestMap, nil - } - - return nil, errTest -} - -func (repository *repositoryStub) GetManifest(d string, m string) (*common.Manifest, error) { - if d == deploymentName && m == manifestName { - return &manifest, nil - } - - return nil, errTest -} - -func (repository *repositoryStub) GetLatestManifest(d string) (*common.Manifest, error) { - if d == deploymentName { - return repository.ManifestAdd[d], nil - } - - return nil, errTest -} - -// Types. -func (repository *repositoryStub) ListCharts() ([]string, error) { - repository.ListTypesCalled = true - return []string{}, nil -} - -func (repository *repositoryStub) GetChartInstances(t string) ([]*common.ChartInstance, error) { - repository.GetChartInstancesCalled = true - return []*common.ChartInstance{}, nil -} - -func (repository *repositoryStub) ClearChartInstancesForDeployment(d string) error { - repository.ChartInstancesCleared = true - return nil -} - -func (repository *repositoryStub) AddChartInstances(is map[string][]*common.ChartInstance) error { - for t, instances := range is { - for _, instance := range instances { - d := instance.Deployment - repository.ChartInstances[d] = append(repository.ChartInstances[d], t) - } - } - - return nil -} - -func (repository *repositoryStub) Close() {} - -var testExpander = &expanderStub{} -var testRepository = newRepositoryStub() -var testDeployer = newDeployerStub() -var testRepoService = repo.NewInmemRepoService() -var testCredentialProvider = repo.NewInmemCredentialProvider() -var testProvider = repo.NewRepoProvider(nil, repo.NewGCSRepoProvider(testCredentialProvider), testCredentialProvider) -var testManager = NewManager(testExpander, testDeployer, testRepository, testProvider, testRepoService, testCredentialProvider) - -func TestListDeployments(t *testing.T) { - testRepository.reset() - d, err := testManager.ListDeployments() - if err != nil { - t.Fatalf(err.Error()) - } - - if !reflect.DeepEqual(d, deploymentList) { - t.Fatalf("invalid deployment list") - } -} - -func TestListDeploymentsFail(t *testing.T) { - testRepository.reset() - testRepository.FailListDeployments = true - d, err := testManager.ListDeployments() - if err != errTest { - t.Fatalf(err.Error()) - } - - if d != nil { - t.Fatalf("deployment list is not empty") - } -} - -func TestGetDeployment(t *testing.T) { - testRepository.reset() - d, err := testManager.GetDeployment(deploymentName) - if err != nil { - t.Fatalf(err.Error()) - } - - if !reflect.DeepEqual(d, &deployment) { - t.Fatalf("invalid deployment") - } -} - -func TestListManifests(t *testing.T) { - testRepository.reset() - m, err := testManager.ListManifests(deploymentName) - if err != nil { - t.Fatalf(err.Error()) - } - - if !reflect.DeepEqual(m, manifestMap) { - t.Fatalf("invalid manifest map") - } -} - -func TestGetManifest(t *testing.T) { - testRepository.reset() - m, err := testManager.GetManifest(deploymentName, manifestName) - if err != nil { - t.Fatalf(err.Error()) - } - - if !reflect.DeepEqual(m, &manifest) { - t.Fatalf("invalid manifest") - } -} - -func TestCreateDeployment(t *testing.T) { - testRepository.reset() - testDeployer.reset() - d, err := testManager.CreateDeployment(&deploymentRequest) - if !reflect.DeepEqual(d, &deployment) || err != nil { - t.Fatalf("Expected a different set of response values from invoking CreateDeployment."+ - "Received: %v, %s. Expected: %#v, %s.", d, err, &deployment, "nil") - } - - if testRepository.Created[0] != deploymentRequest.Name { - t.Fatalf("Repository CreateDeployment was called with %s but expected %s.", - testRepository.Created[0], deploymentRequest.Name) - } - - if !strings.HasPrefix(testRepository.ManifestAdd[deploymentRequest.Name].Name, "manifest-") { - t.Fatalf("Repository AddManifest was called with %s but expected manifest name"+ - "to begin with manifest-.", testRepository.ManifestAdd[deploymentRequest.Name].Name) - } - - if !strings.HasPrefix(testRepository.ManifestSet[deploymentRequest.Name].Name, "manifest-") { - t.Fatalf("Repository SetManifest was called with %s but expected manifest name"+ - "to begin with manifest-.", testRepository.ManifestSet[deploymentRequest.Name].Name) - } - - if !reflect.DeepEqual(*testDeployer.Created[0], configuration) || err != nil { - t.Fatalf("Deployer CreateConfiguration was called with %s but expected %s.", - testDeployer.Created[0], configuration) - } - - if testRepository.DeploymentStates[0].Status != common.DeployedStatus { - t.Fatal("CreateDeployment success did not mark deployment as deployed") - } - - if !testRepository.ChartInstancesCleared { - t.Fatal("Repository did not clear type instances during creation") - } - - if !reflect.DeepEqual(testRepository.ChartInstances, typeInstMap) { - t.Fatalf("Unexpected type instances after CreateDeployment: %s", testRepository.ChartInstances) - } -} - -func TestCreateDeploymentCreationFailure(t *testing.T) { - testRepository.reset() - testDeployer.reset() - testDeployer.FailCreate = true - d, err := testManager.CreateDeployment(&deploymentRequest) - - if testRepository.Created[0] != deploymentRequest.Name { - t.Fatalf("Repository CreateDeployment was called with %s but expected %s.", - testRepository.Created[0], deploymentRequest.Name) - } - - if len(testRepository.Deleted) != 0 { - t.Fatalf("DeleteDeployment was called with %s but not expected", - testRepository.Created[0]) - } - - if testRepository.DeploymentStates[0].Status != common.FailedStatus { - t.Fatal("CreateDeployment failure did not mark deployment as failed") - } - - if err != errTest || d != nil { - t.Fatalf("Expected a different set of response values from invoking CreateDeployment."+ - "Received: %v, %s. Expected: %s, %s.", d, err, "nil", errTest) - } - - if testRepository.ChartInstancesCleared { - t.Fatal("Unexpected change to type instances during CreateDeployment failure.") - } -} - -func TestCreateDeploymentCreationResourceFailure(t *testing.T) { - testRepository.reset() - testDeployer.reset() - testDeployer.FailCreateResource = true - d, err := testManager.CreateDeployment(&deploymentRequest) - - if testRepository.Created[0] != deploymentRequest.Name { - t.Fatalf("Repository CreateDeployment was called with %s but expected %s.", - testRepository.Created[0], deploymentRequest.Name) - } - - if len(testRepository.Deleted) != 0 { - t.Fatalf("DeleteDeployment was called with %s but not expected", - testRepository.Created[0]) - } - - if testRepository.DeploymentStates[0].Status != common.FailedStatus { - t.Fatal("CreateDeployment failure did not mark deployment as failed") - } - - if manifest, ok := testRepository.ManifestAdd[deploymentRequest.Name]; ok { - if !strings.HasPrefix(manifest.Name, "manifest-") { - t.Fatalf("Repository AddManifest was called with %s but expected manifest name"+ - "to begin with manifest-.", manifest.Name) - } - } - - if manifest, ok := testRepository.ManifestSet[deploymentRequest.Name]; ok { - if !strings.HasPrefix(manifest.Name, "manifest-") { - t.Fatalf("Repository AddManifest was called with %s but expected manifest name"+ - "to begin with manifest-.", manifest.Name) - } - } - - if err != nil || !reflect.DeepEqual(d, &deployment) { - t.Fatalf("Expected a different set of response values from invoking CreateDeployment.\n"+ - "Received: %v, %v. Expected: %v, %v.", d, err, &deployment, "nil") - } - - if !testRepository.ChartInstancesCleared { - t.Fatal("Repository did not clear type instances during creation") - } -} - -func TestDeleteDeploymentForget(t *testing.T) { - testRepository.reset() - testDeployer.reset() - d, err := testManager.CreateDeployment(&deploymentRequest) - if !reflect.DeepEqual(d, &deployment) || err != nil { - t.Fatalf("Expected a different set of response values from invoking CreateDeployment."+ - "Received: %v, %s. Expected: %#v, %s.", d, err, &deployment, "nil") - } - - if testRepository.Created[0] != deploymentRequest.Name { - t.Fatalf("Repository CreateDeployment was called with %s but expected %s.", - testRepository.Created[0], deploymentRequest.Name) - } - - if !strings.HasPrefix(testRepository.ManifestAdd[deploymentRequest.Name].Name, "manifest-") { - t.Fatalf("Repository AddManifest was called with %s but expected manifest name"+ - "to begin with manifest-.", testRepository.ManifestAdd[deploymentRequest.Name].Name) - } - - if !strings.HasPrefix(testRepository.ManifestSet[deploymentRequest.Name].Name, "manifest-") { - t.Fatalf("Repository SetManifest was called with %s but expected manifest name"+ - "to begin with manifest-.", testRepository.ManifestSet[deploymentRequest.Name].Name) - } - - if !reflect.DeepEqual(*testDeployer.Created[0], configuration) || err != nil { - t.Fatalf("Deployer CreateConfiguration was called with %s but expected %s.", - testDeployer.Created[0], configuration) - } - d, err = testManager.DeleteDeployment(deploymentName, true) - if err != nil { - t.Fatalf("DeleteDeployment failed with %v", err) - } - - // Make sure the resources were deleted through deployer. - if len(testDeployer.Deleted) > 0 { - c := testDeployer.Deleted[0] - if c != nil { - if !reflect.DeepEqual(*c, configuration) || err != nil { - t.Fatalf("Deployer DeleteConfiguration was called with %s but expected %s.", - testDeployer.Created[0], configuration) - } - } - } - - if !testRepository.ChartInstancesCleared { - t.Fatal("Expected type instances to be cleared during DeleteDeployment.") - } -} - -func TestExpand(t *testing.T) { - m, err := testManager.Expand(&deploymentRequest) - if err != nil { - t.Fatal("Failed to expand deployment request into manifest.") - } - - if !reflect.DeepEqual(*m.ExpandedConfig, configuration) { - t.Fatalf("Expanded config not correct in output manifest: %v", *m) - } - - if !reflect.DeepEqual(*m.Layout, layout) { - t.Fatalf("Layout not correct in output manifest: %v", *m) - } -} - -func TestListTypes(t *testing.T) { - testRepository.reset() - - testManager.ListCharts() - - if !testRepository.ListTypesCalled { - t.Fatal("expected repository ListCharts() call.") - } -} - -func TestListInstances(t *testing.T) { - testRepository.reset() - - testManager.ListChartInstances("all") - - if !testRepository.GetChartInstancesCalled { - t.Fatal("expected repository GetChartInstances() call.") - } -} - -// TODO(jackgr): Implement TestListRepoCharts -func TestListRepoCharts(t *testing.T) { - /* - types, err := testManager.ListRepoCharts("", nil) - if err != nil { - t.Fatalf("cannot list repository types: %s", err) - } - */ -} - -// TODO(jackgr): Implement TestGetDownloadURLs -func TestGetDownloadURLs(t *testing.T) { - /* - urls, err := testManager.GetDownloadURLs("", repo.Type{}) - if err != nil { - t.Fatalf("cannot list get download urls: %s", err) - } - */ -} diff --git a/cmd/manager/repository/persistent/persistent.go b/cmd/manager/repository/persistent/persistent.go deleted file mode 100644 index 06e6abb0c..000000000 --- a/cmd/manager/repository/persistent/persistent.go +++ /dev/null @@ -1,488 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package persistent implements a persistent deployment repository. -// -// This package is currently implemented using MondoDB, but there is no -// guarantee that it will continue to be implemented using MondoDB in the -// future. -package persistent - -import ( - "fmt" - "log" - "net/url" - "os" - "time" - - "github.com/kubernetes/helm/cmd/manager/repository" - "github.com/kubernetes/helm/pkg/common" - - "gopkg.in/mgo.v2" - "gopkg.in/mgo.v2/bson" -) - -type pDeployment struct { - ID string `bson:"_id"` - common.Deployment -} - -type pManifest struct { - ID string `bson:"_id"` - common.Manifest -} - -type pInstance struct { - ID string `bson:"_id"` - common.ChartInstance -} - -type pRepository struct { - Session *mgo.Session // mongodb session - Deployments *mgo.Collection // deployments collection - Manifests *mgo.Collection // manifests collection - Instances *mgo.Collection // instances collection -} - -// Constants used to configure the MongoDB database. -const ( - DatabaseName = "deployment_manager" - DeploymentsCollectionName = "deployments_collection" - ManifestsCollectionName = "manifests_collection" - InstancesCollectionName = "instances_collection" -) - -// NewRepository returns a new persistent repository. Its lifetime is decopuled -// from the lifetime of the current process. When the process dies, its contents -// will not be affected. -// -// The server argument provides connection information for the repository server. -// It is parsed as a URL, and the username, password, host and port, if provided, -// are used to create the connection string. -func NewRepository(server string) (repository.Repository, error) { - travis := os.Getenv("TRAVIS") - if travis == "true" { - err := fmt.Errorf("cannot use MongoDB in Travis CI due to gopkg.in/mgo.v2 issue #218") - log.Println(err.Error()) - return nil, err - } - - u, err := url.Parse(server) - if err != nil { - err2 := fmt.Errorf("cannot parse url '%s': %s\n", server, err) - log.Println(err2.Error()) - return nil, err2 - } - - u2 := &url.URL{Scheme: "mongodb", User: u.User, Host: u.Host} - server = u2.String() - - session, err := mgo.Dial(server) - if err != nil { - err2 := fmt.Errorf("cannot connect to MongoDB at %s: %s\n", server, err) - log.Println(err2.Error()) - return nil, err2 - } - - session.SetMode(mgo.Strong, false) - session.SetSafe(&mgo.Safe{WMode: "majority"}) - database := session.DB(DatabaseName) - deployments, err := createCollection(database, DeploymentsCollectionName, nil) - if err != nil { - return nil, err - } - - manifests, err := createCollection(database, ManifestsCollectionName, - [][]string{{"manifest.deployment"}}) - if err != nil { - return nil, err - } - - instances, err := createCollection(database, InstancesCollectionName, - [][]string{{"typeinstance.type"}, {"typeinstance.deployment"}}) - if err != nil { - return nil, err - } - - pr := &pRepository{ - Session: session, - Deployments: deployments, - Manifests: manifests, - Instances: instances, - } - - return pr, nil -} - -func createCollection(db *mgo.Database, cName string, keys [][]string) (*mgo.Collection, error) { - c := db.C(cName) - for _, key := range keys { - if err := createIndex(c, key...); err != nil { - return nil, err - } - } - - return c, nil -} - -func createIndex(c *mgo.Collection, key ...string) error { - if err := c.EnsureIndexKey(key...); err != nil { - err2 := fmt.Errorf("cannot create index %v for collection %s: %s\n", key, c.Name, err) - log.Println(err2.Error()) - return err2 - } - - return nil -} - -// Reset returns the repository to its initial state. -func (r *pRepository) Reset() error { - database := r.Session.DB(DatabaseName) - if err := database.DropDatabase(); err != nil { - return fmt.Errorf("cannot drop database %s", database.Name) - } - - r.Close() - return nil -} - -// Close cleans up any resources used by the repository. -func (r *pRepository) Close() { - r.Session.Close() -} - -// ListDeployments returns of all of the deployments in the repository. -func (r *pRepository) ListDeployments() ([]common.Deployment, error) { - var result []pDeployment - if err := r.Deployments.Find(nil).All(&result); err != nil { - return nil, fmt.Errorf("cannot list deployments: %s", err) - } - - deployments := []common.Deployment{} - for _, pd := range result { - deployments = append(deployments, pd.Deployment) - } - - return deployments, nil -} - -// GetDeployment returns the deployment with the supplied name. -// If the deployment is not found, it returns an error. -func (r *pRepository) GetDeployment(name string) (*common.Deployment, error) { - result := pDeployment{} - if err := r.Deployments.FindId(name).One(&result); err != nil { - return nil, fmt.Errorf("cannot get deployment %s: %s", name, err) - } - - return &result.Deployment, nil -} - -// GetValidDeployment returns the deployment with the supplied name. -// If the deployment is not found or marked as deleted, it returns an error. -func (r *pRepository) GetValidDeployment(name string) (*common.Deployment, error) { - d, err := r.GetDeployment(name) - if err != nil { - return nil, err - } - - if d.State.Status == common.DeletedStatus { - return nil, fmt.Errorf("deployment %s is deleted", name) - } - - return d, nil -} - -// CreateDeployment creates a new deployment and stores it in the repository. -func (r *pRepository) CreateDeployment(name string) (*common.Deployment, error) { - exists, _ := r.GetValidDeployment(name) - if exists != nil { - return nil, fmt.Errorf("deployment %s already exists", name) - } - - d := common.NewDeployment(name) - if err := r.insertDeployment(d); err != nil { - return nil, err - } - - log.Printf("created deployment: %v", d) - return d, nil -} - -// SetDeploymentStatus sets the DeploymentStatus of the deployment and updates ModifiedAt -func (r *pRepository) SetDeploymentState(name string, state *common.DeploymentState) error { - d, err := r.GetValidDeployment(name) - if err != nil { - return err - } - - d.State = state - return r.updateDeployment(d) -} - -func (r *pRepository) AddManifest(manifest *common.Manifest) error { - deploymentName := manifest.Deployment - d, err := r.GetValidDeployment(deploymentName) - if err != nil { - return err - } - - count, err := r.Manifests.FindId(manifest.Name).Count() - if err != nil { - return fmt.Errorf("cannot search for manifest %s: %s", manifest.Name, err) - } - - if count > 0 { - return fmt.Errorf("manifest %s already exists", manifest.Name) - } - - if err := r.insertManifest(manifest); err != nil { - return err - } - - d.LatestManifest = manifest.Name - if err := r.updateDeployment(d); err != nil { - return err - } - - log.Printf("Added manifest %s to deployment: %s", manifest.Name, deploymentName) - return nil -} - -// DeleteDeployment deletes the deployment with the supplied name. -// If forget is true, then the deployment is removed from the repository. -// Otherwise, it is marked as deleted and retained. -func (r *pRepository) DeleteDeployment(name string, forget bool) (*common.Deployment, error) { - d, err := r.GetValidDeployment(name) - if err != nil { - return nil, err - } - - if !forget { - d.DeletedAt = time.Now() - d.State = &common.DeploymentState{Status: common.DeletedStatus} - if err := r.updateDeployment(d); err != nil { - return nil, err - } - } else { - d.LatestManifest = "" - if err := r.removeManifestsForDeployment(d); err != nil { - return nil, err - } - - if err := r.removeDeployment(d); err != nil { - return nil, err - } - } - - log.Printf("deleted deployment: %v", d) - return d, nil -} - -func (r *pRepository) insertDeployment(d *common.Deployment) error { - if d != nil && d.Name != "" { - wrapper := pDeployment{ID: d.Name, Deployment: *d} - if err := r.Deployments.Insert(&wrapper); err != nil { - return fmt.Errorf("cannot insert deployment %v: %s", wrapper, err) - } - } - - return nil -} - -func (r *pRepository) removeDeployment(d *common.Deployment) error { - if d != nil && d.Name != "" { - if err := r.Deployments.RemoveId(d.Name); err != nil { - return fmt.Errorf("cannot remove deployment %s: %s", d.Name, err) - } - } - - return nil -} - -func (r *pRepository) updateDeployment(d *common.Deployment) error { - if d != nil && d.Name != "" { - if d.State.Status != common.DeletedStatus { - d.ModifiedAt = time.Now() - } - - wrapper := pDeployment{ID: d.Name, Deployment: *d} - if err := r.Deployments.UpdateId(d.Name, &wrapper); err != nil { - return fmt.Errorf("cannot update deployment %v: %s", wrapper, err) - } - } - - return nil -} - -func (r *pRepository) ListManifests(deploymentName string) (map[string]*common.Manifest, error) { - _, err := r.GetValidDeployment(deploymentName) - if err != nil { - return nil, err - } - - return r.listManifestsForDeployment(deploymentName) -} - -func (r *pRepository) GetManifest(deploymentName string, manifestName string) (*common.Manifest, error) { - _, err := r.GetValidDeployment(deploymentName) - if err != nil { - return nil, err - } - - return r.getManifestForDeployment(deploymentName, manifestName) -} - -// GetLatestManifest returns the latest manifest for a given deployment, -// which by definition is the manifest with the largest time stamp. -func (r *pRepository) GetLatestManifest(deploymentName string) (*common.Manifest, error) { - d, err := r.GetValidDeployment(deploymentName) - if err != nil { - return nil, err - } - - if d.LatestManifest == "" { - return nil, nil - } - - return r.getManifestForDeployment(deploymentName, d.LatestManifest) -} - -// SetManifest sets an existing manifest in the repository to provided manifest. -func (r *pRepository) SetManifest(manifest *common.Manifest) error { - _, err := r.GetManifest(manifest.Deployment, manifest.Name) - if err != nil { - return err - } - - return r.updateManifest(manifest) -} - -func (r *pRepository) updateManifest(m *common.Manifest) error { - if m != nil && m.Name != "" { - wrapper := pManifest{ID: m.Name, Manifest: *m} - if err := r.Manifests.UpdateId(m.Name, &wrapper); err != nil { - return fmt.Errorf("cannot update manifest %v: %s", wrapper, err) - } - } - - return nil -} - -func (r *pRepository) listManifestsForDeployment(deploymentName string) (map[string]*common.Manifest, error) { - query := bson.M{"manifest.deployment": deploymentName} - var result []pManifest - if err := r.Manifests.Find(query).All(&result); err != nil { - return nil, fmt.Errorf("cannot list manifests for deployment %s: %s", deploymentName, err) - } - - l := make(map[string]*common.Manifest, 0) - for _, pm := range result { - l[pm.Name] = &pm.Manifest - } - - return l, nil -} - -func (r *pRepository) getManifestForDeployment(deploymentName string, manifestName string) (*common.Manifest, error) { - result := pManifest{} - if err := r.Manifests.FindId(manifestName).One(&result); err != nil { - return nil, fmt.Errorf("cannot get manifest %s: %s", manifestName, err) - } - - if result.Deployment != deploymentName { - return nil, fmt.Errorf("manifest %s not found in deployment %s", manifestName, deploymentName) - } - - return &result.Manifest, nil -} - -func (r *pRepository) insertManifest(m *common.Manifest) error { - if m != nil && m.Name != "" { - wrapper := pManifest{ID: m.Name, Manifest: *m} - if err := r.Manifests.Insert(&wrapper); err != nil { - return fmt.Errorf("cannot insert manifest %v: %s", wrapper, err) - } - } - - return nil -} - -func (r *pRepository) removeManifestsForDeployment(d *common.Deployment) error { - if d != nil && d.Name != "" { - query := bson.M{"manifest.deployment": d.Name} - if _, err := r.Manifests.RemoveAll(query); err != nil { - return fmt.Errorf("cannot remove all manifests for deployment %s: %s", d.Name, err) - } - } - - return nil -} - -// ListCharts returns all types known from existing instances. -func (r *pRepository) ListCharts() ([]string, error) { - var result []string - if err := r.Instances.Find(nil).Distinct("typeinstance.type", &result); err != nil { - return nil, fmt.Errorf("cannot list type instances: %s", err) - } - - return result, nil -} - -// GetChartInstances returns all instances of a given type. If typeName is empty -// or equal to "all", returns all instances of all types. -func (r *pRepository) GetChartInstances(typeName string) ([]*common.ChartInstance, error) { - query := bson.M{"typeinstance.type": typeName} - if typeName == "" || typeName == "all" { - query = nil - } - - var result []pInstance - if err := r.Instances.Find(query).All(&result); err != nil { - return nil, fmt.Errorf("cannot get instances of type %s: %s", typeName, err) - } - - instances := []*common.ChartInstance{} - for _, pi := range result { - instances = append(instances, &pi.ChartInstance) - } - - return instances, nil -} - -// ClearChartInstancesForDeployment deletes all type instances associated with the given -// deployment from the repository. -func (r *pRepository) ClearChartInstancesForDeployment(deploymentName string) error { - if deploymentName != "" { - query := bson.M{"typeinstance.deployment": deploymentName} - if _, err := r.Instances.RemoveAll(query); err != nil { - return fmt.Errorf("cannot clear type instances for deployment %s: %s", deploymentName, err) - } - } - - return nil -} - -// AddChartInstances adds the supplied type instances to the repository. -func (r *pRepository) AddChartInstances(instances map[string][]*common.ChartInstance) error { - for _, is := range instances { - for _, i := range is { - key := fmt.Sprintf("%s.%s.%s", i.Deployment, i.Type, i.Name) - wrapper := pInstance{ID: key, ChartInstance: *i} - if err := r.Instances.Insert(&wrapper); err != nil { - return fmt.Errorf("cannot insert type instance %v: %s", wrapper, err) - } - } - } - - return nil -} diff --git a/cmd/manager/repository/persistent/persistent_test.go b/cmd/manager/repository/persistent/persistent_test.go deleted file mode 100644 index 4581c22bc..000000000 --- a/cmd/manager/repository/persistent/persistent_test.go +++ /dev/null @@ -1,110 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package persistent - -import ( - "github.com/kubernetes/helm/cmd/manager/repository" - - "sync" - "testing" -) - -var tryRepository = true -var repositoryLock sync.RWMutex - -func createRepository() repository.Repository { - repositoryLock.Lock() - defer repositoryLock.Unlock() - - if tryRepository { - r, err := NewRepository("mongodb://localhost") - if err == nil { - return r - } - } - - tryRepository = false - return nil -} - -func resetRepository(t *testing.T, r repository.Repository) { - if r != nil { - if err := r.(*pRepository).Reset(); err != nil { - t.Fatalf("cannot reset repository: %s\n", err) - } - } -} - -func TestRepositoryListEmpty(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryListEmpty(t, r) - } -} - -func TestRepositoryGetFailsWithNonExistentDeployment(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryGetFailsWithNonExistentDeployment(t, r) - } -} - -func TestRepositoryCreateDeploymentWorks(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryCreateDeploymentWorks(t, r) - } -} - -func TestRepositoryMultipleManifestsWorks(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryMultipleManifestsWorks(t, r) - } -} - -func TestRepositoryDeleteFailsWithNonExistentDeployment(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryDeleteFailsWithNonExistentDeployment(t, r) - } -} - -func TestRepositoryDeleteWorksWithNoLatestManifest(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryDeleteWorksWithNoLatestManifest(t, r) - } -} - -func TestRepositoryDeleteDeploymentWorksNoForget(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryDeleteDeploymentWorksNoForget(t, r) - } -} - -func TestRepositoryDeleteDeploymentWorksForget(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryDeleteDeploymentWorksForget(t, r) - } -} - -func TestRepositoryChartInstances(t *testing.T) { - if r := createRepository(); r != nil { - defer resetRepository(t, r) - repository.TestRepositoryChartInstances(t, r) - } -} diff --git a/cmd/manager/repository/repository.go b/cmd/manager/repository/repository.go deleted file mode 100644 index 40ebaee37..000000000 --- a/cmd/manager/repository/repository.go +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package repository defines a deployment repository. -package repository - -import ( - "github.com/kubernetes/helm/pkg/common" -) - -// Repository manages storage for all Helm entities, as well as -// the common operations to store, access and manage them. -type Repository interface { - // Deployments. - ListDeployments() ([]common.Deployment, error) - GetDeployment(name string) (*common.Deployment, error) - GetValidDeployment(name string) (*common.Deployment, error) - CreateDeployment(name string) (*common.Deployment, error) - DeleteDeployment(name string, forget bool) (*common.Deployment, error) - SetDeploymentState(name string, state *common.DeploymentState) error - - // Manifests. - AddManifest(manifest *common.Manifest) error - SetManifest(manifest *common.Manifest) error - ListManifests(deploymentName string) (map[string]*common.Manifest, error) - GetManifest(deploymentName string, manifestName string) (*common.Manifest, error) - GetLatestManifest(deploymentName string) (*common.Manifest, error) - - // Types. - ListCharts() ([]string, error) - GetChartInstances(chartName string) ([]*common.ChartInstance, error) - ClearChartInstancesForDeployment(deploymentName string) error - AddChartInstances(instances map[string][]*common.ChartInstance) error - - Close() -} diff --git a/cmd/manager/repository/test_common.go b/cmd/manager/repository/test_common.go deleted file mode 100644 index 60efbe40f..000000000 --- a/cmd/manager/repository/test_common.go +++ /dev/null @@ -1,340 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package repository - -import ( - "github.com/kubernetes/helm/pkg/common" - - "fmt" - "testing" -) - -// TestRepositoryListEmpty checks that listing an empty repository works. -func TestRepositoryListEmpty(t *testing.T, r Repository) { - d, err := r.ListDeployments() - if err != nil { - t.Fatal("List Deployments failed") - } - - if len(d) != 0 { - t.Fatal("Returned non zero list") - } -} - -// TestRepositoryGetFailsWithNonExistentDeployment checks that getting a non-existent deployment fails. -func TestRepositoryGetFailsWithNonExistentDeployment(t *testing.T, r Repository) { - _, err := r.GetDeployment("nothere") - if err == nil { - t.Fatal("GetDeployment didn't fail with non-existent deployment") - } -} - -func testCreateDeploymentWithManifests(t *testing.T, r Repository, count int) { - var deploymentName = "mydeployment" - - d, err := r.CreateDeployment(deploymentName) - if err != nil { - t.Fatalf("CreateDeployment failed: %v", err) - } - - l, err := r.ListDeployments() - if err != nil { - t.Fatalf("ListDeployments failed: %v", err) - } - - if len(l) != 1 { - t.Fatalf("Number of deployments listed is not 1: %d", len(l)) - } - - dNew, err := r.GetDeployment(deploymentName) - if err != nil { - t.Fatalf("GetDeployment failed: %v", err) - } - - if dNew.Name != d.Name { - t.Fatalf("Deployment Names don't match, got: %v, expected %v", dNew, d) - } - - mList, err := r.ListManifests(deploymentName) - if err != nil { - t.Fatalf("ListManifests failed: %v", err) - } - - if len(mList) != 0 { - t.Fatalf("Deployment has non-zero manifest count: %d", len(mList)) - } - - for i := 0; i < count; i++ { - var manifestName = fmt.Sprintf("manifest-%d", i) - manifest := common.Manifest{Deployment: deploymentName, Name: manifestName} - err := r.AddManifest(&manifest) - if err != nil { - t.Fatalf("AddManifest failed: %v", err) - } - - d, err = r.GetDeployment(deploymentName) - if err != nil { - t.Fatalf("GetDeployment failed: %v", err) - } - - if d.LatestManifest != manifestName { - t.Fatalf("AddManifest did not update LatestManifest: %#v", d) - } - - mListNew, err := r.ListManifests(deploymentName) - if err != nil { - t.Fatalf("ListManifests failed: %v", err) - } - - if len(mListNew) != i+1 { - t.Fatalf("Deployment has unexpected manifest count: want %d, have %d", i+1, len(mListNew)) - } - - m, err := r.GetManifest(deploymentName, manifestName) - if err != nil { - t.Fatalf("GetManifest failed: %v", err) - } - - if m.Name != manifestName { - t.Fatalf("Unexpected manifest name: want %s, have %s", manifestName, m.Name) - } - } -} - -// TestRepositoryCreateDeploymentWorks checks that creating a deployment works. -func TestRepositoryCreateDeploymentWorks(t *testing.T, r Repository) { - testCreateDeploymentWithManifests(t, r, 1) -} - -// TestRepositoryMultipleManifestsWorks checks that creating a deploymente with multiple manifests works. -func TestRepositoryMultipleManifestsWorks(t *testing.T, r Repository) { - testCreateDeploymentWithManifests(t, r, 7) -} - -// TestRepositoryDeleteFailsWithNonExistentDeployment checks that deleting a non-existent deployment fails. -func TestRepositoryDeleteFailsWithNonExistentDeployment(t *testing.T, r Repository) { - var deploymentName = "mydeployment" - d, err := r.DeleteDeployment(deploymentName, false) - if err == nil { - t.Fatalf("DeleteDeployment didn't fail with non existent deployment") - } - - if d != nil { - t.Fatalf("DeleteDeployment returned non-nil for non existent deployment") - } -} - -// TestRepositoryDeleteWorksWithNoLatestManifest checks that deleting a deployment with no latest manifest works. -func TestRepositoryDeleteWorksWithNoLatestManifest(t *testing.T, r Repository) { - var deploymentName = "mydeployment" - _, err := r.CreateDeployment(deploymentName) - if err != nil { - t.Fatalf("CreateDeployment failed: %v", err) - } - - dDeleted, err := r.DeleteDeployment(deploymentName, false) - if err != nil { - t.Fatalf("DeleteDeployment failed: %v", err) - } - - if dDeleted.State.Status != common.DeletedStatus { - t.Fatalf("Deployment Status is not deleted") - } - - if _, err := r.ListManifests(deploymentName); err == nil { - t.Fatalf("Manifests are not deleted") - } -} - -// TestRepositoryDeleteDeploymentWorksNoForget checks that deleting a deployment without forgetting it works. -func TestRepositoryDeleteDeploymentWorksNoForget(t *testing.T, r Repository) { - var deploymentName = "mydeployment" - var manifestName = "manifest-0" - manifest := common.Manifest{Deployment: deploymentName, Name: manifestName} - _, err := r.CreateDeployment(deploymentName) - if err != nil { - t.Fatalf("CreateDeployment failed: %v", err) - } - - err = r.AddManifest(&manifest) - if err != nil { - t.Fatalf("AddManifest failed: %v", err) - } - - dDeleted, err := r.DeleteDeployment(deploymentName, false) - if err != nil { - t.Fatalf("DeleteDeployment failed: %v", err) - } - - if dDeleted.State.Status != common.DeletedStatus { - t.Fatalf("Deployment Status is not deleted") - } -} - -// TestRepositoryDeleteDeploymentWorksForget checks that deleting and forgetting a deployment works. -func TestRepositoryDeleteDeploymentWorksForget(t *testing.T, r Repository) { - var deploymentName = "mydeployment" - var manifestName = "manifest-0" - manifest := common.Manifest{Deployment: deploymentName, Name: manifestName} - _, err := r.CreateDeployment(deploymentName) - if err != nil { - t.Fatalf("CreateDeployment failed: %v", err) - } - - err = r.AddManifest(&manifest) - if err != nil { - t.Fatalf("AddManifest failed: %v", err) - } - - dDeleted, err := r.DeleteDeployment(deploymentName, true) - if err != nil { - t.Fatalf("DeleteDeployment failed: %v", err) - } - - if dDeleted.State.Status != common.CreatedStatus { - t.Fatalf("Deployment Status is not created") - } -} - -// TestRepositoryChartInstances checks that type instances can be listed and retrieved successfully. -func TestRepositoryChartInstances(t *testing.T, r Repository) { - d1Map := map[string][]*common.ChartInstance{ - "t1": { - { - Name: "i1", - Type: "t1", - Deployment: "d1", - Manifest: "m1", - Path: "p1", - }, - }, - } - - d2Map := map[string][]*common.ChartInstance{ - "t2": { - { - Name: "i2", - Type: "t2", - Deployment: "d2", - Manifest: "m2", - Path: "p2", - }, - }, - } - - d3Map := map[string][]*common.ChartInstance{ - "t2": { - { - Name: "i3", - Type: "t2", - Deployment: "d3", - Manifest: "m3", - Path: "p3", - }, - }, - } - - instances, err := r.GetChartInstances("noinstances") - if err != nil { - t.Fatal(err) - } - - if len(instances) != 0 { - t.Fatalf("expected no instances: %v", instances) - } - - types, err := r.ListCharts() - if err != nil { - t.Fatal(err) - } - - if len(types) != 0 { - t.Fatalf("expected no types: %v", types) - } - - r.AddChartInstances(d1Map) - r.AddChartInstances(d2Map) - r.AddChartInstances(d3Map) - - instances, err = r.GetChartInstances("unknowntype") - if err != nil { - t.Fatal(err) - } - - if len(instances) != 0 { - t.Fatalf("expected no instances: %v", instances) - } - - instances, err = r.GetChartInstances("t1") - if err != nil { - t.Fatal(err) - } - - if len(instances) != 1 { - t.Fatalf("expected one instance: %v", instances) - } - - instances, err = r.GetChartInstances("t2") - if err != nil { - t.Fatal(err) - } - - if len(instances) != 2 { - t.Fatalf("expected two instances: %v", instances) - } - - instances, err = r.GetChartInstances("all") - if err != nil { - t.Fatal(err) - } - - if len(instances) != 3 { - t.Fatalf("expected three total instances: %v", instances) - } - - types, err = r.ListCharts() - if err != nil { - t.Fatal(err) - } - - if len(types) != 2 { - t.Fatalf("expected two total types: %v", types) - } - - err = r.ClearChartInstancesForDeployment("d1") - if err != nil { - t.Fatal(err) - } - - instances, err = r.GetChartInstances("t1") - if err != nil { - t.Fatal(err) - } - - if len(instances) != 0 { - t.Fatalf("expected no instances after clear: %v", instances) - } - - types, err = r.ListCharts() - if err != nil { - t.Fatal(err) - } - - if len(types) != 1 { - t.Fatalf("expected one total type: %v", types) - } -} diff --git a/cmd/manager/repository/transient/transient.go b/cmd/manager/repository/transient/transient.go deleted file mode 100644 index 774d26e75..000000000 --- a/cmd/manager/repository/transient/transient.go +++ /dev/null @@ -1,325 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Package transient implements a transient deployment repository. -package transient - -import ( - "fmt" - "log" - "sync" - "time" - - "github.com/kubernetes/helm/cmd/manager/repository" - "github.com/kubernetes/helm/pkg/common" -) - -// deploymentChartInstanceMap stores type instances mapped by deployment name. -// This allows for simple updating and deleting of per-deployment instances -// when deployments are created/updated/deleted. -type deploymentChartInstanceMap map[string][]*common.ChartInstance - -type tRepository struct { - sync.RWMutex - deployments map[string]common.Deployment - manifests map[string]map[string]*common.Manifest - instances map[string]deploymentChartInstanceMap -} - -// NewRepository returns a new transient repository. Its lifetime is coupled -// to the lifetime of the current process. When the process dies, its contents -// will be permanently destroyed. -func NewRepository() repository.Repository { - return &tRepository{ - deployments: make(map[string]common.Deployment, 0), - manifests: make(map[string]map[string]*common.Manifest, 0), - instances: make(map[string]deploymentChartInstanceMap, 0), - } -} - -func (r *tRepository) Close() { - r.deployments = make(map[string]common.Deployment, 0) - r.manifests = make(map[string]map[string]*common.Manifest, 0) - r.instances = make(map[string]deploymentChartInstanceMap, 0) -} - -// ListDeployments returns of all of the deployments in the repository. -func (r *tRepository) ListDeployments() ([]common.Deployment, error) { - l := []common.Deployment{} - for _, deployment := range r.deployments { - l = append(l, deployment) - } - - return l, nil -} - -// GetDeployment returns the deployment with the supplied name. -// If the deployment is not found, it returns an error. -func (r *tRepository) GetDeployment(name string) (*common.Deployment, error) { - d, ok := r.deployments[name] - if !ok { - return nil, fmt.Errorf("deployment %s not found", name) - } - - return &d, nil -} - -// GetValidDeployment returns the deployment with the supplied name. -// If the deployment is not found or marked as deleted, it returns an error. -func (r *tRepository) GetValidDeployment(name string) (*common.Deployment, error) { - d, err := r.GetDeployment(name) - if err != nil { - return nil, err - } - - if d.State.Status == common.DeletedStatus { - return nil, fmt.Errorf("deployment %s is deleted", name) - } - - return d, nil -} - -// SetDeploymentState sets the DeploymentState of the deployment and updates ModifiedAt -func (r *tRepository) SetDeploymentState(name string, state *common.DeploymentState) error { - r.Lock() - defer r.Unlock() - - d, err := r.GetValidDeployment(name) - if err != nil { - return err - } - - d.State = state - d.ModifiedAt = time.Now() - r.deployments[name] = *d - return nil -} - -// CreateDeployment creates a new deployment and stores it in the repository. -func (r *tRepository) CreateDeployment(name string) (*common.Deployment, error) { - r.Lock() - defer r.Unlock() - - exists, _ := r.GetValidDeployment(name) - if exists != nil { - return nil, fmt.Errorf("Deployment %s already exists", name) - } - - d := common.NewDeployment(name) - r.deployments[name] = *d - - log.Printf("created deployment: %v", d) - return d, nil -} - -// AddManifest adds a manifest to the repository and repoints the latest -// manifest to it for the corresponding deployment. -func (r *tRepository) AddManifest(manifest *common.Manifest) error { - r.Lock() - defer r.Unlock() - - deploymentName := manifest.Deployment - l, err := r.ListManifests(deploymentName) - if err != nil { - return err - } - - // Make sure the manifest doesn't already exist, and if not, add the manifest to - // map of manifests this deployment has - if _, ok := l[manifest.Name]; ok { - return fmt.Errorf("Manifest %s already exists in deployment %s", manifest.Name, deploymentName) - } - - d, err := r.GetValidDeployment(deploymentName) - if err != nil { - return err - } - - l[manifest.Name] = manifest - d.LatestManifest = manifest.Name - d.ModifiedAt = time.Now() - r.deployments[deploymentName] = *d - - log.Printf("Added manifest %s to deployment: %s", manifest.Name, deploymentName) - return nil -} - -// SetManifest sets an existing manifest in the repository to provided manifest. -func (r *tRepository) SetManifest(manifest *common.Manifest) error { - r.Lock() - defer r.Unlock() - - l, err := r.ListManifests(manifest.Deployment) - if err != nil { - return err - } - - if _, ok := l[manifest.Name]; !ok { - return fmt.Errorf("manifest %s not found", manifest.Name) - } - - l[manifest.Name] = manifest - return nil -} - -// DeleteDeployment deletes the deployment with the supplied name. -// If forget is true, then the deployment is removed from the repository. -// Otherwise, it is marked as deleted and retained. -func (r *tRepository) DeleteDeployment(name string, forget bool) (*common.Deployment, error) { - r.Lock() - defer r.Unlock() - - d, err := r.GetValidDeployment(name) - if err != nil { - return nil, err - } - - if !forget { - d.DeletedAt = time.Now() - d.State = &common.DeploymentState{Status: common.DeletedStatus} - r.deployments[name] = *d - } else { - delete(r.deployments, name) - delete(r.manifests, name) - d.LatestManifest = "" - } - - log.Printf("deleted deployment: %v", d) - return d, nil -} - -func (r *tRepository) ListManifests(deploymentName string) (map[string]*common.Manifest, error) { - _, err := r.GetValidDeployment(deploymentName) - if err != nil { - return nil, err - } - - return r.listManifestsForDeployment(deploymentName) -} - -func (r *tRepository) listManifestsForDeployment(deploymentName string) (map[string]*common.Manifest, error) { - l, ok := r.manifests[deploymentName] - if !ok { - l = make(map[string]*common.Manifest, 0) - r.manifests[deploymentName] = l - } - - return l, nil -} - -func (r *tRepository) GetManifest(deploymentName string, manifestName string) (*common.Manifest, error) { - _, err := r.GetValidDeployment(deploymentName) - if err != nil { - return nil, err - } - - return r.getManifestForDeployment(deploymentName, manifestName) -} - -func (r *tRepository) getManifestForDeployment(deploymentName string, manifestName string) (*common.Manifest, error) { - l, err := r.listManifestsForDeployment(deploymentName) - if err != nil { - return nil, err - } - - m, ok := l[manifestName] - if !ok { - return nil, fmt.Errorf("manifest %s not found in deployment %s", manifestName, deploymentName) - } - - return m, nil -} - -// GetLatestManifest returns the latest manifest for a given deployment, -// which by definition is the manifest with the largest time stamp. -func (r *tRepository) GetLatestManifest(deploymentName string) (*common.Manifest, error) { - d, err := r.GetValidDeployment(deploymentName) - if err != nil { - return nil, err - } - - if d.LatestManifest == "" { - return nil, nil - } - - return r.getManifestForDeployment(deploymentName, d.LatestManifest) -} - -// ListCharts returns all types known from existing instances. -func (r *tRepository) ListCharts() ([]string, error) { - var keys []string - for k := range r.instances { - keys = append(keys, k) - } - - return keys, nil -} - -// GetChartInstances returns all instances of a given type. If type is empty, -// returns all instances for all types. -func (r *tRepository) GetChartInstances(typeName string) ([]*common.ChartInstance, error) { - var instances []*common.ChartInstance - for t, dInstMap := range r.instances { - if t == typeName || typeName == "" || typeName == "all" { - for _, i := range dInstMap { - instances = append(instances, i...) - } - } - } - - return instances, nil -} - -// ClearChartInstancesForDeployment deletes all type instances associated with the given -// deployment from the repository. -func (r *tRepository) ClearChartInstancesForDeployment(deploymentName string) error { - r.Lock() - defer r.Unlock() - - for t, dMap := range r.instances { - delete(dMap, deploymentName) - if len(dMap) == 0 { - delete(r.instances, t) - } - } - - return nil -} - -// AddChartInstances adds the supplied type instances to the repository. -func (r *tRepository) AddChartInstances(instances map[string][]*common.ChartInstance) error { - r.Lock() - defer r.Unlock() - - // Add instances to the appropriate type and deployment maps. - for t, is := range instances { - if r.instances[t] == nil { - r.instances[t] = make(deploymentChartInstanceMap) - } - - tmap := r.instances[t] - for _, instance := range is { - deployment := instance.Deployment - if tmap[deployment] == nil { - tmap[deployment] = make([]*common.ChartInstance, 0) - } - - tmap[deployment] = append(tmap[deployment], instance) - } - } - - return nil -} diff --git a/cmd/manager/repository/transient/transient_test.go b/cmd/manager/repository/transient/transient_test.go deleted file mode 100644 index d74fbfc36..000000000 --- a/cmd/manager/repository/transient/transient_test.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package transient - -import ( - "github.com/kubernetes/helm/cmd/manager/repository" - "testing" -) - -func TestRepositoryListEmpty(t *testing.T) { - repository.TestRepositoryListEmpty(t, NewRepository()) -} - -func TestRepositoryGetFailsWithNonExistentDeployment(t *testing.T) { - repository.TestRepositoryGetFailsWithNonExistentDeployment(t, NewRepository()) -} - -func TestRepositoryCreateDeploymentWorks(t *testing.T) { - repository.TestRepositoryCreateDeploymentWorks(t, NewRepository()) -} - -func TestRepositoryMultipleManifestsWorks(t *testing.T) { - repository.TestRepositoryMultipleManifestsWorks(t, NewRepository()) -} - -func TestRepositoryDeleteFailsWithNonExistentDeployment(t *testing.T) { - repository.TestRepositoryDeleteFailsWithNonExistentDeployment(t, NewRepository()) -} - -func TestRepositoryDeleteWorksWithNoLatestManifest(t *testing.T) { - repository.TestRepositoryDeleteWorksWithNoLatestManifest(t, NewRepository()) -} - -func TestRepositoryDeleteDeploymentWorksNoForget(t *testing.T) { - repository.TestRepositoryDeleteDeploymentWorksNoForget(t, NewRepository()) -} - -func TestRepositoryDeleteDeploymentWorksForget(t *testing.T) { - repository.TestRepositoryDeleteDeploymentWorksForget(t, NewRepository()) -} - -func TestRepositoryChartInstances(t *testing.T) { - repository.TestRepositoryChartInstances(t, NewRepository()) -} diff --git a/cmd/manager/router/context.go b/cmd/manager/router/context.go deleted file mode 100644 index 6e878c762..000000000 --- a/cmd/manager/router/context.go +++ /dev/null @@ -1,69 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package router - -import ( - "github.com/kubernetes/helm/cmd/manager/manager" - helmhttp "github.com/kubernetes/helm/pkg/httputil" - "github.com/kubernetes/helm/pkg/repo" -) - -// Config holds the global configuration parameters passed into the router. -// -// Config is used concurrently. Once a config is created, it should be treated -// as immutable. -type Config struct { - // Address is the host and port (:8080) - Address string - // MaxTemplateLength is the maximum length of a template. - MaxTemplateLength int64 - // ExpanderPort is the default expander's IP port - ExpanderPort string - // ExpanderURL is the default expander's URL - ExpanderURL string - // DeployerName is the deployer's DNS name - DeployerName string - // DeployerPort is the deployer's IP port - DeployerPort string - // DeployerURL is the deployer's URL - DeployerURL string - // CredentialFile is the file to the credentials. - CredentialFile string - // CredentialSecrets tells the service to use a secrets file instead. - CredentialSecrets bool - // MongoName is the DNS name of the mongo server. - MongoName string - // MongoPort is the port for the MongoDB protocol on the mongo server. - // It is a string for historical reasons. - MongoPort string - // MongoAddress is the name and port. - MongoAddress string -} - -// Context contains dependencies that are passed to each handler function. -// -// Context carries typed information, often scoped to interfaces, so that the -// caller's contract with the service is known at compile time. -// -// Members of the context must be concurrency safe. -type Context struct { - Config *Config - // Manager is a helm/manager/manager.Manager - Manager manager.Manager - Encoder helmhttp.Encoder - CredentialProvider repo.ICredentialProvider -} diff --git a/cmd/manager/router/router.go b/cmd/manager/router/router.go deleted file mode 100644 index 65d1e86fe..000000000 --- a/cmd/manager/router/router.go +++ /dev/null @@ -1,96 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -/*Package router is an HTTP router. - -This router provides appropriate dependency injection/encapsulation for the -HTTP routing layer. This removes the requirement to set global variables for -resources like database handles. - -This library does not replace the default HTTP mux because there is no need. -Instead, it implements an HTTP handler. - -It then defines a handler function that is given a context as well as a -request and response. -*/ -package router - -import ( - "log" - "net/http" - "reflect" - - "github.com/Masterminds/httputil" - helmhttp "github.com/kubernetes/helm/pkg/httputil" -) - -// HandlerFunc responds to an individual HTTP request. -// -// Returned errors will be captured, logged, and returned as HTTP 500 errors. -type HandlerFunc func(w http.ResponseWriter, r *http.Request, c *Context) error - -// Handler implements an http.Handler. -// -// This is the top level route handler. -type Handler struct { - c *Context - resolver *httputil.Resolver - routes map[string]HandlerFunc - paths []string -} - -// NewHandler creates a new Handler. -// -// Routes cannot be modified after construction. The order that the route -// names are returned by Routes.Paths() determines the lookup order. -func NewHandler(c *Context) *Handler { - return &Handler{ - c: c, - resolver: httputil.NewResolver([]string{}), - routes: map[string]HandlerFunc{}, - paths: []string{}, - } -} - -// Add a route to a handler. -// -// The route name is "VERB /ENPOINT/PATH", e.g. "GET /foo". -func (h *Handler) Add(route string, fn HandlerFunc) { - log.Printf("Map %q to %s", route, reflect.ValueOf(fn).Type().Name()) - h.routes[route] = fn - h.paths = append(h.paths, route) - h.resolver = httputil.NewResolver(h.paths) -} - -// ServeHTTP serves an HTTP request. -func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - log.Printf(helmhttp.LogAccess, r.Method, r.URL) - route, err := h.resolver.Resolve(r) - if err != nil { - helmhttp.NotFound(w, r) - return - } - - fn, ok := h.routes[route] - if !ok { - // This is a 500 because the route was registered, but not here. - helmhttp.Fatal(w, r, "route %s missing", route) - } - - if err := fn(w, r, h.c); err != nil { - helmhttp.Fatal(w, r, err.Error()) - } -} diff --git a/cmd/manager/router/router_test.go b/cmd/manager/router/router_test.go deleted file mode 100644 index d34057919..000000000 --- a/cmd/manager/router/router_test.go +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package router - -import ( - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "testing" -) - -func TestHandler(t *testing.T) { - c := &Context{} - - h := NewHandler(c) - h.Add("GET /", func(w http.ResponseWriter, r *http.Request, c *Context) error { - fmt.Fprintln(w, "hello") - return nil - }) - h.Add("POST /", func(w http.ResponseWriter, r *http.Request, c *Context) error { - fmt.Fprintln(w, "goodbye") - return nil - }) - - s := httptest.NewServer(h) - defer s.Close() - - res, err := http.Get(s.URL) - if err != nil { - t.Fatal(err) - } - data, err := ioutil.ReadAll(res.Body) - res.Body.Close() - if err != nil { - t.Fatal(err) - } - - if "hello\n" != string(data) { - t.Errorf("Expected 'hello', got %q", data) - } -} diff --git a/cmd/manager/testutil.go b/cmd/manager/testutil.go deleted file mode 100644 index cdf10bdc1..000000000 --- a/cmd/manager/testutil.go +++ /dev/null @@ -1,172 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "errors" - "fmt" - "net/http/httptest" - "regexp" - - "github.com/kubernetes/helm/cmd/manager/router" - "github.com/kubernetes/helm/pkg/chart" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/httputil" - "github.com/kubernetes/helm/pkg/repo" -) - -// httpHarness is a simple test server fixture. -// Simple fixture for standing up a test server with a single route. -// -// You must Close() the returned server. -func httpHarness(c *router.Context, route string, fn router.HandlerFunc) *httptest.Server { - h := router.NewHandler(c) - h.Add(route, fn) - return httptest.NewServer(h) -} - -// stubContext creates a stub of a Context object. -// -// This creates a stub context with the following properties: -// - Config is initialized to empty values -// - Encoder is initialized to httputil.DefaultEncoder -// - CredentialProvider is initialized to repo.InmemCredentialProvider -// - Manager is initialized to mockManager. -func stubContext() *router.Context { - return &router.Context{ - Config: &router.Config{}, - Manager: newMockManager(), - CredentialProvider: repo.NewInmemCredentialProvider(), - Encoder: httputil.DefaultEncoder, - } -} - -func newMockManager() *mockManager { - return &mockManager{ - deployments: []*common.Deployment{}, - } -} - -type mockManager struct { - deployments []*common.Deployment -} - -func (m *mockManager) ListDeployments() ([]common.Deployment, error) { - d := make([]common.Deployment, len(m.deployments)) - for i, dd := range m.deployments { - d[i] = *dd - } - return d, nil -} - -func (m *mockManager) GetDeployment(name string) (*common.Deployment, error) { - - for _, d := range m.deployments { - if d.Name == name { - return d, nil - } - } - - return nil, errors.New("mock error: No such deployment") -} - -func (m *mockManager) CreateDeployment(depReq *common.DeploymentRequest) (*common.Deployment, error) { - return &common.Deployment{}, nil -} - -func (m *mockManager) DeleteDeployment(name string, forget bool) (*common.Deployment, error) { - for _, d := range m.deployments { - if d.Name == name { - return d, nil - } - } - fmt.Printf("Could not find %s", name) - return nil, errors.New("Deployment not found") -} - -func (m *mockManager) PutDeployment(name string, depReq *common.DeploymentRequest) (*common.Deployment, error) { - for _, d := range m.deployments { - if d.Name == name { - d.State.Status = common.ModifiedStatus - return d, nil - } - } - return nil, errors.New("Deployment not found") -} - -func (m *mockManager) ListManifests(deploymentName string) (map[string]*common.Manifest, error) { - return map[string]*common.Manifest{}, nil -} - -func (m *mockManager) GetManifest(deploymentName string, manifest string) (*common.Manifest, error) { - return &common.Manifest{}, nil -} - -func (m *mockManager) Expand(depReq *common.DeploymentRequest) (*common.Manifest, error) { - return &common.Manifest{}, nil -} - -func (m *mockManager) ListCharts() ([]string, error) { - return []string{}, nil -} - -func (m *mockManager) ListChartInstances(chartName string) ([]*common.ChartInstance, error) { - return []*common.ChartInstance{}, nil -} - -func (m *mockManager) GetRepoForChart(chartName string) (string, error) { - return "", nil -} - -func (m *mockManager) GetMetadataForChart(chartName string) (*chart.Chartfile, error) { - return &chart.Chartfile{}, nil -} - -func (m *mockManager) GetChart(chartName string) (*chart.Chart, error) { - return &chart.Chart{}, nil -} - -func (m *mockManager) ListRepoCharts(repoName string, regex *regexp.Regexp) ([]string, error) { - return []string{}, nil -} - -func (m *mockManager) GetChartForRepo(repoName, chartName string) (*chart.Chart, error) { - return &chart.Chart{}, nil -} - -func (m *mockManager) CreateCredential(name string, c *repo.Credential) error { - return nil -} -func (m *mockManager) GetCredential(name string) (*repo.Credential, error) { - return &repo.Credential{}, nil -} - -func (m *mockManager) ListRepos() (map[string]string, error) { - return map[string]string{}, nil -} - -func (m *mockManager) AddRepo(addition repo.IRepo) error { - return nil -} - -func (m *mockManager) RemoveRepo(name string) error { - return nil -} - -func (m *mockManager) GetRepo(name string) (repo.IRepo, error) { - return &repo.Repo{}, nil -} diff --git a/cmd/resourcifier/configurations.go b/cmd/resourcifier/configurations.go deleted file mode 100644 index c1b457ab8..000000000 --- a/cmd/resourcifier/configurations.go +++ /dev/null @@ -1,255 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "github.com/kubernetes/helm/cmd/resourcifier/configurator" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/util" - - "encoding/json" - "errors" - "flag" - "fmt" - "io" - "io/ioutil" - "net/http" - "net/url" - - "github.com/ghodss/yaml" - "github.com/gorilla/mux" -) - -var configurations = []Route{ - {"ListConfigurations", "/configurations/{type}", "GET", listConfigurationsHandlerFunc, ""}, - {"GetConfiguration", "/configurations/{type}/{name}", "GET", getConfigurationHandlerFunc, ""}, - {"CreateConfiguration", "/configurations", "POST", createConfigurationHandlerFunc, "JSON"}, - {"DeleteConfiguration", "/configurations", "DELETE", deleteConfigurationHandlerFunc, "JSON"}, - {"PutConfiguration", "/configurations", "PUT", putConfigurationHandlerFunc, "JSON"}, -} - -var ( - maxLength = flag.Int64("maxLength", 1024*8, "The maximum length (KB) of a configuration.") - kubePath = flag.String("kubectl", "./kubectl", "The path to the kubectl binary.") - kubeService = flag.String("service", "", "The DNS name of the kubernetes service.") - kubeServer = flag.String("server", "", "The IP address and optional port of the kubernetes master.") - kubeInsecure = flag.Bool("insecure-skip-tls-verify", false, "Do not check the server's certificate for validity.") - kubeConfig = flag.String("config", "", "Path to a kubeconfig file.") - kubeCertAuth = flag.String("certificate-authority", "", "Path to a file for the certificate authority.") - kubeClientCert = flag.String("client-certificate", "", "Path to a client certificate file.") - kubeClientKey = flag.String("client-key", "", "Path to a client key file.") - kubeToken = flag.String("token", "", "A service account token.") - kubeUsername = flag.String("username", "", "The username to use for basic auth.") - kubePassword = flag.String("password", "", "The password to use for basic auth.") -) - -var backend *configurator.Configurator - -func init() { - if !flag.Parsed() { - flag.Parse() - } - - routes = append(routes, configurations...) - backend = getConfigurator() -} - -func listConfigurationsHandlerFunc(w http.ResponseWriter, r *http.Request) { - handler := "resourcifier: list configurations" - util.LogHandlerEntry(handler, r) - rtype, err := getPathVariable(w, r, "type", handler) - if err != nil { - return - } - - c := &common.Configuration{ - Resources: []*common.Resource{ - {Type: rtype}, - }, - } - - output, err := backend.Configure(c, configurator.GetOperation) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return - } - - util.LogHandlerExit(handler, http.StatusOK, output, w) - util.WriteYAML(handler, w, []byte(output), http.StatusOK) -} - -func getConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { - handler := "resourcifier: get configuration" - util.LogHandlerEntry(handler, r) - rtype, err := getPathVariable(w, r, "type", handler) - if err != nil { - return - } - - rname, err := getPathVariable(w, r, "name", handler) - if err != nil { - return - } - - c := &common.Configuration{ - Resources: []*common.Resource{ - {Name: rname, Type: rtype}, - }, - } - - output, err := backend.Configure(c, configurator.GetOperation) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return - } - - util.LogHandlerExit(handler, http.StatusOK, output, w) - util.WriteYAML(handler, w, []byte(output), http.StatusOK) -} - -func createConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { - handler := "resourcifier: create configuration" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - c := getConfiguration(w, r, handler) - if c != nil { - _, err := backend.Configure(c, configurator.CreateOperation) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return - } - - util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated) - return - } - - util.LogHandlerExit(handler, http.StatusOK, "OK", w) -} - -func deleteConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { - handler := "resourcifier: delete configuration" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - c := getConfiguration(w, r, handler) - if c != nil { - if _, err := backend.Configure(c, configurator.DeleteOperation); err != nil { - e := errors.New("cannot delete configuration: " + err.Error() + "\n") - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return - } - - w.WriteHeader(http.StatusNoContent) - util.LogHandlerExit(handler, http.StatusNoContent, "No Content", w) - return - } - - util.LogHandlerExit(handler, http.StatusOK, "OK", w) -} - -func putConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) { - handler := "resourcifier: update configuration" - util.LogHandlerEntry(handler, r) - defer r.Body.Close() - c := getConfiguration(w, r, handler) - if c != nil { - if _, err := backend.Configure(c, configurator.ReplaceOperation); err != nil { - e := errors.New("cannot replace configuration: " + err.Error() + "\n") - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return - } - - util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated) - return - } - - util.LogHandlerExit(handler, http.StatusOK, "OK", w) -} - -func getConfigurator() *configurator.Configurator { - kubernetesConfig := &util.KubernetesConfig{ - KubePath: *kubePath, - KubeService: *kubeService, - KubeServer: *kubeServer, - KubeInsecure: *kubeInsecure, - KubeConfig: *kubeConfig, - KubeCertAuth: *kubeCertAuth, - KubeClientCert: *kubeClientCert, - KubeClientKey: *kubeClientKey, - KubeToken: *kubeToken, - KubeUsername: *kubeUsername, - KubePassword: *kubePassword, - } - return configurator.NewConfigurator(util.NewKubernetesKubectl(kubernetesConfig)) -} - -func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler string) (string, error) { - vars := mux.Vars(r) - escaped, ok := vars[variable] - if !ok { - e := fmt.Errorf("%s name not found in URL", variable) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return "", e - } - - unescaped, err := url.QueryUnescape(escaped) - if err != nil { - e := fmt.Errorf("cannot decode name (%v)", variable) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return "", e - } - - return unescaped, nil -} - -func getConfiguration(w http.ResponseWriter, r *http.Request, handler string) *common.Configuration { - b := io.LimitReader(r.Body, *maxLength*1024) - y, err := ioutil.ReadAll(b) - if err != nil { - util.LogAndReturnError(handler, http.StatusBadRequest, err, w) - return nil - } - - // Reject the input if it exceeded the length limit, - // since we may not have read all of it into the buffer. - if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF { - e := fmt.Errorf("configuration exceeds maximum length of %d KB", *maxLength) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return nil - } - - j, err := yaml.YAMLToJSON(y) - if err != nil { - e := errors.New(err.Error() + "\n" + string(y)) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return nil - } - - c := &common.Configuration{} - if err := json.Unmarshal(j, c); err != nil { - e := errors.New(err.Error() + "\n" + string(j)) - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return nil - } - - if len(c.Resources) < 1 { - e := fmt.Errorf("configuration is empty") - util.LogAndReturnError(handler, http.StatusBadRequest, e, w) - return nil - } - - return c -} diff --git a/cmd/resourcifier/configurator/configurator.go b/cmd/resourcifier/configurator/configurator.go deleted file mode 100644 index 49255be94..000000000 --- a/cmd/resourcifier/configurator/configurator.go +++ /dev/null @@ -1,262 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package configurator - -import ( - "fmt" - "log" - "regexp" - "strings" - - "github.com/ghodss/yaml" - "github.com/kubernetes/helm/pkg/common" - "github.com/kubernetes/helm/pkg/util" -) - -// Configurator configures a Kubernetes cluster using kubectl. -type Configurator struct { - k util.Kubernetes -} - -// NewConfigurator creates a new Configurator. -func NewConfigurator(kubernetes util.Kubernetes) *Configurator { - return &Configurator{kubernetes} -} - -// operation is an enumeration type for kubectl operations. -type operation string - -// These constants implement the operation enumeration type. -const ( - CreateOperation operation = "create" - DeleteOperation operation = "delete" - GetOperation operation = "get" - ReplaceOperation operation = "replace" -) - -// TODO(jackgr): Configure resources without dependencies in parallel. - -// Error is an error type that captures errors from the multiple calls to kubectl -// made for a single configuration. -type Error struct { - errors []error -} - -// Error returns the string value of an Error. -func (e *Error) Error() string { - errs := []string{} - for _, err := range e.errors { - errs = append(errs, err.Error()) - } - - return strings.Join(errs, "\n") -} - -func (e *Error) appendError(err error) error { - e.errors = append(e.errors, err) - return err -} - -// DependencyMap maps a resource name to a set of dependencies. -type DependencyMap map[string]map[string]bool - -var refRe = regexp.MustCompile("\\$\\(ref\\.([^\\.]+)\\.([^\\)]+)\\)") - -// Configure passes each resource in the configuration to kubectl and performs the appropriate -// action on it (create/delete/replace) and updates the State of the resource with the resulting -// status. In case of errors with a resource, Resource.State.Errors is set. -// and then updates the deployment with the completion status and completion time. -func (a *Configurator) Configure(c *common.Configuration, o operation) (string, error) { - errors := &Error{} - var output []string - - deps, err := getDependencies(c, o) - if err != nil { - e := fmt.Errorf("Error generating dependencies: %s", err.Error()) - return "", e - } - - for { - resources := getUnprocessedResources(c) - - // No more resources to process. - if len(resources) == 0 { - break - } - - for _, r := range resources { - // Resource still has dependencies. - if len(deps[r.Name]) != 0 { - continue - } - - out, err := a.configureResource(r, o) - if err != nil { - log.Println(errors.appendError(err)) - abortDependants(c, deps, r.Name) - - // Resource states have changed, need to recalculate unprocessed - // resources. - break - } - - output = append(output, out) - removeDependencies(deps, r.Name) - } - } - - return strings.Join(output, "\n"), nil -} - -func marshalResource(resource *common.Resource) (string, error) { - if len(resource.Properties) > 0 { - y, err := yaml.Marshal(resource.Properties) - if err != nil { - return "", fmt.Errorf("yaml marshal failed for resource: %v: %v", resource.Name, err) - } - return string(y), nil - } - return "", nil -} - -func (a *Configurator) configureResource(resource *common.Resource, o operation) (string, error) { - ret := "" - var err error - - switch o { - case CreateOperation: - obj, err := marshalResource(resource) - if err != nil { - resource.State = failState(err) - return "", err - } - ret, err = a.k.Create(obj) - if err != nil { - resource.State = failState(err) - } else { - resource.State = &common.ResourceState{Status: common.Created} - } - return ret, nil - case ReplaceOperation: - obj, err := marshalResource(resource) - if err != nil { - resource.State = failState(err) - return "", err - } - ret, err = a.k.Replace(obj) - if err != nil { - resource.State = failState(err) - } else { - resource.State = &common.ResourceState{Status: common.Created} - } - return ret, nil - case GetOperation: - return a.k.Get(resource.Name, resource.Type) - case DeleteOperation: - obj, err := marshalResource(resource) - if err != nil { - resource.State = failState(err) - return "", err - } - ret, err = a.k.Delete(obj) - // Treat deleting a non-existent resource as success. - if err != nil { - if strings.HasSuffix(strings.TrimSpace(ret), "not found") { - resource.State = &common.ResourceState{Status: common.Created} - return ret, nil - } - resource.State = failState(err) - } - return ret, err - default: - return "", fmt.Errorf("invalid operation %s for resource: %v: %v", o, resource.Name, err) - } -} - -func failState(e error) *common.ResourceState { - return &common.ResourceState{ - Status: common.Failed, - Errors: []string{e.Error()}, - } -} - -func getUnprocessedResources(c *common.Configuration) []*common.Resource { - var resources []*common.Resource - for _, r := range c.Resources { - if r.State == nil { - resources = append(resources, r) - } - } - - return resources -} - -// getDependencies iterates over resources and returns a map of resource name to -// the set of dependencies that resource has. -// -// Dependencies are reversed for delete operation. -func getDependencies(c *common.Configuration, o operation) (DependencyMap, error) { - deps := DependencyMap{} - - // Prepopulate map. This will be used later to validate referenced resources - // actually exist. - for _, r := range c.Resources { - deps[r.Name] = make(map[string]bool) - } - - for _, r := range c.Resources { - props, err := yaml.Marshal(r.Properties) - if err != nil { - return nil, fmt.Errorf("Failed to deserialize resource properties for resource %s: %v", r.Name, r.Properties) - } - - refs := refRe.FindAllStringSubmatch(string(props), -1) - for _, ref := range refs { - // Validate referenced resource exists in config. - if _, ok := deps[ref[1]]; !ok { - return nil, fmt.Errorf("Invalid resource name in reference: %s", ref[1]) - } - - // Delete dependencies should be reverse of create. - if o == DeleteOperation { - deps[ref[1]][r.Name] = true - } else { - deps[r.Name][ref[1]] = true - } - } - } - - return deps, nil -} - -// updateDependants removes the dependency dep from the set of dependencies for -// all resource. -func removeDependencies(deps DependencyMap, dep string) { - for _, d := range deps { - delete(d, dep) - } -} - -// abortDependants changes the state of all of the dependants of a resource to -// Aborted. -func abortDependants(c *common.Configuration, deps DependencyMap, dep string) { - for _, r := range c.Resources { - if _, ok := deps[r.Name][dep]; ok { - r.State = &common.ResourceState{Status: common.Aborted} - } - } -} diff --git a/cmd/resourcifier/main.go b/cmd/resourcifier/main.go deleted file mode 100644 index bc8ae084a..000000000 --- a/cmd/resourcifier/main.go +++ /dev/null @@ -1,84 +0,0 @@ -/* -Copyright 2015 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "flag" - "fmt" - "log" - "net/http" - "os" - - "github.com/gorilla/handlers" - "github.com/gorilla/mux" - "github.com/kubernetes/helm/pkg/util" - "github.com/kubernetes/helm/pkg/version" -) - -// Route defines a routing table entry to be registered with gorilla/mux. -type Route struct { - Name string - Path string - Methods string - HandlerFunc http.HandlerFunc - Type string -} - -var routes = []Route{ - {"HealthCheck", "/healthz", "GET", healthCheckHandlerFunc, ""}, -} - -// port to listen on -var port = flag.Int("port", 8080, "The port to listen on") - -func main() { - if !flag.Parsed() { - flag.Parse() - } - - router := mux.NewRouter() - router.StrictSlash(true) - for _, route := range routes { - handler := http.Handler(http.HandlerFunc(route.HandlerFunc)) - switch route.Type { - case "JSON": - handler = handlers.ContentTypeHandler(handler, "application/json") - case "": - break - default: - log.Fatalf("invalid route type: %v", route) - } - - r := router.NewRoute() - r.Name(route.Name). - Path(route.Path). - Methods(route.Methods). - Handler(handler) - } - - address := fmt.Sprintf(":%d", *port) - handler := handlers.CombinedLoggingHandler(os.Stderr, router) - log.Printf("Version: %s", version.Version) - log.Printf("Listening on port %d...", *port) - log.Fatal(http.ListenAndServe(address, handler)) -} - -func healthCheckHandlerFunc(w http.ResponseWriter, r *http.Request) { - handler := "manager: get health" - util.LogHandlerEntry(handler, r) - util.LogHandlerExitWithText(handler, w, "OK", http.StatusOK) -} diff --git a/cmd/tiller/environment/environment.go b/cmd/tiller/environment/environment.go new file mode 100644 index 000000000..e9140bf1f --- /dev/null +++ b/cmd/tiller/environment/environment.go @@ -0,0 +1,142 @@ +package environment + +import ( + "github.com/kubernetes/helm/pkg/engine" + "github.com/kubernetes/helm/pkg/proto/hapi/chart" + "github.com/kubernetes/helm/pkg/proto/hapi/release" + "github.com/kubernetes/helm/pkg/storage" +) + +// GoTplEngine is the name of the Go template engine, as registered in the EngineYard. +const GoTplEngine = "gotpl" + +// DefaultEngine points to the engine that the EngineYard should treat as the +// default. A chart that does not specify an engine may be run through the +// default engine. +var DefaultEngine = GoTplEngine + +// EngineYard maps engine names to engine implementations. +type EngineYard map[string]Engine + +// Get retrieves a template engine by name. +// +// If no matching template engine is found, the second return value will +// be false. +func (y EngineYard) Get(k string) (Engine, bool) { + e, ok := y[k] + return e, ok +} + +// Default returns the default template engine. +// +// The default is specified by DefaultEngine. +// +// If the default template engine cannot be found, this panics. +func (y EngineYard) Default() Engine { + d, ok := y[DefaultEngine] + if !ok { + // This is a developer error! + panic("Default template engine does not exist") + } + return d +} + +// Engine represents a template engine that can render templates. +// +// For some engines, "rendering" includes both compiling and executing. (Other +// engines do not distinguish between phases.) +// +// The engine returns a map where the key is the named output entity (usually +// a file name) and the value is the rendered content of the template. +// +// An Engine must be capable of executing multiple concurrent requests, but +// without tainting one request's environment with data from another request. +type Engine interface { + Render(*chart.Chart, *chart.Config) (map[string]string, error) +} + +// ReleaseStorage represents a storage engine for a Release. +// +// Release storage must be concurrency safe. +type ReleaseStorage interface { + + // Create stores a release in the storage. + // + // If a release with the same name exists, this returns an error. + // + // It may return other errors in cases where it cannot write to storage. + Create(*release.Release) error + // Read takes a name and returns a release that has that name. + // + // It will only return releases that are not deleted and not superseded. + // + // It will return an error if no relevant release can be found, or if storage + // is not properly functioning. + Read(name string) (*release.Release, error) + + // Update looks for a release with the same name and updates it with the + // present release contents. + // + // For immutable storage backends, this may result in a new release record + // being created, and the previous release being marked as superseded. + // + // It will return an error if a previous release is not found. It may also + // return an error if the storage backend encounters an error. + Update(*release.Release) error + + // Delete marks a Release as deleted. + // + // It returns the deleted record. If the record is not found or if the + // underlying storage encounters an error, this will return an error. + Delete(name string) (*release.Release, error) + + // List lists all active (non-deleted, non-superseded) releases. + // + // To get deleted or superseded releases, use Query. + List() ([]*release.Release, error) + + // Query takes a map of labels and returns any releases that match. + // + // Query will search all releases, including deleted and superseded ones. + // The provided map will be used to filter results. + Query(map[string]string) ([]*release.Release, error) +} + +// KubeClient represents a client capable of communicating with the Kubernetes API. +// +// A KubeClient must be concurrency safe. +type KubeClient interface { + // Install takes a map where the key is a "file name" (read: unique relational + // id) and the value is a Kubernetes manifest containing one or more resource + // definitions. + // + // TODO: Can these be in YAML or JSON, or must they be in one particular + // format? + Install(manifests map[string]string) error +} + +// Environment provides the context for executing a client request. +// +// All services in a context are concurrency safe. +type Environment struct { + // EngineYard provides access to the known template engines. + EngineYard EngineYard + // Releases stores records of releases. + Releases ReleaseStorage + // KubeClient is a Kubernetes API client. + KubeClient KubeClient +} + +// New returns an environment initialized with the defaults. +func New() *Environment { + e := engine.New() + var ey EngineYard = map[string]Engine{ + // Currently, the only template engine we support is the GoTpl one. But + // we can easily add some here. + GoTplEngine: e, + } + return &Environment{ + EngineYard: ey, + Releases: storage.NewMemory(), + } +} diff --git a/cmd/tiller/environment/environment_test.go b/cmd/tiller/environment/environment_test.go new file mode 100644 index 000000000..abdb015bc --- /dev/null +++ b/cmd/tiller/environment/environment_test.go @@ -0,0 +1,110 @@ +package environment + +import ( + "testing" + + "github.com/kubernetes/helm/pkg/proto/hapi/chart" + "github.com/kubernetes/helm/pkg/proto/hapi/release" +) + +type mockEngine struct { + out map[string]string +} + +func (e *mockEngine) Render(chrt *chart.Chart, v *chart.Config) (map[string]string, error) { + return e.out, nil +} + +type mockReleaseStorage struct { + rel *release.Release +} + +func (r *mockReleaseStorage) Create(v *release.Release) error { + r.rel = v + return nil +} + +func (r *mockReleaseStorage) Read(k string) (*release.Release, error) { + return r.rel, nil +} + +func (r *mockReleaseStorage) Update(v *release.Release) error { + r.rel = v + return nil +} + +func (r *mockReleaseStorage) Delete(k string) (*release.Release, error) { + return r.rel, nil +} + +func (r *mockReleaseStorage) List() ([]*release.Release, error) { + return []*release.Release{}, nil +} + +func (r *mockReleaseStorage) Query(labels map[string]string) ([]*release.Release, error) { + return []*release.Release{}, nil +} + +type mockKubeClient struct { +} + +func (k *mockKubeClient) Install(manifests map[string]string) error { + return nil +} + +var _ Engine = &mockEngine{} +var _ ReleaseStorage = &mockReleaseStorage{} +var _ KubeClient = &mockKubeClient{} + +func TestEngine(t *testing.T) { + eng := &mockEngine{out: map[string]string{"albatross": "test"}} + + env := New() + env.EngineYard = EngineYard(map[string]Engine{"test": eng}) + + if engine, ok := env.EngineYard.Get("test"); !ok { + t.Errorf("failed to get engine from EngineYard") + } else if out, err := engine.Render(&chart.Chart{}, &chart.Config{}); err != nil { + t.Errorf("unexpected template error: %s", err) + } else if out["albatross"] != "test" { + t.Errorf("expected 'test', got %q", out["albatross"]) + } +} + +func TestReleaseStorage(t *testing.T) { + rs := &mockReleaseStorage{} + env := New() + env.Releases = rs + + release := &release.Release{Name: "mariner"} + + if err := env.Releases.Create(release); err != nil { + t.Fatalf("failed to store release: %s", err) + } + + if err := env.Releases.Update(release); err != nil { + t.Fatalf("failed to update release: %s", err) + } + + if v, err := env.Releases.Read("albatross"); err != nil { + t.Errorf("Error fetching release: %s", err) + } else if v.Name != "mariner" { + t.Errorf("Expected mariner, got %q", v.Name) + } + + if _, err := env.Releases.Delete("albatross"); err != nil { + t.Fatalf("failed to delete release: %s", err) + } +} + +func TestKubeClient(t *testing.T) { + kc := &mockKubeClient{} + env := New() + env.KubeClient = kc + + manifests := map[string]string{} + + if err := env.KubeClient.Install(manifests); err != nil { + t.Errorf("Kubeclient failed: %s", err) + } +} diff --git a/cmd/tiller/release_server.go b/cmd/tiller/release_server.go new file mode 100644 index 000000000..68f37daf4 --- /dev/null +++ b/cmd/tiller/release_server.go @@ -0,0 +1,143 @@ +package main + +import ( + "bytes" + "errors" + "log" + + "github.com/kubernetes/helm/cmd/tiller/environment" + "github.com/kubernetes/helm/pkg/proto/hapi/release" + "github.com/kubernetes/helm/pkg/proto/hapi/services" + "github.com/kubernetes/helm/pkg/timeconv" + "github.com/technosophos/moniker" + ctx "golang.org/x/net/context" +) + +func init() { + srv := &releaseServer{ + env: env, + } + services.RegisterReleaseServiceServer(rootServer, srv) +} + +type releaseServer struct { + env *environment.Environment +} + +var ( + // errNotImplemented is a temporary error for uninmplemented callbacks. + errNotImplemented = errors.New("not implemented") + // errMissingChart indicates that a chart was not provided. + errMissingChart = errors.New("no chart provided") + // errMissingRelease indicates that a release (name) was not provided. + errMissingRelease = errors.New("no release provided") +) + +func (s *releaseServer) ListReleases(req *services.ListReleasesRequest, stream services.ReleaseService_ListReleasesServer) error { + return errNotImplemented +} + +func (s *releaseServer) GetReleaseStatus(c ctx.Context, req *services.GetReleaseStatusRequest) (*services.GetReleaseStatusResponse, error) { + if req.Name == "" { + return nil, errMissingRelease + } + rel, err := s.env.Releases.Read(req.Name) + if err != nil { + return nil, err + } + if rel.Info == nil { + return nil, errors.New("release info is missing") + } + return &services.GetReleaseStatusResponse{Info: rel.Info}, nil +} + +func (s *releaseServer) GetReleaseContent(c ctx.Context, req *services.GetReleaseContentRequest) (*services.GetReleaseContentResponse, error) { + if req.Name == "" { + return nil, errMissingRelease + } + rel, err := s.env.Releases.Read(req.Name) + return &services.GetReleaseContentResponse{Release: rel}, err +} + +func (s *releaseServer) UpdateRelease(c ctx.Context, req *services.UpdateReleaseRequest) (*services.UpdateReleaseResponse, error) { + return nil, errNotImplemented +} + +func (s *releaseServer) InstallRelease(c ctx.Context, req *services.InstallReleaseRequest) (*services.InstallReleaseResponse, error) { + if req.Chart == nil { + return nil, errMissingChart + } + + // We should probably make a name generator part of the Environment. + namer := moniker.New() + // TODO: Make sure this is unique. + name := namer.NameSep("-") + ts := timeconv.Now() + + // Render the templates + files, err := s.env.EngineYard.Default().Render(req.Chart, req.Values) + if err != nil { + return nil, err + } + + b := bytes.NewBuffer(nil) + for name, file := range files { + // Ignore empty documents because the Kubernetes library can't handle + // them. + if len(file) > 0 { + b.WriteString("\n---\n# Source: " + name + "\n") + b.WriteString(file) + } + } + + // Store a release. + r := &release.Release{ + Name: name, + Chart: req.Chart, + Config: req.Values, + Info: &release.Info{ + FirstDeployed: ts, + LastDeployed: ts, + Status: &release.Status{Code: release.Status_UNKNOWN}, + }, + Manifest: b.String(), + } + + if req.DryRun { + log.Printf("Dry run for %s", name) + return &services.InstallReleaseResponse{Release: r}, nil + } + + if err := s.env.Releases.Create(r); err != nil { + return nil, err + } + + return &services.InstallReleaseResponse{Release: r}, nil +} + +func (s *releaseServer) UninstallRelease(c ctx.Context, req *services.UninstallReleaseRequest) (*services.UninstallReleaseResponse, error) { + if req.Name == "" { + log.Printf("uninstall: Release not found: %s", req.Name) + return nil, errMissingRelease + } + + rel, err := s.env.Releases.Read(req.Name) + if err != nil { + log.Printf("uninstall: Release not loaded: %s", req.Name) + return nil, err + } + + log.Printf("uninstall: Deleting %s", req.Name) + rel.Info.Status.Code = release.Status_DELETED + rel.Info.Deleted = timeconv.Now() + + // TODO: Once KubeClient is ready, delete the resources. + log.Println("WARNING: Currently not deleting resources from k8s") + + if err := s.env.Releases.Update(rel); err != nil { + log.Printf("uninstall: Failed to store updated release: %s", err) + } + + res := services.UninstallReleaseResponse{Release: rel} + return &res, nil +} diff --git a/cmd/tiller/release_server_test.go b/cmd/tiller/release_server_test.go new file mode 100644 index 000000000..ba479253b --- /dev/null +++ b/cmd/tiller/release_server_test.go @@ -0,0 +1,200 @@ +package main + +import ( + "strings" + "testing" + + "github.com/golang/protobuf/ptypes/timestamp" + "github.com/kubernetes/helm/cmd/tiller/environment" + "github.com/kubernetes/helm/pkg/proto/hapi/chart" + "github.com/kubernetes/helm/pkg/proto/hapi/release" + "github.com/kubernetes/helm/pkg/proto/hapi/services" + "github.com/kubernetes/helm/pkg/storage" + "github.com/kubernetes/helm/pkg/timeconv" + "golang.org/x/net/context" +) + +func rsFixture() *releaseServer { + return &releaseServer{ + env: mockEnvironment(), + } +} + +func releaseMock() *release.Release { + date := timestamp.Timestamp{242085845, 0} + return &release.Release{ + Name: "angry-panda", + Info: &release.Info{ + FirstDeployed: &date, + LastDeployed: &date, + Status: &release.Status{Code: release.Status_DEPLOYED}, + }, + Chart: &chart.Chart{ + Metadata: &chart.Metadata{ + Name: "foo", + Version: "0.1.0-beta.1", + }, + Templates: []*chart.Template{ + {Name: "foo.tpl", Data: []byte("Hello")}, + }, + }, + Config: &chart.Config{Raw: `name = "value"`}, + } +} + +func TestInstallRelease(t *testing.T) { + c := context.Background() + rs := rsFixture() + + req := &services.InstallReleaseRequest{ + Chart: &chart.Chart{ + Metadata: &chart.Metadata{Name: "hello"}, + Templates: []*chart.Template{ + {Name: "hello", Data: []byte("hello: world")}, + }, + }, + } + res, err := rs.InstallRelease(c, req) + if err != nil { + t.Errorf("Failed install: %s", err) + } + if res.Release.Name == "" { + t.Errorf("Expected release name.") + } + + rel, err := rs.env.Releases.Read(res.Release.Name) + if err != nil { + t.Errorf("Expected release for %s (%v).", res.Release.Name, rs.env.Releases) + } + + t.Logf("rel: %v", rel) + + if len(res.Release.Manifest) == 0 { + t.Errorf("No manifest returned: %v", res.Release) + } + + if len(rel.Manifest) == 0 { + t.Errorf("Expected manifest in %v", res) + } + + if !strings.Contains(rel.Manifest, "---\n# Source: hello\nhello: world") { + t.Errorf("unexpected output: %s", rel.Manifest) + } +} + +func TestInstallReleaseDryRun(t *testing.T) { + c := context.Background() + rs := rsFixture() + + req := &services.InstallReleaseRequest{ + Chart: &chart.Chart{ + Metadata: &chart.Metadata{Name: "hello"}, + Templates: []*chart.Template{ + {Name: "hello", Data: []byte("hello: world")}, + {Name: "goodbye", Data: []byte("goodbye: world")}, + {Name: "empty", Data: []byte("")}, + }, + }, + DryRun: true, + } + res, err := rs.InstallRelease(c, req) + if err != nil { + t.Errorf("Failed install: %s", err) + } + if res.Release.Name == "" { + t.Errorf("Expected release name.") + } + + if !strings.Contains(res.Release.Manifest, "---\n# Source: hello\nhello: world") { + t.Errorf("unexpected output: %s", res.Release.Manifest) + } + + if !strings.Contains(res.Release.Manifest, "---\n# Source: goodbye\ngoodbye: world") { + t.Errorf("unexpected output: %s", res.Release.Manifest) + } + + if strings.Contains(res.Release.Manifest, "empty") { + t.Errorf("Should not contain template data for an empty file. %s", res.Release.Manifest) + } + + if _, err := rs.env.Releases.Read(res.Release.Name); err == nil { + t.Errorf("Expected no stored release.") + } +} + +func TestUninstallRelease(t *testing.T) { + c := context.Background() + rs := rsFixture() + rs.env.Releases.Create(&release.Release{ + Name: "angry-panda", + Info: &release.Info{ + FirstDeployed: timeconv.Now(), + Status: &release.Status{ + Code: release.Status_DEPLOYED, + }, + }, + }) + + req := &services.UninstallReleaseRequest{ + Name: "angry-panda", + } + + res, err := rs.UninstallRelease(c, req) + if err != nil { + t.Errorf("Failed uninstall: %s", err) + } + + if res.Release.Name != "angry-panda" { + t.Errorf("Expected angry-panda, got %q", res.Release.Name) + } + + if res.Release.Info.Status.Code != release.Status_DELETED { + t.Errorf("Expected status code to be DELETED, got %d", res.Release.Info.Status.Code) + } + + if res.Release.Info.Deleted.Seconds <= 0 { + t.Errorf("Expected valid UNIX date, got %d", res.Release.Info.Deleted.Seconds) + } +} + +func TestGetReleaseContent(t *testing.T) { + c := context.Background() + rs := rsFixture() + rel := releaseMock() + if err := rs.env.Releases.Create(rel); err != nil { + t.Fatalf("Could not store mock release: %s", err) + } + + res, err := rs.GetReleaseContent(c, &services.GetReleaseContentRequest{Name: rel.Name}) + if err != nil { + t.Errorf("Error getting release content: %s", err) + } + + if res.Release.Chart.Metadata.Name != rel.Chart.Metadata.Name { + t.Errorf("Expected %q, got %q", rel.Chart.Metadata.Name, res.Release.Chart.Metadata.Name) + } +} + +func TestGetReleaseStatus(t *testing.T) { + c := context.Background() + rs := rsFixture() + rel := releaseMock() + if err := rs.env.Releases.Create(rel); err != nil { + t.Fatalf("Could not store mock release: %s", err) + } + + res, err := rs.GetReleaseStatus(c, &services.GetReleaseStatusRequest{Name: rel.Name}) + if err != nil { + t.Errorf("Error getting release content: %s", err) + } + + if res.Info.Status.Code != release.Status_DEPLOYED { + t.Errorf("Expected %d, got %d", release.Status_DEPLOYED, res.Info.Status.Code) + } +} + +func mockEnvironment() *environment.Environment { + e := environment.New() + e.Releases = storage.NewMemory() + return e +} diff --git a/cmd/tiller/tiller.go b/cmd/tiller/tiller.go new file mode 100644 index 000000000..d1fda6c8f --- /dev/null +++ b/cmd/tiller/tiller.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "net" + "os" + + "github.com/kubernetes/helm/cmd/tiller/environment" + "github.com/spf13/cobra" + "google.golang.org/grpc" +) + +// rootServer is the root gRPC server. +// +// Each gRPC service registers itself to this server during init(). +var rootServer = grpc.NewServer() +var env = environment.New() + +const globalUsage = `The Kubernetes Helm server. + +Tiller is the server for Helm. It provides in-cluster resource management. + +By default, Tiller listens for gRPC connections on port 44134. +` + +var rootCommand = &cobra.Command{ + Use: "tiller", + Short: "The Kubernetes Helm server.", + Long: globalUsage, + Run: start, +} + +func main() { + rootCommand.Execute() +} + +func start(c *cobra.Command, args []string) { + addr := ":44134" + lstn, err := net.Listen("tcp", addr) + if err != nil { + fmt.Fprintf(os.Stderr, "Server died: %s\n", err) + os.Exit(1) + } + + fmt.Printf("Tiller is running on %s\n", addr) + + if err := rootServer.Serve(lstn); err != nil { + fmt.Fprintf(os.Stderr, "Server died: %s\n", err) + os.Exit(1) + } +} diff --git a/cmd/tiller/tiller_test.go b/cmd/tiller/tiller_test.go new file mode 100644 index 000000000..55f20ef9c --- /dev/null +++ b/cmd/tiller/tiller_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "testing" + + "github.com/kubernetes/helm/cmd/tiller/environment" + "github.com/kubernetes/helm/pkg/engine" + "github.com/kubernetes/helm/pkg/storage" +) + +// These are canary tests to make sure that the default server actually +// fulfills its requirements. +var _ environment.Engine = &engine.Engine{} +var _ environment.ReleaseStorage = storage.NewMemory() + +func TestInit(t *testing.T) { + defer func() { + if recover() != nil { + t.Fatalf("Panic trapped. Check EngineYard.Default()") + } + }() + + // This will panic if it is not correct. + env.EngineYard.Default() + + e, ok := env.EngineYard.Get(environment.GoTplEngine) + if !ok { + t.Fatalf("Could not find GoTplEngine") + } + if e == nil { + t.Fatalf("Template engine GoTplEngine returned nil.") + } +} diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 000000000..3cf6c186b --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,84 @@ +# The Kubernetes Helm Architecture + +This document describes the Helm architecture at a high level. + +## The Purpose of Helm + +Helm is a tool for managing Kubernetes packages called _charts_. Helm +can do the following: + +- Create new charts from scratch +- Package charts into chart archive (tgz) files +- Interact with chart repositories where charts are stored +- Install and uninstall charts into an existing Kubernetes cluster +- Manage the releases of charts that have been installed with Helm + +For Helm, there are three important concepts: + +1. The _chart_ is a bundle of information necessary to create an + instance of a Kubernetes application. +2. The _config_ contains configuration information that can be merged + into a packaged chart to create a releasable object. +3. A _release_ is a running instance of a _chart_, combined with a + specific _config_. + +Following the formula made famous by the 12 Factor App, _chart + config += release_. + +## Components + +Helm has two major components: + +**The Helm Client** is a command-line client for end users. The client +is responsible for the following domains: + +- Local chart development +- Managing repositories +- Interacting with the Tiller server + - Sending charts to be installed + - Asking for information about releases + - Requesting upgrading or uninstalling of existing releases + +**The Tiller Server** is an in-cluster server that interacts with the +Helm client, and interfaces with the Kubernetes API server. The server +is responsible for the following: + +- Listing for incomming requests from the Helm client +- Combining a chart and configuration to build a release +- Installing charts into Kubernetes, and then tracking the subsequent + release +- Upgrading and uninstalling charts by interacting with Kubernetes + +In a nutshell, the client is responsible for managing charts, and the +server is responsible for managing releases. + +## Implementation + +The Helm client is written in the Go programming language, and uses the +gRPC protocol suite to interact with the Tiller server. + +The Tiller server is also written in Go. It provides a gRPC server to +connect with the client, and it uses the Kubernetes client library to +communicate with Kubernetes. Currently, that library uses REST+JSON. + +The Tiller server stores information in ConfigMaps located inside of +Kubernetes. It does not need its own database. + +### Structure of the Code + +The individual programs are located in `cmd/`. Shared libraries are +stored in `pkg/`. The raw ProtoBuf files are stored in `_proto/hapi` +(where `hapi` stands for the Helm Application Programming Interface). +The Go files generated from the `proto` definitions are stored in +`pkg/proto`. + +Docker images are built by cross-compiling Linux binaries and then +building a Docker image from the files in `rootfs`. + +The `scripts/` directory contains a number of utility scripts, including +`local-cluster.sh`, which can start a full Kubernetes instance inside of +a Docker container. + +Go dependencies are managed with +[Glide](https://github.com/Masterminds/glide) and stored in the +`vendor/` directory. diff --git a/docs/charts.md b/docs/charts.md new file mode 100644 index 000000000..baa779238 --- /dev/null +++ b/docs/charts.md @@ -0,0 +1,196 @@ +# Charts + +Helm uses a packaging format called _charts_. A chart is a collection of files +that collectively describe a set of Kubernetes resources. + +## The Chart File Structure + +A chart is organized as a collection of files inside of a directory. The +directory name is the name of the chart (without versioning information). Thus, +a chart describing Wordpress would be stored in the `wordpress/` directory. + +Inside of this directory, Helm will expect a structure that matches this: + +``` +wordpress/ + Chart.yaml # A YAML file containing information about the chart + LICENSE # A plain text file containing the license for the chart + README.md # A human-readable README file + values.toml # The default configuration values for this chart + charts/ # A directory containing any charts upon which this chart depends. + templates/ # A directory of templates that, when combined with values, + # will generate valid Kubernetes manifest files. +``` + +## The Chart.yaml File + +The Chart.yaml file is required for a chart. It contains the following fields: + +```yaml +name: The name of the chart (required) +version: A SemVer 2 version (required) +description: A single-sentence description of this project (optional) +keywords: + - A list of keywords about this project (optional) +home: The URL of this project's home page (optional) +sources: + - A list of URLs to source code for this project (optional) +maintainers: # (optional) + - name: The maintainer's name (required for each maintainer) + email: The maintainer's email (optional for each maintainer) +``` + +If you are familiar with the Chart.yaml file format for Helm Classic, you will +notice that fields specifying dependencies have been removed. That is because +the new Chart format expresses dependencies using the `charts/` directory. + +## Chart Dependencies + +In Helm, one chart may depend on any number of other charts. These +dependencies are expressed explicitly by copying the dependency charts +into the `charts/` directory. + +For example, if the Wordpress chart depends on the Apache chart, the +Apache chart (of the correct version) is supplied in the Wordpress +chart's `charts/` directory: + +``` +wordpress: + Chart.yaml + # ... + charts/ + apache/ + Chart.yaml + # ... + mysql/ + Chart.yaml + # ... +``` + +The example above shows how the Wordpress chart expresses its dependency +on Apache and MySQL by including those charts inside of its `charts/` +directory. + +## Templates and Values + +In Helm Charts, templates are written in the Go template language, with the +addition of 50 or so add-on template functions. + +All template files are stored in a chart's `templates/` folder. When +Helm renders the charts, it will pass every file in that directory +through the template engine. + +Values for the templates are supplied two ways: + - Chart developers may supply a file called `values.toml` inside of a + chart. This file can contain default values. + - Chart users may supply a TOML file that contains values. This can be + provided on the command line with `helm install`. + +When a user supplies custom values, these values will override the +values in the chart's `values.toml` file. + +### Template Files + +Template files follow the standard conventions for writing Go templates. +An example template file might look something like this: + +```yaml +apiVersion: v1 +kind: ReplicationController +metadata: + name: deis-database + namespace: deis + labels: + heritage: deis +spec: + replicas: 1 + selector: + app: deis-database + template: + metadata: + labels: + app: deis-database + spec: + serviceAccount: deis-database + containers: + - name: deis-database + image: {{.imageRegistry}}/postgres:{{.dockerTag}} + imagePullPolicy: {{.pullPolicy}} + ports: + - containerPort: 5432 + env: + - name: DATABASE_STORAGE + value: {{default "minio" .storage}} +``` + +The above example, based loosely on [https://github.com/deis/charts](the +chart for Deis), is a template for a Kubernetes replication controller. +It can use the following four template values: + +- `imageRegistry`: The source registry for the Docker image. +- `dockerTag`: The tag for the docker image. +- `pullPolicy`: The Kubernetes pull policy. +- `storage`: The storage backend, whose default is set to `"minio"` + +All of these values are defined by the template author. Helm does not +require or dictate parameters. + +### Values files + +Considering the template in the previous section, a `values.toml` file +that supplies the necessary values would look like this: + +```toml +imageRegistry = "quay.io/deis" +dockerTag = "latest" +pullPolicy = "alwaysPull" +storage = "s3" +``` + +When a chart includes dependency charts, values can be supplied to those +charts using TOML tables: + +```toml +imageRegistry = "quay.io/deis" +dockerTag = "latest" +pullPolicy = "alwaysPull" +storage = "s3" + +[router] +hostname = "example.com" +``` + +In the above example, the value of `hostname` will be passed to a chart +named `router` (if it exists) in the `charts/` directory. + +### References +- [Go templates](https://godoc.org/text/template) +- [Extra template functions](https://godoc.org/github.com/Masterminds/sprig) +- [The TOML format](https://github.com/toml-lang/toml) + +## Using Helm to Manage Charts + +The `helm` tool has several commands for working with charts. + +It can create a new chart for you: + +```console +$ helm create mychart +Created mychart/ +``` + +Once you have edited a chart, `helm` can package it into a chart archive +for you: + +```console +$ helm package mychart +Archived mychart-0.1.-.tgz +``` + +You can also use `helm` to help you find issues with your chart's +formatting or information: + +```console +$ helm lint mychart +No issues found +``` diff --git a/docs/design/architecture.dia b/docs/design/architecture.dia deleted file mode 100644 index e2256d456..000000000 Binary files a/docs/design/architecture.dia and /dev/null differ diff --git a/docs/design/architecture.png b/docs/design/architecture.png deleted file mode 100644 index 9b7966c92..000000000 Binary files a/docs/design/architecture.png and /dev/null differ diff --git a/docs/design/chart_format.md b/docs/design/chart_format.md deleted file mode 100644 index 095ff4829..000000000 --- a/docs/design/chart_format.md +++ /dev/null @@ -1,363 +0,0 @@ -# Helm Charts - -This document describes the Helm chart format, its presentation as an -archive, and its storage and retrieval. - - -* [Changes](#changes) -* [tl;dr: Summary](#tldr-summary) -* [Goals](#goals) - * [Non-Goals](#non-goals) -* [The Chart Format](#the-chart-format) - * [Directory Layout](#directory-layout) - * [The Chart File](#the-chart-file) - * [Releasing a Chart](#releasing-a-chart) -* [The Chart Repository](#the-chart-repository) - * [Repository Protocol](#repository-protocol) - * [Aside: Why a Flat Repository Namespace?](#aside-why-a-flat-repository-namespace) - * [Aside: Why Not Git(Hub) or VCS?](#aside-why-not-github-or-vcs) -* [Chart References: Long form, short form, and local reference](#chart-references-long-form-short-form-and-local-reference) - * [Long Form](#long-form) - * [Short Form](#short-form) - * [Local References](#local-references) -* [References](#references) - - -## Changes - -| Date | Author | Changes | -| ------------|:--------------:|:-------------------------------------------------------| -| 2016-01-21 | mbutcher@deis | Added manifests/ to chart. | -| 2016-01-25 | mbutcher@deis | Added clarifications based on comments. | -| 2016-01-26 | mbutcher@deis | Added hook/, removed manifests/. Added generate header.| -| 2016-03-24 | mbutcher@deis | Updated title and intro of document. | -| 2016-04-01 | dcunnin@google | Updated expander / schema post-DM merge | - - -## tl;dr: Summary -* A **chart** is a folder or archive containing a _Chart.yaml_ file, a _templates/_ directory with one or more template files, supporting files, such as schemas and UI directives, and optionally a _README.md_ and _LICENSE_ file. -* When a chart is **released**, it is tested, versioned, and loaded into a chart repository. -* A **chart repository** is organized according to the conventions of object storage. It is accessible by combining a domain with a repository ID (bucket), and can be browsed. A chart repository contains one or more versioned charts. -* There are three ways to reference charts. A **local reference** references a chart by relative or absolute file system path. It is intended for development. A **long name**, or fully qualified URL, refers to a released chart (in a chart repository) by URL. A **short name**, or mnemonic, is a shortened reference to a chart in a chart repository. A short name can be converted to a **long name** deterministically, and can therefore be used anywhere a long name can be used. - -## Goals -The goal of this document is to define the following aspects of Helm charts: - - -* The **format of a chart** -* The **layout of a chart repository**, with recommendations about implementations -* The format of a **short name** (mnemonic name) of a chart, as well as its fully qualified **long name**, and conventions for referencing local charts instead of short/long names. - - -We assume that we are developing a technology that will enjoy widespread use among developers and operators who… - - -* Are familiar with general Kubernetes design -* Are capable of reading and writing high-level formats like JSON, YAML, and maybe shell scripts -* Have low tolerance for complexity, and are not willing to become domain experts, but… -* Advanced users may be interested in, and willing to, learn advanced features to build much more interesting charts - - -Based on these, our design goal is to make it **easy to find, use, author, release, and maintain charts**, while still making it possible for advanced users to build sophisticated charts. - -This design is based on the integration of Deployment Manager (DM) and Helm, formerly at github.com/deis/helm. It does not lose any of the functionality of DM, and loses only Helm functionality that we believe is disposable. Substantial portions of the chart and template handling logic from the original implementations of these two tools have changed as a result of this integration. The new chart format, described in this document, is one of the primary drivers of those changes. - - -### Non-Goals -This document does not define how either the client or the server must use this data, though we make some non-normative recommendations (e.g. “this data may be displayed to the user” or “a suitable backend will be able to translate this to a fully qualified URL”). - -Consequently, this document does not describe the implementation of either client or server sides of this spec. While it defines the pieces necessary for developing and deploying a chart, it does not define a development workflow. Development workflows are discussed in [another document](../workflow/team-workflows.md). - - -## The Chart Format -We define a Chart as a bundle of data and metadata whose contents are sufficient for installing a workload into a Kubernetes cluster. - - -A Chart is composed of the following pieces: - - -1. A human- and machine-readable metadata file called `Chart.yaml` -2. A directory named `templates/` -3. An optional `README.md` file -4. An optional `LICENSE` file -5. An optional `docs/` directory -7. An optional `icon.svg` - - -A chart _may_ have other directories and files, but such files are not defined by or required by this document. For the purposes of this document, any additional files do not contribute to the core functionality of chart installation. - - -The Chart.yaml file format is described [later in this document](#the-chart-file). - - -The `templates/` directory contains one or more template files, as defined in the [Directory Layout](#directory-layout) section. Templates, along with all of their supporting files, are located therein. - - -An optional `README.md` file may be specified, which contains long-form text information about using this chart. Tools may display this information, if present. The README.md file is in Markdown format, and should contain information about a Chart’s purpose, usage, and development. - - -An optional `LICENSE` file may be present, which specifies license information for this chart and/or the images dependent on it. - - -An optional `docs/` directory may be present, and may contain one or more documentation files. This directory contains documentation that is more specific, verbose, or thorough than what is present in the `README.md` file. - -### Directory Layout -A chart is laid out as follows. The top level directory (represented by the placeholder ROOT) must be the name of the chart (verified by linter). For example, if the chart is named `nginx`, the ROOT directory must be named `nginx/`. - -``` -ROOT/ - Chart.yaml - README.md - LICENSE - docs/ - some.md - templates/ - some.yaml - some.jinja - some.jinja.schema -``` - -Templates are stored in a separate directory for the following reasons: - - -* This future-proofs the format: we can add other directories at the top level and deprecate `templates/`. -* It allows authors to add other files (such as documentation) in a straightforward way that will not confuse definitions-aware tools. -* It allows for the possibility that a chart definition may be embedded inside of project code. - - -Charts must not assume that they can perform file system operations to load another chart or supporting resources directly via the filesystem, nor should they store any operational files outside of the chart directory. This point becomes important in the case where there is Python/Jinja or other executable code inside the chart. These executable components especially should not assume they can access the host filesystem. It should be possible to archive the chart directory (e.g. `tar -cf ROOT.tar ROOT/`) and not lose any necessary information. A chart is an all-encompassing unit that can be processed by the client/server. - -### The Chart File -The `Chart.yaml` file specifies package metadata for the definition. Package metadata is any information that explains the package structure, purpose, and provenance. Its intended audience is tooling that surfaces such information to the user (e.g. a command line client, a web interface, a search engine). - - -A definition file does not specify anything about individual pieces of the definition (e.g. description of per-field schema or metadata), nor does it contain information about the parent environment (e.g. that it is hosted on S3 or in GitHub). Its scope is the definition as a whole. - - -Fields: - - -* name: A human-readable name of the definition. This may contain UTF-8 alphanumeric text, together with dash and underscore characters. -* description: A human-readable description of the definition, not to exceed one paragraph of text. No text formatting is supported. A description may use any printable text characters allowed by the file format. -* version: A SemVer 2 semantic version of the chart (template files). Refer to the [instruction on semver.org](http://semver.org/). -* keywords: A list of human-readable keywords. A keyword may contain alphanumeric text and spaces. -* maintainers: A list of author objects, where an author object has two properties: - * name: Author name - * email: Author email (optional) -* source: A URL to the source code for this chart -* home: A URL to the home page for the project -* expander: Indicates how to process the contents of templates/ (optional) - * name: The name of the expander, as a Kubernetes service name or URL. - * entrypoint: If the expander requires an entrypoint, gives the file (optional). -* schema: The file used to validate the properties (user-configurable inputs) of this chart before expansion. (optional) - -Example: - -``` -name: nginx -description: The nginx web server as a replication controller and service pair. -version: 0.5.1 -keywords: -* https -* http -* web server -* proxy -source: https://github.com/foo/bar -home: http://nginx.com -expander: - name: goexpander-service -schema: Schema.yaml -``` - -### Expanders and Templates -The content of the `templates/` directory and the schema file are defined by the particular expander invoked by name in the Chart.yaml file. Such expanders consume these files, in the context of properties, to generate content. If a schema is given, the expander may use it to validate the properties before expansion. Discussion of the available expanders and how they intepret the content of /templates is outside the scope of this document. - -If no expander is specified, files with yaml or json extensions in the templates/ directory are parsed as Kubernetes API objects and included in the deployment without transformation. Charts may therefore contain Kubernetes API objects that do not contain any parameters or require any server side processing before being sent to Kubernetes. Such charts can therefore not invoke other charts. - -### Releasing a Chart -A chart is _released_ when the source of the chart is tested, versioned, packaged into a gzipped tar file. At that point, that particular release of a chart is considered immutable. No further changes are allowed. To enforce immutability through tamper detection, Charts must be digitally signed. - - -Releases must follow a SemVer 2 version pattern. - - -A released chart may be moved into a chart repository. - - -NON-NORMATIVE: A release pattern _might_ look like this: - -``` -$ helm release -r 1.1.2 ./nginx/ --> generated archive --> signed archive. Signature ‘iEYEARECAAYFAkjil’ --> generated ./nginx-1.1.2.tgz --> uploading to gs://kubernetes-charts/nginx-1.1.2.tgz --> done -``` - -## The Chart Repository -A _Chart Repository_ is a place where _released copies_ of one or more charts may reside. A Helm Chart Repository is analogous to a Debian package repository or a Ruby Gems repository. It is a remote storage location accessible over a well-documented protocol (HTTP(S), and following fixed structural conventions. - - -Chart repositories are styled after the predominant pattern of [object storage systems](https://cloud.google.com/storage/docs/key-terms). A _domain_ hosts one or more _repositories_. A repository is a bucket in which one or more charts may be stored. In an object storage system, this is represented by the pattern: **https://domain/bucket/object**. In object storage, the _object_ part of the URL may contain slashes, hyphens, underscores, and dots. Thus in the URL [https://storage.googleapis.com/helm/nginx-1.1.2] the object is _nginx-1.1.2_. The general pattern of a chart repository is: https://domain/repository/chart - - -A chart name should be of the form _name-version.ext_, where _name_ is the chart name (alpha-numeric plus dash and underscore), and version is a SemVer v2 version. The extension, _ext_, should reflect the type of compression used to archive the object. This specification only discusses gzipped tar archives, but other mechanisms could be supported. - - -Because of the way object storage systems work, a repository should be viewable as a directory tree: - -``` -gs://kubernetes-charts/charts/ - apache-2.3.4.tgz - nginx-1.1.1.tgz - nginx-1.1.2.tgz - redis-0.4.0-alpha.1.tgz -``` - -A helm chart is a gzip-compressed tar archive (e.g. `tar -zcf …`). - -### Repository Protocol -A repository must implement HTTP(S) GET operations on both chart names (nginx-1.2.3) and chart signatures (nginx-1.2.3.sig). HTTP GET is a rational base level of functionality because it is well understood, nearly ubiquitous, and simple. - - -A repository may implement the full Object Storage APIs for compatibility with S3 and Google Cloud Storage. In this case, a client may detect this and use those APIs to perform create, read, update, delete, and list operations (as permissions allow) on the object storage system. - - -Object storage is a rational choice for this model because it is [optimized for highly resilient, distributed, highly available read-heavy traffic](https://en.wikipedia.org/wiki/Object_storage). The S3-like API has become a de facto standard, with myriad client libraries and tools, and major implementations both as services (S3, GCS), platforms (OpenStack Swift), and individual stand-alone servers (RiakCS, Minio, Ceph). - - -This document does not mandate a particular authentication mechanism, but implementors may implement the authentication token mechanisms on GCS and S3 compatible object storage systems. - -### Aside: Why a Flat Repository Namespace? -The format for a repository has packages stored in a flat namespace, with version information appended to the filename. There are a few reasons we opted for this over against more hierarchical directory structures: - - -* Keeping package name and version in the file name makes the experience easier for development. In cases where manual examination is necessary, tools and humans are working with filenames that are easily identifiable (_nginx-1.2.3.tgz_). -* The flat namespace means tooling needs to pay less attention to directory hierarchy, which has three positive implications: - * Less directory recursion, especially important for base operations like counting charts in a repo. - * No importance is granted to directories, which means we could introduce non-semantic directory hierarchy if necessary (e.g. for the GitHub model of _username/reponame_. - * For object storage systems, where slashes are part of names but with special meaning, the flat namespace reduces the number of API operations that need to be done, and allows for using some of the [object storage URL components](https://cloud.google.com/storage/docs/json_api/v1/) when necessary. -* Brevity is desirable when developers have occasion to type these URLs directly, as is done in chart/template references (see, for example, the [S3 API](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingObjects.html)). - -### Aside: Why Not Git(Hub) or VCS? -GitHub-backed repositories for released charts suffer from some severe limitations (listed below). Our repository design does not have these limitations. - - -* Versioning Multiple Things - * Git does not provide a good way of versioning independent objects in the same repo. (Two attempts we made at solving this [here](https://github.com/helm/helm/issues/199) and [here](https://github.com/deis/helm-dm/issues/1)) - * A release should be immutable, or very close to it. Achieving this in Git is hard if the thing released is not the entire repository. -* Developer Experience - * Combining release artifacts and source code causes confusion - * Substantial development workflow overhead (and over-reliance on conventions) make directory-based versioning problematic - * In most of the models specified, there is no way of determining whether a resource is in development, or is complete -* Infrastructure - * Both teams have hit GitHub API rate limiting - * Git is optimized for fetching an entire repository, not a small fraction of the repository - * Git is optimized for fetching an entire history, not a snapshot of a particular time - * Discovery of which versions are available is exogenous to Git itself (unless we use a crazy tagging mechanism) - * We hit vendor lock-in for GitHub - - -The object storage based repository solution above solves all of these problems by: - - -* Explicitly defining a release artifact as an archive file with a known format and a known naming convention. -* Explicitly defining a release as an act of archiving, naming, and signing. -* Selecting a service (object storage, but with fallback to plain HTTP) that is resilient, widely deployed, and built specifically for the delivery of release files. -* Distinguishing the development workflow from the release workflow at a location that is intuitive and common for developers -* Providing a method for verifying immutability of a version by checksum -* Removing vendor reliance -* Separating the concept of code from the concept of release artifact - - -We want to make clear that a chart may be developed in any place (such as Github repositories). This aside is referring to released and versioned charts that can be stored and shared. We do not dictate where a chart is developed. - -## Chart References: Long form, short form, and local reference -There are three reference forms for a chart. A fully qualified (long) form, a mnemonic (short) form, and a local path spec. -### Long Form -A long form chart reference is an exact URL to the chart. It should use HTTP or HTTPS protocols, and follow the typical rules of a complete URL: https://example.com/charts/nginx-1.2.3.tgz - - -A long form reference must include a protocol spec. It may also contain any pattern allowed in URLs. - - -The following URI schemes must be supported: -* http: -* https: - - -#### Special Form Google Storage and S3 -The [Google Storage scheme](https://cloud.google.com/storage/docs/interoperability) (`gs://`) and the S3 scheme (`s3://`) can also be supported. These forms do not follow the URL standard (bucket name is placed in the host field), but their behavior is well-documented. - - -Examples: -``` -gs://charts/nginx-1.2.3.tgz -s3://charts/nginx-1.2.3.tgz -``` -### Short Form -A short form package reference provides the minimum necessary information for accessing a chart. Short form URLs use a defined [URI scheme](https://tools.ietf.org/html/rfc3986#section-3.1) of `helm`:. - - -A generic short form maps to an object storage reference (`DOMAIN/REPOSITORY/RELEASE`). - -``` -helm:example.com/charts/nginx-1.2.3.tgz -``` - -Or, in the case of the Google Storage (`gs://`) and the S3 scheme (`s3://`), the domain indicates the storage scheme. - -``` -helm:gs/kubernetes-charts/nginx-1.2.3.tgz -``` - -For the purpose of providing versioning ranges, and also for backward compatibility, version requirements are expressed as a suffix condition, instead of as part of the path: - -``` -helm:example.com/charts/nginx#1.2.3 // Exact version -helm:example.com/charts/nginx#~2.1 // Version range -helm:example.com/charts/nginx // Latest release -``` - -The first of the above three short names is equivalent to helm:example.com/charts/nginx-1.2.3.tgz. The second example uses a semantic version filter ~1.2, which means “>=1.2.0, <1.3.0”, or “any patch version of 1.2”. Other filters include the “^” operator (^1 is “any minor version in the 1.x line), and “>”, “>=”, “=”, “<”, and “<=” operators. These are standard SemVer filter operators. - - -Any short form handler should be able to resolve the default short form as specified above. - - -### Local References -During chart development, and in other special circumstances, it may be desirable to work with an unversioned, unpackaged local copy of a chart. For the sake of consistency across products, an explicit naming formula is to be followed. - - -In cases where a local path is used in lieu of a full or short name, the path string _must_ begin with either a dot (.) or a slash (/) or the file schema(`file:`). - - -Legal examples of this include: -``` - ./foo - ../foo - /foo - //foo - /.foo - file:///example/foo -``` - -Unprefixed relative paths are not valid. For example, `foo/` is not allowed as a local path, as it conflicts with a legal short name, and is thus ambiguous. - - -## References - -The Debian Package Repo. [http://ftp.us.debian.org/debian/pool/main/h/] - -The Debian Maintainers Guide. [https://www.debian.org/doc/manuals/maint-guide/] - -Arch packages: [https://wiki.archlinux.org/index.php/Arch_User_Repository#Creating_a_new_package] - -Keybase.io: [https://keybase.io/] - -Google Cloud Storage API: [https://cloud.google.com/storage/docs/json_api/v1/] - -Amazon S3: [http://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html] - -URIs (RFC 3986): [https://tools.ietf.org/html/rfc3986#section-3.1] diff --git a/docs/design/design.md b/docs/design/design.md deleted file mode 100644 index 290bdceed..000000000 --- a/docs/design/design.md +++ /dev/null @@ -1,434 +0,0 @@ -# Deployment Manager Design - -## Overview - -Deployment Manager (DM) is a service that runs in a Kubernetes cluster, -supported by a command line interface. It provides a declarative `YAML`-based -language for configuring Kubernetes resources, and a mechanism for deploying, -updating, and deleting configurations. This document describes the configuration -language, the API model, and the service architecture in detail. - -## Configuration Language - -DM uses a `YAML`-based configuration language with a templating mechanism. A -configuration is a `YAML` file that describes a list of resources. A resource has -three properties: - -* `name`: the name to use when managing the resource -* `type`: the type of the resource being configured -* `properties`: the configuration properties of the resource - -Here's a snippet from a typical configuration file: - -``` -resources: -- name: my-rc - type: ReplicationController - properties: - metadata: - name: my-rc - spec: - replicas: 1 - ... -- name: my-service - type: Service - properties: - ... -``` - -It describes two resources: - -* A replication controller named `my-rc`, and -* A service named `my-service` - -## Types - -Resource types are either primitives or templates. - -### Primitives - -Primitives are types implemented by the Kubernetes runtime, such as: - -* `Pod` -* `ReplicationController` -* `Service` -* `Namespace` -* `Secret` - -DM processes primitive resources by passing their properties directly to -`kubectl` to create, update, or delete the corresponding objects in the cluster. - -(Note that DM runs `kubectl` server side, in a container.) - -### Templates - -Templates are abstract types created using Python or -[Jinja](http://jinja.pocoo.org/). A template takes a set of properties as input, -and must output a valid `YAML` configuration. Properties are bound to values when -a template is instantiated by a configuration. - -Templates are expanded before primitive resources are processed. The -configuration produced by expanding a template may contain primitive resources -and/or additional template invocations. All template invocations are expanded -recursively until the resulting configuration is a list of primitive resources. - -(Note, however, that DM preserves the template hierarchy and any dependencies -between resources in a layout that can be used to reason programmatically about -the structure of the resulting collection of resources created in the cluster, -as described in greater detail below.) - -Here's an example of a template written in [Jinja](http://jinja.pocoo.org/): - -``` -resources: -- name: {{ env['name'] }}-service - type: Service - properties: - prop1: {{ properties['prop1'] }} - ... -``` - -As you can see, it's just a `YAML` file that contains expansion directives. For -more information about the kinds of things you can do in a Jinja based template, -see [the Jina documentation](http://jinja.pocoo.org/docs/). - -Here's an example of a template written in Python: - -``` -import yaml - -def GenerateConfig(context): - resources = [{ - 'name': context.env['name'] + '-service', - 'type': 'Service', - 'properties': { - 'prop1': context.properties['prop1'], - ... - } - }] - - return yaml.dump({'resources': resources}) -``` - -Of course, you can do a lot more in Python than in Jinja, but basic things, such -as simple parameter substitution, may be easier to implement and easier to read in -Jinja than in Python. - -Templates provide access to multiple sets of data that can be used to -parameterize or further customize configurations: - -* `env`: a map of key/value pairs from the environment, including pairs -defined by Deployment Manager, such as `deployment`, `name`, and `type` -* `properties`: a map of the key/value pairs passed in the properties section -of the template invocation -* `imports`: a map of import file names to file contents for all imports -originally specified for the configuration - -In Jinja, these variables are available in the global scope. In Python, they are -available as properties of the `context` object passed into the `GenerateConfig` -method. - -### Template schemas - -A template can optionally be accompanied by a schema that describes it in more -detail, including: - -* `info`: more information about the template, including long description and title -* `imports`: any files imported by this template (may be relative paths or URLs) -* `required`: properties that must have values when the template is expanded -* `properties`: A `JSON Schema` description of each property the template accepts - -Here's an example of a template schema: - -``` -info: - title: The Example - description: A template being used as an example to illustrate concepts. - -imports: -- path: helper.py - -required: -- prop1 - -properties: - prop1: - description: The first property - type: string - default: prop-value -``` - -When a schema is provided for a template, DM uses it to validate properties -passed to the template by its invocation, and to provide default values for -properties that were not given values. - -Schemas must be supplied to DM along with the templates they describe. - -### Supplying templates - -Templates can be supplied to DM in two different ways: - -* They can be passed to DM along with configurations that import them, or -* They can be retrieved by DM from public HTTP endpoints for configurations that -reference them. - -#### Template imports - -Configurations can import templates using path declarations. For example: - -``` -imports: -- path: example.py - -resources: -- name: example - type: example.py - properties: - prop1: prop-value -``` - -The `imports` list is not understood by the Deployment Manager service. -It's a directive used by client-side tools to specify what additional files -should be included when passing the configuration to the API. - -If you are calling the Deployment Manager service directly, you must embed the -imported templates in the configuration passed to the API. - -#### Template references - -Configurations can also reference templates using URLs for public HTTP endpoints. -DM will attempt to resolve template references during expansion. For example: - -``` -resources: -- name: my-template - type: https://raw.githubusercontent.com/my-template/my-template.py - properties: - prop1: prop-value -``` - -When resolving template references, DM assumes that templates are stored in -directories, which may also contain schemas, examples and other supporting files. -It therefore processes template references as follows: - -1. Attempt to fetch the template, and treat it as an import. -1. Attempt to fetch the schema for the template from -`/