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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
290 changes: 175 additions & 115 deletions go.mod

Large diffs are not rendered by default.

681 changes: 413 additions & 268 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/cmds/cloud_swap/cloud_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ace cloud-swap --src-bucket-url="gs://<google-bucket-name>" \
}

cmd.MarkFlagsMutuallyExclusive("s3proxy.endpoint", "src-bucket-url")
cmd.MarkFlagsRequiredTogether("s3proxy.endpoint", "minio.bucket", "minio.access-id", "minio.secret-key")
cmd.MarkFlagsRequiredTogether("s3proxy.endpoint", "s3proxy.bucket", "s3proxy.access-id", "s3proxy.secret-key")

return cmd
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/cmds/debug/debug.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 debug

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

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

func NewCmdDebug() *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Short: "Debug utilities",
DisableAutoGenTag: true,
}

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

cmd.AddCommand(gateway.NewCmdGateway(f))

return cmd
}
109 changes: 109 additions & 0 deletions pkg/cmds/debug/gateway/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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"
"fmt"
"os"
"path"

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

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kmapi "kmodules.xyz/client-go/api/v1"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

type Binding struct {
Spec BindingSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
Status BindingStatus `json:"status,omitempty" yaml:"status,omitempty"`
}

type BindingSpec struct {
SourceRef SourceRef `json:"sourceRef,omitempty" yaml:"sourceRef,omitempty"`
}

type SourceRef struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}

type BindingStatus struct {
// +optional
Gateway *ofst.Gateway `json:"gateway,omitempty"`
}

func (g *gatewayOpts) collectBindings() error {
var uns unstructured.UnstructuredList
uns.SetGroupVersionKind(schema.GroupVersionKind{
Group: catalogapi.GroupVersion.Group,
Version: catalogapi.GroupVersion.Version,
Kind: g.getKindFromResource(g.db.resource) + "Binding",
})

if err := g.kc.List(context.Background(), &uns); err != nil {
return err
}

dirBindings := path.Join(g.dir, yamlsDir, bindingsDir)
if err := os.MkdirAll(dirBindings, dirPerm); err != nil {
return err
}

g.gw = gwInfo{
gateways: make([]kmapi.ObjectReference, 0),
}
for _, b := range uns.Items {
var binding Binding
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(b.Object, &binding); err != nil {
return fmt.Errorf("failed to unmarshal binding %s: %w", b.GetName(), err)
}

src := binding.Spec.SourceRef
if src.Name != g.db.name || src.Namespace != g.db.namespace {
continue
}
if err := writeYaml(&b, dirBindings); err != nil {
return err
}

if binding.Status.Gateway == nil {
continue
}
for _, ui := range binding.Status.Gateway.UI {
if ui.HelmRelease != nil {
g.gw.gateways = append(g.gw.gateways, kmapi.ObjectReference{
Name: ui.HelmRelease.Name,
Namespace: b.GetNamespace(),
})
g.gw.uiReleases = append(g.gw.uiReleases, kmapi.ObjectReference{
Name: ui.HelmRelease.Name,
Namespace: b.GetNamespace(),
})
}
}
g.gw.gateways = append(g.gw.gateways, kmapi.ObjectReference{
Name: binding.Status.Gateway.Name,
Namespace: binding.Status.Gateway.Namespace,
})
}

return nil
}
93 changes: 93 additions & 0 deletions pkg/cmds/debug/gateway/cert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
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"
"os"
"path"

acmev1 "github.com/cert-manager/cert-manager/pkg/apis/acme/v1"
certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func (g *gatewayOpts) collectCerts() error {
dirCerts := path.Join(g.dir, yamlsDir, certsDir)
err := os.MkdirAll(dirCerts, dirPerm)
if err != nil {
return err
}

var list certv1.CertificateList
err = g.kc.List(context.TODO(), &list, client.InNamespace(g.hr.Namespace))
if err != nil {
return err
}
for _, item := range list.Items {
err = writeYaml(&item, dirCerts)
if err != nil {
return err
}
}
if err := g.collectOrders(); err != nil {
return err
}
return g.collectChallenges()
}

func (g *gatewayOpts) collectOrders() error {
dirOrders := path.Join(g.dir, yamlsDir, ordersDir)
err := os.MkdirAll(dirOrders, dirPerm)
if err != nil {
return err
}

var list acmev1.OrderList
err = g.kc.List(context.TODO(), &list, client.InNamespace(g.hr.Namespace))
if err != nil {
return err
}
for _, item := range list.Items {
err = writeYaml(&item, dirOrders)
if err != nil {
return err
}
}
return nil
}

func (g *gatewayOpts) collectChallenges() error {
dirChallenges := path.Join(g.dir, yamlsDir, challengesDir)
err := os.MkdirAll(dirChallenges, dirPerm)
if err != nil {
return err
}

var list acmev1.ChallengeList
err = g.kc.List(context.TODO(), &list, client.InNamespace(g.hr.Namespace))
if err != nil {
return err
}
for _, item := range list.Items {
err = writeYaml(&item, dirChallenges)
if err != nil {
return err
}
}
return nil
}
87 changes: 87 additions & 0 deletions pkg/cmds/debug/gateway/config_dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
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 (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"

"gomodules.xyz/pointer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kmodules.xyz/client-go/tools/portforward"
)

func (g *gatewayOpts) collectConfigDump(podMeta metav1.ObjectMeta) error {
tunnel, err := g.forwardToPort(podMeta, "pods", pointer.IntP(19000))
if err != nil {
return err
}
defer tunnel.Close()

// curl http://10.42.0.82:19000/config_dump?resource%3D%26mask%3D%26name_regex%3D > out.yaml
u := &url.URL{
Scheme: "http",
Host: fmt.Sprintf("localhost:%d", tunnel.Local),
Path: "/config_dump",
}
q := u.Query()
q.Set("resource", "")
q.Set("mask", "")
q.Set("name_regex", "")
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
fmt.Printf("curl failed: %v\n", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
}

outputFile := path.Join(g.dir, logsDir, fmt.Sprintf("%s.config_dump.json", podMeta.Name))
f, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("cannot create %s: %v", outputFile, err)
}
defer f.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return fmt.Errorf("cannot write %s: %v", outputFile, err)
}
return nil
}

func (g *gatewayOpts) forwardToPort(podMeta metav1.ObjectMeta, resource string, port *int) (*portforward.Tunnel, error) {
tunnel := portforward.NewTunnel(
portforward.TunnelOptions{
Client: g.kubeClient.CoreV1().RESTClient(),
Config: g.config,
Resource: resource,
Namespace: podMeta.Namespace,
Name: podMeta.Name,
Remote: *port,
})
if err := tunnel.ForwardPort(); err != nil {
return nil, err
}

return tunnel, nil
}
Loading
Loading