|
| 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