|
| 1 | +/* |
| 2 | +Copyright 2021 The KCP Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package bind |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + apisv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/apis/v1alpha1" |
| 26 | + kcpclient "github.com/kcp-dev/kcp/pkg/client/clientset/versioned" |
| 27 | + "github.com/kcp-dev/kcp/pkg/cmd/help" |
| 28 | + "k8s.io/client-go/tools/clientcmd" |
| 29 | + "k8s.io/klog/v2" |
| 30 | +) |
| 31 | + |
| 32 | +func NewCmd() { |
| 33 | + bindCmd := &cobra.Command{ |
| 34 | + Use: "bind", |
| 35 | + Short: "Bind a catalog entry to a workspace", |
| 36 | + Long: help.Doc(` |
| 37 | + Bind a catalog entry to a workspace |
| 38 | +
|
| 39 | + The bind process creates an APIBinding using the ExportReference information |
| 40 | + in the CatalogEntry. The RBAC that is required to use APIBinding is not created |
| 41 | + as a part of this process and must be handled separately. |
| 42 | + `), |
| 43 | + RunE: runBindCmdFunc, |
| 44 | + } |
| 45 | + |
| 46 | + // TODO(dinhxuanvu): Use flags to accept inputs for now. Need to reevaluate this |
| 47 | + // approach later to see if this is a good UX |
| 48 | + bindCmd.Flags().StringP("entryname", "-e", "", "the name of the catalog entry") |
| 49 | + if err := bindCmd.MarkFlagRequired("entryname"); err != nil { |
| 50 | + klog.Fatalf("Failed to set required `entryname` flag for `bind` command: %s", err.Error()) |
| 51 | + } |
| 52 | + bindCmd.Flags().StringP("workspace", "-w", "", "the workspace path where catalog entry locates") |
| 53 | + if err := bindCmd.MarkFlagRequired("workspace"); err != nil { |
| 54 | + klog.Fatalf("Failed to set required `workspace` flag for `bind` command: %s", err.Error()) |
| 55 | + } |
| 56 | + bindCmd.Flags().StringP("target", "-t", "", "the targeted workspace where the entry should be binded to") |
| 57 | + if err := bindCmd.MarkFlagRequired("target"); err != nil { |
| 58 | + klog.Fatalf("Failed to set required `target` flag for `bind` command: %s", err.Error()) |
| 59 | + } |
| 60 | + |
| 61 | + return bindCmd |
| 62 | +} |
| 63 | + |
| 64 | +// runBindCmdFunc creates APIBindings from the list of References in CatalogEntry |
| 65 | +// User is responsible to create all of permissons/RBAC necessary to enable |
| 66 | +// APIBindings to work in the particular workspace. |
| 67 | +// TODO(dinhxuanvu): construct a permission/RBAC request model |
| 68 | +func runBindCmdFunc(cmd *cobra.Command, _ []string) error { |
| 69 | + // Get kubeconfig to access the cluster |
| 70 | + // Assume this cluster has kcp all setup and running |
| 71 | + kubeconfigPath := cmd.Flag("kubeconfig").Value.String() |
| 72 | + configLoader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, nil) |
| 73 | + config, err := configLoader.ClientConfig() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + kcpClient, err := kcpclient.NewClusterForConfig(config) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to create kcp client: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + entryName := cmd.Flag("entryname").Value.String() |
| 84 | + workspace := cmd.Flag("workspace").Value.String() |
| 85 | + targetWS := cmd.Flag("target").Value.String() |
| 86 | + // Get the catalog entry from the worksplace using RESTClient for now |
| 87 | + // TODO(dinhxuanvu): potentially want to add catalog entry into typed clientset |
| 88 | + // or create its own clienset in the repo |
| 89 | + // TODO(dinhxuanvu): figure out the AbsPath for workspace and api info |
| 90 | + entry, err := kcpClient.RESTClient(). |
| 91 | + Get(). |
| 92 | + AbsPath("/apis/<api>/<version>"). |
| 93 | + Resource("catalogentries"). |
| 94 | + Name(entryName). |
| 95 | + DoRaw(context.TODO()) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to retrieve catalog entry: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Ensure that the target workspace exists |
| 101 | + wp, err := kcpClient.TenancyV1beta1().Workspaces().Get(context.TODO(), targetWS, metav1.CreateOptions{}) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("failed to retrieve target workspace: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + // Generate the APIBinding for each reference in catalog entry |
| 107 | + apiBindings := []apisv1alpha1.APIBinding{} |
| 108 | + for _, ref := range entry.References { |
| 109 | + // Check if ref is valid. Skip if invalid |
| 110 | + if ref.Workspace.Path == "" || ref.Workspace.ExportName == "" { |
| 111 | + klog.Infof("Invalid reference: %q/%q", ref.Workspace.Path, ref.Workspace.ExportName) |
| 112 | + continue |
| 113 | + } |
| 114 | + apibinding := &apisv1alpha1.APIBinding{ |
| 115 | + ObjectMeta: metav1.ObjectMeta{ |
| 116 | + Name: ref.Workspace.ExportName, |
| 117 | + }, |
| 118 | + Spec: apisv1alpha1.APIBindingSpec{ |
| 119 | + Reference: ref, |
| 120 | + }, |
| 121 | + } |
| 122 | + apiBindings = append(apiBindings, apibinding) |
| 123 | + } |
| 124 | + |
| 125 | + // Apply the APIBinding(s) to the target workspace |
| 126 | + for _, binding := range apiBindings { |
| 127 | + created, err := kcpClient.ApisV1alpha1().APIBindings().Create(context.TODO(), binding, metav1.CreateOptions{}) |
| 128 | + if err != nil { |
| 129 | + klog.Infof("Failed to create API binding %s: %w", binding.Name, err) |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + if apierrors.IsAlreadyExists(err) { |
| 134 | + klog.Infof("APIBinding %s already exists in workspace %s", binding.Name, targetWS) |
| 135 | + } |
| 136 | + |
| 137 | + existing, err := kcpClient.ApisV1alpha1().APIBindings().Get(context.TODO(), binding.Name, metav1.GetOptions{}) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + klog.Infof("Updating API binding %s", binding.Name) |
| 143 | + existing.Spec = binding.Spec |
| 144 | + if _, err := kcpClient.ApisV1alpha1().APIBindings().Update(ctx, existing, metav1.UpdateOptions{}); err != nil { |
| 145 | + return fmt.Errorf("could not update API binding %s in workspace %s: %w", existing.Name, targetWS, err) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments