Commit 37a731db authored by Fabian Ruff's avatar Fabian Ruff Committed by Matthew Fisher

Avoid importing k8s.io/kubernetes from pkg/helm (#4499)

* Avoid importing k8s.io/kubernetes from pkg/helm

When writing a helm client (e.g. a helm plugin) that talks to tiller importing k8s.io/helm/pkg/helm to get the grpc client is key.
This pkg should not have a dependency to the k8s.io/kubernetes to avoid pulling in a lot of code that is only used within tiller and blow up binary sizes.
Signed-off-by: 's avatarFabian Ruff <fabian@progra.de>

* Add references to pull request in errors message
Signed-off-by: 's avatarFabian Ruff <fabian@progra.de>

* copy helper function from pkg/storage/driver
Signed-off-by: 's avatarFabian Ruff <fabian@progra.de>

* Move storage errors to seperate package
Signed-off-by: 's avatarFabian Ruff <fabian@progra.de>

* Keep old  error variables for backward compatibility
Signed-off-by: 's avatarFabian Ruff <fabian@progra.de>
parent 2e9855b9
...@@ -26,7 +26,7 @@ import ( ...@@ -26,7 +26,7 @@ import (
"k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/renderutil" "k8s.io/helm/pkg/renderutil"
"k8s.io/helm/pkg/storage/driver" storageerrors "k8s.io/helm/pkg/storage/errors"
) )
const upgradeDesc = ` const upgradeDesc = `
...@@ -207,7 +207,7 @@ func (u *upgradeCmd) run() error { ...@@ -207,7 +207,7 @@ func (u *upgradeCmd) run() error {
} }
} }
if err != nil && strings.Contains(err.Error(), driver.ErrReleaseNotFound(u.release).Error()) { if err != nil && strings.Contains(err.Error(), storageerrors.ErrReleaseNotFound(u.release).Error()) {
fmt.Fprintf(u.out, "Release %q does not exist. Installing it now.\n", u.release) fmt.Fprintf(u.out, "Release %q does not exist. Installing it now.\n", u.release)
ic := &installCmd{ ic := &installCmd{
chartPath: chartPath, chartPath: chartPath,
......
...@@ -31,7 +31,7 @@ import ( ...@@ -31,7 +31,7 @@ import (
rls "k8s.io/helm/pkg/proto/hapi/services" rls "k8s.io/helm/pkg/proto/hapi/services"
"k8s.io/helm/pkg/proto/hapi/version" "k8s.io/helm/pkg/proto/hapi/version"
"k8s.io/helm/pkg/renderutil" "k8s.io/helm/pkg/renderutil"
storage "k8s.io/helm/pkg/storage/driver" storageerrors "k8s.io/helm/pkg/storage/errors"
) )
// FakeClient implements Interface // FakeClient implements Interface
...@@ -138,7 +138,7 @@ func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.U ...@@ -138,7 +138,7 @@ func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.U
} }
} }
return nil, storage.ErrReleaseNotFound(rlsName) return nil, storageerrors.ErrReleaseNotFound(rlsName)
} }
// GetVersion returns a fake version // GetVersion returns a fake version
...@@ -212,7 +212,7 @@ func (c *FakeClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.G ...@@ -212,7 +212,7 @@ func (c *FakeClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.G
}, nil }, nil
} }
} }
return nil, storage.ErrReleaseNotFound(rlsName) return nil, storageerrors.ErrReleaseNotFound(rlsName)
} }
// ReleaseContent returns the configuration for the matching release name in the fake release client. // ReleaseContent returns the configuration for the matching release name in the fake release client.
...@@ -224,7 +224,7 @@ func (c *FakeClient) ReleaseContent(rlsName string, opts ...ContentOption) (resp ...@@ -224,7 +224,7 @@ func (c *FakeClient) ReleaseContent(rlsName string, opts ...ContentOption) (resp
}, nil }, nil
} }
} }
return resp, storage.ErrReleaseNotFound(rlsName) return resp, storageerrors.ErrReleaseNotFound(rlsName)
} }
// ReleaseHistory returns a release's revision history. // ReleaseHistory returns a release's revision history.
......
...@@ -18,8 +18,10 @@ package helm // import "k8s.io/helm/pkg/helm" ...@@ -18,8 +18,10 @@ package helm // import "k8s.io/helm/pkg/helm"
import ( import (
"errors" "errors"
"os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
...@@ -361,3 +363,15 @@ func loadChart(t *testing.T, name string) *cpb.Chart { ...@@ -361,3 +363,15 @@ func loadChart(t *testing.T, name string) *cpb.Chart {
} }
return c return c
} }
func TestDoesNotImportKubernetes(t *testing.T) {
cmd := exec.Command("go", "list", "-f", "{{.Deps}}", ".")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to execute %s %s: %s", cmd.Path, strings.Join(cmd.Args, " "), err)
}
if strings.Contains(string(output), "k8s.io/kubernetes") {
t.Fatal("k8s.io/helm/pkg/helm contains a dependency on k8s.io/kubernetes. See https://github.com/helm/helm/pull/4499 for more details.")
}
}
...@@ -30,6 +30,7 @@ import ( ...@@ -30,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
rspb "k8s.io/helm/pkg/proto/hapi/release" rspb "k8s.io/helm/pkg/proto/hapi/release"
storageerrors "k8s.io/helm/pkg/storage/errors"
) )
var _ Driver = (*ConfigMaps)(nil) var _ Driver = (*ConfigMaps)(nil)
...@@ -65,7 +66,7 @@ func (cfgmaps *ConfigMaps) Get(key string) (*rspb.Release, error) { ...@@ -65,7 +66,7 @@ func (cfgmaps *ConfigMaps) Get(key string) (*rspb.Release, error) {
obj, err := cfgmaps.impl.Get(key, metav1.GetOptions{}) obj, err := cfgmaps.impl.Get(key, metav1.GetOptions{})
if err != nil { if err != nil {
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return nil, ErrReleaseNotFound(key) return nil, storageerrors.ErrReleaseNotFound(key)
} }
cfgmaps.Log("get: failed to get %q: %s", key, err) cfgmaps.Log("get: failed to get %q: %s", key, err)
...@@ -131,7 +132,7 @@ func (cfgmaps *ConfigMaps) Query(labels map[string]string) ([]*rspb.Release, err ...@@ -131,7 +132,7 @@ func (cfgmaps *ConfigMaps) Query(labels map[string]string) ([]*rspb.Release, err
} }
if len(list.Items) == 0 { if len(list.Items) == 0 {
return nil, ErrReleaseNotFound(labels["NAME"]) return nil, storageerrors.ErrReleaseNotFound(labels["NAME"])
} }
var results []*rspb.Release var results []*rspb.Release
...@@ -164,7 +165,7 @@ func (cfgmaps *ConfigMaps) Create(key string, rls *rspb.Release) error { ...@@ -164,7 +165,7 @@ func (cfgmaps *ConfigMaps) Create(key string, rls *rspb.Release) error {
// push the configmap object out into the kubiverse // push the configmap object out into the kubiverse
if _, err := cfgmaps.impl.Create(obj); err != nil { if _, err := cfgmaps.impl.Create(obj); err != nil {
if apierrors.IsAlreadyExists(err) { if apierrors.IsAlreadyExists(err) {
return ErrReleaseExists(key) return storageerrors.ErrReleaseExists(key)
} }
cfgmaps.Log("create: failed to create: %s", err) cfgmaps.Log("create: failed to create: %s", err)
...@@ -202,7 +203,7 @@ func (cfgmaps *ConfigMaps) Delete(key string) (rls *rspb.Release, err error) { ...@@ -202,7 +203,7 @@ func (cfgmaps *ConfigMaps) Delete(key string) (rls *rspb.Release, err error) {
// fetch the release to check existence // fetch the release to check existence
if rls, err = cfgmaps.Get(key); err != nil { if rls, err = cfgmaps.Get(key); err != nil {
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return nil, ErrReleaseExists(rls.Name) return nil, storageerrors.ErrReleaseExists(rls.Name)
} }
cfgmaps.Log("delete: failed to get release %q: %s", key, err) cfgmaps.Log("delete: failed to get release %q: %s", key, err)
......
...@@ -17,18 +17,17 @@ limitations under the License. ...@@ -17,18 +17,17 @@ limitations under the License.
package driver // import "k8s.io/helm/pkg/storage/driver" package driver // import "k8s.io/helm/pkg/storage/driver"
import ( import (
"fmt"
rspb "k8s.io/helm/pkg/proto/hapi/release" rspb "k8s.io/helm/pkg/proto/hapi/release"
storageerrors "k8s.io/helm/pkg/storage/errors"
) )
var ( var (
// ErrReleaseNotFound indicates that a release is not found. // ErrReleaseNotFound has been deprecated; please use storageerrors.ErrReleaseNotFound instead.
ErrReleaseNotFound = func(release string) error { return fmt.Errorf("release: %q not found", release) } ErrReleaseNotFound = storageerrors.ErrReleaseNotFound
// ErrReleaseExists indicates that a release already exists. // ErrReleaseExists has been deprecated; please use storageerrors.ErrReleaseExists instead.
ErrReleaseExists = func(release string) error { return fmt.Errorf("release: %q already exists", release) } ErrReleaseExists = storageerrors.ErrReleaseExists
// ErrInvalidKey indicates that a release key could not be parsed. // ErrInvalidKey has been deprecated; please use storageerrors.ErrInvalidKey instead.
ErrInvalidKey = func(release string) error { return fmt.Errorf("release: %q invalid key", release) } ErrInvalidKey = storageerrors.ErrInvalidKey
) )
// Creator is the interface that wraps the Create method. // Creator is the interface that wraps the Create method.
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
"sync" "sync"
rspb "k8s.io/helm/pkg/proto/hapi/release" rspb "k8s.io/helm/pkg/proto/hapi/release"
storageerrors "k8s.io/helm/pkg/storage/errors"
) )
var _ Driver = (*Memory)(nil) var _ Driver = (*Memory)(nil)
...@@ -53,16 +54,16 @@ func (mem *Memory) Get(key string) (*rspb.Release, error) { ...@@ -53,16 +54,16 @@ func (mem *Memory) Get(key string) (*rspb.Release, error) {
case 2: case 2:
name, ver := elems[0], elems[1] name, ver := elems[0], elems[1]
if _, err := strconv.Atoi(ver); err != nil { if _, err := strconv.Atoi(ver); err != nil {
return nil, ErrInvalidKey(key) return nil, storageerrors.ErrInvalidKey(key)
} }
if recs, ok := mem.cache[name]; ok { if recs, ok := mem.cache[name]; ok {
if r := recs.Get(key); r != nil { if r := recs.Get(key); r != nil {
return r.rls, nil return r.rls, nil
} }
} }
return nil, ErrReleaseNotFound(key) return nil, storageerrors.ErrReleaseNotFound(key)
default: default:
return nil, ErrInvalidKey(key) return nil, storageerrors.ErrInvalidKey(key)
} }
} }
...@@ -131,7 +132,7 @@ func (mem *Memory) Update(key string, rls *rspb.Release) error { ...@@ -131,7 +132,7 @@ func (mem *Memory) Update(key string, rls *rspb.Release) error {
rs.Replace(key, newRecord(key, rls)) rs.Replace(key, newRecord(key, rls))
return nil return nil
} }
return ErrReleaseNotFound(rls.Name) return storageerrors.ErrReleaseNotFound(rls.Name)
} }
// Delete deletes a release or returns ErrReleaseNotFound. // Delete deletes a release or returns ErrReleaseNotFound.
...@@ -141,12 +142,12 @@ func (mem *Memory) Delete(key string) (*rspb.Release, error) { ...@@ -141,12 +142,12 @@ func (mem *Memory) Delete(key string) (*rspb.Release, error) {
elems := strings.Split(key, ".v") elems := strings.Split(key, ".v")
if len(elems) != 2 { if len(elems) != 2 {
return nil, ErrInvalidKey(key) return nil, storageerrors.ErrInvalidKey(key)
} }
name, ver := elems[0], elems[1] name, ver := elems[0], elems[1]
if _, err := strconv.Atoi(ver); err != nil { if _, err := strconv.Atoi(ver); err != nil {
return nil, ErrInvalidKey(key) return nil, storageerrors.ErrInvalidKey(key)
} }
if recs, ok := mem.cache[name]; ok { if recs, ok := mem.cache[name]; ok {
if r := recs.Remove(key); r != nil { if r := recs.Remove(key); r != nil {
...@@ -155,7 +156,7 @@ func (mem *Memory) Delete(key string) (*rspb.Release, error) { ...@@ -155,7 +156,7 @@ func (mem *Memory) Delete(key string) (*rspb.Release, error) {
return r.rls, nil return r.rls, nil
} }
} }
return nil, ErrReleaseNotFound(key) return nil, storageerrors.ErrReleaseNotFound(key)
} }
// wlock locks mem for writing // wlock locks mem for writing
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
rspb "k8s.io/helm/pkg/proto/hapi/release" rspb "k8s.io/helm/pkg/proto/hapi/release"
storageerrors "k8s.io/helm/pkg/storage/errors"
) )
// records holds a list of in-memory release records // records holds a list of in-memory release records
...@@ -38,7 +39,7 @@ func (rs *records) Add(r *record) error { ...@@ -38,7 +39,7 @@ func (rs *records) Add(r *record) error {
} }
if rs.Exists(r.key) { if rs.Exists(r.key) {
return ErrReleaseExists(r.key) return storageerrors.ErrReleaseExists(r.key)
} }
*rs = append(*rs, r) *rs = append(*rs, r)
......
...@@ -30,6 +30,7 @@ import ( ...@@ -30,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
rspb "k8s.io/helm/pkg/proto/hapi/release" rspb "k8s.io/helm/pkg/proto/hapi/release"
storageerrors "k8s.io/helm/pkg/storage/errors"
) )
var _ Driver = (*Secrets)(nil) var _ Driver = (*Secrets)(nil)
...@@ -65,7 +66,7 @@ func (secrets *Secrets) Get(key string) (*rspb.Release, error) { ...@@ -65,7 +66,7 @@ func (secrets *Secrets) Get(key string) (*rspb.Release, error) {
obj, err := secrets.impl.Get(key, metav1.GetOptions{}) obj, err := secrets.impl.Get(key, metav1.GetOptions{})
if err != nil { if err != nil {
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return nil, ErrReleaseNotFound(key) return nil, storageerrors.ErrReleaseNotFound(key)
} }
secrets.Log("get: failed to get %q: %s", key, err) secrets.Log("get: failed to get %q: %s", key, err)
...@@ -131,7 +132,7 @@ func (secrets *Secrets) Query(labels map[string]string) ([]*rspb.Release, error) ...@@ -131,7 +132,7 @@ func (secrets *Secrets) Query(labels map[string]string) ([]*rspb.Release, error)
} }
if len(list.Items) == 0 { if len(list.Items) == 0 {
return nil, ErrReleaseNotFound(labels["NAME"]) return nil, storageerrors.ErrReleaseNotFound(labels["NAME"])
} }
var results []*rspb.Release var results []*rspb.Release
...@@ -164,7 +165,7 @@ func (secrets *Secrets) Create(key string, rls *rspb.Release) error { ...@@ -164,7 +165,7 @@ func (secrets *Secrets) Create(key string, rls *rspb.Release) error {
// push the secret object out into the kubiverse // push the secret object out into the kubiverse
if _, err := secrets.impl.Create(obj); err != nil { if _, err := secrets.impl.Create(obj); err != nil {
if apierrors.IsAlreadyExists(err) { if apierrors.IsAlreadyExists(err) {
return ErrReleaseExists(rls.Name) return storageerrors.ErrReleaseExists(rls.Name)
} }
secrets.Log("create: failed to create: %s", err) secrets.Log("create: failed to create: %s", err)
...@@ -202,7 +203,7 @@ func (secrets *Secrets) Delete(key string) (rls *rspb.Release, err error) { ...@@ -202,7 +203,7 @@ func (secrets *Secrets) Delete(key string) (rls *rspb.Release, err error) {
// fetch the release to check existence // fetch the release to check existence
if rls, err = secrets.Get(key); err != nil { if rls, err = secrets.Get(key); err != nil {
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return nil, ErrReleaseExists(rls.Name) return nil, storageerrors.ErrReleaseExists(rls.Name)
} }
secrets.Log("delete: failed to get release %q: %s", key, err) secrets.Log("delete: failed to get release %q: %s", key, err)
......
/*
Copyright The Helm Authors.
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 errors // import "k8s.io/helm/pkg/storage/errors"
import "fmt"
var (
// ErrReleaseNotFound indicates that a release is not found.
ErrReleaseNotFound = func(release string) error { return fmt.Errorf("release: %q not found", release) }
// ErrReleaseExists indicates that a release already exists.
ErrReleaseExists = func(release string) error { return fmt.Errorf("release: %q already exists", release) }
// ErrInvalidKey indicates that a release key could not be parsed.
ErrInvalidKey = func(release string) error { return fmt.Errorf("release: %q invalid key", release) }
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment