-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from reactiveops/jg/add-deploys
jg/add deploys
- Loading branch information
Showing
9 changed files
with
190 additions
and
95 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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package validator | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
conf "github.com/reactiveops/fairwinds/pkg/config" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types" | ||
) | ||
|
||
// Validator validates k8s resources. | ||
type Validator struct { | ||
client client.Client | ||
decoder types.Decoder | ||
Config conf.Configuration | ||
} | ||
|
||
var _ inject.Client = &Validator{} | ||
|
||
// InjectClient injects the client. | ||
func (v *Validator) InjectClient(c client.Client) error { | ||
v.client = c | ||
return nil | ||
} | ||
|
||
var _ inject.Decoder = &Validator{} | ||
|
||
// InjectDecoder injects the decoder. | ||
func (v *Validator) InjectDecoder(d types.Decoder) error { | ||
v.decoder = d | ||
return nil | ||
} | ||
|
||
var _ admission.Handler = &Validator{} | ||
|
||
// Handle for Validator to run validation checks. | ||
func (v *Validator) Handle(ctx context.Context, req types.Request) types.Response { | ||
var err error | ||
var allowed bool | ||
var reason string | ||
var results Results | ||
|
||
switch req.AdmissionRequest.Kind.Kind { | ||
case "Deployment": | ||
deploy := appsv1.Deployment{} | ||
err = v.decoder.Decode(req, &deploy) | ||
results = ValidateDeploys(v.Config, &deploy, Results{}) | ||
case "Pod": | ||
pod := corev1.Pod{} | ||
err = v.decoder.Decode(req, &pod) | ||
results = ValidatePods(v.Config, &pod.Spec, Results{}) | ||
} | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusBadRequest, err) | ||
} | ||
|
||
allowed, reason = results.Format() | ||
return admission.ValidationResponse(allowed, reason) | ||
} | ||
|
||
// ValidateDeploys validates that each deployment conforms to the Fairwinds config. | ||
func ValidateDeploys(conf conf.Configuration, deploy *appsv1.Deployment, results Results) Results { | ||
pod := deploy.Spec.Template.Spec | ||
return ValidatePods(conf, &pod, results) | ||
} |
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,33 @@ | ||
package validator | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
conf "github.com/reactiveops/fairwinds/pkg/config" | ||
"github.com/reactiveops/fairwinds/pkg/kube" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// PingHandler is an endpoint to check the server is up. | ||
func PingHandler(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "pong\n") | ||
} | ||
|
||
// DeployHandler creates a handler for to validate the current deploy workloads. | ||
func DeployHandler(w http.ResponseWriter, r *http.Request, c conf.Configuration) { | ||
var results []Results | ||
deploys, err := kube.AppsV1API.Deployments("").List(metav1.ListOptions{}) | ||
if err != nil { | ||
fmt.Fprintf(w, err.Error()) | ||
return | ||
} | ||
for _, deploy := range deploys.Items { | ||
result := ValidateDeploys(c, &deploy, Results{}) | ||
results = append(results, result) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(results) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder" | ||
) | ||
|
||
// NewWebhook creates a validating admission webhook for the apiType. | ||
func NewWebhook(name string, mgr manager.Manager, validator Validator, apiType runtime.Object) *admission.Webhook { | ||
name = fmt.Sprintf("%s.k8s.io", name) | ||
path := fmt.Sprintf("/validating-%s", name) | ||
|
||
webhook, err := builder.NewWebhookBuilder(). | ||
Name(name). | ||
Validating(). | ||
Path(path). | ||
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update). | ||
WithManager(mgr). | ||
ForType(apiType). | ||
Handlers(&validator). | ||
Build() | ||
if err != nil { | ||
log.Error(err, "unable to setup validating webhook:", name) | ||
os.Exit(1) | ||
} | ||
|
||
return webhook | ||
} |
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,22 @@ | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: ng | ||
labels: | ||
app: nginx | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.7.9 | ||
ports: | ||
- containerPort: 80 |
File renamed without changes.