Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.34.3 // indirect
k8s.io/apiserver v0.34.3 // indirect
k8s.io/component-base v0.34.3 // indirect
k8s.io/kube-openapi v0.0.0-20250910181357-589584f1c912 // indirect
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4O
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
github.com/kmodules/apiserver v0.34.4-0.20251227112449-07fa35efc6fc h1:R5bKc1c8Qu7z+7+O0xNWxIPjCYuaHUVZ+dSfeCZEd+c=
github.com/kmodules/apiserver v0.34.4-0.20251227112449-07fa35efc6fc/go.mod h1:QPnnahMO5C2m3lm6fPW3+JmyQbvHZQ8uudAu/493P2w=
github.com/kmodules/controller-runtime v0.22.5-0.20251227114913-f011264689cd h1:cpLV7Pr+pSo3kDYY4HsLZfbdF1WPQuPTP+Jo3hyoWzw=
github.com/kmodules/controller-runtime v0.22.5-0.20251227114913-f011264689cd/go.mod h1:+QX1XUpTXN4mLoblf4tqr5CQcyHPAki2HLXqQMY6vh8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
41 changes: 41 additions & 0 deletions pkg/cmds/expose/expose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright AppsCode Inc. and Contributors

Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md

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 expose

import (
"go.bytebuilders.dev/cli/pkg/cmds/expose/gateway"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

func NewCmdExpose() *cobra.Command {
cmd := &cobra.Command{
Use: "expose",
Short: "Expose databases",
DisableAutoGenTag: true,
}

kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

cmd.AddCommand(gateway.NewCmdGateway(f))

return cmd
}
180 changes: 180 additions & 0 deletions pkg/cmds/expose/gateway/gatway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
Copyright AppsCode Inc. and Contributors

Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md

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 gateway

import (
"context"
"log"

catalogapi "go.bytebuilders.dev/catalog/api/catalog/v1alpha1"
catgwapi "go.bytebuilders.dev/catalog/api/gateway/v1alpha1"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/discovery"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
kutil "kmodules.xyz/client-go"
cu "kmodules.xyz/client-go/client"
dbapi "kubedb.dev/apimachinery/apis/kubedb/v1"
kubedbscheme "kubedb.dev/apimachinery/client/clientset/versioned/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var scheme = runtime.NewScheme()

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(catalogapi.AddToScheme(scheme))
utilruntime.Must(catgwapi.AddToScheme(scheme))
utilruntime.Must(kubedbscheme.AddToScheme(scheme))
}

func NewCmdGateway(f cmdutil.Factory) *cobra.Command {
opt := newGatewayOpts(f)
cmd := &cobra.Command{
Use: "gateway",
Short: "Gateway related info",
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return opt.run()
},
}

cmd.Flags().StringVarP(&opt.db.resource, "db-type", "t", "mongodb", "Database type")
cmd.Flags().StringVarP(&opt.db.name, "name", "m", "mg-test", "Database name")
cmd.Flags().StringVarP(&opt.db.namespace, "namespace", "n", "demo", "Database namespace")
return cmd
}

type gatewayOpts struct {
kc client.Client
disc *discovery.DiscoveryClient
config *rest.Config
db dbInfo

mapResourceToKind map[string]string
mapSingularToKind map[string]string
}

type dbInfo struct {
resource string
name string
namespace string
}

func newGatewayOpts(f cmdutil.Factory) *gatewayOpts {
config, err := f.ToRESTConfig()
if err != nil {
log.Fatal(err)
}
kc, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
log.Fatalf("failed to create client: %v", err)
}

disc, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
log.Fatal("creating discovery client: %w", err)
}

return &gatewayOpts{kc: kc, config: config, disc: disc}
}

func (g *gatewayOpts) run() error {
err := g.initMap()
if err != nil {
return err
}
kind := g.resolveKind(g.db.resource) + "Binding"
var binding unstructured.Unstructured
binding.SetGroupVersionKind(schema.GroupVersionKind{
Group: catalogapi.GroupVersion.Group,
Version: catalogapi.GroupVersion.Version,
Kind: kind,
})
binding.SetNamespace(g.db.namespace)
binding.SetName(g.db.name)

// Set spec.sourceRef
sourceRef := map[string]any{
"name": g.db.name,
}

if g.db.namespace != "" {
sourceRef["namespace"] = g.db.namespace
}

if err := unstructured.SetNestedField(binding.Object, sourceRef, "spec", "sourceRef"); err != nil {
return err
}

vt, err := cu.CreateOrPatch(context.TODO(), g.kc, &binding, func(obj client.Object, createOp bool) client.Object {
return obj
// in := obj.(*catalogapi.BindingInterface)
// return in
})
if vt != kutil.VerbUnchanged {
klog.Infof("%s/%s of kind %s has been %s", g.db.namespace, g.db.name, kind, vt)
}
return err
}

func (g *gatewayOpts) initMap() error {
preferredResources, err := g.disc.ServerPreferredResources()
if err != nil && !discovery.IsGroupDiscoveryFailedError(err) {
return err
}

g.mapSingularToKind = make(map[string]string)
g.mapResourceToKind = make(map[string]string)

for _, p := range preferredResources {
// if p.GroupVersionKind().Group != dbapi.SchemeGroupVersion.Group {continue }
// This can't be done. Cause p.GroupVersionKind() is empty somehow
for _, res := range p.APIResources {
if res.Group != dbapi.SchemeGroupVersion.Group {
continue
}
g.entry(res)
}
}
return nil
}

func (g *gatewayOpts) entry(res metav1.APIResource) {
g.mapResourceToKind[res.Name] = res.Kind
g.mapSingularToKind[res.SingularName] = res.Kind
}

func (g *gatewayOpts) resolveKind(s string) string {
val, exists := g.mapSingularToKind[s]
if exists {
return val
}
val, exists = g.mapResourceToKind[s]
if exists {
return val
}
return s
}
2 changes: 2 additions & 0 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.bytebuilders.dev/cli/pkg/cmds/cluster"
cmdconfig "go.bytebuilders.dev/cli/pkg/cmds/config"
"go.bytebuilders.dev/cli/pkg/cmds/debug"
"go.bytebuilders.dev/cli/pkg/cmds/expose"
"go.bytebuilders.dev/cli/pkg/cmds/installer"
"go.bytebuilders.dev/cli/pkg/config"
ace "go.bytebuilders.dev/client"
Expand Down Expand Up @@ -56,6 +57,7 @@ func NewRootCmd() *cobra.Command {

rootCmd.AddCommand(installer.NewCmdInstaller())
rootCmd.AddCommand(debug.NewCmdDebug())
rootCmd.AddCommand(expose.NewCmdExpose())

rootCmd.AddCommand(v.NewCmdVersion())
rootCmd.AddCommand(NewCmdCompletion())
Expand Down
Loading
Loading