Skip to content

Commit b083161

Browse files
authored
Merge pull request #11 from konoui/fix-exit-code-and-replicaset-panic
Fix exit code and replicaset panic
2 parents 5a7ab31 + d9f7070 commit b083161

13 files changed

+68
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ It then inspects all of the volumes in the containers and looks for any volume w
2424
Supported workload types:
2525

2626
* Pods
27+
* ReplicaSets
2728
* Deployments
2829
* StatefulSets
2930
* DaemonSets

main.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ func runCluster(requestedNamespace string, w *tabwriter.Writer, verbose int) (bo
154154

155155
func printResources(namespace corev1.Namespace, clientset *kubernetes.Clientset, w *tabwriter.Writer, verbose int) (bool, error) {
156156

157-
var sockFoundPod, sockFoundDeploy, sockFoundStatefulSet, sockFoundJob, sockFoundCron bool
157+
sockFound := false
158158

159159
namespaceName := namespace.ObjectMeta.Name
160160

161+
nsReplicasets := make(map[string]*appsv1.ReplicaSet)
161162
nsDeployments := make(map[string]*appsv1.Deployment)
162163
nsDaemonsets := make(map[string]*appsv1.DaemonSet)
163164
nsStatefulsets := make(map[string]*appsv1.StatefulSet)
@@ -197,6 +198,11 @@ func printResources(namespace corev1.Namespace, clientset *kubernetes.Clientset,
197198
continue
198199
}
199200

201+
if len(replica.OwnerReferences) == 0 {
202+
nsReplicasets[replica.Name] = replica
203+
continue
204+
}
205+
200206
deployment, deployErr := clientset.AppsV1().Deployments(namespace.Name).Get(context.TODO(), replica.OwnerReferences[0].Name, metav1.GetOptions{})
201207
if deployErr != nil {
202208
errorList = append(errorList, deployErr)
@@ -267,13 +273,28 @@ func printResources(namespace corev1.Namespace, clientset *kubernetes.Clientset,
267273
}
268274
} else {
269275
// Look up raw pods for volumes here
270-
sockFoundPod = printVolumes(w, p.Spec.Volumes, namespaceName, "pod", p.Name, verbose)
276+
found := printVolumes(w, p.Spec.Volumes, namespaceName, "pod", p.Name, verbose)
277+
if found {
278+
sockFound = true
279+
}
271280
}
272281
}
273282
}
283+
284+
// loop through all the unique ReplicaSets in the namespace
285+
for _, replica := range nsReplicasets {
286+
found := printVolumes(w, replica.Spec.Template.Spec.Volumes, namespaceName, "replicaset", replica.Name, verbose)
287+
if found {
288+
sockFound = true
289+
}
290+
}
291+
274292
// loop through all the unique deployments we found for volumes
275293
for _, deploy := range nsDeployments {
276-
sockFoundDeploy = printVolumes(w, deploy.Spec.Template.Spec.Volumes, namespaceName, "deployment", deploy.Name, verbose)
294+
found := printVolumes(w, deploy.Spec.Template.Spec.Volumes, namespaceName, "deployment", deploy.Name, verbose)
295+
if found {
296+
sockFound = true
297+
}
277298
}
278299

279300
// loop through all the unique DaemonSets in the namespace
@@ -284,6 +305,7 @@ func printResources(namespace corev1.Namespace, clientset *kubernetes.Clientset,
284305
// fmt.Printf("testing %s\n", v.VolumeSource.HostPath.Path)
285306
if containsDockerSock(v.VolumeSource.HostPath.Path) {
286307
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", namespaceName, "daemonset", daemonset.Name, "mounted")
308+
sockFound = true
287309
break
288310
}
289311
}
@@ -296,27 +318,32 @@ func printResources(namespace corev1.Namespace, clientset *kubernetes.Clientset,
296318

297319
// loop through all the unique StatefulSets in the namespace
298320
for _, statefulset := range nsStatefulsets {
299-
sockFoundStatefulSet = printVolumes(w, statefulset.Spec.Template.Spec.Volumes, namespaceName, "statefulset", statefulset.Name, verbose)
321+
found := printVolumes(w, statefulset.Spec.Template.Spec.Volumes, namespaceName, "statefulset", statefulset.Name, verbose)
322+
if found {
323+
sockFound = true
324+
}
300325
}
301326

302327
// loop through all the unique Jobs in the namespace
303328
for _, job := range nsJobs {
304-
sockFoundJob = printVolumes(w, job.Spec.Template.Spec.Volumes, namespaceName, "job", job.Name, verbose)
329+
found := printVolumes(w, job.Spec.Template.Spec.Volumes, namespaceName, "job", job.Name, verbose)
330+
if found {
331+
sockFound = true
332+
}
305333
}
306334

307335
// loop through all the unique CronJobs in the namespace
308336
for _, cron := range nsCronJobs {
309-
sockFoundCron = printVolumes(w, cron.Spec.JobTemplate.Spec.Template.Spec.Volumes, namespaceName, "cron", cron.Name, verbose)
337+
found := printVolumes(w, cron.Spec.JobTemplate.Spec.Template.Spec.Volumes, namespaceName, "cron", cron.Name, verbose)
338+
if found {
339+
sockFound = true
340+
}
310341
}
311342

312343
if len(errorList) > 0 {
313344
return false, utilerrors.NewAggregate(errorList)
314345
}
315-
if sockFoundPod || sockFoundDeploy || sockFoundStatefulSet || sockFoundJob || sockFoundCron {
316-
return true, nil
317-
} else {
318-
return false, nil
319-
}
346+
return sockFound, nil
320347
}
321348

322349
func containsDockerSock(s string) bool {
File renamed without changes.
File renamed without changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: ReplicaSet
4+
metadata:
5+
name: replicaset-docker-volume
6+
labels:
7+
app: rs
8+
spec:
9+
replicas: 3
10+
selector:
11+
matchLabels:
12+
app: rs
13+
template:
14+
metadata:
15+
labels:
16+
app: rs
17+
spec:
18+
containers:
19+
- name: pause
20+
image: public.ecr.aws/eks-distro/kubernetes/pause:v1.21.5-eks-1-21-8
21+
ports:
22+
- containerPort: 80
23+
volumeMounts:
24+
- name: dockersock
25+
mountPath: "/var/run/docker.sock"
26+
volumes:
27+
- name: dockersock
28+
hostPath:
29+
path: /var/run/docker.sock

0 commit comments

Comments
 (0)