/
table_openshift_build_config.go
168 lines (149 loc) · 5.34 KB
/
table_openshift_build_config.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
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
package openshift
import (
"context"
"strings"
client_v1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/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 tableOpenShiftBuildConfig(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "openshift_build_config",
Description: "Retrieve information about OpenShift build configs.",
List: &plugin.ListConfig{
Hydrate: listBuildConfigs,
KeyColumns: getCommonOptionalKeyQuals(),
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "namespace"}),
Hydrate: getBuildConfig,
},
Columns: commonColumns([]*plugin.Column{
{
Name: "common_spec",
Description: "CommonSpec is the desired build specification.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Spec.CommonSpec"),
},
{
Name: "triggers",
Description: "Triggers determine how new Builds can be launched from a BuildConfig. If no triggers are defined, a new build can only occur as a result of an explicit client build creation.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Spec.Triggers"),
},
{
Name: "run_policy",
Description: "RunPolicy describes how the new build created from this build configuration will be scheduled for execution. This is optional, if not specified we default to Serial.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Spec.RunPolicy"),
},
{
Name: "successful_builds_history_limit",
Description: "It is the number of old successful builds to retain. If not specified, all successful builds are retained.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("Spec.SuccessfulBuildsHistoryLimit"),
},
{
Name: "failed_builds_history_limit",
Description: "It is the number of old failed builds to retain. If not specified, all failed builds are retained.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("Spec.FailedBuildsHistoryLimit"),
},
{
Name: "last_version",
Description: "Last version is used to inform about the number of last triggered builds.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("Status.LastVersion"),
},
{
Name: "image_change_triggers",
Description: "ImageChangeTriggers captures the runtime state of any ImageChangeTrigger specified in the BuildConfigSpec, including the value reconciled by the OpenShift APIServer for the lastTriggeredImageID.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Status.ImageChangeTriggers"),
},
// Steampipe standard columns
{
Name: "title",
Description: "Title of the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
}),
}
}
// LIST FUNCTION
func listBuildConfigs(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
config, err := getClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("openshift_build_config.listBuildConfigs", "connection_error", err)
return nil, err
}
client, err := client_v1.NewForConfig(config)
if err != nil {
plugin.Logger(ctx).Error("openshift_build_config.listBuildConfigs", "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,
}
commonFieldSelectorValue := getCommonOptionalKeyQualsValueForFieldSelector(d)
if len(commonFieldSelectorValue) > 0 {
input.FieldSelector = strings.Join(commonFieldSelectorValue, ",")
}
for {
response, err := client.BuildConfigs("").List(ctx, input)
if err != nil {
plugin.Logger(ctx).Error("openshift_build_config.listBuildConfigs", "api_error", err)
return nil, err
}
for _, buildConfig := range response.Items {
d.StreamListItem(ctx, buildConfig)
// 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 getBuildConfig(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
name := d.EqualsQualString("name")
namespace := d.EqualsQualString("namespace")
// Check if name or namespace is empty.
if name == "" || namespace == "" {
return nil, nil
}
config, err := getClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("openshift_build_config.getBuildConfig", "connection_error", err)
return nil, err
}
client, err := client_v1.NewForConfig(config)
if err != nil {
plugin.Logger(ctx).Error("openshift_build_config.getBuildConfig", "NewForConfig_error", err)
return nil, err
}
buildConfig, err := client.BuildConfigs(namespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
plugin.Logger(ctx).Error("openshift_build_config.getBuildConfig", "api_error", err)
return nil, err
}
return buildConfig, nil
}