Skip to content

Commit 94c91e6

Browse files
committed
refactor: apply clean code
1 parent 86de77a commit 94c91e6

File tree

1 file changed

+7
-134
lines changed

1 file changed

+7
-134
lines changed

main.go

Lines changed: 7 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ func createPod(clientset *kubernetes.Clientset) Result {
8989

9090
pod, err := clientset.CoreV1().Pods(expectedNamespace).Get(context.TODO(), expectedPodName, metav1.GetOptions{})
9191

92-
if err != nil {
93-
return Result{
94-
TestName: "Question 1 - Create a pod nginx name with nginx:alpine image",
95-
Passed: false,
96-
Difficulty: "Easy",
97-
}
98-
}
99-
10092
passed := err == nil &&
10193
pod.Spec.Containers[0].Image == expectedImage &&
10294
pod.Name == expectedPodName
@@ -118,14 +110,6 @@ func createDeployment(clientset *kubernetes.Clientset) Result {
118110

119111
deployment, err := clientset.AppsV1().Deployments(expectedNamespace).Get(context.TODO(), expectedDeploymentName, metav1.GetOptions{})
120112

121-
if err != nil {
122-
return Result{
123-
TestName: "Question 2 - Create a deployment nginx-deployment with nginx:alpine image and 4 replicas",
124-
Passed: false,
125-
Difficulty: "Medium",
126-
}
127-
}
128-
129113
passed := err == nil &&
130114
expectedDeploymentName == deployment.Name &&
131115
expectedReplicas == *deployment.Spec.Replicas &&
@@ -150,14 +134,6 @@ func createDeploymentAndService(clientset *kubernetes.Clientset) Result {
150134
deployment, err := clientset.AppsV1().Deployments(expectedNamespace).Get(context.TODO(), expectedDeploymentName, metav1.GetOptions{})
151135
service, err := clientset.CoreV1().Services(expectedNamespace).Get(context.TODO(), expectedServiceName, metav1.GetOptions{})
152136

153-
if err != nil {
154-
return Result{
155-
TestName: "Question 3 - Create a deployment redis name with redis:alpine image and a service with name redis-service and port 6379 in namespace latam",
156-
Passed: false,
157-
Difficulty: "Hard",
158-
}
159-
}
160-
161137
passed := err == nil &&
162138
service != nil &&
163139
expectedDeploymentName == deployment.Name &&
@@ -179,14 +155,6 @@ func createNamespace(clientset *kubernetes.Clientset) Result {
179155

180156
namespace, err := clientset.CoreV1().Namespaces().Get(context.TODO(), expectedNamespace, metav1.GetOptions{})
181157

182-
if err != nil {
183-
return Result{
184-
TestName: "Question 4 - Create a namespace europe",
185-
Passed: false,
186-
Difficulty: "Easy",
187-
}
188-
}
189-
190158
passed := err == nil &&
191159
expectedNamespace == namespace.Name
192160

@@ -207,14 +175,6 @@ func createConfigMap(clientset *kubernetes.Clientset) Result {
207175

208176
configMap, err := clientset.CoreV1().ConfigMaps(expectedNamespace).Get(context.TODO(), expectedConfigMapName, metav1.GetOptions{})
209177

210-
if err != nil {
211-
return Result{
212-
TestName: "Question 5 - Create a configmap europe-configmap with data France=Paris",
213-
Passed: false,
214-
Difficulty: "Medium",
215-
}
216-
}
217-
218178
passed := err == nil &&
219179
expectedConfigMapName == configMap.Name &&
220180
expectedDataValue == configMap.Data[expectedDataKey]
@@ -237,14 +197,6 @@ func createLabel(clientset *kubernetes.Clientset) Result {
237197

238198
pod, err := clientset.CoreV1().Pods(expectedNamespace).Get(context.TODO(), expectedPodName, metav1.GetOptions{})
239199

240-
if err != nil {
241-
return Result{
242-
TestName: "Question 6 - Create a pod thsoot with label country=china with amazon/amazon-ecs-network-sidecar:latest image and namespace asia",
243-
Passed: false,
244-
Difficulty: "Medium",
245-
}
246-
}
247-
248200
passed := err == nil &&
249201
expectedImage == pod.Spec.Containers[0].Image &&
250202
expectedLabelValue == pod.ObjectMeta.Labels[expectedLabelKey]
@@ -266,14 +218,6 @@ func createPersistentVolume(clientset *kubernetes.Clientset) Result {
266218

267219
pv, err := clientset.CoreV1().PersistentVolumes().Get(context.TODO(), expectedPersistentVolumeName, metav1.GetOptions{})
268220

269-
if err != nil {
270-
return Result{
271-
TestName: "Question 7 - Create a persistent volume unicorn-pv with capacity 1Gi and access mode ReadWriteMany and host path /tmp/data",
272-
Passed: false,
273-
Difficulty: "Medium",
274-
}
275-
}
276-
277221
passed := err == nil &&
278222
expectedCapacity == pv.Spec.Capacity.Storage().String() &&
279223
expectedAccessMode == pv.Spec.AccessModes[0] &&
@@ -296,14 +240,6 @@ func createPersistentVolumeClaim(clientset *kubernetes.Clientset) Result {
296240

297241
pvc, err := clientset.CoreV1().PersistentVolumeClaims(expectedNamespace).Get(context.TODO(), expectedPersistentVolumeClaimName, metav1.GetOptions{})
298242

299-
if err != nil {
300-
return Result{
301-
TestName: "Question 8 - Create a persistent volume claim unicorn-pvc with capacity 400Mi and access mode ReadWriteMany",
302-
Passed: false,
303-
Difficulty: "Medium",
304-
}
305-
}
306-
307243
passed := err == nil &&
308244
expectedCapacity == pvc.Spec.Resources.Requests.Storage().String() &&
309245
expectedAccessMode == pvc.Spec.AccessModes[0]
@@ -327,14 +263,6 @@ func createPodVolumeClaim(clientset *kubernetes.Clientset) Result {
327263

328264
pod, err := clientset.CoreV1().Pods(expectedNamespace).Get(context.TODO(), expectedPodName, metav1.GetOptions{})
329265

330-
if err != nil {
331-
return Result{
332-
TestName: "Question 9 - Create a pod webserver in public namespace with nginx:alpine image and a volume mount /usr/share/nginx/html and a persistent volume claim unicorn-pvc",
333-
Passed: false,
334-
Difficulty: "Hard",
335-
}
336-
}
337-
338266
passed := err == nil &&
339267
expectedImage == pod.Spec.Containers[0].Image &&
340268
expectedPersistentVolumeClaim == pod.Spec.Volumes[0].VolumeSource.PersistentVolumeClaim.ClaimName &&
@@ -357,13 +285,6 @@ func checkPodError(clientset *kubernetes.Clientset) Result {
357285

358286
pod, err := clientset.CoreV1().Pods(expectedNamespace).Get(context.TODO(), expectedPodName, metav1.GetOptions{})
359287

360-
if err != nil {
361-
return Result{
362-
TestName: "Question 10 - There is a pod with problem, Can you able to solve it ? Find the problem and fix it",
363-
Passed: false,
364-
Difficulty: "Medium",
365-
}
366-
}
367288
passed := err == nil &&
368289
expectedImage == pod.Spec.Containers[0].Image
369290

@@ -385,14 +306,6 @@ func createNetPolRule(clientset *kubernetes.Clientset) Result {
385306

386307
netPol, err := clientset.NetworkingV1().NetworkPolicies(expectedNamespace).Get(context.TODO(), expectedNetPolName, metav1.GetOptions{})
387308

388-
if err != nil {
389-
return Result{
390-
TestName: "Question 11 - Create a network policy allow-policy-colors to allow redmobile-webserver to access bluemobile-dbcache.",
391-
Passed: false,
392-
Difficulty: "Hard",
393-
}
394-
}
395-
396309
passed := err == nil && hasCorrectIngressRule(netPol.Spec.Ingress)
397310

398311
return Result{
@@ -412,14 +325,6 @@ func createSecret(clientset *kubernetes.Clientset) Result {
412325

413326
secret, err := clientset.CoreV1().Secrets(expectedNamespace).Get(context.TODO(), expectedSecretName, metav1.GetOptions{})
414327

415-
if err != nil {
416-
return Result{
417-
TestName: "Question 12 - Create a secret secret-colors with data color=red in colors namespace",
418-
Passed: false,
419-
Difficulty: "Easy",
420-
}
421-
}
422-
423328
passed := err == nil &&
424329
expectedSecretName == secret.Name &&
425330
expectedDataValue == string(secret.Data[expectedDataKey])
@@ -444,14 +349,6 @@ func createPodAddSecret(clientset *kubernetes.Clientset) Result {
444349
pod, err := clientset.CoreV1().Pods(expectedNamespace).Get(context.TODO(), expectedPodName, metav1.GetOptions{})
445350
secret, err := clientset.CoreV1().Secrets(expectedNamespace).Get(context.TODO(), expectedSecretName, metav1.GetOptions{})
446351

447-
if err != nil {
448-
return Result{
449-
TestName: "Question 13 - Add a secret secret-purple with data singer=prince to the pod purple with image redis:alpine in colors namespace",
450-
Passed: false,
451-
Difficulty: "Medium",
452-
}
453-
}
454-
455352
passed := err == nil &&
456353
expectedSecretName == pod.Spec.Volumes[0].Secret.SecretName &&
457354
expectedDataValue == string(secret.Data[expectedDataKey]) &&
@@ -647,13 +544,13 @@ func createDeploymentYellow(clientset *kubernetes.Clientset) Result {
647544

648545
deployment, err := clientset.AppsV1().Deployments(expectedNamespace).Get(context.TODO(), expectedName, metav1.GetOptions{})
649546

650-
if err != nil {
651-
return Result{
652-
TestName: "Question 20 - Create a deployment yellow-deployment with bonovoo/node-app:1.0 image and 2 replicas in namespace colors",
653-
Passed: false,
654-
Difficulty: "Easy",
655-
}
656-
}
547+
// if err != nil {
548+
// return Result{
549+
// TestName: "Question 20 - Create a deployment yellow-deployment with bonovoo/node-app:1.0 image and 2 replicas in namespace colors",
550+
// Passed: false,
551+
// Difficulty: "Easy",
552+
// }
553+
// }
657554

658555
passed := err == nil &&
659556
expectedImage == deployment.Spec.Template.Spec.Containers[0].Image &&
@@ -787,14 +684,6 @@ func createJob(clientset *kubernetes.Clientset) Result {
787684

788685
job, err := clientset.BatchV1().Jobs(expectedNamespace).Get(context.TODO(), expectedJobName, metav1.GetOptions{})
789686

790-
if err != nil {
791-
return Result{
792-
TestName: "Question 24 - Create a job job-gain with parallelism 2, completions 4, backoffLimit 3 and deadlineSeconds 40",
793-
Passed: false,
794-
Difficulty: "Medium",
795-
}
796-
}
797-
798687
passed := err == nil &&
799688
expectedParallelism == *job.Spec.Parallelism &&
800689
expectedCompletions == *job.Spec.Completions &&
@@ -821,14 +710,6 @@ func createCronjob(clientset *kubernetes.Clientset) Result {
821710

822711
cronjob, err := clientset.BatchV1().CronJobs(expectedNamespace).Get(context.TODO(), expectedName, metav1.GetOptions{})
823712

824-
if err != nil {
825-
return Result{
826-
TestName: "Question 25 - Create a cronjob echo run a each 5 minutes with image busybox:1.28, command 'sleep 3600' and restartPolicy Never",
827-
Passed: false,
828-
Difficulty: "Medium",
829-
}
830-
}
831-
832713
passed := err == nil &&
833714
expectedScheduleTime == cronjob.Spec.Schedule &&
834715
expectedName == cronjob.Name &&
@@ -855,14 +736,6 @@ func createStateFulSet(clientset *kubernetes.Clientset) Result {
855736

856737
statefulset, err := clientset.AppsV1().StatefulSets(expectedNamespace).Get(context.TODO(), expectedName, metav1.GetOptions{})
857738

858-
if err != nil {
859-
return Result{
860-
TestName: "Question 26 - Create a statefulset statefulset-gain with image busybox:1.28, command 'sleep 3600' and replicas 3",
861-
Passed: false,
862-
Difficulty: "Medium",
863-
}
864-
}
865-
866739
passed := err == nil &&
867740
expectedName == statefulset.Name &&
868741
expectedImage == statefulset.Spec.Template.Spec.Containers[0].Image &&

0 commit comments

Comments
 (0)