-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Changed LintContext from struct to interface * Add mock context and some kube objects * Added a test case to container capabilities check * Found a bug and continue to strengthen test... * Added more tests * gofmt fixes * Addressed comments * Unexport New function
- Loading branch information
1 parent
86d6dfa
commit 0177d11
Showing
22 changed files
with
412 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// AddContainerToPod adds a mock container to the specified pod under context | ||
func (l *MockLintContext) AddContainerToPod( | ||
podName, containerName, image string, | ||
ports []v1.ContainerPort, | ||
env []v1.EnvVar, | ||
sc *v1.SecurityContext, | ||
) error { | ||
pod, ok := l.pods[podName] | ||
if !ok { | ||
return errors.Errorf("pod with name %q is not found", podName) | ||
} | ||
// TODO: keep supporting other fields | ||
pod.Spec.Containers = append(pod.Spec.Containers, v1.Container{ | ||
Name: containerName, | ||
Image: image, | ||
Ports: ports, | ||
Env: env, | ||
Resources: v1.ResourceRequirements{}, | ||
SecurityContext: sc, | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mocks | ||
|
||
import ( | ||
"golang.stackrox.io/kube-linter/internal/lintcontext" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// MockLintContext is mock implementation of the LintContext used in unit tests | ||
type MockLintContext struct { | ||
pods map[string]*v1.Pod | ||
} | ||
|
||
// Objects returns all the objects under this MockLintContext | ||
func (l *MockLintContext) Objects() []lintcontext.Object { | ||
result := make([]lintcontext.Object, 0, len(l.pods)) | ||
for _, p := range l.pods { | ||
result = append(result, lintcontext.Object{Metadata: lintcontext.ObjectMetadata{}, K8sObject: p}) | ||
} | ||
return result | ||
} | ||
|
||
// InvalidObjects is not implemented. For now we don't care about invalid objects for mock context. | ||
func (l *MockLintContext) InvalidObjects() []lintcontext.InvalidObject { | ||
return nil | ||
} | ||
|
||
// NewMockContext returns an empty mockLintContext | ||
func NewMockContext() *MockLintContext { | ||
return &MockLintContext{pods: make(map[string]*v1.Pod)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
v1 "k8s.io/api/core/v1" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// AddMockPod adds a mock Pod to LintContext | ||
func (l *MockLintContext) AddMockPod( | ||
podName, namespace, clusterName string, | ||
labels, annotations map[string]string, | ||
) { | ||
l.pods[podName] = | ||
&v1.Pod{ | ||
TypeMeta: metaV1.TypeMeta{}, | ||
ObjectMeta: metaV1.ObjectMeta{ | ||
Name: podName, | ||
Namespace: namespace, | ||
Labels: labels, | ||
Annotations: annotations, | ||
ClusterName: clusterName, | ||
}, | ||
Spec: v1.PodSpec{}, | ||
Status: v1.PodStatus{}, | ||
} | ||
} | ||
|
||
// AddSecurityContextToPod adds a security context to the pod specified by name | ||
func (l *MockLintContext) AddSecurityContextToPod( | ||
podName string, | ||
runAsUser *int64, | ||
runAsNonRoot *bool, | ||
) error { | ||
pod, ok := l.pods[podName] | ||
if !ok { | ||
return errors.Errorf("pod with name %q is not found", podName) | ||
} | ||
// TODO: keep supporting other fields | ||
pod.Spec.SecurityContext = &v1.PodSecurityContext{ | ||
SELinuxOptions: nil, | ||
WindowsOptions: nil, | ||
RunAsUser: runAsUser, | ||
RunAsGroup: nil, | ||
RunAsNonRoot: runAsNonRoot, | ||
SupplementalGroups: nil, | ||
FSGroup: nil, | ||
Sysctls: nil, | ||
FSGroupChangePolicy: nil, | ||
SeccompProfile: nil, | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.