@@ -14,12 +14,15 @@ limitations under the License.
14
14
package kubernetes
15
15
16
16
import (
17
+ "errors"
17
18
"flag"
18
19
"sync"
19
20
21
+ "k8s.io/apimachinery/pkg/runtime"
20
22
k8s "k8s.io/client-go/kubernetes"
21
23
"k8s.io/client-go/rest"
22
24
"k8s.io/client-go/tools/clientcmd"
25
+ "sigs.k8s.io/controller-runtime/pkg/client"
23
26
24
27
scheme "github.com/dapr/dapr/pkg/client/clientset/versioned"
25
28
@@ -31,6 +34,14 @@ import (
31
34
32
35
// oidc auth
33
36
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
37
+
38
+ componentsapi "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
39
+ configurationapi "github.com/dapr/dapr/pkg/apis/configuration/v1alpha1"
40
+ httpendpointsapi "github.com/dapr/dapr/pkg/apis/httpEndpoint/v1alpha1"
41
+ resiliencyapi "github.com/dapr/dapr/pkg/apis/resiliency/v1alpha1"
42
+ subscriptionsapiV1alpha1 "github.com/dapr/dapr/pkg/apis/subscriptions/v1alpha1"
43
+ subapi "github.com/dapr/dapr/pkg/apis/subscriptions/v2alpha1"
44
+ clientgoscheme "k8s.io/client-go/kubernetes/scheme"
34
45
)
35
46
36
47
var (
@@ -96,3 +107,41 @@ func DaprClient() (scheme.Interface, error) {
96
107
}
97
108
return scheme .NewForConfig (config )
98
109
}
110
+
111
+ // buildScheme builds the scheme for the controller-runtime client
112
+ // from https://github.com/dapr/dapr/blob/eb49e564fbd704ceb1379498fc8e94ad7110840f/pkg/operator/operator.go#L444-L466
113
+ func buildScheme () (* runtime.Scheme , error ) {
114
+ builders := []func (* runtime.Scheme ) error {
115
+ clientgoscheme .AddToScheme ,
116
+ componentsapi .AddToScheme ,
117
+ configurationapi .AddToScheme ,
118
+ resiliencyapi .AddToScheme ,
119
+ httpendpointsapi .AddToScheme ,
120
+ subscriptionsapiV1alpha1 .AddToScheme ,
121
+ subapi .AddToScheme ,
122
+ }
123
+
124
+ errs := make ([]error , len (builders ))
125
+ scheme := runtime .NewScheme ()
126
+ for i , builder := range builders {
127
+ errs [i ] = builder (scheme )
128
+ }
129
+
130
+ return scheme , errors .Join (errs ... )
131
+ }
132
+
133
+ // CtrlClient returns a new Controller-Runtime Client (https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client) - no caching
134
+ // with the scheme built with the Dapr API groups.
135
+ func CtrlClient () (client.Client , error ) {
136
+ config , err := getConfig ()
137
+ if err != nil {
138
+ return nil , err
139
+ }
140
+
141
+ scheme , err := buildScheme ()
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+
146
+ return client .New (config , client.Options {Scheme : scheme })
147
+ }
0 commit comments