polish the code

pull/10823/head
Yanshu Zhao 2021-03-17 08:18:36 +00:00
parent 923a6247a2
commit 839aacf580
5 changed files with 16 additions and 8 deletions

View File

@ -826,12 +826,12 @@ out/auto-pause-hook: $(SOURCE_GENERATED) ## Build auto-pause hook addon
.PHONY: auto-pause-hook-image
auto-pause-hook-image: out/auto-pause-hook ## Build docker image for auto-pause hook
docker build -t docker.io/azhao155/auto-pause-hook:1.3 -f deploy/addons/auto-pause/Dockerfile .
docker build -t docker.io/azhao155/auto-pause-hook:1.5 -f deploy/addons/auto-pause/Dockerfile .
.PHONY: push-auto-pause-hook-image
push-auto-pause-hook-image: auto-pause-hook-image
docker login docker.io/azhao155
$(MAKE) push-docker IMAGE=docker.io/azhao155/auto-pause-hook:1.3
$(MAKE) push-docker IMAGE=docker.io/azhao155/auto-pause-hook:1.5
.PHONY: out/performance-bot
out/performance-bot:

View File

@ -28,6 +28,7 @@ import (
"time"
)
// generate https certs for the webhook server
func gencerts() (caCert []byte, serverCert []byte, serverKey []byte) {
var caPEM, serverCertPEM, serverPrivKeyPEM *bytes.Buffer
// CA config

View File

@ -31,6 +31,11 @@ import (
"github.com/golang/glog"
)
var (
webhookName = "env-inject-webhook"
webhookConfigName = "env-inject.zyanshu.io"
)
// get a clientset with in-cluster config.
func getClient() *kubernetes.Clientset {
config, err := rest.InClusterConfig()
@ -81,9 +86,9 @@ func configTLS(clientset *kubernetes.Clientset, serverCert []byte, serverKey []b
func selfRegistration(clientset *kubernetes.Clientset, caCert []byte) {
time.Sleep(10 * time.Second)
client := clientset.AdmissionregistrationV1().MutatingWebhookConfigurations()
_, err := client.Get("env-inject-webhook", metav1.GetOptions{})
_, err := client.Get(webhookName, metav1.GetOptions{})
if err == nil {
if err2 := client.Delete("env-inject-webhook", &metav1.DeleteOptions{}); err2 != nil {
if err2 := client.Delete(webhookName, &metav1.DeleteOptions{}); err2 != nil {
glog.Fatal(err2)
}
}
@ -92,11 +97,11 @@ func selfRegistration(clientset *kubernetes.Clientset, caCert []byte) {
webhookConfig := &v1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "env-inject-webhook",
Name: webhookName,
},
Webhooks: []v1.MutatingWebhook{
{
Name: "env-inject.zyanshu.io",
Name: webhookConfigName,
Rules: []v1.RuleWithOperations{
{
Operations: []v1.OperationType{v1.Create, v1.Update},

View File

@ -23,6 +23,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strconv"
"github.com/golang/glog"
"github.com/mattbaird/jsonpatch"
@ -32,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/minikube/pkg/minikube/constants"
)
var (
@ -132,7 +134,7 @@ func patchConfig(pod *corev1.Pod) ([]byte, error) {
configEnv := []corev1.EnvVar{
{Name: "KUBERNETES_SERVICE_HOST", Value: "192.168.49.2"},
{Name: "KUBERNETES_SERVICE_PORT", Value: "32443"}}
{Name: "KUBERNETES_SERVICE_PORT", Value: strconv.Itoa(constants.AutoPauseProxyPort)}}
for idx, container := range pod.Spec.Containers {
patch = append(patch, addEnv(container.Env, configEnv, fmt.Sprintf("/spec/containers/%d/env", idx))...)
}

View File

@ -101,7 +101,7 @@ var Addons = map[string]*Addon{
//GuestPersistentDir
}, false, "auto-pause", map[string]string{
"haproxy": "haproxy:2.3.5",
"AutoPauseHook": "azhao155/auto-pause-hook:1.3",
"AutoPauseHook": "azhao155/auto-pause-hook:1.5",
}, map[string]string{
"haproxy": "gcr.io",
"AutoPauseHook": "docker.io",