|
8 | 8 | "errors"
|
9 | 9 | "fmt"
|
10 | 10 | "html/template"
|
11 |
| - "net" |
12 | 11 | "reflect"
|
13 | 12 | "regexp"
|
14 | 13 | "sort"
|
|
90 | 89 | }
|
91 | 90 | )
|
92 | 91 |
|
93 |
| -type ValueOverrider func(values *l5dcharts.Values, overrides map[string]string, namedPorts map[string]int32) (*l5dcharts.Values, error) |
| 92 | +// OverriddenValues contains the result of executing an instance of ValueOverrider |
| 93 | +type OverriddenValues struct { |
| 94 | + *l5dcharts.Values |
| 95 | + |
| 96 | + // may contain additional values that are not part of the l5dcharts.Values |
| 97 | + // in order to allow custom ValueOverrider implementations to add their own |
| 98 | + // values to the rendering logic. |
| 99 | + Additional map[string]interface{} |
| 100 | +} |
| 101 | + |
| 102 | +// ValueOverrider is used to override the default values that are used in chart rendering based |
| 103 | +// on the annotations provided in overrides. |
| 104 | +type ValueOverrider func(values *l5dcharts.Values, overrides map[string]string, namedPorts map[string]int32) (*OverriddenValues, error) |
94 | 105 |
|
95 | 106 | // Origin defines where the input YAML comes from. Refer the ResourceConfig's
|
96 | 107 | // 'origin' field
|
@@ -188,15 +199,15 @@ func AppendNamespaceAnnotations(base map[string]string, nsAnn map[string]string,
|
188 | 199 |
|
189 | 200 | // GetOverriddenValues returns the final Values struct which is created
|
190 | 201 | // by overriding annotated configuration on top of default Values
|
191 |
| -func GetOverriddenValues(values *l5dcharts.Values, overrides map[string]string, namedPorts map[string]int32) (*l5dcharts.Values, error) { |
| 202 | +func GetOverriddenValues(values *l5dcharts.Values, overrides map[string]string, namedPorts map[string]int32) (*OverriddenValues, error) { |
192 | 203 | // Make a copy of Values and mutate that
|
193 | 204 | copyValues, err := values.DeepCopy()
|
194 | 205 | if err != nil {
|
195 | 206 | return nil, err
|
196 | 207 | }
|
197 | 208 |
|
198 | 209 | applyAnnotationOverrides(copyValues, overrides, namedPorts)
|
199 |
| - return copyValues, nil |
| 210 | + return &OverriddenValues{Values: copyValues}, nil |
200 | 211 | }
|
201 | 212 |
|
202 | 213 | func applyAnnotationOverrides(values *l5dcharts.Values, annotations map[string]string, namedPorts map[string]int32) {
|
@@ -663,37 +674,12 @@ func (conf *ResourceConfig) getAnnotationOverrides() map[string]string {
|
663 | 674 |
|
664 | 675 | // GetPodPatch returns the JSON patch containing the proxy and init containers specs, if any.
|
665 | 676 | // If injectProxy is false, only the config.linkerd.io annotations are set.
|
666 |
| -func (conf *ResourceConfig) GetPodPatch(injectProxy bool, overrider ValueOverrider) ([]byte, error) { |
667 |
| - namedPorts := make(map[string]int32) |
668 |
| - if conf.HasPodTemplate() { |
669 |
| - namedPorts = util.GetNamedPorts(conf.pod.spec.Containers) |
670 |
| - } |
671 |
| - |
672 |
| - values, err := overrider(conf.values, conf.getAnnotationOverrides(), namedPorts) |
673 |
| - values.Proxy.PodInboundPorts = getPodInboundPorts(conf.pod.spec) |
674 |
| - if err != nil { |
675 |
| - return nil, fmt.Errorf("could not generate Overridden Values: %w", err) |
676 |
| - } |
677 |
| - |
678 |
| - if values.ClusterNetworks != "" { |
679 |
| - for _, network := range strings.Split(strings.Trim(values.ClusterNetworks, ","), ",") { |
680 |
| - if _, _, err := net.ParseCIDR(network); err != nil { |
681 |
| - return nil, fmt.Errorf("cannot parse destination get networks: %w", err) |
682 |
| - } |
683 |
| - } |
684 |
| - } |
685 |
| - |
| 677 | +func GetPodPatch(conf *ResourceConfig, injectProxy bool, values *OverriddenValues, patchPathPrefix string) ([]JSONPatch, error) { |
686 | 678 | patch := &podPatch{
|
687 |
| - Values: *values, |
| 679 | + Values: *values.Values, |
688 | 680 | Annotations: map[string]string{},
|
689 | 681 | Labels: map[string]string{},
|
690 |
| - } |
691 |
| - switch strings.ToLower(conf.workload.metaType.Kind) { |
692 |
| - case k8s.Pod: |
693 |
| - case k8s.CronJob: |
694 |
| - patch.PathPrefix = "/spec/jobTemplate/spec/template" |
695 |
| - default: |
696 |
| - patch.PathPrefix = "/spec/template" |
| 682 | + PathPrefix: patchPathPrefix, |
697 | 683 | }
|
698 | 684 |
|
699 | 685 | if conf.pod.spec != nil {
|
@@ -734,7 +720,12 @@ func (conf *ResourceConfig) GetPodPatch(injectProxy bool, overrider ValueOverrid
|
734 | 720 | // Get rid of invalid trailing commas
|
735 | 721 | res := rTrail.ReplaceAll(buf.Bytes(), []byte("}\n]"))
|
736 | 722 |
|
737 |
| - return res, nil |
| 723 | + patchResult := []JSONPatch{} |
| 724 | + if err := json.Unmarshal(res, &patchResult); err != nil { |
| 725 | + return nil, err |
| 726 | + } |
| 727 | + |
| 728 | + return patchResult, nil |
738 | 729 | }
|
739 | 730 |
|
740 | 731 | // GetConfigAnnotation returns two values. The first value is the annotation
|
|
0 commit comments