From 030e8ead07d5efbe35c3de92279bf5030bf11e1c Mon Sep 17 00:00:00 2001 From: Maciej Kwiek Date: Fri, 17 Mar 2017 14:19:11 +0100 Subject: [PATCH] SplitManifestWithHeads added to releaseutils --- pkg/releaseutil/manifest.go | 32 +++++++++++++++- pkg/releaseutil/manifest_test.go | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 pkg/releaseutil/manifest_test.go diff --git a/pkg/releaseutil/manifest.go b/pkg/releaseutil/manifest.go index b6bb87b0a..9fb17b5da 100644 --- a/pkg/releaseutil/manifest.go +++ b/pkg/releaseutil/manifest.go @@ -19,6 +19,8 @@ package releaseutil import ( "fmt" "strings" + + "github.com/ghodss/yaml" ) // SimpleHead defines what the structure of the head of a manifest file @@ -38,11 +40,39 @@ func SplitManifests(bigfile string) map[string]string { // array of YAML docs. In the current implementation, the file name is just // a place holder, and doesn't have any further meaning. sep := "\n---\n" + cutset := " \n\t" tpl := "manifest-%d" res := map[string]string{} tmp := strings.Split(bigfile, sep) for i, d := range tmp { - res[fmt.Sprintf(tpl, i)] = d + if len(strings.Trim(d, cutset)) > 0 { + res[fmt.Sprintf(tpl, i)] = d + } } return res } + +// Manifest reperestens a single manifest content with SimpleHead added for additional metadata +type Manifest struct { + SimpleHead + Content string +} + +// SplitManifestsWithHeads +func SplitManifestsWithHeads(bigfile string) ([]Manifest, error) { + raws := SplitManifests(bigfile) + + result := make([]Manifest, 0, len(raws)) + var err error + + for _, raw := range raws { + var head SimpleHead + err = yaml.Unmarshal([]byte(raw), &head) + + result = append(result, Manifest{ + Content: raw, + SimpleHead: head, + }) + } + return result, err +} diff --git a/pkg/releaseutil/manifest_test.go b/pkg/releaseutil/manifest_test.go new file mode 100644 index 000000000..0bcc8e269 --- /dev/null +++ b/pkg/releaseutil/manifest_test.go @@ -0,0 +1,63 @@ +/* +Copyright 2017 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 releaseutil + +import ( + "testing" +) + +func TestSplitManifestsWithHeads(t *testing.T) { + rawManifest := `kind: TestKind +apiVersion: v1 +metadata: + name: TestName + annotations: + key1: value1 + key2: value2` + + rawManifests := rawManifest + "\n---\n" + rawManifest + + manifests, err := SplitManifestsWithHeads(rawManifests) + if err != nil { + t.Errorf("Expected error to be nil, got %s", err) + } + + if len(manifests) != 2 { + t.Errorf("Expected manifests length to be 2, got %d", len(manifests)) + } + + for _, m := range manifests { + if m.SimpleHead.Kind != "TestKind" { + t.Errorf("Expected Kind to be TestKind, got %s", m.SimpleHead.Kind) + } + if m.SimpleHead.Version != "v1" { + t.Errorf("Expected Version to be v1, got %s", m.SimpleHead.Version) + } + if m.SimpleHead.Metadata.Name != "TestName" { + t.Errorf("Expected Name to be TestName, got %s", m.SimpleHead.Metadata.Name) + } + if val1, ok := m.SimpleHead.Metadata.Annotations["key1"]; !ok || val1 != "value1" { + t.Errorf("Expected annotation key1 to be value1, got %s", val1) + } + if val2, ok := m.SimpleHead.Metadata.Annotations["key2"]; !ok || val2 != "value2" { + t.Errorf("Expected annotation key2 to be value2, got %s", val2) + } + if m.Content != rawManifest { + t.Errorf("Expected Content to be equal to original, got %s", m.Content) + } + } +}