Skip to content

Commit 8bd7e07

Browse files
JoshVanLdapr-bot
andauthored
Subscriptions: Declarative Go Generic Loader (dapr#7582)
* Subscriptions: Declarative Go Generic Loader Following Components and HTTPEndpoints, creates a generic disk & kubernetes loader for Subscriptions. Removes the adhok loaders from existing runtime pubsub packages. This is required for adding Subscriptions to the hot reloader reconciler and making a more consistent & testable manifest loader package. The disk loader loads both `v1alpha1` and `v2alpha1` Subscriptions, converting `v1alpha1` to `v2alpha1` on successful load. Since the operator returns only `v2alpha1` Subscriptions (Kubernetes API converts `v1alpha1` to `v2alpha1`), the Kubernetes loader only loads `v2alpha1`. `APIVersion() string` func has been added to the generic `meta.Resource` interface type to allow for the generic loader to determine between resource versions, supporting differentiating `v1alpha1` and `v2alpha1` Subscription resource versions. To ensure backwards compatibility of the previous disk Subscription loader, the generic disk loader now tracks the order in which manifests are loaded. This ensures that, even though `v1alpha1` and `v2alpha1` Subscriptions use separate loaders, their file position order is preserved once `v1alpha1` Subscriptions are converted to `v2alpha1`. Subscription backwards compatibility of parsing & ingestion priority is covered by existing extensive integration tests. Notice that _ZERO_ of the comprehensive subscription integration tests have been modified, proving no behaviour change has occurred for loading and actuating Subscriptions. Explanation of integration test changes: - daprd/hotreload/selfhosted/crypto: loader now mandates `apiVersion` on disk manifests which is more correct. Previous behaviour ignoring this field should be considered a bug. Since we use Kubernetes resource API schema, `apiVersion` is (and has been) _always_ required. - framework/process/grpc/operator/server.go: returning non-nil gRPC object for mocked operator Subscription RPC call prevents underlying gRPC library unmarshal nil errors- more correct than previous implementation. Part of [Subscription hot-reloading](dapr#7139). Signed-off-by: joshvanl <[email protected]> * Update errS to patherrs to avoid confusion Signed-off-by: joshvanl <[email protected]> * Updates sub import to use bare version string Signed-off-by: joshvanl <[email protected]> * Remove unneeded code comment Signed-off-by: joshvanl <[email protected]> * Change test manifests to have unique names Signed-off-by: joshvanl <[email protected]> --------- Signed-off-by: joshvanl <[email protected]> Co-authored-by: Dapr Bot <[email protected]>
1 parent 2dc5c87 commit 8bd7e07

File tree

29 files changed

+762
-874
lines changed

29 files changed

+762
-874
lines changed

pkg/apis/components/v1alpha1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (Component) Kind() string {
4747
return "Component"
4848
}
4949

50+
func (Component) APIVersion() string {
51+
return components.GroupName + "/" + Version
52+
}
53+
5054
// GetName returns the component name.
5155
func (c Component) GetName() string {
5256
return c.Name

pkg/apis/httpEndpoint/v1alpha1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (HTTPEndpoint) Kind() string {
4949
return kind
5050
}
5151

52+
func (HTTPEndpoint) APIVersion() string {
53+
return httpendpoint.GroupName + "/" + Version
54+
}
55+
5256
// GetName returns the component name.
5357
func (h HTTPEndpoint) GetName() string {
5458
return h.Name

pkg/apis/subscriptions/v1alpha1/register.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
// SchemeGroupVersion is group version used to register these objects.
2525
var SchemeGroupVersion = schema.GroupVersion{Group: subscriptions.GroupName, Version: "v1alpha1"}
2626

27-
// Kind takes an unqualified kind and returns back a Group qualified GroupKind.
28-
func Kind(kind string) schema.GroupKind {
27+
// GroupKindFromKind takes an unqualified kind and returns back a Group qualified GroupKind.
28+
func GroupKindFromKind(kind string) schema.GroupKind {
2929
return SchemeGroupVersion.WithKind(kind).GroupKind()
3030
}
3131

pkg/apis/subscriptions/v1alpha1/types.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ package v1alpha1
1515

1616
import (
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
19+
"github.com/dapr/dapr/pkg/apis/common"
20+
"github.com/dapr/dapr/pkg/apis/subscriptions"
21+
)
22+
23+
const (
24+
Kind = "Subscription"
25+
Version = "v1alpha1"
1826
)
1927

2028
// +genclient
@@ -59,3 +67,43 @@ type SubscriptionList struct {
5967

6068
Items []Subscription `json:"items"`
6169
}
70+
71+
func (Subscription) Kind() string {
72+
return Kind
73+
}
74+
75+
func (Subscription) APIVersion() string {
76+
return subscriptions.GroupName + "/" + Version
77+
}
78+
79+
// EmptyMetaDeepCopy returns a new instance of the subscription type with the
80+
// TypeMeta's Kind and APIVersion fields set.
81+
func (s Subscription) EmptyMetaDeepCopy() metav1.Object {
82+
n := s.DeepCopy()
83+
n.TypeMeta = metav1.TypeMeta{
84+
Kind: Kind,
85+
APIVersion: subscriptions.GroupName + "/" + Version,
86+
}
87+
n.ObjectMeta = metav1.ObjectMeta{Name: s.Name}
88+
return n
89+
}
90+
91+
func (s Subscription) GetName() string {
92+
return s.Name
93+
}
94+
95+
func (s Subscription) GetNamespace() string {
96+
return s.Namespace
97+
}
98+
99+
func (s Subscription) GetSecretStore() string {
100+
return ""
101+
}
102+
103+
func (s Subscription) LogName() string {
104+
return s.GetName()
105+
}
106+
107+
func (s Subscription) NameValuePairs() []common.NameValuePair {
108+
return nil
109+
}

pkg/apis/subscriptions/v2alpha1/register.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
// SchemeGroupVersion is group version used to register these objects.
2525
var SchemeGroupVersion = schema.GroupVersion{Group: subscriptions.GroupName, Version: "v2alpha1"}
2626

27-
// Kind takes an unqualified kind and returns back a Group qualified GroupKind.
28-
func Kind(kind string) schema.GroupKind {
27+
// GroupKindFromKind takes an unqualified kind and returns back a Group qualified GroupKind.
28+
func GroupKindFromKind(kind string) schema.GroupKind {
2929
return SchemeGroupVersion.WithKind(kind).GroupKind()
3030
}
3131

pkg/apis/subscriptions/v2alpha1/types.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ package v2alpha1
1515

1616
import (
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
19+
"github.com/dapr/dapr/pkg/apis/common"
20+
"github.com/dapr/dapr/pkg/apis/subscriptions"
21+
)
22+
23+
const (
24+
Kind = "Subscription"
25+
Version = "v2alpha1"
1826
)
1927

2028
// +genclient
@@ -90,3 +98,43 @@ type SubscriptionList struct {
9098

9199
Items []Subscription `json:"items"`
92100
}
101+
102+
func (Subscription) Kind() string {
103+
return Kind
104+
}
105+
106+
func (Subscription) APIVersion() string {
107+
return subscriptions.GroupName + "/" + Version
108+
}
109+
110+
// EmptyMetaDeepCopy returns a new instance of the subscription type with the
111+
// TypeMeta's Kind and APIVersion fields set.
112+
func (s Subscription) EmptyMetaDeepCopy() metav1.Object {
113+
n := s.DeepCopy()
114+
n.TypeMeta = metav1.TypeMeta{
115+
Kind: Kind,
116+
APIVersion: subscriptions.GroupName + "/" + Version,
117+
}
118+
n.ObjectMeta = metav1.ObjectMeta{Name: s.Name}
119+
return n
120+
}
121+
122+
func (s Subscription) GetName() string {
123+
return s.Name
124+
}
125+
126+
func (s Subscription) GetNamespace() string {
127+
return s.Namespace
128+
}
129+
130+
func (s Subscription) GetSecretStore() string {
131+
return ""
132+
}
133+
134+
func (s Subscription) LogName() string {
135+
return s.GetName()
136+
}
137+
138+
func (s Subscription) NameValuePairs() []common.NameValuePair {
139+
return nil
140+
}

pkg/internal/apis/namevaluepair.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
)
2323

2424
type GenericNameValueResource struct {
25-
Name string
26-
Namespace string
27-
SecretStore string
28-
ResourceKind string
29-
Pairs []common.NameValuePair
25+
Name string
26+
Namespace string
27+
SecretStore string
28+
ResourceKind string
29+
ResourceAPIVersion string
30+
Pairs []common.NameValuePair
3031
}
3132

3233
func (g GenericNameValueResource) Kind() string {
@@ -37,6 +38,10 @@ func (g GenericNameValueResource) GetName() string {
3738
return g.Name
3839
}
3940

41+
func (g GenericNameValueResource) APIVersion() string {
42+
return g.ResourceAPIVersion
43+
}
44+
4045
func (g GenericNameValueResource) GetNamespace() string {
4146
return g.Namespace
4247
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2024 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package disk
15+
16+
import (
17+
compapi "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
18+
"github.com/dapr/dapr/pkg/internal/loader"
19+
)
20+
21+
func NewComponents(paths ...string) loader.Loader[compapi.Component] {
22+
return new[compapi.Component](paths...)
23+
}

0 commit comments

Comments
 (0)