This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Add CLI command for binding a catalog entry to a workspace #6
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| /* | ||
| Copyright 2022 The KCP 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 catalogentry | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "reflect" | ||
| "time" | ||
|
|
||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| kcpclienthelper "github.com/kcp-dev/apimachinery/pkg/client" | ||
| catalogv1alpha1 "github.com/kcp-dev/catalog/api/v1alpha1" | ||
| apisv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/apis/v1alpha1" | ||
| pluginhelpers "github.com/kcp-dev/kcp/pkg/cliplugins/helpers" | ||
| "github.com/kcp-dev/logicalcluster/v2" | ||
| "github.com/spf13/cobra" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| "k8s.io/cli-runtime/pkg/genericclioptions" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/kcp-dev/kcp/pkg/cliplugins/base" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| // BindOptions contains the options for creating APIBindings for CE | ||
| type BindOptions struct { | ||
| *base.Options | ||
| // CatalogEntryRef is the argument accepted by the command. It contains the | ||
| // reference to where CatalogEntry exists. For ex: <absolute_ref_to_workspace>:<catalogEntry>. | ||
| CatalogEntryRef string | ||
| // BindWaitTimeout is how long to wait for the apibindings to be created and successful. | ||
| BindWaitTimeout time.Duration | ||
| } | ||
|
|
||
| // NewBindOptions returns new BindOptions. | ||
| func NewBindOptions(streams genericclioptions.IOStreams) *BindOptions { | ||
| return &BindOptions{ | ||
| Options: base.NewOptions(streams), | ||
| BindWaitTimeout: 30 * time.Second, | ||
| } | ||
| } | ||
|
|
||
| // BindFlags binds fields to cmd's flagset. | ||
| func (b *BindOptions) BindFlags(cmd *cobra.Command) { | ||
| b.Options.BindFlags(cmd) | ||
| cmd.Flags().DurationVar(&b.BindWaitTimeout, "timeout", b.BindWaitTimeout, "Duration to wait for the bindings to be created and bound successfully.") | ||
| } | ||
|
|
||
| // Complete ensures all fields are initialized. | ||
| func (b *BindOptions) Complete(args []string) error { | ||
| if err := b.Options.Complete(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(args) > 0 { | ||
| b.CatalogEntryRef = args[0] | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Validate validates the BindOptions are complete and usable. | ||
| func (b *BindOptions) Validate() error { | ||
| if b.CatalogEntryRef == "" { | ||
| return errors.New("`root:ws:catalogentry_object` reference to bind is required as an argument") | ||
| } | ||
|
|
||
| if !strings.HasPrefix(b.CatalogEntryRef, "root") || !logicalcluster.New(b.CatalogEntryRef).IsValid() { | ||
| return fmt.Errorf("fully qualified reference to workspace where catalog entry exists is required. The format is `root:<ws>:<catalogentry>`") | ||
| } | ||
|
|
||
| return b.Options.Validate() | ||
| } | ||
|
|
||
| // Run creates an apibinding for the user. | ||
| func (b *BindOptions) Run(ctx context.Context) error { | ||
| config, err := b.ClientConfig.ClientConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| baseURL, currentClusterName, err := pluginhelpers.ParseClusterURL(config.Host) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // get the base config, which is needed for creation of clients. | ||
| path, entryName := logicalcluster.New(b.CatalogEntryRef).Split() | ||
| cfg := rest.CopyConfig(config) | ||
| cfg.Host = baseURL.String() | ||
| client, err := newClient(cfg, path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // get the entry referenced in the command to which the user wants to bind. | ||
| entry := catalogv1alpha1.CatalogEntry{} | ||
| err = client.Get(ctx, types.NamespacedName{Name: entryName}, &entry) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot find the catalog entry %q referenced in the command in the workspace %q", entryName, path) | ||
| } | ||
|
|
||
| kcpClient, err := newClient(cfg, currentClusterName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| allErrors := []error{} | ||
|
|
||
| apiBindings := []apisv1alpha1.APIBinding{} | ||
| for _, ref := range entry.Spec.Exports { | ||
| // check if ref is valid. Skip if invalid by logging error. | ||
| if ref.Workspace.Path == "" || ref.Workspace.ExportName == "" { | ||
| if _, err := fmt.Fprintf(b.Out, "invalid reference %q/%q", ref.Workspace.Path, ref.Workspace.ExportName); err != nil { | ||
| allErrors = append(allErrors, err) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| apiBinding := &apisv1alpha1.APIBinding{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| GenerateName: ref.Workspace.ExportName + "-", | ||
| }, | ||
| Spec: apisv1alpha1.APIBindingSpec{ | ||
| Reference: ref, | ||
| }, | ||
| } | ||
|
|
||
| apiBindings = append(apiBindings, *apiBinding) | ||
| } | ||
|
|
||
| // fetch a list of existing binding in the current workspace. | ||
| existingBindingList := apisv1alpha1.APIBindingList{} | ||
| err = kcpClient.List(ctx, &existingBindingList) | ||
| if err != nil { | ||
| allErrors = append(allErrors, err) | ||
| } | ||
|
|
||
| // Create bindings to the target workspace | ||
| bindingsCreatedByClient := []apisv1alpha1.APIBinding{} | ||
| for _, binding := range apiBindings { | ||
| found, err := bindingAlreadyExists(binding, existingBindingList, b.Out) | ||
| if err != nil { | ||
| allErrors = append(allErrors, err) | ||
| } | ||
|
|
||
| // if the binding exists continue, if not create the binding | ||
| if found { | ||
| continue | ||
| } | ||
|
|
||
| err = kcpClient.Create(ctx, &binding) | ||
| if err != nil { | ||
| allErrors = append(allErrors, err) | ||
| } | ||
|
|
||
| bindingsCreatedByClient = append(bindingsCreatedByClient, binding) | ||
| } | ||
|
|
||
| if err := wait.PollImmediate(time.Millisecond*500, b.BindWaitTimeout, func() (done bool, err error) { | ||
| availableBindings := []apisv1alpha1.APIBinding{} | ||
| for _, binding := range bindingsCreatedByClient { | ||
| createdBinding := apisv1alpha1.APIBinding{} | ||
| err = kcpClient.Get(ctx, types.NamespacedName{Name: binding.GetName()}, &createdBinding) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| availableBindings = append(availableBindings, createdBinding) | ||
| } | ||
| return bindReady(availableBindings), nil | ||
| }); err != nil { | ||
| return fmt.Errorf("bindings for catalog entry %s could not be created successfully: %v", entryName, err) | ||
| } | ||
|
|
||
| if _, err := fmt.Fprintf(b.Out, "Apibinding created and bound to catalog entry %s.\n", entryName); err != nil { | ||
| allErrors = append(allErrors, err) | ||
| } | ||
| return utilerrors.NewAggregate(allErrors) | ||
| } | ||
|
|
||
| func bindReady(bindings []apisv1alpha1.APIBinding) bool { | ||
| for _, binding := range bindings { | ||
| if binding.Status.Phase != apisv1alpha1.APIBindingPhaseBound { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func newClient(cfg *rest.Config, clusterName logicalcluster.Name) (client.Client, error) { | ||
| scheme := runtime.NewScheme() | ||
| err := apisv1alpha1.AddToScheme(scheme) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = catalogv1alpha1.AddToScheme(scheme) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return client.New(kcpclienthelper.SetCluster(rest.CopyConfig(cfg), clusterName), client.Options{ | ||
| Scheme: scheme, | ||
| }) | ||
| } | ||
|
|
||
| // bindingAlreadyExists lists out the existing bindings in a workspace, checks if the export reference is the same. If so, | ||
| // it further checks the permission claims and updates the existing binding's claims. | ||
| func bindingAlreadyExists(expectedBinding apisv1alpha1.APIBinding, existingBindingList apisv1alpha1.APIBindingList, wr io.Writer) (bool, error) { | ||
| found := false | ||
|
|
||
| for _, b := range existingBindingList.Items { | ||
| if reflect.DeepEqual(&b.Spec.Reference, &expectedBinding.Spec.Reference) { | ||
| found = true | ||
| // if the specified export reference matches the expected export reference, then check if permission | ||
| // claims also match. | ||
| if !reflect.DeepEqual(b.Spec.PermissionClaims, expectedBinding.Spec.PermissionClaims) { | ||
| // if the permission claims are not equal then print the message. | ||
| // TODO: Add a command to print the differences and print the bindings. | ||
| if _, err := fmt.Fprintf(wr, "Binding for %s already exists, but the permission claims are different. Skipping any action.\n", b.Name); err != nil { | ||
| return found, err | ||
| } | ||
| } | ||
|
|
||
| // if the permission claims are equal then no action is to be done. | ||
| if _, err := fmt.Fprintf(wr, "Found an existing APIExport %s pointing to the same export reference.\n", b.Name); err != nil { | ||
| return found, err | ||
| } | ||
varshaprasad96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| break | ||
| } | ||
| } | ||
| return found, nil | ||
| } | ||
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,64 @@ | ||
| /* | ||
| Copyright 2022 The KCP 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 catalogentry | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "k8s.io/cli-runtime/pkg/genericclioptions" | ||
| ) | ||
|
|
||
| var ( | ||
| bindExampleUses = ` | ||
| # binds to the mentioned catalog entry in the command, e.g the below command will create | ||
| # APIBindings referenced in catalog entry "certificates" present in "root:catalog:cert-manager" workspace. | ||
| %[1]s bind catalogentry root:catalog:cert-manager:certificates | ||
ncdc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ` | ||
| ) | ||
|
|
||
| func New(streams genericclioptions.IOStreams) (*cobra.Command, error) { | ||
| cmd := &cobra.Command{ | ||
| Use: "bind", | ||
| Short: "Operations related to binding with API", | ||
| SilenceUsage: true, | ||
| TraverseChildren: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Help() | ||
| }, | ||
| } | ||
|
|
||
| bindOpts := NewBindOptions(streams) | ||
| bindCmd := &cobra.Command{ | ||
| Use: "catalogentry <workspace_path:catalogentry-name>", | ||
| Short: "Bind to a Catalog Entry", | ||
| Example: fmt.Sprintf(bindExampleUses, "kubectl catalog"), | ||
| SilenceUsage: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := bindOpts.Complete(args); err != nil { | ||
| return err | ||
| } | ||
| if err := bindOpts.Validate(); err != nil { | ||
| return err | ||
| } | ||
| return bindOpts.Run(cmd.Context()) | ||
| }, | ||
| } | ||
| bindOpts.BindFlags(bindCmd) | ||
| cmd.AddCommand(bindCmd) | ||
| return cmd, nil | ||
| } | ||
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.
(no action required) I would probably check for the InitialBindingCompleted or BindingUpToDate condition, but this works too