Skip to content

Commit b15e712

Browse files
killian2kchatton
andauthored
CLOUDP-90457 Create kubelinter-check.yml (#605)
* Create kubelinter-check.yml Added the kubelinter action + reduce the CPU request when doing the e2e tests. Now, the manager.yaml and the openshift operator are in the mongodb namespace * correction kubelinter * add setup go * use cat * adding script to manage the kube-linter * correct script to manage the kube-linter * correct script to manage the kube-linter 2 * list folders in github_workspace * list folders in github_workspace and parent * add dump github context * add dump github context * Makes script more readable and add checkout * list hidden files * fix script file * fix script file 2 * fix script file 2 * add yamls to test_and_integrated_action * add yamls to test_and_integrated_action * Add the exception for specific error * Solve the yaml error files with kube-linter * fix tests * remove useless files * modifier e2e role * Modified a few files, readiness not working * modified the manager file TEMPORARLY to see if the modifications that will be made on openshift may have issues * Solved a few issues, removed probe * Solved a few issues, removed probe 2 * Fix typo * Fix typo 2 * Tests passes locally * fixed role * removed sec context * set manager to default workspace * Fixing minimum CPU required * PModify the e2e cpu requests to lower values * Now tests should pass * reduced cpu amount * PModify the e2e cpu requests to lower values again * added security to manager.yaml * corrected files * Update kubelinter-check.yml * Update role.yaml Removed spaces in last line * Update role.yaml * Update role.yaml * Throw an error when withCPURequest() or other with...() function is called with incorrect params. Changed namespace from default to mongodb * Cancel the tests on mongodb namespace * Update kustomization.yaml * Update operator_openshift.yaml * Update .github/config_files/config_lint.yaml Co-authored-by: Cian Hatton <[email protected]> * Update service_account.yaml * Update kubelinter-check.yml * Update service_account.yaml Co-authored-by: Cian Hatton <[email protected]>
1 parent 0256e7a commit b15e712

File tree

10 files changed

+192
-15
lines changed

10 files changed

+192
-15
lines changed

.github/config_files/config_lint.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
checks:
2+
addAllBuiltIn: true
3+
4+
#Reasons to exclude:
5+
# non-existent-service-account because the service account is created in another file
6+
# minimum-three-replicas because the deployment contains only 1 replica of the operator
7+
# no-readiness-probe & no-liveness-probe because for now, it brings nothing to add these probes
8+
# because they will not check whether the operator is actually ready/living
9+
exclude:
10+
- "non-existent-service-account"
11+
- "minimum-three-replicas"
12+
- "no-liveness-probe"
13+
- "no-readiness-probe"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
checks:
2+
addAllBuiltIn: true
3+
4+
#Reasons to exclude
5+
# non-existent-service-account because the service account is created in another file
6+
# minimum-three-replicas because the deployment contains only 1 replica of the operator
7+
# no-readiness-probe & no-liveness-probe because for now it brings nothing to add theses probes
8+
# because they will not check whether the operator is actually ready/living
9+
# run-as-non-root & no-read-only-root-fs because the security is managed somewhere else
10+
exclude:
11+
- "non-existent-service-account"
12+
- "minimum-three-replicas"
13+
- "no-liveness-probe"
14+
- "no-readiness-probe"
15+
- "run-as-non-root"
16+
- "no-read-only-root-fs"
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Kubelinter-check
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths-ignore:
8+
- docs/**
9+
pull_request:
10+
branches:
11+
- master
12+
workflow_dispatch: {}
13+
14+
jobs:
15+
Kubelinter-check:
16+
name: Run Kube-linter check
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout Code
20+
uses: actions/checkout@v2
21+
22+
- name: Scan directory ./deploy/clusterwide/ with kube-linter
23+
uses: stackrox/kube-linter-action@v1
24+
with:
25+
directory: deploy/clusterwide
26+
config: ${GITHUB_WORKSPACE}/.github/config_files/config_lint.yaml
27+
28+
- name: Scan directory ./deploy/openshift/ with kube-linter
29+
uses: stackrox/kube-linter-action@v1
30+
with:
31+
directory: deploy/openshift
32+
config: ${GITHUB_WORKSPACE}/.github/config_files/config_lint_openshift.yaml
33+
34+
- name: Scan directory ./config/manager/ with kube-linter
35+
uses: stackrox/kube-linter-action@v1
36+
with:
37+
directory: config/manager/manager.yaml
38+
config: ${GITHUB_WORKSPACE}/.github/config_files/config_lint.yaml
39+
40+
- name: Scan directory ./config/samples/ with kube-linter
41+
uses: stackrox/kube-linter-action@v1
42+
with:
43+
directory: config/samples
44+
config: ${GITHUB_WORKSPACE}/.github/config_files/config_lint.yaml

config/manager/manager.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@ apiVersion: apps/v1
22
kind: Deployment
33
metadata:
44
name: mongodb-kubernetes-operator
5+
namespace: mongodb
6+
labels:
7+
owner: mongodb
8+
annotations:
9+
510
spec:
611
replicas: 1
712
selector:
813
matchLabels:
914
name: mongodb-kubernetes-operator
15+
strategy:
16+
type: RollingUpdate
17+
rollingUpdate:
18+
maxUnavailable: 1
1019
template:
1120
metadata:
1221
labels:
1322
name: mongodb-kubernetes-operator
1423
spec:
24+
affinity:
25+
podAntiAffinity:
26+
requiredDuringSchedulingIgnoredDuringExecution:
27+
- labelSelector:
28+
matchExpressions:
29+
- key: name
30+
operator: In
31+
values:
32+
- mongodb-kubernetes-operator
33+
topologyKey: "kubernetes.io/hostname"
1534
containers:
1635
- command:
1736
- /usr/local/bin/entrypoint
@@ -39,4 +58,15 @@ spec:
3958
image: quay.io/mongodb/mongodb-kubernetes-operator:0.6.2
4059
imagePullPolicy: Always
4160
name: mongodb-kubernetes-operator
61+
resources:
62+
limits:
63+
cpu: 1100m
64+
memory: 1Gi
65+
requests:
66+
cpu: 500m
67+
memory: 200Mi
68+
securityContext:
69+
readOnlyRootFilesystem: true
70+
runAsUser: 2000
71+
4272
serviceAccountName: mongodb-kubernetes-operator

config/rbac/role.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apiVersion: rbac.authorization.k8s.io/v1
33
kind: Role
44
metadata:
55
name: mongodb-kubernetes-operator
6+
namespace: mongodb
67
rules:
78
- apiGroups:
89
- ""

deploy/clusterwide/role.yaml

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ rules:
66
- apiGroups:
77
- ""
88
resources:
9-
- pods
109
- services
1110
- configmaps
12-
- secrets
1311
verbs:
1412
- create
1513
- delete
@@ -22,8 +20,18 @@ rules:
2220
- apps
2321
resources:
2422
- statefulsets
23+
verbs:
24+
- delete
25+
- get
26+
- list
27+
- patch
28+
- update
29+
- watch
30+
- apiGroups:
31+
- ""
32+
resources:
33+
- pods
2534
verbs:
26-
- create
2735
- delete
2836
- get
2937
- list

deploy/clusterwide/role_binding.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
subjects:
66
- kind: ServiceAccount
77
name: mongodb-kubernetes-operator
8-
namespace: default
8+
namespace: mongodb
99
roleRef:
1010
kind: ClusterRole
1111
name: mongodb-kubernetes-operator

deploy/e2e/service_account.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ apiVersion: v1
22
kind: ServiceAccount
33
metadata:
44
name: e2e-test
5+
# namespace is 'default' for the e2e tests because the TLS certificates
6+
# generated used as test fixture (in the TLS tests) only work with the
7+
# default namespace.
8+
namespace: default

deploy/openshift/operator_openshift.yaml

+28-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,38 @@ apiVersion: apps/v1
22
kind: Deployment
33
metadata:
44
name: mongodb-kubernetes-operator
5+
namespace: mongodb
6+
labels:
7+
owner: mongodb
8+
annotations:
9+
510
spec:
611
replicas: 1
712
selector:
813
matchLabels:
914
name: mongodb-kubernetes-operator
15+
strategy:
16+
type: RollingUpdate
17+
rollingUpdate:
18+
maxUnavailable: 1
1019
template:
1120
metadata:
1221
labels:
1322
name: mongodb-kubernetes-operator
1423
spec:
24+
affinity:
25+
podAntiAffinity:
26+
requiredDuringSchedulingIgnoredDuringExecution:
27+
- labelSelector:
28+
matchExpressions:
29+
- key: name
30+
operator: In
31+
values:
32+
- mongodb-kubernetes-operator
33+
topologyKey: "kubernetes.io/hostname"
1534
containers:
16-
- command:
35+
- name: mongodb-kubernetes-operator
36+
command:
1737
- mongodb-kubernetes-operator
1838
env:
1939
- name: WATCH_NAMESPACE
@@ -40,5 +60,11 @@ spec:
4060
value: docker.io
4161
image: quay.io/mongodb/mongodb-kubernetes-operator:0.6.2
4262
imagePullPolicy: Always
43-
name: mongodb-kubernetes-operator
63+
resources:
64+
limits:
65+
cpu: 1100m
66+
memory: 1Gi
67+
requests:
68+
cpu: 500m
69+
memory: 200Mi
4470
serviceAccountName: mongodb-kubernetes-operator

test/e2e/setup/setup.go

+44-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
corev1 "k8s.io/api/core/v1"
1919
rbacv1 "k8s.io/api/rbac/v1"
2020
apiErrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/api/resource"
2122
"k8s.io/apimachinery/pkg/runtime"
2223
"k8s.io/apimachinery/pkg/types"
2324
"k8s.io/apimachinery/pkg/util/wait"
@@ -167,6 +168,7 @@ func deployOperator(ctx *e2eutil.Context) error {
167168
withEnvVar(construct.AgentImageEnv, testConfig.agentImage),
168169
withEnvVar(construct.ReadinessProbeImageEnv, testConfig.readinessProbeImage),
169170
withEnvVar(construct.VersionUpgradeHookImageEnv, testConfig.versionUpgradeHookImage),
171+
withCPURequest("50m"),
170172
); err != nil {
171173
return errors.Errorf("error building operator deployment: %s", err)
172174
}
@@ -202,7 +204,7 @@ func hasDeploymentRequiredReplicas(dep *appsv1.Deployment) wait.ConditionFunc {
202204

203205
// buildKubernetesResourceFromYamlFile will create the kubernetes resource defined in yamlFilePath. All of the functional options
204206
// provided will be applied before creation.
205-
func buildKubernetesResourceFromYamlFile(ctx *e2eutil.Context, yamlFilePath string, obj client.Object, options ...func(obj runtime.Object)) error {
207+
func buildKubernetesResourceFromYamlFile(ctx *e2eutil.Context, yamlFilePath string, obj client.Object, options ...func(obj runtime.Object) error) error {
206208
data, err := ioutil.ReadFile(yamlFilePath)
207209
if err != nil {
208210
return errors.Errorf("error reading file: %s", err)
@@ -213,7 +215,9 @@ func buildKubernetesResourceFromYamlFile(ctx *e2eutil.Context, yamlFilePath stri
213215
}
214216

215217
for _, opt := range options {
216-
opt(obj)
218+
if err := opt(obj); err != nil {
219+
return err
220+
}
217221
}
218222

219223
return createOrUpdate(ctx, obj)
@@ -239,11 +243,33 @@ func createOrUpdate(ctx *e2eutil.Context, obj client.Object) error {
239243
return nil
240244
}
241245

246+
// withCPURequest assumes that the underlying type is an appsv1.Deployment.
247+
// it returns a function which will change the amount
248+
// requested for the CPUresource. There will be
249+
// no effect when used with a non-deployment type
250+
func withCPURequest(cpuRequest string) func(runtime.Object) error {
251+
return func(obj runtime.Object) error {
252+
dep, ok := obj.(*appsv1.Deployment)
253+
if !ok {
254+
return errors.Errorf("withCPURequest() called on a non-deployment object")
255+
}
256+
quantityCPU, okCPU := resource.ParseQuantity(cpuRequest)
257+
if okCPU != nil {
258+
return okCPU
259+
}
260+
for _, cont := range dep.Spec.Template.Spec.Containers {
261+
cont.Resources.Requests["cpu"] = quantityCPU
262+
}
263+
264+
return nil
265+
}
266+
}
267+
242268
// withNamespace returns a function which will assign the namespace
243269
// of the underlying type to the value specified. We can
244270
// add new types here as required.
245-
func withNamespace(ns string) func(runtime.Object) {
246-
return func(obj runtime.Object) {
271+
func withNamespace(ns string) func(runtime.Object) error {
272+
return func(obj runtime.Object) error {
247273
switch v := obj.(type) {
248274
case *rbacv1.Role:
249275
v.Namespace = ns
@@ -255,18 +281,24 @@ func withNamespace(ns string) func(runtime.Object) {
255281
v.Namespace = ns
256282
case *appsv1.Deployment:
257283
v.Namespace = ns
284+
default:
285+
return errors.Errorf("withNamespace() called on a non supported object")
258286
}
287+
288+
return nil
259289
}
260290
}
261291

262-
func withEnvVar(key, val string) func(obj runtime.Object) {
263-
return func(obj runtime.Object) {
292+
func withEnvVar(key, val string) func(obj runtime.Object) error {
293+
return func(obj runtime.Object) error {
264294
if testPod, ok := obj.(*corev1.Pod); ok {
265295
testPod.Spec.Containers[0].Env = updateEnvVarList(testPod.Spec.Containers[0].Env, key, val)
266296
}
267297
if testDeployment, ok := obj.(*appsv1.Deployment); ok {
268298
testDeployment.Spec.Template.Spec.Containers[0].Env = updateEnvVarList(testDeployment.Spec.Template.Spec.Containers[0].Env, key, val)
269299
}
300+
301+
return nil
270302
}
271303
}
272304

@@ -282,11 +314,14 @@ func updateEnvVarList(envVarList []corev1.EnvVar, key, val string) []corev1.EnvV
282314

283315
// withOperatorImage assumes that the underlying type is an appsv1.Deployment
284316
// which has the operator container as the first container. There will be
285-
// no effect when used with a non-deployment type
286-
func withOperatorImage(image string) func(runtime.Object) {
287-
return func(obj runtime.Object) {
317+
// an error return when used with a non-deployment type
318+
func withOperatorImage(image string) func(runtime.Object) error {
319+
return func(obj runtime.Object) error {
288320
if dep, ok := obj.(*appsv1.Deployment); ok {
289321
dep.Spec.Template.Spec.Containers[0].Image = image
322+
return nil
290323
}
324+
325+
return fmt.Errorf("withOperatorImage() called on a non-deployment object")
291326
}
292327
}

0 commit comments

Comments
 (0)