-
Notifications
You must be signed in to change notification settings - Fork 209
Add --kubernetes flag to make dapr invoke cmd support kubernetes
#862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
imneov
wants to merge
37
commits into
dapr:master
Choose a base branch
from
imneov:feat/interact-with-kubernetes-dapr-apps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
4e87a7a
feat: support invoke cmd on kubernetes mode
bcf8f1a
feat: update cmd help
2c3a2cd
feat: support windows style delimiter (#834)
LKI 0a2e985
fix: only dapr appPort can be accessed in proxy mode
4bb0f15
feat: add test
f75e0cf
feat: update test
8df3681
fix: collect all matching pods
525921c
fix: --data has an unclosed single quote
9dad732
fix: optimize error handling
c2740ad
fix: line break
284b79f
fix: remove extra line
8ce99ea
feat: remove no need to use fmt.Sprintf
fd352d7
feat: add usage instructions
5b24dfd
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov 37f88b3
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov d577657
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov a07791f
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
pravinpushkar b9a6aff
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
mukundansundar 758011b
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
pravinpushkar df70af9
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
pravinpushkar c722cae
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
mukundansundar 1915b6c
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
mukundansundar a32578f
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov a2e55e8
fix lint
imneov bfff724
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov 114ad66
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov c5139e7
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
pravinpushkar 1924033
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov e1e8191
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
pravinpushkar ab63a7d
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov ae41303
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
pravinpushkar 6da5ebb
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
shubham1172 0b010cb
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov 7bf3b40
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
yaron2 f62550c
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov 5ad56cc
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov a92ea7f
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
imneov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /* | ||
| Copyright 2021 The Dapr Authors | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package kubernetes | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| core_v1 "k8s.io/api/core/v1" | ||
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/net" | ||
| k8s "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| type AppInfo struct { | ||
| AppID string `csv:"APP ID" json:"appId" yaml:"appId"` | ||
| HTTPPort string `csv:"HTTP PORT" json:"httpPort" yaml:"httpPort"` | ||
| GRPCPort string `csv:"GRPC PORT" json:"grpcPort" yaml:"grpcPort"` | ||
| AppPort string `csv:"APP PORT" json:"appPort" yaml:"appPort"` | ||
| PodName string `csv:"POD NAME" json:"podName" yaml:"podName"` | ||
| Namespace string `csv:"NAMESPACE" json:"namespace" yaml:"namespace"` | ||
| } | ||
|
|
||
| type ( | ||
| DaprPod core_v1.Pod | ||
| DaprAppList []*AppInfo | ||
| ) | ||
|
|
||
| // Invoke is a command to invoke a remote or local dapr instance. | ||
| func Invoke(appID, method string, data []byte, verb string) (string, error) { | ||
| client, err := Client() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| app, err := GetAppInfo(client, appID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return invoke(client.CoreV1().RESTClient(), app, method, data, verb) | ||
| } | ||
|
|
||
| func invoke(client rest.Interface, app *AppInfo, method string, data []byte, verb string) (string, error) { | ||
| res, err := app.Request(client.Verb(verb), method, data, verb) | ||
| if err != nil { | ||
| return "", fmt.Errorf("error get request: %w", err) | ||
| } | ||
|
|
||
| result := res.Do(context.TODO()) | ||
| rawbody, err := result.Raw() | ||
| if err != nil { | ||
| return "", fmt.Errorf("error get raw: %w", err) | ||
| } | ||
|
|
||
| if len(rawbody) > 0 { | ||
| return string(rawbody), nil | ||
| } | ||
|
|
||
| return "", nil | ||
| } | ||
|
|
||
| func GetAppInfo(client k8s.Interface, appID string) (*AppInfo, error) { | ||
| list, err := ListAppInfos(client, appID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(list) == 0 { | ||
| return nil, fmt.Errorf("%s not found", appID) | ||
| } | ||
| app := list[0] | ||
| return app, nil | ||
| } | ||
|
|
||
| // List outputs plugins. | ||
| func ListAppInfos(client k8s.Interface, appIDs ...string) (DaprAppList, error) { | ||
| opts := v1.ListOptions{} | ||
| podList, err := client.CoreV1().Pods(v1.NamespaceAll).List(context.TODO(), opts) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("err get pods list:%w", err) | ||
| } | ||
|
|
||
| fn := func(*AppInfo) bool { | ||
| return true | ||
| } | ||
| if len(appIDs) > 0 { | ||
| fn = func(a *AppInfo) bool { | ||
| for _, id := range appIDs { | ||
| if id != "" && a.AppID == id { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| l := make(DaprAppList, 0) | ||
| for _, p := range podList.Items { | ||
| p := DaprPod(p) | ||
| for _, c := range p.Spec.Containers { | ||
| if c.Name == "daprd" { | ||
| app := getAppInfoFromPod(&p) | ||
| if fn(app) { | ||
| l = append(l, app) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return l, nil | ||
| } | ||
|
|
||
| func getAppInfoFromPod(p *DaprPod) *AppInfo { | ||
| var appInfo *AppInfo | ||
| for _, c := range p.Spec.Containers { | ||
| if c.Name == "daprd" { | ||
| appInfo = &AppInfo{ | ||
| PodName: p.Name, | ||
| Namespace: p.Namespace, | ||
| } | ||
| for i, arg := range c.Args { | ||
| if arg == "--app-port" { | ||
imneov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| port := c.Args[i+1] | ||
| appInfo.AppPort = port | ||
| } else if arg == "--dapr-http-port" { | ||
| port := c.Args[i+1] | ||
| appInfo.HTTPPort = port | ||
| } else if arg == "--dapr-grpc-port" { | ||
| port := c.Args[i+1] | ||
| appInfo.GRPCPort = port | ||
| } else if arg == "--app-id" { | ||
| id := c.Args[i+1] | ||
| appInfo.AppID = id | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return appInfo | ||
| } | ||
|
|
||
| func (a *AppInfo) Request(r *rest.Request, method string, data []byte, verb string) (*rest.Request, error) { | ||
| r = r.Namespace(a.Namespace). | ||
| Resource("pods"). | ||
| SubResource("proxy"). | ||
| SetHeader("Content-Type", "application/json"). | ||
| Name(net.JoinSchemeNamePort("", a.PodName, a.AppPort)) | ||
| if data != nil { | ||
| r = r.Body(data) | ||
| } | ||
|
|
||
| u, err := url.Parse(method) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error parse method %s: %w", method, err) | ||
| } | ||
|
|
||
| r = r.Suffix(u.Path) | ||
|
|
||
| for k, vs := range u.Query() { | ||
| r = r.Param(k, strings.Join(vs, ",")) | ||
| } | ||
|
|
||
| return r, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.