Skip to content

Commit

Permalink
Add support for parsing manifests contained into List objects (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreadecorte authored Feb 16, 2022
1 parent f2bbb29 commit 32a6a57
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 2 deletions.
9 changes: 9 additions & 0 deletions e2e/tests/00_static_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ testcases:
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldEqual "No output to display"

- name: static files in a List
steps:
- script: pluto detect-files -d assets/list --target-versions k8s=v1.15.0
assertions:
- result.code ShouldEqual 2
- result.systemout ShouldContainSubstring "NAME KIND VERSION REPLACEMENT REMOVED DEPRECATED"
- result.systemout ShouldContainSubstring "utilities Deployment extensions/v1beta1 apps/v1 false true"
- result.systemout ShouldNotContainSubstring "utilities Deployment apps/v1 false false"
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
apiVersion: v1
items:
- apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: utilities
labels:
app: utilities
spec:
replicas: 1
selector:
matchLabels:
app: utilities
template:
metadata:
labels:
app: utilities
spec:
containers:
- name: utilities
image: quay.io/sudermanjr/utilities:latest
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 10324
capabilities:
drop:
- ALL
resources:
requests:
cpu: 30m
memory: 64Mi
limits:
cpu: 100m
memory: 128Mi
- apiVersion: apps/v1
kind: Deployment
metadata:
name: utilities
labels:
app: utilities
spec:
replicas: 1
selector:
matchLabels:
app: utilities
template:
metadata:
labels:
app: utilities
spec:
containers:
- name: utilities
image: quay.io/sudermanjr/utilities:latest
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 10324
capabilities:
drop:
- ALL
resources:
requests:
cpu: 30m
memory: 64Mi
limits:
cpu: 100m
memory: 128Mi
kind: List
metadata:
resourceVersion: ''
selfLink: ''
42 changes: 42 additions & 0 deletions e2e/tests/assets/list/list-deployment-non-deprecated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: v1
items:
- apiVersion: apps/v1
kind: Deployment
metadata:
name: utilities
labels:
app: utilities
spec:
replicas: 1
selector:
matchLabels:
app: utilities
template:
metadata:
labels:
app: utilities
spec:
containers:
- name: utilities
image: quay.io/sudermanjr/utilities:latest
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 10324
capabilities:
drop:
- ALL
resources:
requests:
cpu: 30m
memory: 64Mi
limits:
cpu: 100m
memory: 128Mi
kind: List
metadata:
resourceVersion: ''
selfLink: ''
19 changes: 17 additions & 2 deletions pkg/api/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Stub struct {
Kind string `json:"kind" yaml:"kind"`
APIVersion string `json:"apiVersion" yaml:"apiVersion"`
Metadata StubMeta `json:"metadata" yaml:"metadata"`
Items []Stub `json:"items" yaml:"items"`
}

// StubMeta will catch kube resource metadata
Expand Down Expand Up @@ -132,7 +133,7 @@ func jsonToStub(data []byte) ([]*Stub, error) {
if err != nil {
return nil, err
}
stubs = append(stubs, stub)
expandList(&stubs, stub)
return stubs, nil
}

Expand All @@ -155,14 +156,28 @@ func yamlToStub(data []byte) ([]*Stub, error) {
}
return stubs, err
}
stubs = append(stubs, stub)
expandList(&stubs, stub)
}
if stubs == nil && len(errs) > 0 {
return nil, fmt.Errorf("one or more errors parsing yaml resulted in no versions found: %v", errs)
}
return stubs, nil
}

// expandList checks if we have a List manifest.
// If it is the case, the manifests inside are expanded, otherwise we just return the single manifest
func expandList(stubs *[]*Stub, currentStub *Stub) {
if currentStub.Items != nil {
klog.V(5).Infof("found a list with %d items, attempting to expand", len(currentStub.Items))
for _, stub := range currentStub.Items {
currentItem := stub
*stubs = append(*stubs, &currentItem)
}
} else {
*stubs = append(*stubs, currentStub)
}
}

// IsDeprecatedIn returns true if the version is deprecated in the applicable targetVersion
// Will return false if the targetVersion passed is not a valid semver string
func (v *Version) isDeprecatedIn(targetVersions map[string]string) bool {
Expand Down
24 changes: 24 additions & 0 deletions pkg/api/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func Test_jsonToStub(t *testing.T) {
want: []*Stub{{Kind: "foo", APIVersion: "bar"}},
wantErr: false,
},
{
name: "json list is multiple stubs",
data: []byte(`{"kind": "List", "apiVersion": "v1", "items": [{"kind": "foo", "apiVersion": "bar"},{"kind": "bar", "apiVersion": "foo"}]}`),
want: []*Stub{{Kind: "foo", APIVersion: "bar"},{Kind: "bar", APIVersion: "foo"}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -122,6 +128,12 @@ func Test_yamlToStub(t *testing.T) {
want: []*Stub{{Kind: "foo", APIVersion: "bar"}},
wantErr: false,
},
{
name: "yaml list is multiple stubs",
data: []byte("kind: List\napiVersion: v1\nitems:\n- kind: foo\n apiVersion: bar\n- kind: bar\n apiVersion: foo"),
want: []*Stub{{Kind: "foo", APIVersion: "bar"},{Kind: "bar", APIVersion: "foo"}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -226,6 +238,12 @@ func Test_IsVersioned(t *testing.T) {
want: []*Output{{APIVersion: &testVersionDeployment}},
wantErr: false,
},
{
name: "yaml list has version",
data: []byte("kind: List\napiVersion: v1\nitems:\n- kind: Deployment\n apiVersion: extensions/v1beta1"),
want: []*Output{{APIVersion: &testVersionDeployment}},
wantErr: false,
},
{
name: "json no version",
data: []byte("{}"),
Expand All @@ -250,6 +268,12 @@ func Test_IsVersioned(t *testing.T) {
want: []*Output{{APIVersion: &testVersionDeployment}},
wantErr: false,
},
{
name: "json list has version",
data: []byte(`{"kind": "List", "apiVersion": "v1", "items": [{"kind": "Deployment", "apiVersion": "extensions/v1beta1"}]}`),
want: []*Output{{APIVersion: &testVersionDeployment}},
wantErr: false,
},
{
name: "not yaml",
data: []byte("*."),
Expand Down

0 comments on commit 32a6a57

Please sign in to comment.