-
Notifications
You must be signed in to change notification settings - Fork 328
/
status.go
127 lines (113 loc) · 3.42 KB
/
status.go
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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.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://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package action
import (
"bytes"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
"tkestack.io/tke/pkg/util/log"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/kubectl/pkg/cmd/get"
)
// StatusOptions is the query options to a status call.
type StatusOptions struct {
Namespace string
ReleaseName string
}
type ReleaseStatus struct {
Release *release.Release
Resources map[string][]string
}
// KubeClient represents a client capable of communicating with the Kubernetes API.
type KubeClient struct {
kube.Interface
}
// ResourceActorFunc performs an action on a single resource.
type ResourceActorFunc func(*resource.Info) error
// Status returning release status.
func (c *Client) Status(options *StatusOptions) (ReleaseStatus, error) {
actionConfig, err := c.buildActionConfig(options.Namespace)
if err != nil {
return ReleaseStatus{}, err
}
client := action.NewStatus(actionConfig)
rel, err := client.Run(options.ReleaseName)
if err != nil {
return ReleaseStatus{}, err
}
resources, err := actionConfig.KubeClient.Build(bytes.NewBufferString(rel.Manifest), true)
if err != nil {
return ReleaseStatus{}, err
}
kubeClient := &KubeClient{actionConfig.KubeClient}
resp, err := kubeClient.GetResource(resources)
if err != nil {
return ReleaseStatus{}, err
}
status := ReleaseStatus{
Release: rel,
Resources: resp,
}
return status, nil
}
// GetResource gets Kubernetes resources as pretty-printed string.
//
// Namespace will set the namespace.
func (c *KubeClient) GetResource(resources kube.ResourceList) (map[string][]string, error) {
printFlags := get.NewGetPrintFlags()
typePrinter, _ := printFlags.JSONYamlPrintFlags.ToPrinter("json")
printer := &get.TablePrinter{Delegate: typePrinter}
objs := make(map[string][]runtime.Object)
objsJSON := make(map[string][]string)
err := resources.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
gvk := info.ResourceMapping().GroupVersionKind
vk := gvk.Group + "/" + gvk.Version + "/" + gvk.Kind
obj, err := getResource(info)
if err != nil {
return err
}
objs[vk] = append(objs[vk], obj)
return nil
})
if err != nil {
return nil, err
}
for t, ot := range objs {
for _, o := range ot {
buf := new(bytes.Buffer)
if err := printer.PrintObj(o, buf); err != nil {
log.Warnf("failed to print object type %s, object: %q :\n %v", t, o, err)
return nil, err
}
objsJSON[t] = append(objsJSON[t], buf.String())
}
}
return objsJSON, nil
}
func getResource(info *resource.Info) (runtime.Object, error) {
obj, err := resource.NewHelper(info.Client, info.Mapping).Get(info.Namespace, info.Name, false)
if err != nil {
return nil, err
}
return obj, nil
}