Skip to content

Commit b1c0b13

Browse files
authored
fix(dpp): save and update labels from Dataplane resource on Universal (#12975)
## Motivation We need to handle dpp labels properly in order to be able to select them in policies <!-- > Changelog: skip --> <!-- Uncomment the above section to explicitly set a [`> Changelog:` entry here](https://github.com/kumahq/kuma/blob/master/CONTRIBUTING.md#submitting-a-patch)? --> --------- Signed-off-by: Marcin Skalski <[email protected]>
1 parent d3569f3 commit b1c0b13

File tree

8 files changed

+45
-10
lines changed

8 files changed

+45
-10
lines changed

pkg/core/managers/apis/dataplane/dataplane_manager.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ func (m *dataplaneManager) Update(ctx context.Context, resource core_model.Resou
9494

9595
m.setInboundsClusterTag(dp)
9696
m.setGatewayClusterTag(dp)
97+
98+
opts := core_store.NewUpdateOptions(fs...)
9799
labels, err := core_model.ComputeLabels(
98100
resource.Descriptor(),
99101
resource.GetSpec(),
100-
resource.GetMeta().GetLabels(),
102+
opts.Labels,
101103
core_model.GetNamespace(resource.GetMeta(), m.systemNamespace),
102104
resource.GetMeta().GetMesh(),
103105
m.mode,

pkg/core/resources/manager/manager.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type ConflictRetry struct {
127127
type UpsertOpts struct {
128128
ConflictRetry ConflictRetry
129129
Transactions store.Transactions
130+
Labels map[string]string
130131
}
131132

132133
type UpsertFunc func(opts *UpsertOpts)
@@ -155,6 +156,12 @@ func NewUpsertOpts(fs ...UpsertFunc) UpsertOpts {
155156
return opts
156157
}
157158

159+
func UpsertWithLabels(labels map[string]string) UpsertFunc {
160+
return func(opts *UpsertOpts) {
161+
opts.Labels = labels
162+
}
163+
}
164+
158165
var ErrSkipUpsert = errors.New("don't do upsert")
159166

160167
func Upsert(ctx context.Context, manager ResourceManager, key model.ResourceKey, resource model.Resource, fn func(resource model.Resource) error, fs ...UpsertFunc) error {
@@ -177,9 +184,9 @@ func Upsert(ctx context.Context, manager ResourceManager, key model.ResourceKey,
177184
return err
178185
}
179186
if create {
180-
return manager.Create(ctx, resource, store.CreateBy(key))
187+
return manager.Create(ctx, resource, store.CreateBy(key), store.CreateWithLabels(opts.Labels))
181188
} else {
182-
return manager.Update(ctx, resource)
189+
return manager.Update(ctx, resource, store.UpdateWithLabels(opts.Labels))
183190
}
184191
})
185192
}

pkg/test/resources/builders/dataplane_builder.go

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func (d *DataplaneBuilder) WithMesh(mesh string) *DataplaneBuilder {
6767
return d
6868
}
6969

70+
func (d *DataplaneBuilder) WithLabels(labels map[string]string) *DataplaneBuilder {
71+
d.res.Meta.(*test_model.ResourceMeta).Labels = labels
72+
return d
73+
}
74+
7075
func (d *DataplaneBuilder) WithVersion(version string) *DataplaneBuilder {
7176
d.res.Meta.(*test_model.ResourceMeta).Version = version
7277
return d

pkg/xds/server/callbacks/dataplane_lifecycle.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (d *DataplaneLifecycle) register(
125125
return errors.Wrap(err, "you are trying to override existing proxy to which you don't have an access.")
126126
}
127127
return existing.SetSpec(md.Resource.GetSpec())
128-
})
128+
}, manager.UpsertWithLabels(md.Resource.GetMeta().GetLabels()))
129129
if err != nil {
130130
log.Info("cannot register proxy", "reason", err.Error())
131131
if !loaded {

pkg/xds/server/callbacks/dataplane_lifecycle_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ var _ = Describe("Dataplane Lifecycle", func() {
7373
"type": "Dataplane",
7474
"mesh": "default",
7575
"name": "backend-01",
76+
"labels": {
77+
"app": "backend",
78+
},
7679
"networking": {
7780
"address": "127.0.0.1",
7881
"inbound": [
@@ -106,8 +109,10 @@ var _ = Describe("Dataplane Lifecycle", func() {
106109

107110
// then dp is created
108111
Expect(err).ToNot(HaveOccurred())
109-
err = resManager.Get(context.Background(), core_mesh.NewDataplaneResource(), core_store.GetByKey("backend-01", "default"))
112+
dp := core_mesh.NewDataplaneResource()
113+
err = resManager.Get(context.Background(), dp, core_store.GetByKey("backend-01", "default"))
110114
Expect(err).ToNot(HaveOccurred())
115+
Expect(dp.GetMeta().GetLabels()).To(HaveKeyWithValue("app", "backend"))
111116

112117
// when
113118
callbacks.OnStreamClosed(streamId, node)

test/e2e_env/universal/auth/dp_auth.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,27 @@ func DpAuth() {
4949
It("should be able to override old Dataplane of same service", func() {
5050
// given
5151
dp := builders.Dataplane().
52-
WithName("dp-01").
52+
WithName("dp-02").
5353
WithMesh(meshName).
54+
WithLabels(map[string]string{"app": "not-test-server"}).
5455
WithAddress("192.168.0.2").
5556
WithServices("test-server").
5657
Build()
5758
Expect(universal.Cluster.Install(ResourceUniversal(dp))).To(Succeed())
5859

5960
// when
60-
err := TestServerUniversal("dp-02", meshName, WithServiceName("test-server"))(universal.Cluster)
61+
err := TestServerUniversal("dp-02", meshName, WithServiceName("test-server"), WithAppLabel("test-server"))(universal.Cluster)
6162
Expect(err).ToNot(HaveOccurred())
6263

6364
// then
6465
Eventually(func() (string, error) {
65-
return universal.Cluster.GetKumactlOptions().RunKumactlAndGetOutput("get", "dataplanes", "-oyaml")
66-
}, "30s", "1s").ShouldNot(ContainSubstring("192.168.0.2"))
66+
return universal.Cluster.GetKumactlOptions().RunKumactlAndGetOutput("get", "dataplanes", "--mesh", meshName, "-oyaml")
67+
}, "30s", "1s").Should(
68+
And(
69+
Not(ContainSubstring("192.168.0.2")),
70+
Not(ContainSubstring("not-test-server")),
71+
ContainSubstring("test-server"),
72+
),
73+
)
6774
})
6875
}

test/framework/interface.go

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type appDeploymentOptions struct {
9595
name string
9696
appYaml string
9797
appArgs []string
98+
appLabel string
9899
token string
99100
transparent *bool
100101
builtindns *bool // true by default
@@ -424,6 +425,12 @@ func WithServiceName(name string) AppDeploymentOption {
424425
})
425426
}
426427

428+
func WithAppLabel(app string) AppDeploymentOption {
429+
return AppOptionFunc(func(o *appDeploymentOptions) {
430+
o.appLabel = app
431+
})
432+
}
433+
427434
func WithAppendDataplaneYaml(config string) AppDeploymentOption {
428435
return AppOptionFunc(func(o *appDeploymentOptions) {
429436
o.appendDataplaneConfig = config

test/framework/setup.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ func TestServerUniversal(name string, mesh string, opt ...AppDeploymentOption) I
926926
type: Dataplane
927927
mesh: %s
928928
name: {{ name }}
929+
labels:
930+
app: %s
929931
networking:
930932
address: {{ address }}
931933
inbound:
@@ -942,7 +944,7 @@ networking:
942944
%s
943945
%s
944946
%s
945-
`, mesh, "80", "8080", serviceAddress, opts.serviceName, opts.protocol, opts.serviceVersion, opts.serviceInstance, additionalTags, serviceProbe, transparentProxy, opts.appendDataplaneConfig)
947+
`, mesh, opts.appLabel, "80", "8080", serviceAddress, opts.serviceName, opts.protocol, opts.serviceVersion, opts.serviceInstance, additionalTags, serviceProbe, transparentProxy, opts.appendDataplaneConfig)
946948

947949
opt = append(opt,
948950
WithName(name),

0 commit comments

Comments
 (0)