Skip to content

Commit

Permalink
Add Support for Services (#353)
Browse files Browse the repository at this point in the history
Co-authored-by: Dani <santos.adaniele@gmail.com>
  • Loading branch information
rxbchen and dani-santos-code committed Jul 29, 2021
1 parent 7b57f85 commit 50c618b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 11 deletions.
20 changes: 20 additions & 0 deletions internal/k8sinternal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func GetAllResources(clientset kubernetes.Interface, options ClientOptions) []k8
resources = append(resources, GetCronJobs(clientset, options)...)
resources = append(resources, GetServiceAccounts(clientset, options)...)
resources = append(resources, GetNamespaces(clientset, options)...)
resources = append(resources, GetServices(clientset, options)...)

resources = excludeGenerated(resources)

Expand Down Expand Up @@ -325,6 +326,25 @@ func GetNamespaces(clientset kubernetes.Interface, options ClientOptions) []k8s.
return namespaces
}

// GetServices gets all Service resources from a namespace in a cluster
func GetServices(clientset kubernetes.Interface, option ClientOptions) []k8s.Resource {
serviceClient := clientset.CoreV1().Services(option.Namespace)
listOptions := k8s.ListOptionsV1{}

serviceList, err := serviceClient.List(context.Background(), listOptions)

if err != nil {
log.Error(err)
return nil
}
services := make([]k8s.Resource, 0, len(serviceList.Items))
for _, s := range serviceList.Items {
s.TypeMeta = k8s.NewService().TypeMeta
services = append(services, (&s).DeepCopyObject())
}
return services
}

// GetKubernetesVersion returns the kubernetes client version
func GetKubernetesVersion(clientset kubernetes.Interface) (*version.Info, error) {
discoveryClient := clientset.Discovery()
Expand Down
1 change: 1 addition & 0 deletions internal/k8sinternal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestGetAllResources(t *testing.T) {
k8s.NewPodTemplate(),
k8s.NewCronJob(),
k8s.NewServiceAccount(),
k8s.NewService(),
}
namespaces := []string{"foo", "bar"}

Expand Down
13 changes: 13 additions & 0 deletions internal/test/fixtures/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: test-service
spec:
selector:
app: test-service
ports:
- protocol: TCP
port: 80
targetPort: 9376
clusterIP: 10.0.171.239
type: LoadBalancer
14 changes: 14 additions & 0 deletions internal/test/fixtures/unknown_resource_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: networking.k8s.io
kind: Ingress
metadata:
name: unknown_resource_type
spec:
rules:
- http:
paths:
- path: /test-unknownpath
backend:
service:
name: test-unknown
port:
number: 80
10 changes: 0 additions & 10 deletions internal/test/fixtures/unknown_type_v1.yml

This file was deleted.

2 changes: 1 addition & 1 deletion kubeaudit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestAuditCluster(t *testing.T) {

func TestUnknownResource(t *testing.T) {
// Make sure we produce only warning results for resources kubeaudit doesn't know how to audit
files := []string{"unknown_type_v1.yml", "custom_resource_definition.yml"}
files := []string{"unknown_resource_type.yml", "custom_resource_definition.yml"}

allAuditors, err := all.Auditors(config.KubeauditConfig{})
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func GetObjectMeta(resource Resource) *ObjectMetaV1 {
return &kubeType.ObjectMeta
case *ServiceAccountV1:
return &kubeType.ObjectMeta
case *ServiceV1:
return &kubeType.ObjectMeta
}

return nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/k8s/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,14 @@ func NewServiceAccount() *ServiceAccountV1 {
ObjectMeta: ObjectMetaV1{},
}
}

// NewService creates a new Service resource
func NewService() *ServiceV1 {
return &ServiceV1{
TypeMeta: TypeMetaV1{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: ObjectMetaV1{},
}
}
4 changes: 4 additions & 0 deletions pkg/k8s/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type SecurityContextV1 = apiv1.SecurityContext
// ServiceAccountV1 is a type alias for the v1 version of the k8s API.
type ServiceAccountV1 = apiv1.ServiceAccount

// ServiceV1 is a type alias for the v1 version of the k8s API.
type ServiceV1 = apiv1.Service

// StatefulSetSpecV1 is a type alias for the v1 version of the k8s apps API.
type StatefulSetSpecV1 = appsv1.StatefulSetSpec

Expand Down Expand Up @@ -136,6 +139,7 @@ func IsSupportedResourceType(obj Resource) bool {
*PodTemplateV1,
*ReplicationControllerV1,
*ServiceAccountV1,
*ServiceV1,
*StatefulSetV1, *StatefulSetV1Beta1:
return true
default:
Expand Down

0 comments on commit 50c618b

Please sign in to comment.