-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathgitops.go
More file actions
176 lines (152 loc) · 4.51 KB
/
gitops.go
File metadata and controls
176 lines (152 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package debug
import (
"context"
"fmt"
"log"
"os"
"path"
gitops "kubedb.dev/apimachinery/apis/gitops/v1alpha1"
opsapi "kubedb.dev/apimachinery/apis/ops/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type GitOpsStatus struct {
GitOps gitops.GitOpsStatus `json:"gitops,omitempty" yaml:"gitops,omitempty"`
}
type GitOps struct {
Status GitOpsStatus `json:"status,omitempty" yaml:"status,omitempty"`
}
type Ops struct {
Status *opsapi.OpsRequestStatus `json:"status,omitempty" yaml:"status,omitempty"`
}
type dbInfo struct {
kind string
name string
namespace string
}
type gitOpsOpts struct {
kc client.Client
db dbInfo
dir string
summary []string
}
func (g *gitOpsOpts) collectGitOpsYamls() error {
err := g.collectGitOpsDatabase()
if err != nil {
return err
}
fmt.Println("Summary:")
for _, line := range g.summary {
fmt.Println("- ", line)
}
fmt.Println("--------------- Done ---------------")
return nil
}
func newGitOpsOpts(kc client.Client, name, namespace, kind, dir string) (*gitOpsOpts, error) {
err := os.MkdirAll(dir, dirPerm)
if err != nil {
return nil, err
}
opts := &gitOpsOpts{
kc: kc,
db: dbInfo{
kind: kind,
name: name,
namespace: namespace,
},
dir: dir,
summary: make([]string, 0),
}
return opts, nil
}
func (g *gitOpsOpts) collectGitOpsDatabase() error {
var uns unstructured.Unstructured
uns.SetGroupVersionKind(schema.GroupVersionKind{
Group: gitops.SchemeGroupVersion.Group,
Version: gitops.SchemeGroupVersion.Version,
Kind: g.db.kind,
})
err := g.kc.Get(context.Background(), types.NamespacedName{
Namespace: g.db.namespace,
Name: g.db.name,
}, &uns)
if err != nil {
log.Fatalf("failed to get gitops database obj: %v", err)
return err
}
var gitOpsObj GitOps
err = runtime.DefaultUnstructuredConverter.FromUnstructured(uns.Object, &gitOpsObj)
if err != nil {
log.Fatalf("failed to convert unstructured to gitops obj: %v", err)
return err
}
status := string(gitops.ChangeRequestStatusInCurrent)
// last status is the latest status
if len(gitOpsObj.Status.GitOps.GitOpsInfo) > 0 {
status = string(gitOpsObj.Status.GitOps.GitOpsInfo[len(gitOpsObj.Status.GitOps.GitOpsInfo)-1].ChangeRequestStatus)
}
g.summary = append(g.summary, fmt.Sprintf("GitOps Database Status for: %s/%s is %s", g.db.namespace, g.db.name, status))
if err := g.collectOpsRequests(gitOpsObj.Status); err != nil {
return err
}
return writeYaml(&uns, g.dir)
}
func (g *gitOpsOpts) collectOpsRequests(gitOpsStatus GitOpsStatus) error {
opsYamlDir := path.Join(g.dir, "ops")
err := os.MkdirAll(opsYamlDir, dirPerm)
if err != nil {
return err
}
for _, info := range gitOpsStatus.GitOps.GitOpsInfo {
for _, op := range info.Operations {
var uns unstructured.Unstructured
uns.SetGroupVersionKind(schema.GroupVersionKind{
Group: opsapi.SchemeGroupVersion.Group,
Version: opsapi.SchemeGroupVersion.Version,
Kind: g.db.kind + "OpsRequest",
})
err := g.kc.Get(context.Background(), types.NamespacedName{
Namespace: g.db.namespace,
Name: op.Name,
}, &uns)
if err != nil {
log.Fatalf("failed to get opsrequest: %v", err)
return err
}
err = writeYaml(&uns, opsYamlDir)
if err != nil {
return err
}
var ops Ops
err = runtime.DefaultUnstructuredConverter.FromUnstructured(uns.Object, &ops)
if err != nil {
log.Fatalf("failed to convert unstructured to opsrequest obj: %v", err)
return err
}
if ops.Status.Phase == opsapi.OpsRequestPhaseFailed {
for _, cond := range ops.Status.Conditions {
if cond.Reason == opsapi.Failed {
g.summary = append(g.summary, fmt.Sprintf("RequestName %s: %s", op.Name, cond.Message))
}
}
}
}
}
return nil
}