-
Notifications
You must be signed in to change notification settings - Fork 303
/
target.go
130 lines (112 loc) · 3.54 KB
/
target.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package k8s
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/pkg/errors"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
)
func MustTarget(name model.TargetName, yaml string) model.K8sTarget {
kt, err := NewTargetForYAML(name, yaml, nil)
if err != nil {
panic(fmt.Errorf("MustTarget: %v", err))
}
return kt
}
func NewTargetForEntities(name model.TargetName, entities []K8sEntity, locators []ImageLocator) (model.K8sTarget, error) {
entities = SortedEntities(entities)
yaml, err := SerializeSpecYAML(entities)
if err != nil {
return model.K8sTarget{}, err
}
applySpec := v1alpha1.KubernetesApplySpec{
Cluster: v1alpha1.ClusterNameDefault,
DiscoveryStrategy: v1alpha1.KubernetesDiscoveryStrategyDefault,
YAML: yaml,
}
for _, locator := range locators {
if LocatorMatchesOne(locator, entities) {
applySpec.ImageLocators = append(applySpec.ImageLocators, locator.ToSpec())
}
}
target, err := NewTarget(name, applySpec, model.PodReadinessIgnore, nil)
if err != nil {
return model.K8sTarget{}, err
}
return target, nil
}
func NewTargetForYAML(name model.TargetName, yaml string, locators []ImageLocator) (model.K8sTarget, error) {
entities, err := ParseYAMLFromString(yaml)
if err != nil {
return model.K8sTarget{}, err
}
return NewTargetForEntities(name, entities, locators)
}
func NewTarget(
name model.TargetName,
applySpec v1alpha1.KubernetesApplySpec,
podReadinessMode model.PodReadinessMode,
links []model.Link) (model.K8sTarget, error) {
kapp := &v1alpha1.KubernetesApply{Spec: applySpec}
err := kapp.Validate(context.TODO())
if err != nil {
return model.K8sTarget{}, err.ToAggregate()
}
return model.K8sTarget{
KubernetesApplySpec: applySpec,
Name: name,
PodReadinessMode: podReadinessMode,
Links: links,
}, nil
}
func ParseImageLocators(locators []v1alpha1.KubernetesImageLocator) ([]ImageLocator, error) {
result := []ImageLocator{}
for _, locator := range locators {
selector, err := ParseObjectSelector(locator.ObjectSelector)
if err != nil {
return nil, errors.Wrap(err, "parsing image locator")
}
if locator.Object != nil {
parsedLocator, err := NewJSONPathImageObjectLocator(selector, locator.Path, locator.Object.RepoField, locator.Object.TagField)
if err != nil {
return nil, errors.Wrap(err, "parsing image locator")
}
result = append(result, parsedLocator)
} else {
parsedLocator, err := NewJSONPathImageLocator(selector, locator.Path)
if err != nil {
return nil, errors.Wrap(err, "parsing image locator")
}
result = append(result, parsedLocator)
}
}
return result, nil
}
// PortForwardTemplateSpec creates a port-forward template if necessary. Returns nil if no port-forwards.
func PortForwardTemplateSpec(forwards []model.PortForward) *v1alpha1.PortForwardTemplateSpec {
if len(forwards) == 0 {
return nil
}
res := make([]v1alpha1.Forward, len(forwards))
for i, fwd := range forwards {
res[i] = v1alpha1.Forward{
LocalPort: int32(fwd.LocalPort),
ContainerPort: int32(fwd.ContainerPort),
Host: fwd.Host,
Name: fwd.Name,
Path: fwd.PathForAppend(),
}
}
return &v1alpha1.PortForwardTemplateSpec{
Forwards: res,
}
}
func SetsAsLabelSelectors(sets []labels.Set) []metav1.LabelSelector {
var extraSelectors []metav1.LabelSelector
for _, s := range sets {
extraSelectors = append(extraSelectors, *metav1.SetAsLabelSelector(s))
}
return extraSelectors
}