Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Obtain scope of manifest resources (uncached for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Jan 22, 2019
1 parent fd5e329 commit 00570c5
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 65 deletions.
6 changes: 4 additions & 2 deletions cluster/kubernetes/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ package kubernetes

import (
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
kresource "github.com/weaveworks/flux/cluster/kubernetes/resource"
"github.com/weaveworks/flux/image"
"github.com/weaveworks/flux/resource"
)

type Manifests struct {
AllowedNamespaces []string
Scoper cluster.Scoper
}

func (c *Manifests) LoadManifests(base string, paths []string) (map[string]resource.Resource, error) {
resources, err := kresource.Load(base, paths)
resources, err := kresource.Load(base, paths, c.Scoper)
c.discardDisallowed(resources)
return resources, err
}

func (c *Manifests) ParseManifests(allDefs []byte) (map[string]resource.Resource, error) {
resources, err := kresource.ParseMultidoc(allDefs, "exported")
resources, err := kresource.ParseMultidoc(allDefs, "exported", c.Scoper)
c.discardDisallowed(resources)
return resources, err
}
Expand Down
7 changes: 4 additions & 3 deletions cluster/kubernetes/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
kresource "github.com/weaveworks/flux/cluster/kubernetes/resource"
"github.com/weaveworks/flux/policy"
"github.com/weaveworks/flux/resource"
Expand All @@ -21,7 +22,7 @@ func (m *Manifests) UpdatePolicies(def []byte, id flux.ResourceID, update policy
// what all the containers are.
if tagAll, ok := update.Add.Get(policy.TagAll); ok {
add = add.Without(policy.TagAll)
containers, err := extractContainers(def, id)
containers, err := extractContainers(def, id, m.Scoper)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,8 +67,8 @@ func extractAnnotations(def []byte) (map[string]string, error) {
return m.Metadata.Annotations, nil
}

func extractContainers(def []byte, id flux.ResourceID) ([]resource.Container, error) {
resources, err := kresource.ParseMultidoc(def, "stdin")
func extractContainers(def []byte, id flux.ResourceID, scoper cluster.Scoper) ([]resource.Container, error) {
resources, err := kresource.ParseMultidoc(def, "stdin", scoper)
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions cluster/kubernetes/resource/fluxhelmrelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/resource"
)

Expand All @@ -26,7 +27,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -72,7 +73,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -116,7 +117,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -186,7 +187,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -253,7 +254,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -304,7 +305,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -391,7 +392,7 @@ spec:
enabled: false
`

resources, err := ParseMultidoc([]byte(doc), "test")
resources, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 7 additions & 4 deletions cluster/kubernetes/resource/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"path/filepath"

"github.com/pkg/errors"

"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/resource"
)

// Load takes paths to directories or files, and creates an object set
// based on the file(s) therein. Resources are named according to the
// file content, rather than the file name of directory structure.
func Load(base string, paths []string) (map[string]resource.Resource, error) {
func Load(base string, paths []string, scoper cluster.Scoper) (map[string]resource.Resource, error) {
if _, err := os.Stat(base); os.IsNotExist(err) {
return nil, fmt.Errorf("git path %q not found", base)
}
Expand Down Expand Up @@ -47,7 +49,7 @@ func Load(base string, paths []string) (map[string]resource.Resource, error) {
if err != nil {
return errors.Wrapf(err, "path to scan %q is not under base %q", path, base)
}
docsInFile, err := ParseMultidoc(bytes, source)
docsInFile, err := ParseMultidoc(bytes, source, scoper)
if err != nil {
return err
}
Expand Down Expand Up @@ -127,7 +129,7 @@ func looksLikeChart(dir string) bool {

// ParseMultidoc takes a dump of config (a multidoc YAML) and
// constructs an object set from the resources represented therein.
func ParseMultidoc(multidoc []byte, source string) (map[string]resource.Resource, error) {
func ParseMultidoc(multidoc []byte, source string, scoper cluster.Scoper) (map[string]resource.Resource, error) {
objs := map[string]resource.Resource{}
chunks := bufio.NewScanner(bytes.NewReader(multidoc))
initialBuffer := make([]byte, 4096) // Matches startBufSize in bufio/scan.go
Expand All @@ -143,7 +145,7 @@ func ParseMultidoc(multidoc []byte, source string) (map[string]resource.Resource
bytes := chunks.Bytes()
bytes2 := make([]byte, len(bytes), cap(bytes))
copy(bytes2, bytes)
if obj, err = unmarshalObject(source, bytes2); err != nil {
if obj, err = unmarshalObject(source, bytes2, scoper); err != nil {
return nil, errors.Wrapf(err, "parsing YAML doc from %q", source)
}
if obj == nil {
Expand All @@ -163,6 +165,7 @@ func ParseMultidoc(multidoc []byte, source string) (map[string]resource.Resource
if err := chunks.Err(); err != nil {
return objs, errors.Wrapf(err, "scanning multidoc from %q", source)
}

return objs, nil
}

Expand Down
37 changes: 22 additions & 15 deletions cluster/kubernetes/resource/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/stretchr/testify/assert"

"github.com/weaveworks/flux"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
"github.com/weaveworks/flux/resource"
)

// for convenience
func base(source, kind, namespace, name string) baseObject {
b := baseObject{source: source, Kind: kind}
func base(source, apiVersion, kind, namespace, name string) baseObject {
b := baseObject{source: source, scope: cluster.NamespaceScope, APIVersion: apiVersion, Kind: kind}
b.Meta.Namespace = namespace
b.Meta.Name = name
return b
Expand All @@ -24,7 +25,7 @@ func base(source, kind, namespace, name string) baseObject {
func TestParseEmpty(t *testing.T) {
doc := ``

objs, err := ParseMultidoc([]byte(doc), "test")
objs, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
if err != nil {
t.Error(err)
}
Expand All @@ -35,22 +36,24 @@ func TestParseEmpty(t *testing.T) {

func TestParseSome(t *testing.T) {
docs := `---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: b-deployment
namespace: b-namespace
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: a-deployment
`
objs, err := ParseMultidoc([]byte(docs), "test")
objs, err := ParseMultidoc([]byte(docs), "test", cluster.MockScoper)
if err != nil {
t.Error(err)
}

objA := base("test", "Deployment", "", "a-deployment")
objB := base("test", "Deployment", "b-namespace", "b-deployment")
objA := base("test", "apps/v1beta1", "Deployment", "", "a-deployment")
objB := base("test", "apps/v1beta1", "Deployment", "b-namespace", "b-deployment")
expected := map[string]resource.Resource{
objA.ResourceID().String(): &Deployment{baseObject: objA},
objB.ResourceID().String(): &Deployment{baseObject: objB},
Expand All @@ -67,22 +70,24 @@ metadata:
func TestParseSomeWithComment(t *testing.T) {
docs := `# some random comment
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: b-deployment
namespace: b-namespace
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: a-deployment
`
objs, err := ParseMultidoc([]byte(docs), "test")
objs, err := ParseMultidoc([]byte(docs), "test", cluster.MockScoper)
if err != nil {
t.Error(err)
}

objA := base("test", "Deployment", "", "a-deployment")
objB := base("test", "Deployment", "b-namespace", "b-deployment")
objA := base("test", "apps/v1beta1", "Deployment", "", "a-deployment")
objB := base("test", "apps/v1beta1", "Deployment", "b-namespace", "b-deployment")
expected := map[string]resource.Resource{
objA.ResourceID().String(): &Deployment{baseObject: objA},
objB.ResourceID().String(): &Deployment{baseObject: objB},
Expand All @@ -103,6 +108,7 @@ metadata:

func TestParseSomeLong(t *testing.T) {
doc := `---
apiVersion: v1
kind: ConfigMap
metadata:
name: bigmap
Expand All @@ -115,7 +121,7 @@ data:
buffer.WriteString(line)
}

_, err := ParseMultidoc(buffer.Bytes(), "test")
_, err := ParseMultidoc(buffer.Bytes(), "test", cluster.MockScoper)
if err != nil {
t.Error(err)
}
Expand All @@ -137,7 +143,7 @@ spec:
- name: weekly-curl-homepage
image: centos:7 # Has curl installed by default
`
objs, err := ParseMultidoc([]byte(doc), "test")
objs, err := ParseMultidoc([]byte(doc), "test", cluster.MockScoper)
assert.NoError(t, err)

obj, ok := objs["default:cronjob/weekly-curl-homepage"]
Expand All @@ -154,6 +160,7 @@ spec:

func TestUnmarshalList(t *testing.T) {
doc := `---
apiVersion: v1
kind: List
metadata:
name: list
Expand All @@ -165,7 +172,7 @@ items:
metadata:
name: bar
`
res, err := unmarshalObject("", []byte(doc))
res, err := unmarshalObject("", []byte(doc), cluster.MockScoper)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -200,7 +207,7 @@ func TestLoadSome(t *testing.T) {
if err := testfiles.WriteTestFiles(dir); err != nil {
t.Fatal(err)
}
objs, err := Load(dir, []string{dir})
objs, err := Load(dir, []string{dir}, cluster.MockScoper)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -231,7 +238,7 @@ func TestChartTracker(t *testing.T) {
if f == "garbage" {
continue
}
if m, err := Load(dir, []string{fq}); err != nil || len(m) == 0 {
if m, err := Load(dir, []string{fq}, cluster.MockScoper); err != nil || len(m) == 0 {
t.Errorf("Load returned 0 objs, err=%v", err)
}
}
Expand All @@ -250,7 +257,7 @@ func TestChartTracker(t *testing.T) {
}
for _, f := range chartfiles {
fq := filepath.Join(dir, f)
if m, err := Load(dir, []string{fq}); err != nil || len(m) != 0 {
if m, err := Load(dir, []string{fq}, cluster.MockScoper); err != nil || len(m) != 0 {
t.Errorf("%q not ignored as a chart should be", f)
}
}
Expand Down
Loading

0 comments on commit 00570c5

Please sign in to comment.