Skip to content

Commit

Permalink
kubeapi: return pointers to make them type-assertable
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Jan 9, 2025
1 parent d8ebea9 commit 399ac4b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
30 changes: 17 additions & 13 deletions cli/cmd/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/edgelesssys/contrast/internal/manifest"
)

type k8sObject interface {
GetName() string
}

func policiesFromKubeResources(yamlPaths []string) ([]deployment, error) {
var kubeObjs []any
for _, path := range yamlPaths {
Expand All @@ -29,30 +33,30 @@ func policiesFromKubeResources(yamlPaths []string) ([]deployment, error) {

var deployments []deployment
for _, objAny := range kubeObjs {
var name, annotation, role string
meta, ok := objAny.(k8sObject)
if !ok {
continue
}
name := meta.GetName()

var annotation, role string
switch obj := objAny.(type) {
case kubeapi.Pod:
name = obj.Name
case *kubeapi.Pod:
annotation = obj.Annotations[kataPolicyAnnotationKey]
role = obj.Annotations[contrastRoleAnnotationKey]
case kubeapi.Deployment:
name = obj.Name
case *kubeapi.Deployment:
annotation = obj.Spec.Template.Annotations[kataPolicyAnnotationKey]
role = obj.Spec.Template.Annotations[contrastRoleAnnotationKey]
case kubeapi.ReplicaSet:
name = obj.Name
case *kubeapi.ReplicaSet:
annotation = obj.Spec.Template.Annotations[kataPolicyAnnotationKey]
role = obj.Spec.Template.Annotations[contrastRoleAnnotationKey]
case kubeapi.StatefulSet:
name = obj.Name
case *kubeapi.StatefulSet:
annotation = obj.Spec.Template.Annotations[kataPolicyAnnotationKey]
role = obj.Spec.Template.Annotations[contrastRoleAnnotationKey]
case kubeapi.DaemonSet:
name = obj.Name
case *kubeapi.DaemonSet:
annotation = obj.Spec.Template.Annotations[kataPolicyAnnotationKey]
role = obj.Spec.Template.Annotations[contrastRoleAnnotationKey]
case kubeapi.Job:
name = obj.Name
case *kubeapi.Job:
annotation = obj.Spec.Template.Annotations[kataPolicyAnnotationKey]
role = obj.Spec.Template.Annotations[contrastRoleAnnotationKey]
case kubeapi.CronJob:
Expand Down
14 changes: 7 additions & 7 deletions internal/kubeapi/kubeapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,49 @@ func UnmarshalK8SResources(data []byte) ([]any, error) {
if err != nil {
return nil, err
}
result = append(result, pod)
result = append(result, &pod)
case "Deployment":
var deployment appsv1.Deployment
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &deployment)
if err != nil {
return nil, err
}
result = append(result, deployment)
result = append(result, &deployment)
case "StatefulSet":
var statefulSet appsv1.StatefulSet
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &statefulSet)
if err != nil {
return nil, err
}
result = append(result, statefulSet)
result = append(result, &statefulSet)
case "ReplicaSet":
var replicaSet appsv1.ReplicaSet
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &replicaSet)
if err != nil {
return nil, err
}
result = append(result, replicaSet)
result = append(result, &replicaSet)
case "DaemonSet":
var daemonSet appsv1.DaemonSet
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &daemonSet)
if err != nil {
return nil, err
}
result = append(result, daemonSet)
result = append(result, &daemonSet)
case "Job":
var job batchv1.Job
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &job)
if err != nil {
return nil, err
}
result = append(result, job)
result = append(result, &job)
case "CronJob":
var cronJob batchv1.CronJob
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &cronJob)
if err != nil {
return nil, err
}
result = append(result, cronJob)
result = append(result, &cronJob)
}
}
return result, nil
Expand Down
10 changes: 5 additions & 5 deletions internal/kubeapi/kubeapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ metadata:
`,
},
"pod": {
wantTypes: []any{Pod{}},
wantTypes: []any{&Pod{}},
resources: `
apiVersion: v1
kind: Pod
Expand All @@ -35,7 +35,7 @@ metadata:
`,
},
"deployment, ignored service, daemonset": {
wantTypes: []any{Deployment{}, DaemonSet{}},
wantTypes: []any{&Deployment{}, &DaemonSet{}},
resources: `
apiVersion: apps/v1
kind: Deployment
Expand All @@ -54,7 +54,7 @@ metadata:
`,
},
"statefulset, replicaset": {
wantTypes: []any{StatefulSet{}, ReplicaSet{}},
wantTypes: []any{&StatefulSet{}, &ReplicaSet{}},
resources: `
apiVersion: apps/v1
kind: StatefulSet
Expand All @@ -68,7 +68,7 @@ metadata:
`,
},
"job": {
wantTypes: []any{Job{}},
wantTypes: []any{&Job{}},
resources: `
apiVersion: batch/v1
kind: Job
Expand All @@ -77,7 +77,7 @@ metadata:
`,
},
"cronjob": {
wantTypes: []any{CronJob{}},
wantTypes: []any{&CronJob{}},
resources: `
apiVersion: batch/v1
kind: CronJob
Expand Down

0 comments on commit 399ac4b

Please sign in to comment.