-
Notifications
You must be signed in to change notification settings - Fork 749
rpk/registry: add context list and delete commands #29689
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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
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,36 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "context", | ||
| srcs = [ | ||
| "context.go", | ||
| "delete.go", | ||
| "list.go", | ||
| ], | ||
| importpath = "github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/registry/context", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//src/go/rpk/pkg/adminapi", | ||
| "//src/go/rpk/pkg/config", | ||
| "//src/go/rpk/pkg/out", | ||
| "//src/go/rpk/pkg/schemaregistry", | ||
| "@com_github_spf13_afero//:afero", | ||
| "@com_github_spf13_cobra//:cobra", | ||
| "@com_github_twmb_franz_go_pkg_sr//:sr", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "context_test", | ||
| srcs = ["context_test.go"], | ||
| deps = [ | ||
| ":context", | ||
| "//src/go/rpk/pkg/cli/registry", | ||
| "//src/go/rpk/pkg/config", | ||
| "@com_github_spf13_afero//:afero", | ||
| "@com_github_spf13_cobra//:cobra", | ||
| "@com_github_stretchr_testify//require", | ||
| "@com_github_twmb_franz_go_pkg_sr//:sr", | ||
| "@com_github_twmb_franz_go_pkg_sr//srfake", | ||
| ], | ||
| ) |
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,128 @@ | ||
| // Copyright 2026 Redpanda Data, Inc. | ||
| // | ||
| // Use of this software is governed by the Business Source License | ||
| // included in the file licenses/BSL.md | ||
| // | ||
| // As of the Change Date specified in that file, in accordance with | ||
| // the Business Source License, use of this software will be governed | ||
| // by the Apache License, Version 2.0 | ||
|
|
||
| package context | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/spf13/afero" | ||
| "github.com/spf13/cobra" | ||
| "github.com/twmb/franz-go/pkg/sr" | ||
|
|
||
| "github.com/redpanda-data/redpanda/src/go/rpk/pkg/adminapi" | ||
| "github.com/redpanda-data/redpanda/src/go/rpk/pkg/config" | ||
| ) | ||
|
|
||
| const qualifiedSubjectsConfigKey = "schema_registry_enable_qualified_subjects" | ||
|
|
||
| type contextResponse struct { | ||
| Name string `json:"name" yaml:"name"` | ||
| Mode string `json:"mode" yaml:"mode"` | ||
| Compatibility string `json:"compatibility" yaml:"compatibility"` | ||
| } | ||
|
|
||
| // ListContexts calls cl.Contexts and translates a 404 into a message | ||
| // indicating that the Redpanda cluster does not support schema contexts. | ||
| func ListContexts(ctx context.Context, cl *sr.Client) ([]string, error) { | ||
| contexts, err := cl.Contexts(ctx) | ||
| if err != nil { | ||
| var re *sr.ResponseError | ||
| if errors.As(err, &re) && re.StatusCode == 404 { | ||
| return nil, fmt.Errorf("schema registry contexts are not supported by this cluster") | ||
| } | ||
| return nil, err | ||
| } | ||
| return contexts, nil | ||
| } | ||
|
|
||
| // checkQualifiedSubjectsEnabled verifies that the cluster has the | ||
| // schema_registry_enable_qualified_subjects config set to true via the | ||
| // Admin API. | ||
| func checkQualifiedSubjectsEnabled(ctx context.Context, fs afero.Fs, profile *config.RpkProfile) error { | ||
| ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
| defer cancel() | ||
| cl, err := adminapi.NewClient(ctx, fs, profile) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to verify schema context support via admin API: %w\nUse --skip-context-check to skip this verification", err) | ||
| } | ||
| cfg, err := cl.SingleKeyConfig(ctx, qualifiedSubjectsConfigKey) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to verify schema context support via admin API: %w\nUse --skip-context-check to skip this verification", err) | ||
| } | ||
| val, exists := cfg[qualifiedSubjectsConfigKey] | ||
| if !exists { | ||
| return fmt.Errorf("schema contexts are not supported by this cluster (config key %q not found); the cluster may need upgrading", qualifiedSubjectsConfigKey) | ||
| } | ||
| enabled, ok := val.(bool) | ||
| if !ok { | ||
| return fmt.Errorf("schema contexts are not supported by this cluster (unexpected value for %q: %v)", qualifiedSubjectsConfigKey, val) | ||
| } | ||
| if !enabled { | ||
| return fmt.Errorf("schema contexts are not enabled on this cluster; you may enable it using:\n rpk cluster config set %s true", qualifiedSubjectsConfigKey) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // IsContextSupported checks whether the cluster supports schema contexts | ||
| // by verifying the admin API feature flag. | ||
| func IsContextSupported(ctx context.Context, fs afero.Fs, profile *config.RpkProfile, skipAdminCheck bool) error { | ||
| if skipAdminCheck { | ||
| return nil | ||
| } | ||
| return checkQualifiedSubjectsEnabled(ctx, fs, profile) | ||
| } | ||
|
|
||
| // ValidateContext validates the schema context name format, loads the | ||
| // profile, and confirms the cluster supports contexts via the admin API | ||
| // feature flag. | ||
| func ValidateContext(ctx context.Context, schemaCtx string, fs afero.Fs, p *config.Params, skipAdminCheck bool) error { | ||
| if schemaCtx[0] != '.' { | ||
| return fmt.Errorf("invalid schema context %q: context names must start with a '.'", schemaCtx) | ||
| } | ||
| if strings.Contains(schemaCtx, ":") { | ||
| return fmt.Errorf("invalid schema context %q: context names must not contain ':'", schemaCtx) | ||
| } | ||
| profile, err := p.LoadVirtualProfile(fs) | ||
| if err != nil { | ||
| return fmt.Errorf("rpk unable to load config: %w", err) | ||
| } | ||
| return IsContextSupported(ctx, fs, profile, skipAdminCheck) | ||
| } | ||
|
|
||
| func NewCommand(fs afero.Fs, p *config.Params, _ *string) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "context", | ||
| Args: cobra.ExactArgs(0), | ||
| Short: "Manage schema registry contexts", | ||
|
c-julin marked this conversation as resolved.
|
||
| Long: `Manage schema registry contexts. | ||
|
|
||
| Schema contexts provide namespace isolation within the schema registry, | ||
| allowing multiple independent sets of subjects and schemas to coexist. | ||
|
|
||
| Before using schema contexts, the cluster must have the | ||
| schema_registry_enable_qualified_subjects configuration set to true. You | ||
| can enable it with: | ||
|
|
||
| rpk cluster config set schema_registry_enable_qualified_subjects true | ||
|
|
||
| Use the --schema-context flag on the parent 'registry' command to scope | ||
| operations to a specific context. | ||
| `, | ||
| } | ||
| cmd.AddCommand( | ||
| listCommand(fs, p), | ||
| deleteCommand(fs, p), | ||
| ) | ||
| return cmd | ||
| } | ||
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.
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.
[food for thought, not for this PR, but maybe as a follow-up in next releases]
Would be cool to list the context, including the Mode and Compatibility that has been set. There are some other places like
subject list, where we could add this as well.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.
Its a good suggestion and ive updated it to show mode and compatability for context list, haven't gone deeper than that but something we can look in to