-
Notifications
You must be signed in to change notification settings - Fork 65
/
cluster_image_template.go
100 lines (80 loc) · 2.74 KB
/
cluster_image_template.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2021 VMware
//
// 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 templates
//go:generate go run -modfile ../../hack/tools/go.mod github.com/maxbrunsfeld/counterfeiter/v6 -generate
import (
"crypto/sha256"
"fmt"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/strings"
"github.com/vmware-tanzu/cartographer/pkg/apis/v1alpha1"
)
type clusterImageTemplate struct {
template *v1alpha1.ClusterImageTemplate
evaluator evaluator
stampedObject *unstructured.Unstructured
}
func (t *clusterImageTemplate) GetKind() string {
return t.template.Kind
}
func NewClusterImageTemplateModel(template *v1alpha1.ClusterImageTemplate, eval evaluator) *clusterImageTemplate {
return &clusterImageTemplate{template: template, evaluator: eval}
}
func (t *clusterImageTemplate) GetName() string {
return t.template.Name
}
func (t *clusterImageTemplate) SetInputs(_ *Inputs) {}
func (t *clusterImageTemplate) SetStampedObject(stampedObject *unstructured.Unstructured) {
t.stampedObject = stampedObject
}
func (t *clusterImageTemplate) GetOutput() (*Output, error) {
image, err := t.evaluator.EvaluateJsonPath(t.template.Spec.ImagePath, t.stampedObject.UnstructuredContent())
if err != nil {
return nil, JsonPathError{
Err: fmt.Errorf("failed to evaluate the url path [%s]: %w",
t.template.Spec.ImagePath, err),
expression: t.template.Spec.ImagePath,
}
}
return &Output{
Image: image,
}, nil
}
func (t *clusterImageTemplate) GenerateResourceOutput(output *Output) ([]v1alpha1.Output, error) {
if output == nil || output.Image == nil {
return nil, nil
}
imageBytes, err := yaml.Marshal(output.Image)
if err != nil {
return nil, err
}
imageSHA := sha256.Sum256(imageBytes)
return []v1alpha1.Output{
{
Name: "image",
Preview: strings.ShortenString(string(imageBytes), PREVIEW_CHARACTER_LIMIT),
Digest: fmt.Sprintf("sha256:%x", imageSHA),
},
}, nil
}
func (t *clusterImageTemplate) GetResourceTemplate() v1alpha1.TemplateSpec {
return t.template.Spec.TemplateSpec
}
func (t *clusterImageTemplate) GetDefaultParams() v1alpha1.TemplateParams {
return t.template.Spec.Params
}
func (t *clusterImageTemplate) GetHealthRule() *v1alpha1.HealthRule {
return t.template.Spec.HealthRule
}