Skip to content

Commit 3fc4051

Browse files
Add gitops CLI (#804)
Signed-off-by: obaydullahmhs <obaydullah@appscode.com>
1 parent 8749544 commit 3fc4051

28 files changed

+216
-0
lines changed

pkg/cmds/debug.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
* mysql
3737
* postgres
3838
* redis
39+
* gitops
3940
`)
4041
)
4142

pkg/debug/cassandra.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func CassandraDebugCMD(f cmdutil.Factory) *cobra.Command {
7373
if err != nil {
7474
log.Fatalln(err)
7575
}
76+
opts.db.OwnerReferences = db.OwnerReferences
7677

7778
err = writeYaml(&db, getDir(db.GetName()))
7879
if err != nil {

pkg/debug/clickhouse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func ClickHouseDebugCMD(f cmdutil.Factory) *cobra.Command {
7373
if err != nil {
7474
log.Fatalln(err)
7575
}
76+
opts.db.OwnerReferences = db.OwnerReferences
7677

7778
err = writeYaml(&db, getDir(db.GetName()))
7879
if err != nil {

pkg/debug/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ func (opts *dbOpts) collectALl() error {
117117
if err != nil {
118118
return err
119119
}
120+
if IsOwnByGitOpsObject(opts.db.GetOwnerReferences(), opts.db.GetName(), opts.kind) {
121+
gitOpsOpts, err := newGitOpsOpts(opts.kc, opts.db.GetName(), opts.db.GetNamespace(), opts.kind, path.Join(opts.dir, "gitops"))
122+
if err != nil {
123+
return err
124+
}
125+
err = gitOpsOpts.collectGitOpsYamls()
126+
if err != nil {
127+
return err
128+
}
129+
}
120130
return nil
121131
}
122132

pkg/debug/druid.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func DruidDebugCMD(f cmdutil.Factory) *cobra.Command {
7373
if err != nil {
7474
log.Fatalln(err)
7575
}
76+
opts.db.OwnerReferences = db.OwnerReferences
7677

7778
err = writeYaml(&db, getDir(db.GetName()))
7879
if err != nil {

pkg/debug/elasticsearch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func ElasticsearchDebugCMD(f cmdutil.Factory) *cobra.Command {
7373
if err != nil {
7474
log.Fatalln(err)
7575
}
76+
opts.db.OwnerReferences = db.OwnerReferences
7677

7778
err = writeYaml(&db, getDir(db.GetName()))
7879
if err != nil {

pkg/debug/ferretdb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func FerretDBDebugCMD(f cmdutil.Factory) *cobra.Command {
7373
if err != nil {
7474
log.Fatalln(err)
7575
}
76+
opts.db.OwnerReferences = db.OwnerReferences
7677

7778
err = writeYaml(&db, getDir(db.GetName()))
7879
if err != nil {

pkg/debug/gitops.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
"context"
21+
"fmt"
22+
"os"
23+
"path"
24+
25+
gitops "kubedb.dev/apimachinery/apis/gitops/v1alpha1"
26+
opsapi "kubedb.dev/apimachinery/apis/ops/v1alpha1"
27+
28+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29+
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/runtime/schema"
31+
"k8s.io/apimachinery/pkg/types"
32+
_ "k8s.io/client-go/plugin/pkg/client/auth"
33+
"sigs.k8s.io/controller-runtime/pkg/client"
34+
)
35+
36+
type GitOpsStatus struct {
37+
GitOps gitops.GitOpsStatus `json:"gitops,omitempty" yaml:"gitops,omitempty"`
38+
}
39+
40+
type GitOps struct {
41+
Status GitOpsStatus `json:"status,omitempty" yaml:"status,omitempty"`
42+
}
43+
44+
type Ops struct {
45+
Status *opsapi.OpsRequestStatus `json:"status,omitempty" yaml:"status,omitempty"`
46+
}
47+
48+
type dbInfo struct {
49+
kind string
50+
name string
51+
namespace string
52+
}
53+
54+
type gitOpsOpts struct {
55+
kc client.Client
56+
db dbInfo
57+
58+
dir string
59+
summary []string
60+
}
61+
62+
func (g *gitOpsOpts) collectGitOpsYamls() error {
63+
err := g.collectGitOpsDatabase()
64+
if err != nil {
65+
return err
66+
}
67+
68+
fmt.Println("Summary:")
69+
for _, line := range g.summary {
70+
fmt.Println("- ", line)
71+
}
72+
fmt.Println("--------------- Done ---------------")
73+
return nil
74+
}
75+
76+
func newGitOpsOpts(kc client.Client, name, namespace, kind, dir string) (*gitOpsOpts, error) {
77+
err := os.MkdirAll(dir, dirPerm)
78+
if err != nil {
79+
return nil, err
80+
}
81+
opts := &gitOpsOpts{
82+
kc: kc,
83+
db: dbInfo{
84+
kind: kind,
85+
name: name,
86+
namespace: namespace,
87+
},
88+
dir: dir,
89+
summary: make([]string, 0),
90+
}
91+
return opts, nil
92+
}
93+
94+
func (g *gitOpsOpts) collectGitOpsDatabase() error {
95+
var uns unstructured.Unstructured
96+
uns.SetGroupVersionKind(schema.GroupVersionKind{
97+
Group: gitops.SchemeGroupVersion.Group,
98+
Version: gitops.SchemeGroupVersion.Version,
99+
Kind: g.db.kind,
100+
})
101+
err := g.kc.Get(context.Background(), types.NamespacedName{
102+
Namespace: g.db.namespace,
103+
Name: g.db.name,
104+
}, &uns)
105+
if err != nil {
106+
return err
107+
}
108+
109+
var gitOpsObj GitOps
110+
err = runtime.DefaultUnstructuredConverter.FromUnstructured(uns.Object, &gitOpsObj)
111+
if err != nil {
112+
return err
113+
}
114+
115+
status := string(gitops.ChangeRequestStatusInCurrent)
116+
// last status is the latest status
117+
if len(gitOpsObj.Status.GitOps.GitOpsInfo) > 0 {
118+
status = string(gitOpsObj.Status.GitOps.GitOpsInfo[len(gitOpsObj.Status.GitOps.GitOpsInfo)-1].ChangeRequestStatus)
119+
}
120+
121+
g.summary = append(g.summary, fmt.Sprintf("GitOps Database Status for: %s/%s is %s", g.db.namespace, g.db.name, status))
122+
123+
if err := g.collectOpsRequests(gitOpsObj.Status); err != nil {
124+
return err
125+
}
126+
127+
return writeYaml(&uns, g.dir)
128+
}
129+
130+
func (g *gitOpsOpts) collectOpsRequests(gitOpsStatus GitOpsStatus) error {
131+
opsYamlDir := path.Join(g.dir, "ops")
132+
err := os.MkdirAll(opsYamlDir, dirPerm)
133+
if err != nil {
134+
return err
135+
}
136+
for _, info := range gitOpsStatus.GitOps.GitOpsInfo {
137+
for _, op := range info.Operations {
138+
var uns unstructured.Unstructured
139+
uns.SetGroupVersionKind(schema.GroupVersionKind{
140+
Group: opsapi.SchemeGroupVersion.Group,
141+
Version: opsapi.SchemeGroupVersion.Version,
142+
Kind: g.db.kind + "OpsRequest",
143+
})
144+
err := g.kc.Get(context.Background(), types.NamespacedName{
145+
Namespace: g.db.namespace,
146+
Name: op.Name,
147+
}, &uns)
148+
if err != nil {
149+
return err
150+
}
151+
err = writeYaml(&uns, opsYamlDir)
152+
if err != nil {
153+
return err
154+
}
155+
var ops Ops
156+
err = runtime.DefaultUnstructuredConverter.FromUnstructured(uns.Object, &ops)
157+
if err != nil {
158+
return err
159+
}
160+
if ops.Status.Phase == opsapi.OpsRequestPhaseFailed {
161+
for _, cond := range ops.Status.Conditions {
162+
if cond.Reason == opsapi.Failed {
163+
g.summary = append(g.summary, fmt.Sprintf("RequestName %s: %s", op.Name, cond.Message))
164+
}
165+
}
166+
}
167+
}
168+
}
169+
170+
return nil
171+
}

pkg/debug/hazelcast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func HazelcastDebugCMD(f cmdutil.Factory) *cobra.Command {
7373
if err != nil {
7474
log.Fatalln(err)
7575
}
76+
opts.db.OwnerReferences = db.OwnerReferences
7677

7778
err = writeYaml(&db, getDir(db.GetName()))
7879
if err != nil {

pkg/debug/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ import (
2020
"os"
2121
"path"
2222

23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
"sigs.k8s.io/controller-runtime/pkg/client"
2425
"sigs.k8s.io/yaml"
2526
)
2627

28+
func IsOwnByGitOpsObject(owners []metav1.OwnerReference, name, kind string) bool {
29+
for _, owner := range owners {
30+
if owner.Kind == kind && owner.Name == name && owner.APIVersion == "gitops.kubedb.com/v1alpha1" {
31+
return true
32+
}
33+
}
34+
return false
35+
}
36+
2737
func writeYaml(obj client.Object, fullPath string) error {
2838
b, err := yaml.Marshal(obj)
2939
if err != nil {

0 commit comments

Comments
 (0)