Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing manifests contained into List objects #263

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
andreadecorte marked this conversation as resolved.
Show resolved Hide resolved
}
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