-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add stats, status, querystate, putstate, trigger cmds for Pulsar Functions #34
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a1dad2d
Add stats, status, querystate, putstate, trigger cmds for Pulsar Func…
wolfstudy 1ad77e5
merge master code
wolfstudy 63586d4
debug ci error
wolfstudy 368311c
debug ci error
wolfstudy 16c613a
fix ci error
wolfstudy ffc24b0
fix ci error
wolfstudy 0cc45af
fix ci error
wolfstudy 3b0a0cb
fix ci error
wolfstudy 228ea17
fix ci error
wolfstudy 6c231fd
fix comments
wolfstudy f2ab31e
fix comments
wolfstudy bd416cc
fix comments
wolfstudy 7750959
fix comments
wolfstudy 665dc63
fix comments
wolfstudy 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 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 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 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 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,171 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 functions | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/pflag" | ||
"github.com/streamnative/pulsarctl/pkg/cmdutils" | ||
"github.com/streamnative/pulsarctl/pkg/pulsar" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
func putstateFunctionsCmd(vc *cmdutils.VerbCmd) { | ||
desc := pulsar.LongDescription{} | ||
desc.CommandUsedFor = "Put a key/value pair to the state associated with a Pulsar Function." | ||
desc.CommandPermission = "This command requires namespace function permissions." | ||
|
||
var examples []pulsar.Example | ||
putstate := pulsar.Example{ | ||
Desc: "Put a key/<string value> pair to the state associated with a Pulsar Function", | ||
Command: "pulsarctl functions putstate \n" + | ||
"\t--tenant public\n" + | ||
"\t--namespace default\n" + | ||
"\t--name <the name of Pulsar Function> \n" + | ||
"\t<key name> - <string value> ", | ||
} | ||
examples = append(examples, putstate) | ||
|
||
putstateWithByte := pulsar.Example{ | ||
Desc: "Put a key/<file path> pair to the state associated with a Pulsar Function", | ||
Command: "pulsarctl functions putstate \n" + | ||
"\t--tenant public\n" + | ||
"\t--namespace default\n" + | ||
"\t--name <the name of Pulsar Function> \n" + | ||
"\t<key name> = <file path> ", | ||
} | ||
examples = append(examples, putstateWithByte) | ||
|
||
putstateWithFQFN := pulsar.Example{ | ||
Desc: "Put a key/value pair to the state associated with a Pulsar Function with FQFN", | ||
Command: "pulsarctl functions putstate \n" + | ||
"\t--fqfn tenant/namespace/name [eg: public/default/ExampleFunctions] \n" + | ||
"\t<key name> - <string value> ", | ||
} | ||
examples = append(examples, putstateWithFQFN) | ||
desc.CommandExamples = examples | ||
|
||
var out []pulsar.Output | ||
successOut := pulsar.Output{ | ||
Desc: "normal output", | ||
Out: "Put state <the function state> successfully", | ||
} | ||
|
||
failOut := pulsar.Output{ | ||
Desc: "You must specify a name for the Pulsar Functions or a FQFN, please check the --name args", | ||
Out: "[✖] you must specify a name for the function or a Fully Qualified Function Name (FQFN)", | ||
} | ||
|
||
failOutWithNameNotExist := pulsar.Output{ | ||
Desc: "The name of Pulsar Functions doesn't exist, please check the `--name` arg", | ||
Out: "[✖] code: 404 reason: Function <your function name> doesn't exist", | ||
} | ||
|
||
failOutWithKeyOrValueNotExist := pulsar.Output{ | ||
Desc: "The state key and state value not specified, please check your input format", | ||
Out: "[✖] need to specified the state key and state value", | ||
} | ||
|
||
fileOutErrInputFormat := pulsar.Output{ | ||
Desc: "The format of the input is incorrect, please check.", | ||
Out: "[✖] error input format", | ||
} | ||
|
||
out = append(out, successOut, failOut, failOutWithNameNotExist, failOutWithKeyOrValueNotExist, fileOutErrInputFormat) | ||
desc.CommandOutput = out | ||
|
||
vc.SetDescription( | ||
"putstate", | ||
"Put a key/value pair to the state associated with a Pulsar Function", | ||
desc.ToString(), | ||
"putstate", | ||
) | ||
|
||
functionData := &pulsar.FunctionData{} | ||
|
||
// set the run function | ||
vc.SetRunFuncWithNameArgs(func() error { | ||
return doPutStateFunction(vc, functionData) | ||
}, checkPutStateArgs) | ||
|
||
// register the params | ||
vc.FlagSetGroup.InFlagSet("FunctionsConfig", func(flagSet *pflag.FlagSet) { | ||
flagSet.StringVar( | ||
&functionData.FQFN, | ||
"fqfn", | ||
"", | ||
"The Fully Qualified Function Name (FQFN) for the function") | ||
|
||
flagSet.StringVar( | ||
&functionData.Tenant, | ||
"tenant", | ||
"", | ||
"The tenant of a Pulsar Function") | ||
|
||
flagSet.StringVar( | ||
&functionData.Namespace, | ||
"namespace", | ||
"", | ||
"The namespace of a Pulsar Function") | ||
|
||
flagSet.StringVar( | ||
&functionData.FuncName, | ||
"name", | ||
"", | ||
"The name of a Pulsar Function") | ||
}) | ||
} | ||
|
||
func doPutStateFunction(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error { | ||
err := processBaseArguments(funcData) | ||
if err != nil { | ||
vc.Command.Help() | ||
return err | ||
} | ||
admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) | ||
|
||
var state pulsar.FunctionState | ||
|
||
state.Key = vc.NameArgs[0] | ||
value := vc.NameArgs[1] | ||
|
||
fmt.Println("value:", value) | ||
|
||
if value == "-" { | ||
state.StringValue = strings.Join(vc.NameArgs[2:], " ") | ||
} else if value == "=" { | ||
contents, err := ioutil.ReadFile(vc.NameArgs[2]) | ||
if err != nil { | ||
return err | ||
} | ||
state.ByteValue = contents | ||
} else { | ||
return errors.New("error input format") | ||
} | ||
|
||
err = admin.Functions().PutFunctionState(funcData.Tenant, funcData.Namespace, funcData.FuncName, state) | ||
if err != nil { | ||
cmdutils.PrintError(vc.Command.OutOrStderr(), err) | ||
} else { | ||
vc.Command.Printf("Put state %+v successfully", state) | ||
} | ||
|
||
return err | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please take a look at kubectl.
As I commented before, I was suggesting all the named args after
-
(we can use--
similar as kubectl as well) are the string value.I will explain that using following examples as below:
for bytes-value, I was suggesting using
>
as separator. but I forgot>
is a special character in bash. we can use a different separator. maybe---
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use
=
as the separator between key/byteValue pair?>
or<
means input or output redirection--
This marks end of parameter (option) list.---
bad flag syntax: ---