/
table_openshift_project.go
133 lines (118 loc) · 3.56 KB
/
table_openshift_project.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
128
129
130
131
132
133
package openshift
import (
"context"
client_v1 "github.com/openshift/client-go/project/clientset/versioned/typed/project/v1"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//// TABLE DEFINITION
func tableOpenShiftProject(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "openshift_project",
Description: "Retrieve information about OpenShift projects.",
List: &plugin.ListConfig{
Hydrate: listProjects,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getProject,
},
Columns: commonColumns([]*plugin.Column{
{
Name: "phase",
Description: "Phase is the current lifecycle phase of the project.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Status.Phase"),
},
{
Name: "conditions",
Description: "Represents the latest available observations of the project's current state.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Status.Conditions"),
},
{
Name: "spec_finalizers",
Description: "Finalizers is an opaque list of values that must be empty to permanently remove an object from storage.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Spec.Finalizers"),
},
// Steampipe standard columns
{
Name: "title",
Description: "Title of the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
}),
}
}
// LIST FUNCTION
func listProjects(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
config, err := getClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("openshift_project.listProjects", "connection_error", err)
return nil, err
}
client, err := client_v1.NewForConfig(config)
if err != nil {
plugin.Logger(ctx).Error("openshift_project.listProjects", "NewForConfig_error", err)
return nil, err
}
// Limiting the results
maxLimit := int64(1000)
if d.QueryContext.Limit != nil {
limit := *d.QueryContext.Limit
if limit < maxLimit {
maxLimit = limit
}
}
input := v1.ListOptions{
Limit: maxLimit,
}
for {
response, err := client.Projects().List(ctx, input)
if err != nil {
plugin.Logger(ctx).Error("openshift_project.listProjects", "api_error", err)
return nil, err
}
for _, project := range response.Items {
d.StreamListItem(ctx, project)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if response.Continue != "" {
input.Continue = response.Continue
} else {
break
}
}
return nil, nil
}
// HYDRATE FUNCTIONS
func getProject(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
name := d.EqualsQualString("name")
// Check if name is empty.
if name == "" {
return nil, nil
}
config, err := getClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("openshift_project.getProject", "connection_error", err)
return nil, err
}
client, err := client_v1.NewForConfig(config)
if err != nil {
plugin.Logger(ctx).Error("openshift_project.getProject", "NewForConfig_error", err)
return nil, err
}
project, err := client.Projects().Get(ctx, name, v1.GetOptions{})
if err != nil {
plugin.Logger(ctx).Error("openshift_project.getProject", "api_error", err)
return nil, err
}
return project, nil
}