Skip to content

Commit 50928a4

Browse files
cbruno10graza-io
andauthored
Deprecate source_type config arg and add source_types config arg closes #166 (#167)
Co-authored-by: Graza <code@graza.io>
1 parent 7afb777 commit 50928a4

File tree

8 files changed

+108
-72
lines changed

8 files changed

+108
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
# vendor/
1616

1717
.vscode/*
18+
.idea

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ connection "kubernetes" {
5454
# If no kubeconfig file can be found, the plugin will attempt to use the service account Kubernetes gives to pods.
5555
# This authentication method is intended for clients that expect to be running inside a pod running on Kubernetes.
5656
57-
# Specify the source of the resource. Possible values: `deployed`, `helm`, `manifest`, and `all`.
58-
# Default set to `all`. Set the argument to override the default value.
59-
# If the value is set to `deployed`, tables will show all the deployed resources.
60-
# If set to `helm`, tables will only show resources from the configured helm charts.
61-
# If set to `manifest`, tables will show all the resources from the kubernetes manifest. Make sure that the `manifest_file_paths` arg is set.
62-
# If `all`, tables will show all the deployed and manifest resources.
63-
# source_type = "all"
57+
# Specify the source(s) of the resource(s). Possible values: `deployed`, `helm` and `manifest`.
58+
# Defaults to all possible values. Set the argument to override the default value.
59+
# If `deployed` is contained in the value, tables will show all the deployed resources.
60+
# If `helm` is contained in the value, tables will show resources from the configured helm charts.
61+
# If `manifest` is contained in the value, tables will show all the resources from the kubernetes manifest. Make sure that the `manifest_file_paths` arg is set.
62+
# source_types = ["deployed", "helm", "manifest"]
6463
6564
# Manifest File Configuration
6665

config/kubernetes.spc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ connection "kubernetes" {
2828
# If no kubeconfig file can be found, the plugin will attempt to use the service account Kubernetes gives to pods.
2929
# This authentication method is intended for clients that expect to be running inside a pod running on Kubernetes.
3030

31-
# Specify the source of the resource. Possible values: `deployed`, `helm`, `manifest`, and `all`.
32-
# Default set to `all`. Set the argument to override the default value.
33-
# If the value is set to `deployed`, tables will show all the deployed resources.
34-
# If set to `helm`, tables will only show resources from the configured helm charts.
35-
# If set to `manifest`, tables will show all the resources from the kubernetes manifest. Make sure that the `manifest_file_paths` arg is set.
36-
# If `all`, tables will show all the deployed and manifest resources.
37-
# source_type = "all"
31+
# Specify the source(s) of the resource(s). Possible values: `deployed`, `helm` and `manifest`.
32+
# Defaults to all possible values. Set the argument to override the default value.
33+
# If `deployed` is contained in the value, tables will show all the deployed resources.
34+
# If `helm` is contained in the value, tables will show resources from the configured helm charts.
35+
# If `manifest` is contained in the value, tables will show all the resources from the kubernetes manifest. Make sure that the `manifest_file_paths` arg is set.
36+
# source_types = ["deployed", "helm", "manifest"]
3837

3938
# Manifest File Configuration
4039

docs/index.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ connection "kubernetes" {
4343
plugin = "kubernetes"
4444
config_path = "~/.kube/config"
4545
config_context = "myCluster"
46+
source_types = ["deployed"]
4647
}
4748
```
4849

@@ -113,12 +114,14 @@ connection "kubernetes_cluster_aks" {
113114
plugin = "kubernetes"
114115
config_path = "~/.kube/config"
115116
config_context = "myAKSCluster"
117+
source_types = ["deployed"]
116118
}
117119
118120
connection "kubernetes_cluster_eks" {
119121
plugin = "kubernetes"
120122
config_path = "~/.kube/config"
121123
config_context = "arn:aws:eks:us-east-1:123456789012:cluster/myEKSCluster"
124+
source_types = ["deployed"]
122125
}
123126
```
124127

@@ -174,20 +177,22 @@ connection "kubernetes" {
174177
"github.com/GoogleCloudPlatform/microservices-demo//release//kubernetes-manifests.yaml",
175178
"s3::https://bucket.s3.us-east-1.amazonaws.com/test_folder//*.yml"
176179
]
180+
181+
source_types = ["manifest"]
177182
}
178183
```
179184

180185
**Note**: If any path matches on `*` without `.yml` or `.yaml` or `.json`, all files (including non-Kubernetes manifest files) in the directory will be matched, which may cause errors if incompatible file types exist.
181186

182-
By default the plugin always lists the resources deployed in the current Kubernetes cluster context. If you want to restrict this behavior to read resource configurations from the configured manifest files only, add the `source_type` argument to the config and set the value to `manifest`. For example:
187+
By default the plugin always lists the resources deployed in the current Kubernetes cluster context. If you want to restrict this behavior to read resource configurations from the configured manifest files only, add the `source_types` argument to the config and set the value to `manifest`. For example:
183188

184189
```hcl
185190
connection "kubernetes" {
186191
plugin = "kubernetes"
187192
188193
manifest_file_paths = [ ... ]
189194
190-
source_type = "manifest"
195+
source_types = ["manifest"]
191196
}
192197
```
193198

@@ -209,6 +214,8 @@ connection "kubernetes" {
209214
plugin = "kubernetes"
210215
211216
manifest_file_paths = [ "*.yml", "*.yaml", "*.json", "/path/to/dir/main.yml" ]
217+
218+
source_types = ["manifest"]
212219
}
213220
```
214221

@@ -230,6 +237,8 @@ connection "kubernetes" {
230237
plugin = "kubernetes"
231238
232239
manifest_file_paths = [ "bitbucket.org/atlassian/kubectl-run//test/kustomization//deploy.yml" ]
240+
241+
source_types = ["manifest"]
233242
}
234243
```
235244

@@ -255,6 +264,8 @@ connection "kubernetes" {
255264
"s3::https://bucket-2.s3.us-east-1.amazonaws.com//*.yml?aws_profile=<AWS_PROFILE>",
256265
"s3::https://bucket-2.s3.us-east-1.amazonaws.com/test_folder//*.yaml?aws_profile=<AWS_PROFILE>"
257266
]
267+
268+
source_types = ["manifest"]
258269
}
259270
```
260271

@@ -299,6 +310,8 @@ connection "kubernetes" {
299310
"s3::https://bucket-1.s3.us-east-1.amazonaws.com/test_folder//*.yml",
300311
"s3::https://bucket-2.s3.us-east-1.amazonaws.com/test_folder//**/*.yaml"
301312
]
313+
314+
source_types = ["manifest"]
302315
}
303316
```
304317

@@ -332,6 +345,8 @@ connection "kubernetes" {
332345
values_file_paths = [] # works with values from chart's default values.yaml file
333346
}
334347
}
348+
349+
source_types = ["helm"]
335350
}
336351
```
337352

kubernetes/connection_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type kubernetesConfig struct {
1111
CustomResourceTables []string `hcl:"custom_resource_tables,optional"`
1212
ManifestFilePaths []string `hcl:"manifest_file_paths,optional" steampipe:"watch"`
1313
SourceType *string `hcl:"source_type,optional"`
14+
SourceTypes []string `hcl:"source_types,optional"`
1415
HelmRenderedCharts map[string]chartConfig `hcl:"helm_rendered_charts,optional"`
1516
}
1617

kubernetes/helm_template_render.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func getHelmRenderedTemplatesUncached(ctx context.Context, d *plugin.QueryData,
7777
if err != nil {
7878
return nil, err
7979
}
80-
helmConfig := GetConfig(d.Connection)
80+
kubernetesConfig := GetConfig(d.Connection)
8181

8282
var renderedTemplates []HelmRenderedTemplate
8383
for _, chart := range charts {
@@ -89,7 +89,7 @@ func getHelmRenderedTemplatesUncached(ctx context.Context, d *plugin.QueryData,
8989
}
9090

9191
var processedHelmConfigs []string
92-
for name, c := range helmConfig.HelmRenderedCharts {
92+
for name, c := range kubernetesConfig.HelmRenderedCharts {
9393
if c.ChartPath == chart.Path && !helpers.StringSliceContains(processedHelmConfigs, name) {
9494

9595
// Add the processed Helm render configs into processedHelmConfigs to avoid duplicate entries

kubernetes/helm_utils.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,32 @@ var parsedHelmChartCached = plugin.HydrateFunc(parsedHelmChartUncached).Memoize(
4444
// getParsedHelmChart instead.
4545
func parsedHelmChartUncached(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (any, error) {
4646
// Read the config
47-
helmConfig := GetConfig(d.Connection)
47+
kubernetesConfig := GetConfig(d.Connection)
4848

49-
// Return nil, if the source_type is set to other than "all" or "helm"
50-
if helmConfig.SourceType != nil &&
51-
!helpers.StringSliceContains([]string{"all", "helm"}, *helmConfig.SourceType) {
49+
// Check for the sourceTypes argument in the config.
50+
// Default set to include values.
51+
var sources = All.ToSourceTypes()
52+
if kubernetesConfig.SourceTypes != nil {
53+
sources = kubernetesConfig.SourceTypes
54+
}
55+
// TODO: Remove once `SourceType` is obsolete
56+
if kubernetesConfig.SourceTypes == nil && kubernetesConfig.SourceType != nil {
57+
if *kubernetesConfig.SourceType != "all" { // if is all, sources is already set by default
58+
sources = []string{*kubernetesConfig.SourceType}
59+
}
60+
}
61+
62+
if !helpers.StringSliceContains(sources, "helm") {
5263
return nil, nil
5364
}
5465

5566
var charts []*parsedHelmChart
5667

57-
for _, v := range helmConfig.HelmRenderedCharts {
58-
// Return error if source_tpe arg is explicitly set to "helm" in the config, but
68+
for _, v := range kubernetesConfig.HelmRenderedCharts {
69+
// Return error if source_types arg includes "helm" in the config, but
5970
// helm_chart_dir arg is not set.
60-
if helmConfig.SourceType != nil &&
61-
*helmConfig.SourceType == "helm" &&
62-
v.ChartPath == "" {
63-
return nil, errors.New("helm_chart_dir must be set in the config while the source_type is 'helm'")
71+
if v.ChartPath == "" {
72+
return nil, errors.New("helm_chart_dir must be set in the config while source_types includes 'helm'")
6473
}
6574

6675
// Return empty parsedHelmChart object if no Helm chart directory path provided in the config
@@ -124,7 +133,7 @@ func getUniqueValueFilesFromConfig(ctx context.Context, d *plugin.QueryData) []s
124133

125134
// getHelmClient creates the client for Helm
126135
func getHelmClient(ctx context.Context, namespace string) (helmClient.Client, error) {
127-
// Return nil, if no namespace provided
136+
// Return nil if no namespace provided
128137
if namespace == "" {
129138
return nil, nil
130139
}

kubernetes/utils.go

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ func (sourceType SourceType) String() string {
5858
return string(sourceType)
5959
}
6060

61+
// ToSourceTypes is used to convert SourceType to []string
62+
func (sourceType SourceType) ToSourceTypes() []string {
63+
if sourceType == All {
64+
return []string{Deployed.String(), Helm.String(), Manifest.String()}
65+
} else {
66+
return []string{sourceType.String()}
67+
}
68+
}
69+
6170
// GetNewClientset :: gets client for querying k8s apis for the provided context
6271
func GetNewClientset(ctx context.Context, d *plugin.QueryData) (*kubernetes.Clientset, error) {
6372
logger := plugin.Logger(ctx)
@@ -75,7 +84,7 @@ func GetNewClientset(ctx context.Context, d *plugin.QueryData) (*kubernetes.Clie
7584
return nil, err
7685
}
7786

78-
// Return nil, if the config is set only to list the manifest resources.
87+
// Return nil if deployed resources should not be included
7988
if kubeconfig == nil {
8089
return nil, nil
8190
}
@@ -142,7 +151,7 @@ func GetNewClientCRD(ctx context.Context, d *plugin.QueryData) (*apiextension.Cl
142151
return nil, err
143152
}
144153

145-
// Return nil, if the config is set to only list the manifest resources.
154+
// Return nil if deployed resources should not be included
146155
if kubeconfig == nil {
147156
return nil, nil
148157
}
@@ -196,7 +205,7 @@ func GetNewClientCRDRaw(ctx context.Context, cc *connection.ConnectionCache, c *
196205
return nil, err
197206
}
198207

199-
// Return nil, if the config is set to only list the manifest resources.
208+
// Return nil if deployed resources should not be included
200209
if kubeconfig == nil {
201210
return nil, nil
202211
}
@@ -336,24 +345,21 @@ func getK8Config(ctx context.Context, d *plugin.QueryData) (clientcmd.ClientConf
336345
// get kubernetes config info
337346
kubernetesConfig := GetConfig(d.Connection)
338347

339-
// Check for the sourceType argument in the config. Valid values are: "deployed", "manifest" and "all".
340-
// Default set to "all".
341-
var source SourceType = "all"
342-
if kubernetesConfig.SourceType != nil {
343-
source = SourceType(*kubernetesConfig.SourceType)
344-
if err := source.IsValid(); err != nil {
345-
plugin.Logger(ctx).Debug("getK8Config", "invalid_source_type_error", "connection", d.Connection.Name, "error", err)
346-
return nil, err
348+
// Check for the sourceTypes argument in the config
349+
// Default set to include values
350+
var sources = All.ToSourceTypes()
351+
if kubernetesConfig.SourceTypes != nil {
352+
sources = kubernetesConfig.SourceTypes
353+
}
354+
// TODO: Remove once `SourceType` is obsolete
355+
if kubernetesConfig.SourceTypes == nil && kubernetesConfig.SourceType != nil {
356+
if *kubernetesConfig.SourceType != "all" { // if is all, sources is already set by default
357+
sources = []string{*kubernetesConfig.SourceType}
347358
}
348359
}
349360

350-
// By default source type is set to "all", which indicates querying the table will return both deployed, helm and manifest resources.
351-
// If the source type is explicitly set, other plugin will list resources based on that. For example:
352-
// If set to "manifest", the table will only return the manifest resources.
353-
// If set to "helm", the table will return the resources after rendering the templates defined in the configured chart.
354-
// Similarly, setting the value as "deployed" will return all the deployed resources.
355-
if source.String() == "manifest" || source.String() == "helm" {
356-
plugin.Logger(ctx).Debug("getK8Config", "Returning nil for API server client.", "Source type", source.String(), "connection", d.Connection.Name)
361+
if !helpers.StringSliceContains(sources, "deployed") {
362+
plugin.Logger(ctx).Debug("getK8Config", "Returning nil for API server client.", "source_types", sources, "connection", d.Connection.Name)
357363
return nil, nil
358364
}
359365

@@ -427,24 +433,21 @@ func getK8ConfigRaw(ctx context.Context, cc *connection.ConnectionCache, c *plug
427433
// get kubernetes config info
428434
kubernetesConfig := GetConfig(c)
429435

430-
// Check for the sourceType argument in the config. Valid values are: "deployed", "manifest" and "all".
431-
// Default set to "all".
432-
var source SourceType = "all"
433-
if kubernetesConfig.SourceType != nil {
434-
source = SourceType(*kubernetesConfig.SourceType)
435-
if err := source.IsValid(); err != nil {
436-
plugin.Logger(ctx).Debug("getK8ConfigRaw", "invalid_source_type_error", "connection", c.Name, "error", err)
437-
return nil, err
436+
// Check for the sourceTypes argument in the config.
437+
// Default set to include values.
438+
var sources = All.ToSourceTypes()
439+
if kubernetesConfig.SourceTypes != nil {
440+
sources = kubernetesConfig.SourceTypes
441+
}
442+
// TODO: Remove once `SourceType` is obsolete
443+
if kubernetesConfig.SourceTypes == nil && kubernetesConfig.SourceType != nil {
444+
if *kubernetesConfig.SourceType != "all" { // if is all, sources is already set by default
445+
sources = []string{*kubernetesConfig.SourceType}
438446
}
439447
}
440448

441-
// By default source type is set to "all", which indicates querying the table will return all the deployed, helm and manifest resources.
442-
// If the source type is explicitly set, other plugin will list resources based on that. For example:
443-
// If set to "manifest", the table will only return the manifest resources.
444-
// If set to "helm", the table will return the resources after rendering the templates defined in the configured chart.
445-
// Similarly, setting the value as "deployed" will return all the deployed resources.
446-
if source.String() == "manifest" || source.String() == "helm" {
447-
plugin.Logger(ctx).Debug("getK8ConfigRaw", "Returning nil for API server client.", "Source type", source.String(), "connection", c.Name)
449+
if !helpers.StringSliceContains(sources, "deployed") {
450+
plugin.Logger(ctx).Debug("getK8ConfigRaw", "Returning nil for API server client.", "source_types", sources, "connection", c.Name)
448451
return nil, nil
449452
}
450453

@@ -794,25 +797,34 @@ func parsedManifestFileContentUncached(ctx context.Context, d *plugin.QueryData,
794797
// Returns the list of file paths/glob patterns after resolving all the given manifest file paths.
795798
func resolveManifestFilePaths(ctx context.Context, d *plugin.QueryData) ([]string, error) {
796799
// Read the config
797-
k8sConfig := GetConfig(d.Connection)
800+
kubernetesConfig := GetConfig(d.Connection)
801+
802+
// Check for the sourceTypes argument in the config. Valid values are: "deployed", "manifest" and "helm".
803+
// Default set to include values.
804+
var sources = All.ToSourceTypes()
805+
if kubernetesConfig.SourceTypes != nil {
806+
sources = kubernetesConfig.SourceTypes
807+
}
808+
// TODO: Remove once `SourceType` is obsolete
809+
if kubernetesConfig.SourceTypes == nil && kubernetesConfig.SourceType != nil {
810+
if *kubernetesConfig.SourceType != "all" { // if is all, sources is already set by default
811+
sources = []string{*kubernetesConfig.SourceType}
812+
}
813+
}
798814

799-
// Return nil, if the source_type is set other than "manifest", or "all"
800-
if k8sConfig.SourceType != nil &&
801-
!helpers.StringSliceContains([]string{"all", "manifest"}, *k8sConfig.SourceType) {
815+
if !helpers.StringSliceContains(sources, "manifest") {
802816
return nil, nil
803817
}
804818

805-
// Return error if source_tpe arg is explicitly set to "manifest" in the config, but
819+
// Return error if source_types arg includes "manifest" in the config, but
806820
// manifest_file_paths arg is not set.
807-
if k8sConfig.SourceType != nil &&
808-
*k8sConfig.SourceType == "manifest" &&
809-
k8sConfig.ManifestFilePaths == nil {
810-
return nil, errors.New("manifest_file_paths must be set in the config while the source_type is 'manifest'")
821+
if kubernetesConfig.ManifestFilePaths == nil {
822+
return nil, errors.New("manifest_file_paths must be set in the config while the source_types includes 'manifest'")
811823
}
812824

813825
// Gather file path matches for the glob
814826
var matches, resolvedPaths []string
815-
paths := k8sConfig.ManifestFilePaths
827+
paths := kubernetesConfig.ManifestFilePaths
816828
for _, i := range paths {
817829

818830
// List the files in the given source directory

0 commit comments

Comments
 (0)