@ -19,10 +19,10 @@ package helm // import "k8s.io/helm/pkg/helm"
import (
"errors"
"fmt"
" strings "
" math/rand "
"sync"
"github.com/ technosophos/moniker "
"github.com/ golang/protobuf/ptypes/timestamp "
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/proto/hapi/release"
rls "k8s.io/helm/pkg/proto/hapi/services"
@ -56,32 +56,19 @@ func (c *FakeClient) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesR
return resp , nil
}
// InstallRelease returns a response with the first Release on the fake release client
// InstallRelease creates a new release and returns a InstallReleaseResponse containing that release
func ( c * FakeClient ) InstallRelease ( chStr , ns string , opts ... InstallOption ) ( * rls . InstallReleaseResponse , error ) {
chart := & chart . Chart { }
return c . InstallReleaseFromChart ( chart , ns , opts ... )
}
// InstallReleaseFromChart returns a response with the first Release on the fake release client
// InstallReleaseFromChart adds a new MockRelease to the fake client and returns a InstallReleaseResponse containing that release
func ( c * FakeClient ) InstallReleaseFromChart ( chart * chart . Chart , ns string , opts ... InstallOption ) ( * rls . InstallReleaseResponse , error ) {
for _ , opt := range opts {
opt ( & c . Opts )
}
releaseName := c . Opts . instReq . Name
if releaseName == "" {
for i := 0 ; i < 5 ; i ++ {
namer := moniker . New ( )
name := namer . NameSep ( "-" )
if len ( name ) > 53 {
name = name [ : 53 ]
}
if _ , err := c . ReleaseStatus ( name , nil ) ; strings . Contains ( err . Error ( ) , "No such release" ) {
releaseName = name
break
}
}
}
// Check to see if the release already exists.
rel , err := c . ReleaseStatus ( releaseName , nil )
@ -89,11 +76,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts
return nil , errors . New ( "cannot re-use a name that is still in use" )
}
release := & release . Release {
Name : releaseName ,
Namespace : ns ,
}
release := ReleaseMock ( & MockReleaseOptions { Name : releaseName , Namespace : ns } )
c . Rels = append ( c . Rels , release )
return & rls . InstallReleaseResponse {
@ -101,7 +84,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts
} , nil
}
// DeleteRelease returns nil, nil
// DeleteRelease deletes a release from the FakeClient
func ( c * FakeClient ) DeleteRelease ( rlsName string , opts ... DeleteOption ) ( * rls . UninstallReleaseResponse , error ) {
for i , rel := range c . Rels {
if rel . Name == rlsName {
@ -124,12 +107,12 @@ func (c *FakeClient) GetVersion(opts ...VersionOption) (*rls.GetVersionResponse,
} , nil
}
// UpdateRelease returns nil, nil
// UpdateRelease returns an UpdateReleaseResponse containing the updated release, if it exists
func ( c * FakeClient ) UpdateRelease ( rlsName string , chStr string , opts ... UpdateOption ) ( * rls . UpdateReleaseResponse , error ) {
return c . UpdateReleaseFromChart ( rlsName , & chart . Chart { } , opts ... )
}
// UpdateReleaseFromChart returns nil, nil
// UpdateReleaseFromChart returns an UpdateReleaseResponse containing the updated release, if it exists
func ( c * FakeClient ) UpdateReleaseFromChart ( rlsName string , chart * chart . Chart , opts ... UpdateOption ) ( * rls . UpdateReleaseResponse , error ) {
// Check to see if the release already exists.
rel , err := c . ReleaseContent ( rlsName , nil )
@ -145,7 +128,7 @@ func (c *FakeClient) RollbackRelease(rlsName string, opts ...RollbackOption) (*r
return nil , nil
}
// ReleaseStatus returns a release status response with info from the matching release name in the fake release client .
// ReleaseStatus returns a release status response with info from the matching release name .
func ( c * FakeClient ) ReleaseStatus ( rlsName string , opts ... StatusOption ) ( * rls . GetReleaseStatusResponse , error ) {
for _ , rel := range c . Rels {
if rel . Name == rlsName {
@ -200,3 +183,90 @@ func (c *FakeClient) RunReleaseTest(rlsName string, opts ...ReleaseTestOption) (
return results , errc
}
// MockHookTemplate is the hook template used for all mock release objects.
var MockHookTemplate = ` apiVersion : v1
kind : Job
metadata :
annotations :
"helm.sh/hooks" : pre - install
`
// MockManifest is the manifest used for all mock release objects.
var MockManifest = ` apiVersion : v1
kind : Secret
metadata :
name : fixture
`
// MockReleaseOptions allows for user-configurable options on mock release objects.
type MockReleaseOptions struct {
Name string
Version int32
Chart * chart . Chart
StatusCode release . Status_Code
Namespace string
}
// ReleaseMock creates a mock release object based on options set by MockReleaseOptions. This function should typically not be used outside of testing.
func ReleaseMock ( opts * MockReleaseOptions ) * release . Release {
date := timestamp . Timestamp { Seconds : 242085845 , Nanos : 0 }
name := opts . Name
if name == "" {
name = "testrelease-" + string ( rand . Intn ( 100 ) )
}
var version int32 = 1
if opts . Version != 0 {
version = opts . Version
}
namespace := opts . Namespace
if namespace == "" {
namespace = "default"
}
ch := opts . Chart
if opts . Chart == nil {
ch = & chart . Chart {
Metadata : & chart . Metadata {
Name : "foo" ,
Version : "0.1.0-beta.1" ,
} ,
Templates : [ ] * chart . Template {
{ Name : "templates/foo.tpl" , Data : [ ] byte ( MockManifest ) } ,
} ,
}
}
scode := release . Status_DEPLOYED
if opts . StatusCode > 0 {
scode = opts . StatusCode
}
return & release . Release {
Name : name ,
Info : & release . Info {
FirstDeployed : & date ,
LastDeployed : & date ,
Status : & release . Status { Code : scode } ,
Description : "Release mock" ,
} ,
Chart : ch ,
Config : & chart . Config { Raw : ` name: "value" ` } ,
Version : version ,
Namespace : namespace ,
Hooks : [ ] * release . Hook {
{
Name : "pre-install-hook" ,
Kind : "Job" ,
Path : "pre-install-hook.yaml" ,
Manifest : MockHookTemplate ,
LastRun : & date ,
Events : [ ] release . Hook_Event { release . Hook_PRE_INSTALL } ,
} ,
} ,
Manifest : MockManifest ,
}
}