-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/kg/*: sub command peer validation webhook
This commit adds a sub command `webhook` to Kilo. It will start a https web server that answeres request from a Kubernetes API server to validate updates and creations of Kilo peers. Signed-off-by: leonnicolas <leonloechner@gmx.de>
- Loading branch information
1 parent
aca32ab
commit 032a8e3
Showing
12 changed files
with
2,828 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// Copyright 2021 the Kilo authors | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/go-kit/kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/spf13/cobra" | ||
kilo "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1" | ||
"github.com/squat/kilo/pkg/version" | ||
v1 "k8s.io/api/admission/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
) | ||
|
||
var webhookCmd = &cobra.Command{ | ||
Use: "webhook", | ||
PreRunE: func(c *cobra.Command, a []string) error { | ||
if c.HasParent() { | ||
return c.Parent().PreRunE(c, a) | ||
} | ||
return nil | ||
}, | ||
Short: "webhook starts a https server to validate updates and creations of Kilo peers.", | ||
Run: webhook, | ||
} | ||
|
||
var ( | ||
certPath string | ||
keyPath string | ||
metricsAddr string | ||
listenAddr string | ||
) | ||
|
||
func init() { | ||
webhookCmd.Flags().StringVar(&certPath, "cert-file", "", "file path to certificat file") | ||
webhookCmd.Flags().StringVar(&keyPath, "key-file", "", "file path to key file") | ||
webhookCmd.Flags().StringVar(&metricsAddr, "metrics-address", ":1107", "The metrics server will be listening to that address with port\ne.g. 172.0.0.1:9090") | ||
webhookCmd.Flags().StringVar(&listenAddr, "listen", ":8443", "The webhook server will be listening to that address") | ||
} | ||
|
||
var deserializer = serializer.NewCodecFactory(runtime.NewScheme()).UniversalDeserializer() | ||
|
||
var ( | ||
validationCounter = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "https_admission_requests_total", | ||
Help: "The number of received admission reviews requests", | ||
}, | ||
[]string{"operation", "response"}, | ||
) | ||
errorCounter = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "errors_total", | ||
Help: "The total number of errors", | ||
}, | ||
) | ||
) | ||
|
||
func validationHandler(w http.ResponseWriter, r *http.Request) { | ||
level.Debug(logger).Log("msg", "handling request", "source", r.RemoteAddr) | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
errorCounter.Inc() | ||
level.Error(logger).Log("err", "failed to parse body from incoming request", "source", r.RemoteAddr) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var admissionReview v1.AdmissionReview | ||
|
||
contentType := r.Header.Get("Content-Type") | ||
if contentType != "application/json" { | ||
errorCounter.Inc() | ||
msg := fmt.Sprintf("Content-Type=%s, expect application/json", contentType) | ||
level.Error(logger).Log("err", msg) | ||
http.Error(w, msg, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
response := v1.AdmissionReview{} | ||
|
||
_, gvk, err := deserializer.Decode(body, nil, &admissionReview) | ||
if err != nil { | ||
errorCounter.Inc() | ||
msg := fmt.Sprintf("Request could not be decoded: %v", err) | ||
level.Error(logger).Log("err", msg) | ||
http.Error(w, msg, http.StatusBadRequest) | ||
return | ||
} | ||
if *gvk != v1.SchemeGroupVersion.WithKind("AdmissionReview") { | ||
errorCounter.Inc() | ||
level.Error(logger).Log("err", "only api v1 is supported") | ||
http.Error(w, "only api v1 is supported", http.StatusBadRequest) | ||
return | ||
} else { | ||
response.SetGroupVersionKind(*gvk) | ||
response.Response = &v1.AdmissionResponse{ | ||
UID: admissionReview.Request.UID, | ||
} | ||
} | ||
|
||
rawExtension := admissionReview.Request.Object | ||
var peer kilo.Peer | ||
|
||
if err = json.Unmarshal(rawExtension.Raw, &peer); err != nil { | ||
errorCounter.Inc() | ||
msg := fmt.Sprintf("could not unmarshal extension to peer spec: %v:", err) | ||
log.Println(msg) | ||
level.Error(logger).Log("err", msg) | ||
http.Error(w, msg, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err := peer.Validate(); err == nil { | ||
validationCounter.With(prometheus.Labels{"operation": string(admissionReview.Request.Operation), "response": "allowed"}).Inc() | ||
response.Response.Allowed = true | ||
} else { | ||
validationCounter.With(prometheus.Labels{"operation": string(admissionReview.Request.Operation), "response": "denied"}).Inc() | ||
response.Response.Result = &metav1.Status{ | ||
Message: err.Error(), | ||
} | ||
} | ||
|
||
res, err := json.Marshal(response) | ||
if err != nil { | ||
errorCounter.Inc() | ||
msg := fmt.Sprintf("failed to marshal response: %v", err) | ||
level.Error(logger).Log("err", msg) | ||
http.Error(w, msg, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
if _, err := w.Write(res); err != nil { | ||
level.Error(logger).Log("err", err, "msg", "failed to write response") | ||
} | ||
} | ||
|
||
func webhook(_ *cobra.Command, _ []string) { | ||
if printVersion { | ||
fmt.Println(version.Version) | ||
os.Exit(0) | ||
} | ||
registry.MustRegister( | ||
errorCounter, | ||
validationCounter, | ||
) | ||
mm := http.NewServeMux() | ||
mm.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) | ||
|
||
exit := make(chan error, 1) | ||
go func() { | ||
exit <- http.ListenAndServe(metricsAddr, mm) | ||
}() | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/validate", validationHandler) | ||
server := &http.Server{ | ||
Addr: listenAddr, | ||
Handler: mux, | ||
} | ||
go func() { | ||
exit <- server.ListenAndServeTLS(certPath, keyPath) | ||
}() | ||
e := <-exit | ||
level.Error(logger).Log("err", e.Error(), "msg", "shutting down") | ||
} |
Oops, something went wrong.