-
Notifications
You must be signed in to change notification settings - Fork 113
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 #1302 from qu1queee/qu1queee/conversion_webhook
Conversion webhook
- Loading branch information
Showing
76 changed files
with
32,574 additions
and
9 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,109 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/shipwright-io/build/pkg/ctxlog" | ||
"github.com/shipwright-io/build/pkg/webhook/conversion" | ||
"github.com/shipwright-io/build/version" | ||
"github.com/spf13/pflag" | ||
"knative.dev/pkg/signals" | ||
) | ||
|
||
var ( | ||
versionGiven = flag.String("version", "devel", "Version of Shipwright webhook running") | ||
) | ||
|
||
func printVersion(ctx context.Context) { | ||
ctxlog.Info(ctx, fmt.Sprintf("Shipwright Build Webhook Version: %s", version.Version)) | ||
ctxlog.Info(ctx, fmt.Sprintf("Go Version: %s", runtime.Version())) | ||
ctxlog.Info(ctx, fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) | ||
} | ||
|
||
func main() { | ||
// Add the zap logger flag set to the CLI. The flag set must | ||
// be added before calling pflag.Parse(). | ||
pflag.CommandLine.AddGoFlagSet(ctxlog.CustomZapFlagSet()) | ||
|
||
// Add flags registered by imported packages (e.g. glog and | ||
// controller-runtime) | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
||
pflag.Parse() | ||
|
||
if err := Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
} | ||
|
||
func Execute() error { | ||
l := ctxlog.NewLogger("shp-build-webhook") | ||
|
||
ctx := ctxlog.NewParentContext(l) | ||
|
||
version.SetVersion(*versionGiven) | ||
printVersion(ctx) | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/health", health) | ||
ctxlog.Info(ctx, "adding handlefunc() /health") | ||
|
||
// convert endpoint handles ConversionReview API object serialized to JSON | ||
mux.HandleFunc("/convert", conversion.CRDConvertHandler(ctx)) | ||
ctxlog.Info(ctx, "adding handlefunc() /convert") | ||
|
||
server := &http.Server{ | ||
Addr: ":8443", | ||
Handler: mux, | ||
ReadHeaderTimeout: 32 * time.Second, | ||
TLSConfig: &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
CurvePreferences: []tls.CurveID{tls.CurveP256, tls.CurveP384, tls.X25519}, | ||
CipherSuites: []uint16{ | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, | ||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, | ||
}, | ||
}, | ||
} | ||
|
||
go func() { | ||
ctxlog.Info(ctx, "starting webhook server") | ||
// blocking call, returns on error | ||
if err := server.ListenAndServeTLS(path.Join("/etc/webhook/certs", "tls.crt"), path.Join("/etc/webhook/certs", "tls.key")); err != nil { | ||
ctxlog.Error(ctx, err, "webhook server failed to start") | ||
} | ||
}() | ||
|
||
stopCh := signals.SetupSignalHandler() | ||
sig := <-stopCh | ||
|
||
l.Info("Shutting down server.", "signal", sig) | ||
ctxlog.Info(ctx, "shutting down webhook server,", "signal:", sig) | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
l.Error(err, "Failed to gracefully shutdown the server.") | ||
return err | ||
} | ||
return nil | ||
|
||
} | ||
|
||
func health(resp http.ResponseWriter, _ *http.Request) { | ||
resp.WriteHeader(http.StatusNoContent) | ||
} |
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,24 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: shipwright-build-webhook | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
- events | ||
- configmaps | ||
- secrets | ||
- limitranges | ||
- namespaces | ||
- services | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- admissionregistration.k8s.io | ||
- admissionregistration.k8s.io/v1beta1 | ||
resources: | ||
- validatingwebhookconfigurations | ||
verbs: | ||
- '*' |
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,14 @@ | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: shipwright-build-webhook | ||
subjects: | ||
- kind: ServiceAccount | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build |
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,5 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build |
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,12 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: shp-build-webhook | ||
namespace: shipwright-build | ||
spec: | ||
ports: | ||
- name: https-webhook | ||
port: 443 | ||
targetPort: 8443 | ||
selector: | ||
name: shp-build-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,51 @@ | ||
|
||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build | ||
labels: | ||
app: shp-build-webhook | ||
spec: | ||
replicas: 1 | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 0 | ||
maxUnavailable: 1 | ||
type: RollingUpdate | ||
selector: | ||
matchLabels: | ||
name: shp-build-webhook | ||
template: | ||
metadata: | ||
name: shp-build-webhook | ||
labels: | ||
name: shp-build-webhook | ||
spec: | ||
securityContext: | ||
runAsNonRoot: true | ||
serviceAccountName: shipwright-build-webhook | ||
containers: | ||
- name: shp-build-webhook | ||
image: ko://github.com/shipwright-io/build/cmd/shipwright-build-webhook | ||
volumeMounts: | ||
- name: webhook-certs | ||
mountPath: /etc/webhook/certs | ||
readOnly: true | ||
ports: | ||
- containerPort: 8443 | ||
name: https-port | ||
securityContext: | ||
allowPrivilegeEscalation: false | ||
capabilities: | ||
drop: | ||
- ALL | ||
readOnlyRootFilesystem: true | ||
runAsUser: 1000 | ||
runAsGroup: 1000 | ||
seccompProfile: | ||
type: RuntimeDefault | ||
volumes: | ||
- name: webhook-certs | ||
secret: | ||
secretName: shipwright-build-webhook-cert |
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 |
---|---|---|
|
@@ -12231,7 +12231,7 @@ spec: | |
required: | ||
- spec | ||
type: object | ||
served: false | ||
served: true | ||
storage: false | ||
subresources: | ||
status: {} |
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 |
---|---|---|
|
@@ -4083,7 +4083,7 @@ spec: | |
required: | ||
- spec | ||
type: object | ||
served: false | ||
served: true | ||
storage: false | ||
subresources: | ||
status: {} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
spec: | ||
conversion: | ||
strategy: Webhook | ||
webhook: | ||
clientConfig: | ||
caBundle: CA_BUNDLE | ||
service: | ||
namespace: shipwright-build | ||
name: shp-build-webhook | ||
path: /convert | ||
conversionReviewVersions: | ||
- v1 |
Oops, something went wrong.