Skip to content

Commit 27df459

Browse files
Implement license issue debug util (#28)
Signed-off-by: Arnob kumar saha <arnob@appscode.com>
1 parent 54e4ee7 commit 27df459

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

pkg/cmds/debug/debug.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package debug
1919
import (
2020
"go.bytebuilders.dev/cli/pkg/cmds/debug/clientorg"
2121
"go.bytebuilders.dev/cli/pkg/cmds/debug/gateway"
22+
"go.bytebuilders.dev/cli/pkg/cmds/debug/license"
2223

2324
"github.com/spf13/cobra"
2425
"k8s.io/cli-runtime/pkg/genericclioptions"
@@ -38,6 +39,7 @@ func NewCmdDebug() *cobra.Command {
3839

3940
cmd.AddCommand(gateway.NewCmdGateway(f))
4041
cmd.AddCommand(clientorg.NewCmdClientOrg(f))
42+
cmd.AddCommand(license.NewCmdLicense(f))
4143

4244
return cmd
4345
}

pkg/cmds/debug/license/helpers.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 license
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path"
23+
"strings"
24+
25+
"gomodules.xyz/go-sh"
26+
)
27+
28+
const (
29+
dirPerm = 0o755
30+
filePerm = 0o644
31+
32+
kubectlCommand = "kubectl"
33+
defaultDir = "license-debug-info"
34+
defaultFile = "info.txt"
35+
)
36+
37+
func (g *licenseOpts) collectInfo() error {
38+
out := []byte("\n\n===== License status =====\n")
39+
args := []any{"get", "licensestatus"}
40+
o, err := sh.Command(kubectlCommand, args...).Output()
41+
if err != nil {
42+
return err
43+
}
44+
out = append(out, o...)
45+
46+
err = os.WriteFile(path.Join(g.rootPath, defaultFile), out, filePerm) // empty the file first, & write
47+
if err != nil {
48+
return err
49+
}
50+
// kube-system namespace
51+
out = []byte("\n\n===== kube-system namespace =====\n")
52+
args = []any{"get", "ns", "kube-system", "-o", "yaml"}
53+
o, err = sh.Command(kubectlCommand, args...).Output()
54+
if err != nil {
55+
return err
56+
}
57+
out = append(out, o...)
58+
59+
// license-proxyserver-licenses secret
60+
out = append(out, []byte("\n\n===== License secret =====\n")...)
61+
args = []any{"get", "secrets", "license-proxyserver-licenses", "-n", "kubeops", "-o", "yaml"}
62+
o, err = sh.Command(kubectlCommand, args...).Output()
63+
if err != nil {
64+
return err
65+
}
66+
out = append(out, o...)
67+
err = g.writeContent(defaultFile, out)
68+
if err != nil {
69+
return err
70+
}
71+
return nil
72+
}
73+
74+
func (g *licenseOpts) collectLogs(res, ns string) error {
75+
args := []any{"logs", res, "-n", ns}
76+
out, err := sh.Command(kubectlCommand, args...).Output()
77+
if err != nil {
78+
return err
79+
}
80+
81+
parts := strings.Split(res, "/")
82+
if len(parts) != 2 {
83+
return fmt.Errorf("unexpected res: %s", parts)
84+
}
85+
err = g.writeContent(parts[1]+".log", out)
86+
if err != nil {
87+
return err
88+
}
89+
return nil
90+
}
91+
92+
func (g *licenseOpts) writeContent(fileName string, content []byte) error {
93+
f, err := os.OpenFile(path.Join(g.rootPath, fileName), os.O_APPEND|os.O_CREATE|os.O_WRONLY, filePerm)
94+
if err != nil {
95+
return err
96+
}
97+
defer func(f *os.File) {
98+
_ = f.Close()
99+
}(f)
100+
101+
_, err = f.Write(content)
102+
if err != nil {
103+
return err
104+
}
105+
return nil
106+
}

pkg/cmds/debug/license/license.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 license
18+
19+
import (
20+
"log"
21+
"os"
22+
"path"
23+
24+
catalogapi "go.bytebuilders.dev/catalog/api/catalog/v1alpha1"
25+
catgwapi "go.bytebuilders.dev/catalog/api/gateway/v1alpha1"
26+
27+
acmev1 "github.com/cert-manager/cert-manager/pkg/apis/acme/v1"
28+
certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
29+
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
30+
flux "github.com/fluxcd/helm-controller/api/v2"
31+
"github.com/spf13/cobra"
32+
"k8s.io/apimachinery/pkg/runtime"
33+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34+
"k8s.io/client-go/kubernetes"
35+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
36+
"k8s.io/client-go/rest"
37+
"k8s.io/klog/v2"
38+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
39+
kubedbscheme "kubedb.dev/apimachinery/client/clientset/versioned/scheme"
40+
"sigs.k8s.io/controller-runtime/pkg/client"
41+
gwapi "sigs.k8s.io/gateway-api/apis/v1"
42+
gwapia3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
43+
gwapib1 "sigs.k8s.io/gateway-api/apis/v1beta1"
44+
vgapi "voyagermesh.dev/gateway-api/apis/gateway/v1alpha1"
45+
)
46+
47+
var scheme = runtime.NewScheme()
48+
49+
func init() {
50+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
51+
utilruntime.Must(catalogapi.AddToScheme(scheme))
52+
utilruntime.Must(catgwapi.AddToScheme(scheme))
53+
utilruntime.Must(kubedbscheme.AddToScheme(scheme))
54+
utilruntime.Must(gwapi.Install(scheme))
55+
utilruntime.Must(gwapia3.Install(scheme))
56+
utilruntime.Must(gwapib1.Install(scheme))
57+
utilruntime.Must(vgapi.AddToScheme(scheme))
58+
utilruntime.Must(egv1a1.AddToScheme(scheme))
59+
utilruntime.Must(flux.AddToScheme(scheme))
60+
utilruntime.Must(certv1.AddToScheme(scheme))
61+
utilruntime.Must(acmev1.AddToScheme(scheme))
62+
}
63+
64+
func NewCmdLicense(f cmdutil.Factory) *cobra.Command {
65+
opt := newLicenseOrgOpts(f)
66+
cmd := &cobra.Command{
67+
Use: "license",
68+
Short: "License related info",
69+
DisableAutoGenTag: true,
70+
RunE: func(cmd *cobra.Command, args []string) error {
71+
klog.Infof("The debug info will be generated in current directory under '%s' folder", defaultDir)
72+
return opt.run()
73+
},
74+
}
75+
return cmd
76+
}
77+
78+
type licenseOpts struct {
79+
kc client.Client
80+
config *rest.Config
81+
kubeClient kubernetes.Interface
82+
rootPath string
83+
}
84+
85+
func newLicenseOrgOpts(f cmdutil.Factory) *licenseOpts {
86+
config, err := f.ToRESTConfig()
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
kc, err := client.New(config, client.Options{Scheme: scheme})
91+
if err != nil {
92+
log.Fatalf("failed to create client: %v", err)
93+
}
94+
95+
cs, err := kubernetes.NewForConfig(config)
96+
if err != nil {
97+
log.Fatalf("failed to create kube client: %v", err)
98+
}
99+
100+
return &licenseOpts{
101+
kc: kc,
102+
config: config,
103+
kubeClient: cs,
104+
}
105+
}
106+
107+
func (g *licenseOpts) run() error {
108+
pwd, _ := os.Getwd()
109+
g.rootPath = path.Join(pwd, defaultDir)
110+
err := os.MkdirAll(g.rootPath, dirPerm)
111+
if err != nil {
112+
return err
113+
}
114+
115+
if err := g.collectInfo(); err != nil {
116+
return err
117+
}
118+
if err := g.collectLogs("deploy/license-proxyserver", "kubeops"); err != nil {
119+
return err
120+
}
121+
if err := g.collectLogs("sts/kubedb-kubedb-provisioner", "kubedb"); err != nil {
122+
return err
123+
}
124+
return err
125+
}

0 commit comments

Comments
 (0)