Skip to content

Commit 3e1ec31

Browse files
committed
add option to create resources in k8s run app
Signed-off-by: Filinto Duran <[email protected]>
1 parent 8e4cea5 commit 3e1ec31

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ require (
253253
k8s.io/kubectl v0.26.0 // indirect
254254
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
255255
oras.land/oras-go v1.2.2 // indirect
256-
sigs.k8s.io/controller-runtime v0.16.3 // indirect
256+
sigs.k8s.io/controller-runtime v0.16.3
257257
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
258258
sigs.k8s.io/kustomize/api v0.15.0 // indirect
259259
sigs.k8s.io/kustomize/kyaml v0.15.0 // indirect

pkg/kubernetes/client.go

+49
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ limitations under the License.
1414
package kubernetes
1515

1616
import (
17+
"errors"
1718
"flag"
1819
"sync"
1920

21+
"k8s.io/apimachinery/pkg/runtime"
2022
k8s "k8s.io/client-go/kubernetes"
2123
"k8s.io/client-go/rest"
2224
"k8s.io/client-go/tools/clientcmd"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
2326

2427
scheme "github.com/dapr/dapr/pkg/client/clientset/versioned"
2528

@@ -31,6 +34,14 @@ import (
3134

3235
// oidc auth
3336
_ "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"
3445
)
3546

3647
var (
@@ -96,3 +107,41 @@ func DaprClient() (scheme.Interface, error) {
96107
}
97108
return scheme.NewForConfig(config)
98109
}
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+
}

pkg/kubernetes/run.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ func Run(runFilePath string, config runfileconfig.RunFileConfig) (bool, error) {
119119
runStates := []runState{}
120120
print.InfoStatusEvent(os.Stdout, "This is a preview feature and subject to change in future releases.")
121121

122+
ctrlClient, cErr := CtrlClient()
123+
if cErr != nil {
124+
// exit with error.
125+
return true, fmt.Errorf("error getting controller-runtime k8s client: %w", cErr)
126+
}
127+
128+
resources, err := getResources(config.Common.ResourcesPath)
129+
if err != nil {
130+
print.FailureStatusEvent(os.Stderr, "Error getting resources from %q: %s", config.Common.ResourcesPath, err.Error())
131+
exitWithError = true
132+
}
133+
134+
if err := createOrUpdateResources(context.Background(), ctrlClient, resources, namespace); err != nil {
135+
print.FailureStatusEvent(os.Stderr, "Error creating or updating resources: %s", err.Error())
136+
exitWithError = true
137+
}
138+
122139
for _, app := range config.Apps {
123140
print.StatusEvent(os.Stdout, print.LogInfo, "Validating config and starting app %q", app.RunConfig.AppID)
124141
// Set defaults if zero value provided in config yaml.
@@ -140,11 +157,7 @@ func Run(runFilePath string, config runfileconfig.RunFileConfig) (bool, error) {
140157

141158
// create default deployment config.
142159
dep := createDeploymentConfig(daprClient, app)
143-
if err != nil {
144-
print.FailureStatusEvent(os.Stderr, "Error creating deployment file for app %q present in %s: %s", app.RunConfig.AppID, runFilePath, err.Error())
145-
exitWithError = true
146-
break
147-
}
160+
148161
// overwrite <app-id>/.dapr/deploy/service.yaml.
149162
// overwrite <app-id>/.dapr/deploy/deployment.yaml.
150163

0 commit comments

Comments
 (0)