mirror of https://github.com/helm/helm
parent
9654e63d19
commit
9777e72692
@ -0,0 +1,4 @@
|
|||||||
|
resources:
|
||||||
|
- name: cassandra
|
||||||
|
type: github.com/helm/charts/cassandra
|
||||||
|
properties: null
|
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
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 registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/go-github/github"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GithubPackageRegistry implements the Registry interface that talks to github and
|
||||||
|
// expects packages in helm format without versioning and no qualifier in the path.
|
||||||
|
// Format of the directory for a type is like so:
|
||||||
|
// package/
|
||||||
|
// Chart.yaml
|
||||||
|
// manifests/
|
||||||
|
// foo.yaml
|
||||||
|
// bar.yaml
|
||||||
|
// ...
|
||||||
|
type GithubPackageRegistry struct {
|
||||||
|
owner string
|
||||||
|
repository string
|
||||||
|
client *github.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGithubRegistry creates a Registry that can be used to talk to github.
|
||||||
|
func NewGithubPackageRegistry(owner, repository string) *GithubPackageRegistry {
|
||||||
|
return &GithubPackageRegistry{
|
||||||
|
owner: owner,
|
||||||
|
repository: repository,
|
||||||
|
client: github.NewClient(nil),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// List the types from the Registry.
|
||||||
|
// TODO(vaikas): Figure out how the versions work here.
|
||||||
|
func (g *GithubPackageRegistry) List() ([]Type, error) {
|
||||||
|
// Just list all the types at the top level.
|
||||||
|
types, err := g.getDirs("")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to list templates: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var retTypes []Type
|
||||||
|
for _, t := range types {
|
||||||
|
// Check to see if there's a Chart.yaml file in the directory
|
||||||
|
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, t, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to list package files at path: %s: %v", t, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, f := range dc {
|
||||||
|
if *f.Type == "file" && *f.Name == "Chart.yaml" {
|
||||||
|
retTypes = append(retTypes, Type{Name: t})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return retTypes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetURLs fetches the download URLs for a given Type.
|
||||||
|
func (g *GithubPackageRegistry) GetURLs(t Type) ([]string, error) {
|
||||||
|
path, err := g.MakeRepositoryPath(t)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, path, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to list package files at path: %s: %v", path, err)
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
downloadURLs := []string{}
|
||||||
|
for _, f := range dc {
|
||||||
|
if *f.Type == "file" {
|
||||||
|
if strings.HasSuffix(*f.Name, ".yaml") {
|
||||||
|
downloadURLs = append(downloadURLs, *f.DownloadURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return downloadURLs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GithubPackageRegistry) getDirs(dir string) ([]string, error) {
|
||||||
|
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, dir, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to get contents at path: %s: %v", dir, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var dirs []string
|
||||||
|
for _, entry := range dc {
|
||||||
|
if *entry.Type == "dir" {
|
||||||
|
dirs = append(dirs, *entry.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dirs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeRepositoryPath constructs a github path to a given type based on a repository, and type name.
|
||||||
|
// The returned repository path will be of the form:
|
||||||
|
// Type.Name/manifests
|
||||||
|
func (g *GithubPackageRegistry) MakeRepositoryPath(t Type) (string, error) {
|
||||||
|
// Construct the return path
|
||||||
|
return t.Name + "/manifests", nil
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
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 util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
|
"github.com/kubernetes/deployment-manager/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseKubernetesObject(object []byte) (*common.Resource, error) {
|
||||||
|
o := &common.KubernetesObject{}
|
||||||
|
if err := yaml.Unmarshal(object, &o); err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot unmarshal native kubernetes object (%#v)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ok, it appears to be a valid object, create a Resource out of it.
|
||||||
|
r := &common.Resource{}
|
||||||
|
r.Name = getRandomName(o.Metadata["name"].(string))
|
||||||
|
r.Type = o.Kind
|
||||||
|
|
||||||
|
r.Properties = make(map[string]interface{})
|
||||||
|
if err := yaml.Unmarshal(object, &r.Properties); err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot unmarshal native kubernetes object (%#v)", err)
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRandomName(prefix string) string {
|
||||||
|
return fmt.Sprintf("%s-%d", prefix, time.Now().UTC().UnixNano())
|
||||||
|
}
|
@ -0,0 +1,164 @@
|
|||||||
|
/*
|
||||||
|
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 util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
|
|
||||||
|
"github.com/kubernetes/deployment-manager/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var serviceInput = `
|
||||||
|
kind: "Service"
|
||||||
|
apiVersion: "v1"
|
||||||
|
metadata:
|
||||||
|
name: "mock"
|
||||||
|
labels:
|
||||||
|
app: "mock"
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
-
|
||||||
|
protocol: "TCP"
|
||||||
|
port: 99
|
||||||
|
targetPort: 9949
|
||||||
|
selector:
|
||||||
|
app: "mock"
|
||||||
|
`
|
||||||
|
|
||||||
|
var serviceExpected = `
|
||||||
|
name: mock
|
||||||
|
type: Service
|
||||||
|
properties:
|
||||||
|
kind: "Service"
|
||||||
|
apiVersion: "v1"
|
||||||
|
metadata:
|
||||||
|
name: "mock"
|
||||||
|
labels:
|
||||||
|
app: "mock"
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
-
|
||||||
|
protocol: "TCP"
|
||||||
|
port: 99
|
||||||
|
targetPort: 9949
|
||||||
|
selector:
|
||||||
|
app: "mock"
|
||||||
|
`
|
||||||
|
|
||||||
|
var rcInput = `
|
||||||
|
kind: "ReplicationController"
|
||||||
|
apiVersion: "v1"
|
||||||
|
metadata:
|
||||||
|
name: "mockname"
|
||||||
|
labels:
|
||||||
|
app: "mockapp"
|
||||||
|
foo: "bar"
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
app: "mockapp"
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: "mocklabel"
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
-
|
||||||
|
name: "mock-container"
|
||||||
|
image: "kubernetes/pause"
|
||||||
|
ports:
|
||||||
|
-
|
||||||
|
containerPort: 9949
|
||||||
|
protocol: "TCP"
|
||||||
|
-
|
||||||
|
containerPort: 9949
|
||||||
|
protocol: "TCP"
|
||||||
|
`
|
||||||
|
|
||||||
|
var rcExpected = `
|
||||||
|
name: mockname
|
||||||
|
type: ReplicationController
|
||||||
|
properties:
|
||||||
|
kind: "ReplicationController"
|
||||||
|
apiVersion: "v1"
|
||||||
|
metadata:
|
||||||
|
name: "mockname"
|
||||||
|
labels:
|
||||||
|
app: "mockapp"
|
||||||
|
foo: "bar"
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
app: "mockapp"
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: "mocklabel"
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
-
|
||||||
|
name: "mock-container"
|
||||||
|
image: "kubernetes/pause"
|
||||||
|
ports:
|
||||||
|
-
|
||||||
|
containerPort: 9949
|
||||||
|
protocol: "TCP"
|
||||||
|
-
|
||||||
|
containerPort: 9949
|
||||||
|
protocol: "TCP"
|
||||||
|
`
|
||||||
|
|
||||||
|
func unmarshalResource(t *testing.T, object []byte) (*common.Resource, error) {
|
||||||
|
r := &common.Resource{}
|
||||||
|
if err := yaml.Unmarshal([]byte(object), &r); err != nil {
|
||||||
|
t.Errorf("cannot unmarshal test object (%#v)", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConversion(t *testing.T, object []byte, expected []byte) {
|
||||||
|
e, err := unmarshalResource(t, expected)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to unmarshal expected Resource: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := ParseKubernetesObject(object)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseKubernetesObject failed: %v")
|
||||||
|
}
|
||||||
|
// Since the object name gets created on the fly, we have to rejigger the returned object
|
||||||
|
// slightly to make sure the DeepEqual works as expected.
|
||||||
|
// First validate the name matches the expected format.
|
||||||
|
var i int
|
||||||
|
format := e.Name + "-%d"
|
||||||
|
count, err := fmt.Sscanf(result.Name, format, &i)
|
||||||
|
if err != nil || count != 1 {
|
||||||
|
t.Errorf("Name is not as expected, wanted of the form %s got %s", format, result.Name)
|
||||||
|
}
|
||||||
|
e.Name = result.Name
|
||||||
|
if !reflect.DeepEqual(result, e) {
|
||||||
|
t.Errorf("expected %+v but found %+v", e, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimple(t *testing.T) {
|
||||||
|
testConversion(t, []byte(rcInput), []byte(rcExpected))
|
||||||
|
testConversion(t, []byte(serviceInput), []byte(serviceExpected))
|
||||||
|
}
|
Loading…
Reference in new issue