forked from kanisterio/kanister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueprint.go
56 lines (47 loc) · 1.88 KB
/
blueprint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2019 The Kanister 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.
// NOTE:
// Prerequisites:
// - To use blueprint method - "blueprint.ReadFromFile()",
// one needs to create symlink to the kanister/pkg/blueprints dir where main pkg exists.
// - In case of test files, create symlink in the pkg where test files are placed
// - Use relative path to the kanister/pkg/blueprints dir while creating the symlink
// e.g if you have to use this pkg in tests of pkg/testing pkg, the command should look like -
// "ln -sf ../../pkg/blueprint/blueprints blueprints"
package blueprint
import (
"bytes"
"fmt"
"io/ioutil"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/yaml"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
)
// ReadFromFile parsed and returns Blueprint specs placed at blueprints/{app}-blueprint.yaml
func ReadFromFile(path string) (*crv1alpha1.Blueprint, error) {
bpRaw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var bp crv1alpha1.Blueprint
dec := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(bpRaw), 1000)
if err := dec.Decode(&bp); err != nil {
return nil, err
}
// set the name to a dynamically generated value
// so that the name wont conflict with the same application
// installed as part of k10
bp.ObjectMeta.Name = fmt.Sprintf("%s-%s", bp.ObjectMeta.Name, rand.String(5))
return &bp, err
}