-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-pause handle internal kubernetes requests #10823
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
91f9bd6
Add webhook to inject env to redirect inner cluster requests to haproxy
azhao155 74209ab
add auto-pause hook deployment template to auto-pause add on
azhao155 4072ea7
Add header file
azhao155 c6e3e09
add glog into module
azhao155 923a624
fix lint
azhao155 839aacf
polish the code
azhao155 1073312
Add template for reverse proxy ip
azhao155 9ea99ae
Add skip into label object of web hook, clean up code
azhao155 30287b4
Add ignore file
azhao155 14ec53f
Fix typo
azhao155 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,112 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors All rights reserved. | ||
|
||
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 ( | ||
"bytes" | ||
cryptorand "crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
// generate https certs for the webhook server | ||
func gencerts() (caCert []byte, serverCert []byte, serverKey []byte) { | ||
var caPEM, serverCertPEM, serverPrivKeyPEM *bytes.Buffer | ||
// CA config | ||
ca := &x509.Certificate{ | ||
SerialNumber: big.NewInt(2020), | ||
Subject: pkix.Name{ | ||
Organization: []string{"velotio.com"}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().AddDate(1, 0, 0), | ||
IsCA: true, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, | ||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
// CA private key | ||
caPrivKey, err := rsa.GenerateKey(cryptorand.Reader, 4096) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// Self signed CA certificate | ||
caBytes, err := x509.CreateCertificate(cryptorand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// PEM encode CA cert | ||
caPEM = new(bytes.Buffer) | ||
_ = pem.Encode(caPEM, &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: caBytes, | ||
}) | ||
|
||
dnsNames := []string{"webhook", | ||
"webhook.auto-pause", "webhook.auto-pause.svc"} | ||
commonName := "webhook.auto-pause.svc" | ||
|
||
// server cert config | ||
cert := &x509.Certificate{ | ||
DNSNames: dnsNames, | ||
SerialNumber: big.NewInt(1658), | ||
Subject: pkix.Name{ | ||
CommonName: commonName, | ||
Organization: []string{"velotio.com"}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().AddDate(1, 0, 0), | ||
SubjectKeyId: []byte{1, 2, 3, 4, 6}, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, | ||
KeyUsage: x509.KeyUsageDigitalSignature, | ||
} | ||
|
||
// server private key | ||
serverPrivKey, err := rsa.GenerateKey(cryptorand.Reader, 4096) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// sign the server cert | ||
serverCertBytes, err := x509.CreateCertificate(cryptorand.Reader, cert, ca, &serverPrivKey.PublicKey, caPrivKey) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// PEM encode the server cert and key | ||
serverCertPEM = new(bytes.Buffer) | ||
_ = pem.Encode(serverCertPEM, &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: serverCertBytes, | ||
}) | ||
|
||
serverPrivKeyPEM = new(bytes.Buffer) | ||
_ = pem.Encode(serverPrivKeyPEM, &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: x509.MarshalPKCS1PrivateKey(serverPrivKey), | ||
}) | ||
|
||
return caPEM.Bytes(), serverCertPEM.Bytes(), serverPrivKeyPEM.Bytes() | ||
} |
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,147 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors All rights reserved. | ||
|
||
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 ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"log" | ||
|
||
v1 "k8s.io/api/admissionregistration/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
var ( | ||
webhookName = "env-inject-webhook" | ||
webhookConfigName = "env-inject.zyanshu.io" | ||
skipLabel = "auto-pause-skip" | ||
) | ||
|
||
// Create a clientset with in-cluster config. | ||
azhao155 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func client() *kubernetes.Clientset { | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
return clientset | ||
} | ||
|
||
// Retrieve the CA cert that will signed the cert used by the | ||
// "GenericAdmissionWebhook" plugin admission controller. | ||
func apiServerCert(clientset *kubernetes.Clientset) []byte { | ||
c, err := clientset.CoreV1().ConfigMaps("kube-system").Get("extension-apiserver-authentication", metav1.GetOptions{}) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
|
||
pem, ok := c.Data["requestheader-client-ca-file"] | ||
if !ok { | ||
glog.Fatalf(fmt.Sprintf("cannot find the ca.crt in the configmap, configMap.Data is %#v", c.Data)) | ||
} | ||
glog.Info("client-ca-file=", pem) | ||
return []byte(pem) | ||
} | ||
|
||
func configTLS(clientset *kubernetes.Clientset, serverCert []byte, serverKey []byte) *tls.Config { | ||
cert := apiServerCert(clientset) | ||
apiserverCA := x509.NewCertPool() | ||
apiserverCA.AppendCertsFromPEM(cert) | ||
|
||
sCert, err := tls.X509KeyPair(serverCert, serverKey) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
return &tls.Config{ | ||
Certificates: []tls.Certificate{sCert}, | ||
ClientCAs: apiserverCA, | ||
ClientAuth: tls.VerifyClientCertIfGiven, // TODO: actually require client cert | ||
} | ||
} | ||
|
||
// register this example webhook admission controller with the kube-apiserver | ||
// by creating externalAdmissionHookConfigurations. | ||
func selfRegistration(clientset *kubernetes.Clientset, caCert []byte) { | ||
client := clientset.AdmissionregistrationV1().MutatingWebhookConfigurations() | ||
_, err := client.Get(webhookName, metav1.GetOptions{}) | ||
if err == nil { | ||
if err2 := client.Delete(webhookName, &metav1.DeleteOptions{}); err2 != nil { | ||
glog.Fatal(err2) | ||
} | ||
} | ||
var failurePolicy v1.FailurePolicyType = v1.Fail | ||
var sideEffects v1.SideEffectClass = v1.SideEffectClassNone | ||
|
||
webhookConfig := &v1.MutatingWebhookConfiguration{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: webhookName, | ||
}, | ||
Webhooks: []v1.MutatingWebhook{ | ||
{ | ||
Name: webhookConfigName, | ||
Rules: []v1.RuleWithOperations{ | ||
{ | ||
Operations: []v1.OperationType{v1.Create, v1.Update}, | ||
Rule: v1.Rule{ | ||
APIGroups: []string{""}, | ||
APIVersions: []string{"v1"}, | ||
Resources: []string{"pods"}, | ||
}, | ||
}, | ||
{ | ||
Operations: []v1.OperationType{v1.Create, v1.Update}, | ||
Rule: v1.Rule{ | ||
APIGroups: []string{"extensions"}, | ||
APIVersions: []string{"v1"}, | ||
Resources: []string{"deployments"}, | ||
}, | ||
}, | ||
}, | ||
FailurePolicy: &failurePolicy, | ||
ObjectSelector: &metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{ | ||
Key: skipLabel, | ||
Operator: metav1.LabelSelectorOpDoesNotExist, | ||
}, | ||
}, | ||
}, | ||
ClientConfig: v1.WebhookClientConfig{ | ||
Service: &v1.ServiceReference{ | ||
Namespace: "auto-pause", | ||
Name: "webhook", | ||
}, | ||
CABundle: caCert, | ||
}, | ||
AdmissionReviewVersions: []string{"v1"}, | ||
SideEffects: &sideEffects, | ||
}, | ||
}, | ||
} | ||
if _, err := client.Create(webhookConfig); err != nil { | ||
glog.Fatalf("Client creation failed with %s", err) | ||
} | ||
log.Println("CLIENT CREATED") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets not use the Dot notation here, it should be a simple One letter version,
like V1