Skip to content

Commit

Permalink
Support both v1beta1 and v1 admission control webhooks (#124)
Browse files Browse the repository at this point in the history
We have a number of checks that operate on admission control webhook 
configuration. Older clusters support only v1beta1 of admission control, while 
newer clusters support v1. Currently clusterlint fails to run on these older 
clusters because we can't fetch v1 admission control objects from them. 

This change covers the following modifications:

- When listing objects, ignore "not found" errors, which mean the cluster 
  doesn't support the resource we're trying to list.
- Duplicate our existing admission control webhook checks for v1beta1, so that 
  older clusters get the same checks as newer clusters. 
- Enhance the errors we return when listing objects fails so that we can tell 
  which resource we failed to list.
- Remove extraneous empty import: client auth plugins are already loaded in 
  objects.go, so no need for the import in object_filter.go. 
- Ensure all object lists are non-nil after fetching objects. (Since we now 
  ignore not found errors, it's possible for some object lists to be nil.) 
- Skip v1beta1 admission control tests when v1 objects exist.

Co-authored-by: Timo Reimann <treimann@digitalocean.com>
  • Loading branch information
adamwg and timoreimann committed Sep 19, 2021
1 parent 9cd6ee5 commit 5eeabb8
Show file tree
Hide file tree
Showing 9 changed files with 1,801 additions and 46 deletions.
120 changes: 120 additions & 0 deletions checks/basic/admission_controller_webhook_v1beta1.go
@@ -0,0 +1,120 @@
/*
Copyright 2019 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package basic

import (
"fmt"

"github.com/digitalocean/clusterlint/checks"
"github.com/digitalocean/clusterlint/kube"
)

func init() {
checks.Register(&betaWebhookCheck{})
}

type betaWebhookCheck struct{}

// Name returns a unique name for this check.
func (w *betaWebhookCheck) Name() string {
return "admission-controller-webhook-v1beta1"
}

// Groups returns a list of group names this check should be part of.
func (w *betaWebhookCheck) Groups() []string {
return []string{"basic"}
}

// Description returns a detailed human-readable description of what this check
// does.
func (w *betaWebhookCheck) Description() string {
return "Check for admission control webhooks"
}

// Run runs this check on a set of Kubernetes objects.
func (w *betaWebhookCheck) Run(objects *kube.Objects) ([]checks.Diagnostic, error) {
if len(objects.ValidatingWebhookConfigurations.Items) > 0 ||
len(objects.MutatingWebhookConfigurations.Items) > 0 {
// Skip this check if there are v1 webhook configurations. On clusters
// that support both v1beta1 and v1 admission control, the same webhook
// configurations will be returned for both versions.
return nil, nil
}

const apiserverServiceName = "kubernetes"

var diagnostics []checks.Diagnostic

for _, config := range objects.ValidatingWebhookConfigurationsBeta.Items {
for _, wh := range config.Webhooks {
if wh.ClientConfig.Service != nil {
// Ensure that the service (and its namespace) that is configure actually exists.

if !namespaceExists(objects.Namespaces, wh.ClientConfig.Service.Namespace) {
diagnostics = append(diagnostics, checks.Diagnostic{
Severity: checks.Error,
Message: fmt.Sprintf("Validating webhook %s is configured against a service in a namespace that does not exist.", wh.Name),
Kind: checks.ValidatingWebhookConfiguration,
Object: &config.ObjectMeta,
Owners: config.ObjectMeta.GetOwnerReferences(),
})
continue
}

if !serviceExists(objects.Services, wh.ClientConfig.Service.Name, wh.ClientConfig.Service.Namespace) {
diagnostics = append(diagnostics, checks.Diagnostic{
Severity: checks.Error,
Message: fmt.Sprintf("Validating webhook %s is configured against a service that does not exist.", wh.Name),
Kind: checks.ValidatingWebhookConfiguration,
Object: &config.ObjectMeta,
Owners: config.ObjectMeta.GetOwnerReferences(),
})
}
}
}
}

for _, config := range objects.MutatingWebhookConfigurationsBeta.Items {
for _, wh := range config.Webhooks {
if wh.ClientConfig.Service != nil {
// Ensure that the service (and its namespace) that is configure actually exists.

if !namespaceExists(objects.Namespaces, wh.ClientConfig.Service.Namespace) {
diagnostics = append(diagnostics, checks.Diagnostic{
Severity: checks.Error,
Message: fmt.Sprintf("Mutating webhook %s is configured against a service in a namespace that does not exist.", wh.Name),
Kind: checks.MutatingWebhookConfiguration,
Object: &config.ObjectMeta,
Owners: config.ObjectMeta.GetOwnerReferences(),
})
continue
}

if !serviceExists(objects.Services, wh.ClientConfig.Service.Name, wh.ClientConfig.Service.Namespace) {
diagnostics = append(diagnostics, checks.Diagnostic{
Severity: checks.Error,
Message: fmt.Sprintf("Mutating webhook %s is configured against a service that does not exist.", wh.Name),
Kind: checks.MutatingWebhookConfiguration,
Object: &config.ObjectMeta,
Owners: config.ObjectMeta.GetOwnerReferences(),
})
}
}
}
}
return diagnostics, nil
}

0 comments on commit 5eeabb8

Please sign in to comment.