|
| 1 | +/* |
| 2 | +Copyright AppsCode Inc. and Contributors |
| 3 | +
|
| 4 | +Licensed under the AppsCode Community License 1.0.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 | + https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md |
| 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 gateway |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log" |
| 22 | + |
| 23 | + catalogapi "go.bytebuilders.dev/catalog/api/catalog/v1alpha1" |
| 24 | + catgwapi "go.bytebuilders.dev/catalog/api/gateway/v1alpha1" |
| 25 | + |
| 26 | + "github.com/spf13/cobra" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 31 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 32 | + "k8s.io/client-go/discovery" |
| 33 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 34 | + "k8s.io/client-go/rest" |
| 35 | + "k8s.io/klog/v2" |
| 36 | + cmdutil "k8s.io/kubectl/pkg/cmd/util" |
| 37 | + kutil "kmodules.xyz/client-go" |
| 38 | + cu "kmodules.xyz/client-go/client" |
| 39 | + dbapi "kubedb.dev/apimachinery/apis/kubedb/v1" |
| 40 | + kubedbscheme "kubedb.dev/apimachinery/client/clientset/versioned/scheme" |
| 41 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 42 | +) |
| 43 | + |
| 44 | +var scheme = runtime.NewScheme() |
| 45 | + |
| 46 | +func init() { |
| 47 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 48 | + utilruntime.Must(catalogapi.AddToScheme(scheme)) |
| 49 | + utilruntime.Must(catgwapi.AddToScheme(scheme)) |
| 50 | + utilruntime.Must(kubedbscheme.AddToScheme(scheme)) |
| 51 | +} |
| 52 | + |
| 53 | +func NewCmdGateway(f cmdutil.Factory) *cobra.Command { |
| 54 | + opt := newGatewayOpts(f) |
| 55 | + cmd := &cobra.Command{ |
| 56 | + Use: "gateway", |
| 57 | + Short: "Gateway related info", |
| 58 | + DisableAutoGenTag: true, |
| 59 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 60 | + return opt.run() |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + cmd.Flags().StringVarP(&opt.db.resource, "db-type", "t", "mongodb", "Database type") |
| 65 | + cmd.Flags().StringVarP(&opt.db.name, "name", "m", "mg-test", "Database name") |
| 66 | + cmd.Flags().StringVarP(&opt.db.namespace, "namespace", "n", "demo", "Database namespace") |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +type gatewayOpts struct { |
| 71 | + kc client.Client |
| 72 | + disc *discovery.DiscoveryClient |
| 73 | + config *rest.Config |
| 74 | + db dbInfo |
| 75 | + |
| 76 | + mapResourceToKind map[string]string |
| 77 | + mapSingularToKind map[string]string |
| 78 | +} |
| 79 | + |
| 80 | +type dbInfo struct { |
| 81 | + resource string |
| 82 | + name string |
| 83 | + namespace string |
| 84 | +} |
| 85 | + |
| 86 | +func newGatewayOpts(f cmdutil.Factory) *gatewayOpts { |
| 87 | + config, err := f.ToRESTConfig() |
| 88 | + if err != nil { |
| 89 | + log.Fatal(err) |
| 90 | + } |
| 91 | + kc, err := client.New(config, client.Options{Scheme: scheme}) |
| 92 | + if err != nil { |
| 93 | + log.Fatalf("failed to create client: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + disc, err := discovery.NewDiscoveryClientForConfig(config) |
| 97 | + if err != nil { |
| 98 | + log.Fatal("creating discovery client: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + return &gatewayOpts{kc: kc, config: config, disc: disc} |
| 102 | +} |
| 103 | + |
| 104 | +func (g *gatewayOpts) run() error { |
| 105 | + err := g.initMap() |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + kind := g.resolveKind(g.db.resource) + "Binding" |
| 110 | + var binding unstructured.Unstructured |
| 111 | + binding.SetGroupVersionKind(schema.GroupVersionKind{ |
| 112 | + Group: catalogapi.GroupVersion.Group, |
| 113 | + Version: catalogapi.GroupVersion.Version, |
| 114 | + Kind: kind, |
| 115 | + }) |
| 116 | + binding.SetNamespace(g.db.namespace) |
| 117 | + binding.SetName(g.db.name) |
| 118 | + |
| 119 | + // Set spec.sourceRef |
| 120 | + sourceRef := map[string]any{ |
| 121 | + "name": g.db.name, |
| 122 | + } |
| 123 | + |
| 124 | + if g.db.namespace != "" { |
| 125 | + sourceRef["namespace"] = g.db.namespace |
| 126 | + } |
| 127 | + |
| 128 | + if err := unstructured.SetNestedField(binding.Object, sourceRef, "spec", "sourceRef"); err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + vt, err := cu.CreateOrPatch(context.TODO(), g.kc, &binding, func(obj client.Object, createOp bool) client.Object { |
| 133 | + return obj |
| 134 | + // in := obj.(*catalogapi.BindingInterface) |
| 135 | + // return in |
| 136 | + }) |
| 137 | + if vt != kutil.VerbUnchanged { |
| 138 | + klog.Infof("%s/%s of kind %s has been %s", g.db.namespace, g.db.name, kind, vt) |
| 139 | + } |
| 140 | + return err |
| 141 | +} |
| 142 | + |
| 143 | +func (g *gatewayOpts) initMap() error { |
| 144 | + preferredResources, err := g.disc.ServerPreferredResources() |
| 145 | + if err != nil && !discovery.IsGroupDiscoveryFailedError(err) { |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + g.mapSingularToKind = make(map[string]string) |
| 150 | + g.mapResourceToKind = make(map[string]string) |
| 151 | + |
| 152 | + for _, p := range preferredResources { |
| 153 | + // if p.GroupVersionKind().Group != dbapi.SchemeGroupVersion.Group {continue } |
| 154 | + // This can't be done. Cause p.GroupVersionKind() is empty somehow |
| 155 | + for _, res := range p.APIResources { |
| 156 | + if res.Group != dbapi.SchemeGroupVersion.Group { |
| 157 | + continue |
| 158 | + } |
| 159 | + g.entry(res) |
| 160 | + } |
| 161 | + } |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func (g *gatewayOpts) entry(res metav1.APIResource) { |
| 166 | + g.mapResourceToKind[res.Name] = res.Kind |
| 167 | + g.mapSingularToKind[res.SingularName] = res.Kind |
| 168 | +} |
| 169 | + |
| 170 | +func (g *gatewayOpts) resolveKind(s string) string { |
| 171 | + val, exists := g.mapSingularToKind[s] |
| 172 | + if exists { |
| 173 | + return val |
| 174 | + } |
| 175 | + val, exists = g.mapResourceToKind[s] |
| 176 | + if exists { |
| 177 | + return val |
| 178 | + } |
| 179 | + return s |
| 180 | +} |
0 commit comments