-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
183 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ rules: | |
- leases | ||
verbs: | ||
- create | ||
- update | ||
- list | ||
- watch | ||
- apiGroups: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package controller | ||
package audit | ||
|
||
import ( | ||
"github.com/gardener/gardener/extensions/pkg/controller/extension" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package healthcheck | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/gardener/gardener/extensions/pkg/controller/healthcheck" | ||
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
type BackendHealthChecker struct { | ||
logger logr.Logger | ||
httpClient *http.Client | ||
} | ||
|
||
func backendHealth() healthcheck.HealthCheck { | ||
return &BackendHealthChecker{ | ||
httpClient: http.DefaultClient, | ||
} | ||
} | ||
|
||
func (h *BackendHealthChecker) SetLoggerSuffix(provider, extension string) { | ||
h.logger = h.logger.WithName(fmt.Sprintf("%s-%s-healthcheck-backend", provider, extension)) | ||
} | ||
|
||
func (h *BackendHealthChecker) DeepCopy() healthcheck.HealthCheck { | ||
copy := *h | ||
return © | ||
} | ||
|
||
func (h *BackendHealthChecker) Check(ctx context.Context, request types.NamespacedName) (*healthcheck.SingleCheckResult, error) { | ||
err := h.check(ctx, request.Namespace) | ||
if err != nil { | ||
return &healthcheck.SingleCheckResult{ // nolint:nilerr | ||
Status: gardencorev1beta1.ConditionFalse, | ||
Detail: err.Error(), | ||
}, nil | ||
} | ||
|
||
return &healthcheck.SingleCheckResult{ | ||
Status: gardencorev1beta1.ConditionTrue, | ||
}, nil | ||
} | ||
|
||
func (h *BackendHealthChecker) check(ctx context.Context, namespace string) error { | ||
url := fmt.Sprintf("http://audit-webhook-backend.%s.svc.cluster.local:2020/api/v1/health", namespace) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return fmt.Errorf("unable to create http request: %w", err) | ||
} | ||
|
||
resp, err := h.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("unable to do http request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
return nil | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("unable to read http body: %w", err) | ||
} | ||
|
||
return fmt.Errorf("backend is unhealthy since errors or failued have occurred in the last minute time frame: %s", string(body)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package healthcheck | ||
|
||
import ( | ||
"time" | ||
|
||
extensionsconfig "github.com/gardener/gardener/extensions/pkg/apis/config" | ||
"github.com/gardener/gardener/extensions/pkg/controller/healthcheck" | ||
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1" | ||
"github.com/metal-stack/gardener-extension-audit/pkg/controller/audit" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
var ( | ||
defaultSyncPeriod = time.Second * 30 | ||
// DefaultAddOptions contains configuration for the health check controller. | ||
DefaultAddOptions = healthcheck.DefaultAddArgs{ | ||
HealthCheckConfig: extensionsconfig.HealthCheckConfig{SyncPeriod: metav1.Duration{Duration: defaultSyncPeriod}}, | ||
} | ||
) | ||
|
||
// RegisterHealthChecks registers health checks for each extension resource | ||
// HealthChecks are grouped by extension (e.g worker), extension.type (e.g aws) and Health Check Type (e.g SystemComponentsHealthy) | ||
func RegisterHealthChecks(mgr manager.Manager, opts healthcheck.DefaultAddArgs) error { | ||
return healthcheck.DefaultRegistration( | ||
audit.Type, | ||
extensionsv1alpha1.SchemeGroupVersion.WithKind(extensionsv1alpha1.ExtensionResource), | ||
func() client.ObjectList { return &extensionsv1alpha1.ExtensionList{} }, | ||
func() extensionsv1alpha1.Object { return &extensionsv1alpha1.Extension{} }, | ||
mgr, | ||
opts, | ||
nil, | ||
[]healthcheck.ConditionTypeToHealthCheck{ | ||
{ | ||
ConditionType: string(gardencorev1beta1.ShootControlPlaneHealthy), | ||
HealthCheck: backendHealth(), | ||
}, | ||
}, | ||
sets.New(gardencorev1beta1.ShootSystemComponentsHealthy), | ||
) | ||
} | ||
|
||
// AddToManager adds a controller with the default Options. | ||
func AddToManager(mgr manager.Manager) error { | ||
return RegisterHealthChecks(mgr, DefaultAddOptions) | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.