Skip to content

Commit

Permalink
Cleanup SupportedControllers
Browse files Browse the repository at this point in the history
  • Loading branch information
baderbuddy committed Mar 25, 2020
1 parent a5828a2 commit 3c46f40
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 56 deletions.
6 changes: 3 additions & 3 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ func (check SchemaCheck) CheckObject(obj interface{}) (bool, error) {
}

// IsActionable decides if this check applies to a particular target
func (check SchemaCheck) IsActionable(target TargetKind, controllerType SupportedController, isInit bool) bool {
func (check SchemaCheck) IsActionable(target TargetKind, controllerType string, isInit bool) bool {
if check.Target != target {
return false
}
isIncluded := len(check.Controllers.Include) == 0
for _, inclusion := range check.Controllers.Include {
if GetSupportedControllerFromString(inclusion) == controllerType {
if inclusion == controllerType {
isIncluded = true
break
}
Expand All @@ -159,7 +159,7 @@ func (check SchemaCheck) IsActionable(target TargetKind, controllerType Supporte
return false
}
for _, exclusion := range check.Controllers.Exclude {
if GetSupportedControllerFromString(exclusion) == controllerType {
if exclusion == controllerType {
return false
}
}
Expand Down
32 changes: 30 additions & 2 deletions pkg/kube/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sYaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -148,11 +149,38 @@ func CreateResourceProviderFromAPI(kube kubernetes.Interface, clusterName string
CreationTime: time.Now(),
Nodes: nodes.Items,
Namespaces: namespaces.Items,
Controllers: controllers.LoadControllers(pods.Items, dynamic, &restMapper),
Controllers: LoadControllers(pods.Items, dynamic, &restMapper),
}
return &api, nil
}

// LoadControllers loads a list of controllers from the kubeResources Pods
func LoadControllers(pods []corev1.Pod, dynamicClientPointer *dynamic.Interface, restMapperPointer *meta.RESTMapper) []controllers.GenericController {
interfaces := []controllers.GenericController{}
for _, pod := range pods {
interfaces = append(interfaces, controllers.NewGenericPodController(pod, dynamicClientPointer, restMapperPointer))
}
return deduplicateControllers(interfaces)
}

// Because the controllers with an Owner take on the name of the Owner, this eliminates any duplicates.
// In cases like CronJobs older children can hang around, so this takes the most recent.
func deduplicateControllers(inputControllers []controllers.GenericController) []controllers.GenericController {
controllerMap := make(map[string]controllers.GenericController)
for _, controller := range inputControllers {
key := controller.GetNamespace() + "/" + controller.GetKind() + "/" + controller.Name
oldController, ok := controllerMap[key]
if !ok || controller.CreatedTime.After(oldController.CreatedTime) {
controllerMap[key] = controller
}
}
results := make([]controllers.GenericController, 0)
for _, controller := range controllerMap {
results = append(results, controller)
}
return results
}

func getPodSpec(yaml map[string]interface{}) interface{} {
allowedChildren := []string{"jobTemplate", "spec", "template"}
for _, child := range allowedChildren {
Expand Down Expand Up @@ -203,7 +231,7 @@ func addResourceFromString(contents string, resources *ResourceProvider) error {
pod := corev1.Pod{}
err = decoder.Decode(&pod)
newController := controllers.NewGenericPodController(pod, nil, nil)
newController.KindString = resource.Kind
newController.Kind = resource.Kind
resources.Controllers = append(resources.Controllers, newController)
}
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/validator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ValidateController(conf *conf.Configuration, controller controller.GenericC
return ControllerResult{}, err
}
result := ControllerResult{
Kind: controller.GetKindString(),
Kind: controller.GetKind(),
Name: controller.GetName(),
Namespace: controller.GetObjectMeta().Namespace,
Results: ResultSet{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/validator/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestControllerExemptions(t *testing.T) {
},
}
newController := test.MockGenericController()
newController.KindString = "Deployment"
newController.Kind = "Deployment"
resources := &kube.ResourceProvider{
Controllers: []controller.GenericController{newController},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/validator/controllers/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewCronJobController(originalResource kubeAPIBatchV1beta1.CronJob) GenericC
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec.JobTemplate.Spec.Template.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.CronJobs
controller.Kind = config.CronJobs.String()
if controller.Name == "" {
logrus.Warn("Name is missing from controller", originalResource.Namespace)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/validator/controllers/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func NewDaemonSetController(originalResource kubeAPIAppsV1.DaemonSet) GenericCon
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec.Template.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.DaemonSets
controller.Kind = config.DaemonSets.String()
return controller
}
2 changes: 1 addition & 1 deletion pkg/validator/controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func NewDeploymentController(originalResource kubeAPIAppsV1.Deployment) GenericC
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec.Template.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.Deployments
controller.Kind = config.Deployments.String()
return controller
}
49 changes: 7 additions & 42 deletions pkg/validator/controllers/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"time"

"github.com/fairwindsops/polaris/pkg/config"
"github.com/sirupsen/logrus"
kubeAPICoreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -18,8 +17,7 @@ type GenericController struct {
Namespace string
PodSpec kubeAPICoreV1.PodSpec
ObjectMeta kubeAPIMetaV1.ObjectMeta
Kind config.SupportedController
KindString string
Kind string
CreatedTime time.Time
}

Expand All @@ -34,18 +32,10 @@ func (g GenericController) GetObjectMeta() kubeAPIMetaV1.ObjectMeta {
}

// GetKind returns the supportedcontroller enum type
func (g GenericController) GetKind() config.SupportedController {
func (g GenericController) GetKind() string {
return g.Kind
}

// GetKindString returns a string representing what kind of object the top level controller is.
func (g GenericController) GetKindString() string {
if g.KindString == "" {
return g.Kind.String()
}
return g.KindString
}

// GetName is inherited by all controllers using generic controller to get the name of the controller
func (g GenericController) GetName() string {
return g.Name
Expand All @@ -56,42 +46,14 @@ func (g GenericController) GetNamespace() string {
return g.Namespace
}

// LoadControllers loads a list of controllers from the kubeResources Pods
func LoadControllers(pods []kubeAPICoreV1.Pod, dynamicClientPointer *dynamic.Interface, restMapperPointer *meta.RESTMapper) []GenericController {
interfaces := []GenericController{}
for _, pod := range pods {
interfaces = append(interfaces, NewGenericPodController(pod, dynamicClientPointer, restMapperPointer))
}
return deduplicateControllers(interfaces)
}

// Because the controllers with an Owner take on the name of the Owner, this eliminates any duplicates.
// In cases like CronJobs older children can hang around, so this takes the most recent.
func deduplicateControllers(controllers []GenericController) []GenericController {
controllerMap := make(map[string]GenericController)
for _, controller := range controllers {
key := controller.GetNamespace() + "/" + controller.GetKindString() + "/" + controller.Name
oldController, ok := controllerMap[key]
if !ok || controller.CreatedTime.After(oldController.CreatedTime) {
controllerMap[key] = controller
}
}
results := make([]GenericController, 0)
for _, controller := range controllerMap {
results = append(results, controller)
}
return results
}

// NewGenericPodController builds a new controller interface for anytype of Pod
func NewGenericPodController(originalResource kubeAPICoreV1.Pod, dynamicClientPointer *dynamic.Interface, restMapperPointer *meta.RESTMapper) GenericController {
controller := GenericController{}
controller.Name = originalResource.Name
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.NakedPods
controller.KindString = "Pod"
controller.Kind = "Pod"
controller.CreatedTime = controller.GetObjectMeta().CreationTimestamp.Time

owners := controller.GetObjectMeta().OwnerReferences
Expand All @@ -101,8 +63,11 @@ func NewGenericPodController(originalResource kubeAPICoreV1.Pod, dynamicClientPo
// If an owner exists then set the name to the controller.
// This allows us to handle CRDs creating Controllers or DeploymentConfigs in OpenShift.
for len(owners) > 0 {
if len(owners) > 1 {
logrus.Warn("More than 1 owner found")
}
firstOwner := owners[0]
controller.KindString = firstOwner.Kind
controller.Kind = firstOwner.Kind
controller.Name = firstOwner.Name

dynamicClient := *dynamicClientPointer
Expand Down
2 changes: 1 addition & 1 deletion pkg/validator/controllers/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func NewJobController(originalResource kubeAPIBatchV1.Job) GenericController {
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec.Template.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.Jobs
controller.Kind = config.Jobs.String()
return controller
}
2 changes: 1 addition & 1 deletion pkg/validator/controllers/naked-pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func NewNakedPodController(originalResource kubeAPICoreV1.Pod) GenericController
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.NakedPods
controller.Kind = config.NakedPods.String()

return controller
}
2 changes: 1 addition & 1 deletion pkg/validator/controllers/replicationcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func NewReplicationControllerController(originalResource kubeAPICoreV1.Replicati
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec.Template.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.ReplicationControllers
controller.Kind = config.ReplicationControllers.String()
return controller
}
2 changes: 1 addition & 1 deletion pkg/validator/controllers/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewStatefulSetController(originalResource kubeAPIAppsV1.StatefulSet) Generi
controller.Namespace = originalResource.Namespace
controller.PodSpec = originalResource.Spec.Template.Spec
controller.ObjectMeta = originalResource.ObjectMeta
controller.Kind = config.StatefulSets
controller.Kind = config.StatefulSets.String()
if controller.Name == "" {
logrus.Warn("Name is missing from controller", originalResource.Namespace)
}
Expand Down

0 comments on commit 3c46f40

Please sign in to comment.