Skip to content

Commit 7dfcc6f

Browse files
finalized
Signed-off-by: Arnob Kumar Saha <arnob@appscode.com>
1 parent 8400f58 commit 7dfcc6f

4 files changed

Lines changed: 53 additions & 64 deletions

File tree

client-org/active/org1.txt

Lines changed: 0 additions & 33 deletions
This file was deleted.

pkg/cmds/debug/clientorg/cleanup.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ limitations under the License.
1616

1717
package clientorg
1818

19-
import "gomodules.xyz/go-sh"
19+
import (
20+
"gomodules.xyz/go-sh"
21+
"k8s.io/klog/v2"
22+
)
2023

2124
func (g *clientOrgOpts) cleanup() error {
25+
if !g.clean || g.mode == modeActive {
26+
return nil
27+
}
28+
klog.Infof("Cleaning up gw resources from %v(mode) organizations", g.mode)
2229
for _, org := range g.terminatingOrganizations {
2330
if !org.gwNamespace {
2431
continue

pkg/cmds/debug/clientorg/client-org.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,33 +72,35 @@ func NewCmdClientOrg(f cmdutil.Factory) *cobra.Command {
7272
if opt.dir == "" {
7373
opt.dir = "client-org"
7474
}
75-
klog.Infof("The debug into will be generated in current directory under '%s' folder", opt.dir)
75+
klog.Infof("The debug info will be generated in current directory under '%s' folder", opt.dir)
7676
return opt.run()
7777
},
7878
}
7979

8080
cmd.Flags().StringVarP(&opt.org, "name", "m", "", "Client org name")
81+
cmd.Flags().StringVarP(&opt.mode, "mode", "d", "terminating", "Possible values: 'active', 'terminating' or 'both'. Default is terminating")
82+
cmd.Flags().BoolVarP(&opt.clean, "cleanup", "c", true, "cleanup should happen or not")
8183
return cmd
8284
}
8385

8486
type clientOrgOpts struct {
8587
kc client.Client
8688
config *rest.Config
8789
kubeClient kubernetes.Interface
88-
org string
89-
dir string
9090

9191
activeOrganizations []resourceGroup
9292
terminatingOrganizations []resourceGroup
93+
94+
org string
95+
mode string
96+
dir string
97+
clean bool
9398
}
9499

95100
type resourceGroup struct {
96101
clientOrgName string
97102
gwNamespace bool
98103
monitoringNamespace bool
99-
mainResources string
100-
gwResources string
101-
monitoringResources string
102104
}
103105

104106
func newClientOrgOpts(f cmdutil.Factory) *clientOrgOpts {
@@ -133,17 +135,21 @@ func (g *clientOrgOpts) run() error {
133135
return err
134136
}
135137

138+
klog.Infof("[[ Values passed: --name=%v, --mode=%v, --cleanup=%v ]]", g.org, g.mode, g.clean)
136139
if err := g.collectNamespaces(); err != nil {
137140
return err
138141
}
139142

140-
klog.Infof("Collecting all resources from clientOrg namespaces")
143+
orgName := g.org
144+
if orgName == "" {
145+
orgName = "all"
146+
}
147+
klog.Infof("Collecting resources from %v clientOrg namespace", orgName)
141148
err = g.collectAllResources()
142149
if err != nil {
143150
return err
144151
}
145152

146-
klog.Infof("Cleaning up gw resources from terminating organizations")
147153
err = g.cleanup()
148154
return err
149155
}

pkg/cmds/debug/clientorg/helpers.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import (
2929
)
3030

3131
const (
32-
logsDir = "logs"
33-
yamlsDir = "yamls"
3432
dirPerm = 0o755
3533
filePerm = 0o644
3634

37-
kubectlCommand = "kubectl"
35+
kubectlCommand = "kubectl"
36+
modeActive = "active"
37+
modeTerminating = "terminating"
3838
)
3939

4040
var (
@@ -73,13 +73,16 @@ func (g *clientOrgOpts) collectNamespaces() error {
7373
}
7474

7575
for _, ns := range nsList.Items {
76+
if g.org != "" && ns.Name != g.org { // if ran for specific org, ignore all other ones.
77+
continue
78+
}
7679
if val, exists := ns.Labels[kmapi.ClientOrgKey]; exists {
7780
rg := constructResourceGroup(ns.Name)
7881

7982
switch val {
8083
case "true":
8184
g.activeOrganizations = append(g.activeOrganizations, rg)
82-
case "terminating":
85+
case modeTerminating:
8386
g.terminatingOrganizations = append(g.terminatingOrganizations, rg)
8487
}
8588
}
@@ -106,28 +109,32 @@ func getResourceHeader(res string) []byte {
106109
}
107110

108111
func (g *clientOrgOpts) collectAllResources() error {
109-
//err := os.MkdirAll(path.Join(g.dir, "active"), dirPerm)
110-
//if err != nil {
111-
// return err
112-
//}
113-
//
114-
//for _, org := range g.activeOrganizations {
115-
// err := g.collectForOneOrg(org, path.Join(g.dir, "active"))
116-
// if err != nil {
117-
// return err
118-
// }
119-
//}
120-
121-
err := os.MkdirAll(path.Join(g.dir, "terminating"), dirPerm)
122-
if err != nil {
123-
return err
112+
if g.mode == "both" || g.mode == modeActive {
113+
err := os.MkdirAll(path.Join(g.dir, modeActive), dirPerm)
114+
if err != nil {
115+
return err
116+
}
117+
118+
for _, org := range g.activeOrganizations {
119+
err := g.collectForOneOrg(org, path.Join(g.dir, modeActive))
120+
if err != nil {
121+
return err
122+
}
123+
}
124124
}
125125

126-
for _, org := range g.terminatingOrganizations {
127-
err := g.collectForOneOrg(org, path.Join(g.dir, "terminating"))
126+
if g.mode == "both" || g.mode == modeTerminating {
127+
err := os.MkdirAll(path.Join(g.dir, modeTerminating), dirPerm)
128128
if err != nil {
129129
return err
130130
}
131+
132+
for _, org := range g.terminatingOrganizations {
133+
err := g.collectForOneOrg(org, path.Join(g.dir, modeTerminating))
134+
if err != nil {
135+
return err
136+
}
137+
}
131138
}
132139
return nil
133140
}
@@ -193,7 +200,9 @@ func (g *clientOrgOpts) writeContent(groupDir, fileName string, content []byte)
193200
if err != nil {
194201
return err
195202
}
196-
defer f.Close()
203+
defer func(f *os.File) {
204+
_ = f.Close()
205+
}(f)
197206

198207
_, err = f.Write(content)
199208
if err != nil {

0 commit comments

Comments
 (0)