Skip to content

Commit 5f693ef

Browse files
Add debug util for gateway
Signed-off-by: Arnob kumar saha <[email protected]>
1 parent b969d0d commit 5f693ef

File tree

5,266 files changed

+2024295
-93127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

5,266 files changed

+2024295
-93127
lines changed

go.mod

Lines changed: 175 additions & 115 deletions
Large diffs are not rendered by default.

go.sum

Lines changed: 413 additions & 268 deletions
Large diffs are not rendered by default.

pkg/cmds/debug/debug.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 debug
18+
19+
import (
20+
"go.bytebuilders.dev/cli/pkg/cmds/debug/gateway"
21+
22+
"github.com/spf13/cobra"
23+
"k8s.io/cli-runtime/pkg/genericclioptions"
24+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
25+
)
26+
27+
func NewCmdDebug() *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "debug",
30+
Short: "Debug utilities",
31+
DisableAutoGenTag: true,
32+
}
33+
34+
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
35+
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
36+
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
37+
38+
cmd.AddCommand(gateway.NewCmdGateway(f))
39+
40+
return cmd
41+
}

pkg/cmds/debug/gateway/binding.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
"fmt"
22+
"os"
23+
"path"
24+
25+
catalogapi "go.bytebuilders.dev/catalog/api/catalog/v1alpha1"
26+
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
30+
)
31+
32+
type BindingSpec struct {
33+
SourceRef SourceRef `json:"sourceRef,omitempty" yaml:"sourceRef,omitempty"`
34+
}
35+
36+
type SourceRef struct {
37+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
38+
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
39+
}
40+
41+
type Binding struct {
42+
Spec BindingSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
43+
}
44+
45+
func (g *gatewayOpts) collectBindings() error {
46+
var uns unstructured.UnstructuredList
47+
uns.SetGroupVersionKind(schema.GroupVersionKind{
48+
Group: catalogapi.GroupVersion.Group,
49+
Version: catalogapi.GroupVersion.Version,
50+
Kind: g.getKindFromResource(g.db.resource) + "Binding",
51+
})
52+
53+
if err := g.kc.List(context.Background(), &uns); err != nil {
54+
return err
55+
}
56+
57+
dirBindings := path.Join(g.dir, yamlsDir, bindingsDir)
58+
if err := os.MkdirAll(dirBindings, dirPerm); err != nil {
59+
return err
60+
}
61+
62+
for _, b := range uns.Items {
63+
var binding Binding
64+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(b.Object, &binding); err != nil {
65+
return fmt.Errorf("failed to unmarshal binding %s: %w", b.GetName(), err)
66+
}
67+
68+
src := binding.Spec.SourceRef
69+
if src.Name != g.db.name || src.Namespace != g.db.namespace {
70+
continue
71+
}
72+
if err := writeYaml(&b, dirBindings); err != nil {
73+
return err
74+
}
75+
}
76+
77+
return nil
78+
}

pkg/cmds/debug/gateway/cert.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
"os"
22+
"path"
23+
24+
acmev1 "github.com/cert-manager/cert-manager/pkg/apis/acme/v1"
25+
certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
func (g *gatewayOpts) collectCerts() error {
30+
dirCerts := path.Join(g.dir, yamlsDir, certsDir)
31+
err := os.MkdirAll(dirCerts, dirPerm)
32+
if err != nil {
33+
return err
34+
}
35+
36+
var list certv1.CertificateList
37+
err = g.kc.List(context.TODO(), &list, client.InNamespace(g.hr.Namespace))
38+
if err != nil {
39+
return err
40+
}
41+
for _, item := range list.Items {
42+
err = writeYaml(&item, dirCerts)
43+
if err != nil {
44+
return err
45+
}
46+
}
47+
if err := g.collectOrders(); err != nil {
48+
return err
49+
}
50+
return g.collectChallenges()
51+
}
52+
53+
func (g *gatewayOpts) collectOrders() error {
54+
dirOrders := path.Join(g.dir, yamlsDir, ordersDir)
55+
err := os.MkdirAll(dirOrders, dirPerm)
56+
if err != nil {
57+
return err
58+
}
59+
60+
var list acmev1.OrderList
61+
err = g.kc.List(context.TODO(), &list, client.InNamespace(g.hr.Namespace))
62+
if err != nil {
63+
return err
64+
}
65+
for _, item := range list.Items {
66+
err = writeYaml(&item, dirOrders)
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
return nil
72+
}
73+
74+
func (g *gatewayOpts) collectChallenges() error {
75+
dirChallenges := path.Join(g.dir, yamlsDir, challengesDir)
76+
err := os.MkdirAll(dirChallenges, dirPerm)
77+
if err != nil {
78+
return err
79+
}
80+
81+
var list acmev1.ChallengeList
82+
err = g.kc.List(context.TODO(), &list, client.InNamespace(g.hr.Namespace))
83+
if err != nil {
84+
return err
85+
}
86+
for _, item := range list.Items {
87+
err = writeYaml(&item, dirChallenges)
88+
if err != nil {
89+
return err
90+
}
91+
}
92+
return nil
93+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"fmt"
21+
"io"
22+
"net/http"
23+
"net/url"
24+
"os"
25+
"path"
26+
27+
"gomodules.xyz/pointer"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"kmodules.xyz/client-go/tools/portforward"
30+
)
31+
32+
func (g *gatewayOpts) collectConfigDump(podMeta metav1.ObjectMeta) error {
33+
tunnel, err := g.forwardToPort(podMeta, "pods", pointer.IntP(19000))
34+
if err != nil {
35+
return err
36+
}
37+
defer tunnel.Close()
38+
39+
// curl http://10.42.0.82:19000/config_dump?resource%3D%26mask%3D%26name_regex%3D > out.yaml
40+
u := &url.URL{
41+
Scheme: "http",
42+
Host: fmt.Sprintf("localhost:%d", tunnel.Local),
43+
Path: "/config_dump",
44+
}
45+
q := u.Query()
46+
q.Set("resource", "")
47+
q.Set("mask", "")
48+
q.Set("name_regex", "")
49+
u.RawQuery = q.Encode()
50+
resp, err := http.Get(u.String())
51+
if err != nil {
52+
fmt.Printf("curl failed: %v\n", err)
53+
return err
54+
}
55+
defer resp.Body.Close()
56+
if resp.StatusCode != http.StatusOK {
57+
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
58+
}
59+
60+
outputFile := path.Join(g.dir, logsDir, fmt.Sprintf("%s.config_dump.json", podMeta.Name))
61+
f, err := os.Create(outputFile)
62+
if err != nil {
63+
return fmt.Errorf("cannot create %s: %v", outputFile, err)
64+
}
65+
defer f.Close()
66+
if _, err := io.Copy(f, resp.Body); err != nil {
67+
return fmt.Errorf("cannot write %s: %v", outputFile, err)
68+
}
69+
return nil
70+
}
71+
72+
func (g *gatewayOpts) forwardToPort(podMeta metav1.ObjectMeta, resource string, port *int) (*portforward.Tunnel, error) {
73+
tunnel := portforward.NewTunnel(
74+
portforward.TunnelOptions{
75+
Client: g.kubeClient.CoreV1().RESTClient(),
76+
Config: g.config,
77+
Resource: resource,
78+
Namespace: podMeta.Namespace,
79+
Name: podMeta.Name,
80+
Remote: *port,
81+
})
82+
if err := tunnel.ForwardPort(); err != nil {
83+
return nil, err
84+
}
85+
86+
return tunnel, nil
87+
}

0 commit comments

Comments
 (0)