Skip to content

Commit

Permalink
fix namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Aug 14, 2019
1 parent adcfede commit 86e8416
Show file tree
Hide file tree
Showing 18 changed files with 862 additions and 21 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.3.2
github.com/google/go-cmp v0.3.0
github.com/google/go-cmp v0.3.1
github.com/google/go-containerregistry v0.0.0-20190717132004-e8c6a4993fa7
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
Expand Down Expand Up @@ -79,6 +79,7 @@ require (
k8s.io/apimachinery v0.0.0-20190620073744-d16981aedf33
k8s.io/client-go v0.0.0-20190620074045-585a16d2e773
k8s.io/kubectl v0.0.0-20190622051205-955b067cc6d3
k8s.io/utils v0.0.0-20190221042446-c2654d5206da
knative.dev/pkg v0.0.0-20190730155243-972acd413fb9 // indirect
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-containerregistry v0.0.0-20190717132004-e8c6a4993fa7 h1:Tx9G0xLagQdDraS6GdVgRLFOXWURkSnC8RbszNCqUUc=
github.com/google/go-containerregistry v0.0.0-20190717132004-e8c6a4993fa7/go.mod h1:yZAFP63pRshzrEYLXLGPmUt0Ay+2zdjmMN1loCnRLUk=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func (h *HelmDeployer) Deploy(ctx context.Context, out io.Writer, builds []build
}
// collect namespaces
for _, r := range results {
nsMap[r.Namespace] = true
if trimmed := strings.TrimSpace(r.Namespace); trimmed != "" {
nsMap[trimmed] = true
}
}

dRes = append(dRes, results...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/kubectl/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (l *ManifestList) CollectNamespaces() ([]string, error) {
if _, err := l.Visit(replacer); err != nil {
return nil, errors.Wrap(err, "collecting namespaces")
}

namespaces := make([]string, 0, len(replacer.namespaces))
for ns := range replacer.namespaces {
namespaces = append(namespaces, ns)
Expand Down
32 changes: 27 additions & 5 deletions pkg/skaffold/deploy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@ package deploy

import (
"bufio"
"bytes"
"fmt"
"io"

"github.com/sirupsen/logrus"

k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/scheme"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
)

func parseRuntimeObject(namespace string, b []byte) (Artifact, error) {
func parseRuntimeObject(namespace string, b []byte) (*Artifact, error) {
d := scheme.Codecs.UniversalDeserializer()
obj, _, err := d.Decode(b, nil, nil)
if err != nil {
return Artifact{}, fmt.Errorf("error decoding parsed yaml: %s", err.Error())
return nil, fmt.Errorf("error decoding parsed yaml: %s", err.Error())
}
return Artifact{
return &Artifact{
Obj: obj,
Namespace: namespace,
}, nil
Expand All @@ -51,12 +54,31 @@ func parseReleaseInfo(namespace string, b *bufio.Reader) []Artifact {
logrus.Infof("error parsing object from string: %s", err.Error())
continue
}
obj, err := parseRuntimeObject(namespace, doc)
objNamespace, err := getObjectNamespaceIfDefined(doc, namespace)
if err != nil {
logrus.Infof("error parsing object from string: %s", err.Error())
continue
}
obj, err := parseRuntimeObject(objNamespace, doc)
if err != nil {
logrus.Infof(err.Error())
} else {
results = append(results, obj)
results = append(results, *obj)
}
}
return results
}

func getObjectNamespaceIfDefined(doc []byte, ns string) (string, error) {
if i := bytes.Index(doc, []byte("apiVersion")); i >= 0 {
manifests := kubectl.ManifestList{doc[i:]}
namespaces, err := manifests.CollectNamespaces()
if err != nil {
return ns, err
}
if len(namespaces) > 0 {
return namespaces[0], nil
}
}
return ns, nil
}
144 changes: 144 additions & 0 deletions pkg/skaffold/deploy/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright 2019 The Skaffold 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 deploy

import (
"bufio"
"bytes"
"testing"

"github.com/google/go-cmp/cmp/cmpopts"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestParseReleaseInfo(t *testing.T) {
tests := []struct {
description string
yaml []byte
expected []Artifact
}{
{
description: "parse valid release info yaml with single artifact with namespace",
yaml: []byte(`# Source: skaffold-helm/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: skaffold-helm-skaffold-helm
namespace: test
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Tiller
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app: skaffold-helm
release: skaffold-helm`),
expected: []Artifact{{Namespace: "test"}},
},
{
description: "parse valid release info yaml with single artifact without namespace sets helm namespace",
yaml: []byte(`# Source: skaffold-helm/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: skaffold-helm-skaffold-helm
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Tiller
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app: skaffold-helm
release: skaffold-helm`),
expected: []Artifact{{
Namespace: "testNamespace",
}},
},
{
description: "parse valid release info yaml with multiple artifacts",
yaml: []byte(`# Source: skaffold-helm/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: skaffold-helm-skaffold-helm
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Tiller
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app: skaffold-helm
release: skaffold-helm
---
# Source: skaffold-helm/templates/ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: skaffold-helm-skaffold-helm
namespace: test
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Tiller
annotations:
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: skaffold-helm-skaffold-helm
servicePort: 80`),
expected: []Artifact{{Namespace: "testNamespace"}, {Namespace: "test"}},
},
{
description: "parse invalid release info yaml",
yaml: []byte(`invalid release info`),
expected: []Artifact{},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
r := bufio.NewReader(bytes.NewBuffer(test.yaml))
actual := parseReleaseInfo(testNamespace, r)
t.CheckDeepEqual(test.expected, actual, cmpopts.IgnoreFields(Artifact{}, "Obj"))
})
}
}
89 changes: 89 additions & 0 deletions vendor/github.com/google/go-cmp/cmp/cmpopts/equate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 86e8416

Please sign in to comment.