|
| 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 | +} |
0 commit comments