Skip to content

Commit b2adbea

Browse files
authored
chore(inject): remove PatchProducer abstraction (#14346)
In a recent change, we introduced the `PatchProducer` abstraction in order to provide a way to add customizable injection logic to the injector. After some further experimentation, it has became clear that there might be better ways to customize the injection logic. For now, we remove this abstraction as we are not going to be using it in the near future. Signed-off-by: Zahari Dichev <zaharidichev@gmail.com>
1 parent 2c7afa7 commit b2adbea

File tree

9 files changed

+41
-398
lines changed

9 files changed

+41
-398
lines changed

cli/cmd/inject.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (rt resourceTransformerInject) transform(bytes []byte) ([]byte, []inject.Re
207207
conf.AppendPodAnnotation(k8s.ProxyInjectAnnotation, k8s.ProxyInjectEnabled)
208208
}
209209

210-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, rt.injectProxy, rt.overrider)
210+
patchJSON, err := conf.GetPodPatch(rt.injectProxy, rt.overrider)
211211
if err != nil {
212212
return nil, nil, err
213213
}

controller/cmd/proxy-injector/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Main(args []string) {
2525
webhook.Launch(
2626
context.Background(),
2727
[]k8s.APIResource{k8s.NS, k8s.Deploy, k8s.RC, k8s.RS, k8s.Job, k8s.DS, k8s.SS, k8s.Pod, k8s.CJ},
28-
injector.Inject(*linkerdNamespace, inject.GetOverriddenValues, inject.PatchProducers),
28+
injector.Inject(*linkerdNamespace, inject.GetOverriddenValues),
2929
"linkerd-proxy-injector",
3030
*metricsAddr,
3131
*addr,

controller/proxy-injector/webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828
// Inject returns the function that produces an AdmissionResponse containing
2929
// the patch, if any, to apply to the pod (proxy sidecar and eventually the
3030
// init container to set it up)
31-
func Inject(linkerdNamespace string, overrider inject.ValueOverrider, patchProducers []inject.PatchProducer) webhook.Handler {
31+
func Inject(linkerdNamespace string, overrider inject.ValueOverrider) webhook.Handler {
3232
return func(
3333
ctx context.Context,
3434
api *k8s.MetadataAPI,
@@ -116,7 +116,7 @@ func Inject(linkerdNamespace string, overrider inject.ValueOverrider, patchProdu
116116
}
117117
}
118118

119-
patchJSON, err := inject.ProduceMergedPatch(patchProducers, resourceConfig, true, overrider)
119+
patchJSON, err := resourceConfig.GetPodPatch(true, overrider)
120120
if err != nil {
121121
return nil, err
122122
}

controller/proxy-injector/webhook_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestGetPodPatch(t *testing.T) {
119119
t.Fatal(err)
120120
}
121121

122-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, fullConf, true, inject.GetOverriddenValues)
122+
patchJSON, err := fullConf.GetPodPatch(true, inject.GetOverriddenValues)
123123
if err != nil {
124124
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
125125
}
@@ -142,7 +142,7 @@ func TestGetPodPatch(t *testing.T) {
142142
t.Fatal(err)
143143
}
144144

145-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, true, inject.GetOverriddenValues)
145+
patchJSON, err := conf.GetPodPatch(true, inject.GetOverriddenValues)
146146
if err != nil {
147147
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
148148
}
@@ -163,7 +163,7 @@ func TestGetPodPatch(t *testing.T) {
163163
t.Fatal(err)
164164
}
165165

166-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, true, inject.GetOverriddenValues)
166+
patchJSON, err := conf.GetPodPatch(true, inject.GetOverriddenValues)
167167
if err != nil {
168168
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
169169
}
@@ -186,7 +186,7 @@ func TestGetPodPatch(t *testing.T) {
186186
t.Fatal(err)
187187
}
188188

189-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, true, inject.GetOverriddenValues)
189+
patchJSON, err := conf.GetPodPatch(true, inject.GetOverriddenValues)
190190
if err != nil {
191191
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
192192
}
@@ -210,7 +210,7 @@ func TestGetPodPatch(t *testing.T) {
210210
t.Fatal(err)
211211
}
212212

213-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, true, inject.GetOverriddenValues)
213+
patchJSON, err := conf.GetPodPatch(true, inject.GetOverriddenValues)
214214
if err != nil {
215215
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
216216
}
@@ -240,7 +240,7 @@ func TestGetPodPatch(t *testing.T) {
240240
// The namespace has two config annotations: one valid and one invalid
241241
// the pod patch should only contain the valid annotation.
242242
inject.AppendNamespaceAnnotations(conf.GetOverrideAnnotations(), conf.GetNsAnnotations(), conf.GetWorkloadAnnotations())
243-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, true, inject.GetOverriddenValues)
243+
patchJSON, err := conf.GetPodPatch(true, inject.GetOverriddenValues)
244244
if err != nil {
245245
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
246246
}
@@ -254,7 +254,7 @@ func TestGetPodPatch(t *testing.T) {
254254
deployment := fileContents(factory, t, "deployment-with-injected-proxy.yaml")
255255
fakeReq := getFakePodReq(deployment)
256256
conf := confNsDisabled().WithKind(fakeReq.Kind.Kind)
257-
patchJSON, err := inject.ProduceMergedPatch(inject.PatchProducers, conf, true, inject.GetOverriddenValues)
257+
patchJSON, err := conf.GetPodPatch(true, inject.GetOverriddenValues)
258258
if err != nil {
259259
t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err)
260260
}

pkg/inject/inject.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"html/template"
11+
"net"
1112
"reflect"
1213
"regexp"
1314
"sort"
@@ -89,19 +90,9 @@ var (
8990
}
9091
)
9192

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-
10293
// ValueOverrider is used to override the default values that are used in chart rendering based
10394
// on the annotations provided in overrides.
104-
type ValueOverrider func(rc *ResourceConfig) (*OverriddenValues, error)
95+
type ValueOverrider func(rc *ResourceConfig) (*l5dcharts.Values, error)
10596

10697
// Origin defines where the input YAML comes from. Refer the ResourceConfig's
10798
// 'origin' field
@@ -199,7 +190,7 @@ func AppendNamespaceAnnotations(base map[string]string, nsAnn map[string]string,
199190

200191
// GetOverriddenValues returns the final Values struct which is created
201192
// by overriding annotated configuration on top of default Values
202-
func GetOverriddenValues(rc *ResourceConfig) (*OverriddenValues, error) {
193+
func GetOverriddenValues(rc *ResourceConfig) (*l5dcharts.Values, error) {
203194
// Make a copy of Values and mutate that
204195
copyValues, err := rc.GetValues().DeepCopy()
205196
if err != nil {
@@ -212,7 +203,7 @@ func GetOverriddenValues(rc *ResourceConfig) (*OverriddenValues, error) {
212203
}
213204

214205
ApplyAnnotationOverrides(copyValues, rc.GetAnnotationOverrides(), namedPorts)
215-
return &OverriddenValues{Values: copyValues}, nil
206+
return copyValues, nil
216207
}
217208

218209
func ApplyAnnotationOverrides(values *l5dcharts.Values, annotations map[string]string, namedPorts map[string]int32) {
@@ -686,12 +677,32 @@ func (conf *ResourceConfig) GetAnnotationOverrides() map[string]string {
686677

687678
// GetPodPatch returns the JSON patch containing the proxy and init containers specs, if any.
688679
// If injectProxy is false, only the config.linkerd.io annotations are set.
689-
func GetPodPatch(conf *ResourceConfig, injectProxy bool, values *OverriddenValues, patchPathPrefix string) ([]JSONPatch, error) {
680+
func (conf *ResourceConfig) GetPodPatch(injectProxy bool, overrider ValueOverrider) ([]byte, error) {
681+
values, err := overrider(conf)
682+
values.Proxy.PodInboundPorts = getPodInboundPorts(conf.pod.spec)
683+
if err != nil {
684+
return nil, fmt.Errorf("could not generate Overridden Values: %w", err)
685+
}
686+
687+
if values.ClusterNetworks != "" {
688+
for _, network := range strings.Split(strings.Trim(values.ClusterNetworks, ","), ",") {
689+
if _, _, err := net.ParseCIDR(network); err != nil {
690+
return nil, fmt.Errorf("cannot parse destination get networks: %w", err)
691+
}
692+
}
693+
}
694+
690695
patch := &podPatch{
691-
Values: *values.Values,
696+
Values: *values,
692697
Annotations: map[string]string{},
693698
Labels: map[string]string{},
694-
PathPrefix: patchPathPrefix,
699+
}
700+
switch strings.ToLower(conf.workload.metaType.Kind) {
701+
case k8s.Pod:
702+
case k8s.CronJob:
703+
patch.PathPrefix = "/spec/jobTemplate/spec/template"
704+
default:
705+
patch.PathPrefix = "/spec/template"
695706
}
696707

697708
if conf.pod.spec != nil {
@@ -732,12 +743,7 @@ func GetPodPatch(conf *ResourceConfig, injectProxy bool, values *OverriddenValue
732743
// Get rid of invalid trailing commas
733744
res := rTrail.ReplaceAll(buf.Bytes(), []byte("}\n]"))
734745

735-
patchResult := []JSONPatch{}
736-
if err := json.Unmarshal(res, &patchResult); err != nil {
737-
return nil, err
738-
}
739-
740-
return patchResult, nil
746+
return res, nil
741747
}
742748

743749
// GetConfigAnnotation returns two values. The first value is the annotation

pkg/inject/inject_fuzzer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ func FuzzInject(data []byte) int {
2626
return 0
2727
}
2828

29-
values, err := GetOverriddenValues(conf)
30-
if err != nil {
31-
return 0
32-
}
33-
34-
_, _ = GetPodPatch(conf, injectProxy, values, getPatchPathPrefix(conf))
29+
_, _ = conf.GetPodPatch(injectProxy, GetOverriddenValues)
3530
_, _ = conf.CreateOpaquePortsPatch()
3631

3732
report := &Report{}

pkg/inject/inject_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func TestGetOverriddenValues(t *testing.T) {
330330
t.Fatal(err)
331331
}
332332
expected := testCase.expected()
333-
if diff := deep.Equal(actual.Values, expected); diff != nil {
333+
if diff := deep.Equal(actual, expected); diff != nil {
334334
t.Errorf("%+v", diff)
335335
}
336336
})

pkg/inject/patch.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)