-
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
feat: add inactive topic policies command for topic #444
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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 topic | ||
|
||
import ( | ||
"github.com/streamnative/pulsarctl/pkg/cmdutils" | ||
"github.com/streamnative/pulsarctl/pkg/pulsar/utils" | ||
) | ||
|
||
func GetInactiveTopicCmd(vc *cmdutils.VerbCmd) { | ||
var desc cmdutils.LongDescription | ||
desc.CommandUsedFor = "Get the inactive topic policies on a topic" | ||
desc.CommandPermission = "This command requires tenant admin permissions." | ||
|
||
var examples []cmdutils.Example | ||
examples = append(examples, cmdutils.Example{ | ||
Desc: desc.CommandUsedFor, | ||
Command: "pulsarctl topics get-inactive-topic-policies [topic]", | ||
}) | ||
desc.CommandExamples = examples | ||
|
||
vc.SetDescription( | ||
"get-inactive-topic-policies", | ||
desc.CommandUsedFor, | ||
desc.ToString(), | ||
desc.ExampleToString()) | ||
|
||
var applied bool | ||
vc.Command.Flags().BoolVarP(&applied, "applied", "", false, "Get the applied policy for the topic") | ||
|
||
vc.SetRunFuncWithNameArg(func() error { | ||
return doGetInactiveTopic(vc, applied) | ||
}, "the topic name is not specified or the topic name is specified more than one") | ||
} | ||
|
||
func doGetInactiveTopic(vc *cmdutils.VerbCmd, applied bool) error { | ||
topic, err := utils.GetTopicName(vc.NameArg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
admin := cmdutils.NewPulsarClient() | ||
response, err := admin.Topics().GetInactiveTopicPolicies(*topic, applied) | ||
if err == nil { | ||
cmdutils.PrintJSON(vc.Command.OutOrStdout(), &response) | ||
} | ||
|
||
return err | ||
} |
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,78 @@ | ||
// 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 topic | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/streamnative/pulsarctl/pkg/pulsar/utils" | ||
"github.com/streamnative/pulsarctl/pkg/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInactiveTopicCmd(t *testing.T) { | ||
topicName := fmt.Sprintf("persistent://public/default/test-inactive-topic-%s", | ||
test.RandomSuffix()) | ||
createArgs := []string{"create", topicName, "1"} | ||
_, execErr, _, _ := TestTopicCommands(CreateTopicCmd, createArgs) | ||
assert.Nil(t, execErr) | ||
|
||
<-time.After(5 * time.Second) | ||
|
||
setArgs := []string{"set-inactive-topic-policies", topicName, | ||
"-e=true", | ||
"-t", "1h", | ||
"-m", "delete_when_no_subscriptions"} | ||
out, execErr, _, _ := TestTopicCommands(SetInactiveTopicCmd, setArgs) | ||
assert.Nil(t, execErr) | ||
assert.Equal(t, out.String(), | ||
fmt.Sprintf("Set inactive topic policies successfully for [%s]", topicName)) | ||
|
||
<-time.After(5 * time.Second) | ||
|
||
getArgs := []string{"get-inactive-topic-policies", topicName} | ||
out, execErr, _, _ = TestTopicCommands(GetInactiveTopicCmd, getArgs) | ||
assert.Nil(t, execErr) | ||
|
||
var inactiveTopic utils.InactiveTopicPolicies | ||
err := json.Unmarshal(out.Bytes(), &inactiveTopic) | ||
assert.NoError(t, err) | ||
assert.Equal(t, true, inactiveTopic.DeleteWhileInactive) | ||
assert.Equal(t, 3600, inactiveTopic.MaxInactiveDurationSeconds) | ||
assert.Equal(t, "delete_when_no_subscriptions", inactiveTopic.InactiveTopicDeleteMode.String()) | ||
|
||
removeArgs := []string{"remove-inactive-topic-policies", topicName} | ||
out, execErr, _, _ = TestTopicCommands(RemoveInactiveTopicCmd, removeArgs) | ||
assert.Nil(t, execErr) | ||
assert.Equal(t, out.String(), | ||
fmt.Sprintf("Remove inactive topic policies successfully from [%s]", topicName)) | ||
|
||
<-time.After(5 * time.Second) | ||
|
||
out, execErr, _, _ = TestTopicCommands(GetInactiveTopicCmd, getArgs) | ||
assert.Nil(t, execErr) | ||
|
||
err = json.Unmarshal(out.Bytes(), &inactiveTopic) | ||
assert.NoError(t, err) | ||
assert.Equal(t, false, inactiveTopic.DeleteWhileInactive) | ||
assert.Equal(t, 0, inactiveTopic.MaxInactiveDurationSeconds) | ||
assert.Nil(t, inactiveTopic.InactiveTopicDeleteMode) | ||
} |
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,60 @@ | ||
// 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 topic | ||
|
||
import ( | ||
"github.com/streamnative/pulsarctl/pkg/cmdutils" | ||
"github.com/streamnative/pulsarctl/pkg/pulsar/utils" | ||
) | ||
|
||
func RemoveInactiveTopicCmd(vc *cmdutils.VerbCmd) { | ||
var desc cmdutils.LongDescription | ||
desc.CommandUsedFor = "Remove inactive topic policies from a topic" | ||
desc.CommandPermission = "This command requires tenant admin permissions." | ||
|
||
var examples []cmdutils.Example | ||
examples = append(examples, cmdutils.Example{ | ||
Desc: desc.CommandUsedFor, | ||
Command: "pulsarctl topics remove-inactive-topic-policies [topic]", | ||
}) | ||
desc.CommandExamples = examples | ||
|
||
vc.SetDescription( | ||
"remove-inactive-topic-policies", | ||
desc.CommandUsedFor, | ||
desc.ToString(), | ||
desc.ExampleToString()) | ||
|
||
vc.SetRunFuncWithNameArg(func() error { | ||
return doRemoveInactiveTopic(vc) | ||
}, "the topic name is not specified or the topic name is specified more than one") | ||
} | ||
|
||
func doRemoveInactiveTopic(vc *cmdutils.VerbCmd) error { | ||
topic, err := utils.GetTopicName(vc.NameArg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
admin := cmdutils.NewPulsarClient() | ||
err = admin.Topics().RemoveInactiveTopicPolicies(*topic) | ||
if err == nil { | ||
vc.Command.Printf("Remove inactive topic policies successfully from [%s]", topic.String()) | ||
} | ||
return err | ||
} |
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,112 @@ | ||
// 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 topic | ||
|
||
import ( | ||
"github.com/streamnative/pulsarctl/pkg/cmdutils" | ||
ctlutils "github.com/streamnative/pulsarctl/pkg/ctl/utils" | ||
"github.com/streamnative/pulsarctl/pkg/pulsar/utils" | ||
) | ||
|
||
type setInactiveTopicPoliciesArgs struct { | ||
deleteWhileInactive bool | ||
deleteInactiveTopicsMaxInactiveDuration string | ||
inactiveTopicDeleteMode string | ||
} | ||
|
||
func SetInactiveTopicCmd(vc *cmdutils.VerbCmd) { | ||
var desc cmdutils.LongDescription | ||
desc.CommandUsedFor = "Set the inactive topic policies on a topic" | ||
desc.CommandPermission = "This command requires tenant admin permissions." | ||
|
||
var examples []cmdutils.Example | ||
set := cmdutils.Example{ | ||
Desc: desc.CommandUsedFor, | ||
Command: "pulsarctl topics set-inactive-topic-policies [topic] \n" + | ||
"\t--enable-delete-while-inactive true \n" + | ||
"\t--max-inactive-duration 1h \n" + | ||
"\t--delete-mode delete_when_no_subscriptions", | ||
} | ||
|
||
examples = append(examples, set) | ||
desc.CommandExamples = examples | ||
|
||
args := setInactiveTopicPoliciesArgs{} | ||
|
||
vc.Command.Flags().BoolVarP(&args.deleteWhileInactive, | ||
"enable-delete-while-inactive", | ||
"e", | ||
false, | ||
"Control whether deletion is enabled while inactive") | ||
|
||
vc.Command.Flags().StringVarP(&args.deleteInactiveTopicsMaxInactiveDuration, | ||
"max-inactive-duration", | ||
"t", | ||
"", | ||
"Max duration of topic inactivity in seconds, "+ | ||
"topics that are inactive for longer than this value will be deleted (eg: 1s, 10s, 1m, 5h, 3d)") | ||
vc.Command.Flags().StringVarP(&args.inactiveTopicDeleteMode, | ||
"delete-mode", | ||
"m", | ||
"", | ||
"Mode of delete inactive topic, "+ | ||
"Valid options are: [delete_when_no_subscriptions, delete_when_subscriptions_caught_up]") | ||
|
||
_ = vc.Command.MarkFlagRequired("delete-mode") | ||
_ = vc.Command.MarkFlagRequired("max-inactive-duration") | ||
|
||
vc.SetDescription( | ||
"set-inactive-topic-policies", | ||
desc.CommandUsedFor, | ||
desc.ToString(), | ||
desc.ExampleToString()) | ||
|
||
vc.SetRunFuncWithNameArg(func() error { | ||
return doSetInactiveTopic(vc, args) | ||
}, "the topic name is not specified or the topic name is specified more than one") | ||
} | ||
|
||
func doSetInactiveTopic(vc *cmdutils.VerbCmd, args setInactiveTopicPoliciesArgs) error { | ||
inactiveTopicDeleteMode, err := utils.ParseInactiveTopicDeleteMode(args.inactiveTopicDeleteMode) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
maxInactiveDuration, err := ctlutils.ParseRelativeTimeInSeconds(args.deleteInactiveTopicsMaxInactiveDuration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body := utils.NewInactiveTopicPolicies( | ||
&inactiveTopicDeleteMode, | ||
int(maxInactiveDuration.Seconds()), | ||
args.deleteWhileInactive) | ||
|
||
topic, err := utils.GetTopicName(vc.NameArg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
admin := cmdutils.NewPulsarClient() | ||
err = admin.Topics().SetInactiveTopicPolicies(*topic, body) | ||
if err == nil { | ||
vc.Command.Printf("Set inactive topic policies successfully for [%s]", topic.String()) | ||
} | ||
|
||
return err | ||
} |
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
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.
We also need to add some failure case tests.
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.
I think we just need to test the flag required by
set-inactive-topic-policies
.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.
The failure case is used to check the error response is parsed correctly at the pulsarctl
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.
Do you want to test the response from pulsar?