Skip to content

Commit

Permalink
parser: refactor decodeItem
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Jan 16, 2019
1 parent 44ac966 commit c589d82
Showing 1 changed file with 112 additions and 109 deletions.
221 changes: 112 additions & 109 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ func Empty() ks.AllTypes {
func ParseFiles(cnf config.Configuration) (ks.AllTypes, error) {
s := &parsedObjects{}

addPodSpeccer := func(ps ks.PodSpecer) {
s.podspecers = append(s.podspecers, ps)
s.bothMetas = append(s.bothMetas, ks.BothMeta{ps.GetTypeMeta(), ps.GetObjectMeta()})
}

for _, file := range cnf.AllFiles {
fullFile, err := ioutil.ReadAll(file)
if err != nil {
Expand All @@ -127,113 +122,121 @@ func ParseFiles(cnf config.Configuration) (ks.AllTypes, error) {
return nil, err
}

decode := func(data []byte, object runtime.Object) {
deserializer := codecs.UniversalDeserializer()
if _, _, err := deserializer.Decode(data, nil, object); err != nil {
panic(err)
}
}

detectedVersion := schema.FromAPIVersionAndKind(detect.ApiVersion, detect.Kind)

switch detectedVersion {
case corev1.SchemeGroupVersion.WithKind("Pod"):
var pod corev1.Pod
decode(fileContents, &pod)
s.pods = append(s.pods, pod)
s.bothMetas = append(s.bothMetas, ks.BothMeta{pod.TypeMeta, pod.ObjectMeta})

case batchv1.SchemeGroupVersion.WithKind("Job"):
var job batchv1.Job
decode(fileContents, &job)
addPodSpeccer(internal.Batchv1Job{job})

case batchv1beta1.SchemeGroupVersion.WithKind("CronJob"):
var cronjob batchv1beta1.CronJob
decode(fileContents, &cronjob)
addPodSpeccer(internal.Batchv1beta1CronJob{cronjob})
s.cronjobs = append(s.cronjobs, cronjob)

case appsv1.SchemeGroupVersion.WithKind("Deployment"):
var deployment appsv1.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Appsv1Deployment{deployment})

// TODO: Support older versions of Deployment as well?
s.deployments = append(s.deployments, deployment)
case appsv1beta1.SchemeGroupVersion.WithKind("Deployment"):
var deployment appsv1beta1.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Appsv1beta1Deployment{deployment})
case appsv1beta2.SchemeGroupVersion.WithKind("Deployment"):
var deployment appsv1beta2.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Appsv1beta2Deployment{deployment})
case extensionsv1beta1.SchemeGroupVersion.WithKind("Deployment"):
var deployment extensionsv1beta1.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Extensionsv1beta1Deployment{deployment})

case appsv1.SchemeGroupVersion.WithKind("StatefulSet"):
var statefulSet appsv1.StatefulSet
decode(fileContents, &statefulSet)
addPodSpeccer(internal.Appsv1StatefulSet{statefulSet})

// TODO: Support older versions of StatefulSet as well?
s.statefulsets = append(s.statefulsets, statefulSet)
case appsv1beta1.SchemeGroupVersion.WithKind("StatefulSet"):
var statefulSet appsv1beta1.StatefulSet
decode(fileContents, &statefulSet)
addPodSpeccer(internal.Appsv1beta1StatefulSet{statefulSet})
case appsv1beta2.SchemeGroupVersion.WithKind("StatefulSet"):
var statefulSet appsv1beta2.StatefulSet
decode(fileContents, &statefulSet)
addPodSpeccer(internal.Appsv1beta2StatefulSet{statefulSet})

case appsv1.SchemeGroupVersion.WithKind("DaemonSet"):
var daemonset appsv1.DaemonSet
decode(fileContents, &daemonset)
addPodSpeccer(internal.Appsv1DaemonSet{daemonset})
case appsv1beta2.SchemeGroupVersion.WithKind("DaemonSet"):
var daemonset appsv1beta2.DaemonSet
decode(fileContents, &daemonset)
addPodSpeccer(internal.Appsv1beta2DaemonSet{daemonset})
case extensionsv1beta1.SchemeGroupVersion.WithKind("DaemonSet"):
var daemonset extensionsv1beta1.DaemonSet
decode(fileContents, &daemonset)
addPodSpeccer(internal.Extensionsv1beta1DaemonSet{daemonset})

case networkingv1.SchemeGroupVersion.WithKind("NetworkPolicy"):
var netpol networkingv1.NetworkPolicy
decode(fileContents, &netpol)
s.networkPolicies = append(s.networkPolicies, netpol)
s.bothMetas = append(s.bothMetas, ks.BothMeta{netpol.TypeMeta, netpol.ObjectMeta})

case corev1.SchemeGroupVersion.WithKind("Service"):
var service corev1.Service
decode(fileContents, &service)
s.services = append(s.services, service)
s.bothMetas = append(s.bothMetas, ks.BothMeta{service.TypeMeta, service.ObjectMeta})

case policyv1beta1.SchemeGroupVersion.WithKind("PodDisruptionBudget"):
var disruptBudget policyv1beta1.PodDisruptionBudget
decode(fileContents, &disruptBudget)
s.podDisruptionBudgets = append(s.podDisruptionBudgets, disruptBudget)
s.bothMetas = append(s.bothMetas, ks.BothMeta{disruptBudget.TypeMeta, disruptBudget.ObjectMeta})

case extensionsv1beta1.SchemeGroupVersion.WithKind("Ingress"):
var ingress extensionsv1beta1.Ingress
decode(fileContents, &ingress)
s.ingresses = append(s.ingresses, ingress)
s.bothMetas = append(s.bothMetas, ks.BothMeta{ingress.TypeMeta, ingress.ObjectMeta})

default:
if cnf.VerboseOutput {
log.Printf("Unknown datatype: %s", detect.Kind)
}
}
decodeItem(cnf, s, detectedVersion, fileContents)
}
}

return s, nil
}

func decodeItem(cnf config.Configuration, s *parsedObjects, detectedVersion schema.GroupVersionKind, fileContents []byte) {
addPodSpeccer := func(ps ks.PodSpecer) {
s.podspecers = append(s.podspecers, ps)
s.bothMetas = append(s.bothMetas, ks.BothMeta{ps.GetTypeMeta(), ps.GetObjectMeta()})
}

decode := func(data []byte, object runtime.Object) {
deserializer := codecs.UniversalDeserializer()
if _, _, err := deserializer.Decode(data, nil, object); err != nil {
panic(err)
}
}

switch detectedVersion {
case corev1.SchemeGroupVersion.WithKind("Pod"):
var pod corev1.Pod
decode(fileContents, &pod)
s.pods = append(s.pods, pod)
s.bothMetas = append(s.bothMetas, ks.BothMeta{pod.TypeMeta, pod.ObjectMeta})

case batchv1.SchemeGroupVersion.WithKind("Job"):
var job batchv1.Job
decode(fileContents, &job)
addPodSpeccer(internal.Batchv1Job{job})

case batchv1beta1.SchemeGroupVersion.WithKind("CronJob"):
var cronjob batchv1beta1.CronJob
decode(fileContents, &cronjob)
addPodSpeccer(internal.Batchv1beta1CronJob{cronjob})
s.cronjobs = append(s.cronjobs, cronjob)

case appsv1.SchemeGroupVersion.WithKind("Deployment"):
var deployment appsv1.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Appsv1Deployment{deployment})

// TODO: Support older versions of Deployment as well?
s.deployments = append(s.deployments, deployment)
case appsv1beta1.SchemeGroupVersion.WithKind("Deployment"):
var deployment appsv1beta1.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Appsv1beta1Deployment{deployment})
case appsv1beta2.SchemeGroupVersion.WithKind("Deployment"):
var deployment appsv1beta2.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Appsv1beta2Deployment{deployment})
case extensionsv1beta1.SchemeGroupVersion.WithKind("Deployment"):
var deployment extensionsv1beta1.Deployment
decode(fileContents, &deployment)
addPodSpeccer(internal.Extensionsv1beta1Deployment{deployment})

case appsv1.SchemeGroupVersion.WithKind("StatefulSet"):
var statefulSet appsv1.StatefulSet
decode(fileContents, &statefulSet)
addPodSpeccer(internal.Appsv1StatefulSet{statefulSet})

// TODO: Support older versions of StatefulSet as well?
s.statefulsets = append(s.statefulsets, statefulSet)
case appsv1beta1.SchemeGroupVersion.WithKind("StatefulSet"):
var statefulSet appsv1beta1.StatefulSet
decode(fileContents, &statefulSet)
addPodSpeccer(internal.Appsv1beta1StatefulSet{statefulSet})
case appsv1beta2.SchemeGroupVersion.WithKind("StatefulSet"):
var statefulSet appsv1beta2.StatefulSet
decode(fileContents, &statefulSet)
addPodSpeccer(internal.Appsv1beta2StatefulSet{statefulSet})

case appsv1.SchemeGroupVersion.WithKind("DaemonSet"):
var daemonset appsv1.DaemonSet
decode(fileContents, &daemonset)
addPodSpeccer(internal.Appsv1DaemonSet{daemonset})
case appsv1beta2.SchemeGroupVersion.WithKind("DaemonSet"):
var daemonset appsv1beta2.DaemonSet
decode(fileContents, &daemonset)
addPodSpeccer(internal.Appsv1beta2DaemonSet{daemonset})
case extensionsv1beta1.SchemeGroupVersion.WithKind("DaemonSet"):
var daemonset extensionsv1beta1.DaemonSet
decode(fileContents, &daemonset)
addPodSpeccer(internal.Extensionsv1beta1DaemonSet{daemonset})

case networkingv1.SchemeGroupVersion.WithKind("NetworkPolicy"):
var netpol networkingv1.NetworkPolicy
decode(fileContents, &netpol)
s.networkPolicies = append(s.networkPolicies, netpol)
s.bothMetas = append(s.bothMetas, ks.BothMeta{netpol.TypeMeta, netpol.ObjectMeta})

case corev1.SchemeGroupVersion.WithKind("Service"):
var service corev1.Service
decode(fileContents, &service)
s.services = append(s.services, service)
s.bothMetas = append(s.bothMetas, ks.BothMeta{service.TypeMeta, service.ObjectMeta})

case policyv1beta1.SchemeGroupVersion.WithKind("PodDisruptionBudget"):
var disruptBudget policyv1beta1.PodDisruptionBudget
decode(fileContents, &disruptBudget)
s.podDisruptionBudgets = append(s.podDisruptionBudgets, disruptBudget)
s.bothMetas = append(s.bothMetas, ks.BothMeta{disruptBudget.TypeMeta, disruptBudget.ObjectMeta})

case extensionsv1beta1.SchemeGroupVersion.WithKind("Ingress"):
var ingress extensionsv1beta1.Ingress
decode(fileContents, &ingress)
s.ingresses = append(s.ingresses, ingress)
s.bothMetas = append(s.bothMetas, ks.BothMeta{ingress.TypeMeta, ingress.ObjectMeta})

default:
if cnf.VerboseOutput {
log.Printf("Unknown datatype: %s", detectedVersion.String())
}
}
}

0 comments on commit c589d82

Please sign in to comment.