Skip to content

Commit

Permalink
Merge pull request #5 from bvwells/typo
Browse files Browse the repository at this point in the history
Fix typos and formatting
  • Loading branch information
Morven Cao authored Apr 1, 2020
2 parents ccb2714 + 297175e commit 1c160e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
34 changes: 17 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"os/signal"
"syscall"

"github.com/golang/glog"
)

Expand All @@ -22,42 +22,42 @@ func main() {
flag.StringVar(&parameters.keyFile, "tlsKeyFile", "/etc/webhook/certs/key.pem", "File containing the x509 private key to --tlsCertFile.")
flag.StringVar(&parameters.sidecarCfgFile, "sidecarCfgFile", "/etc/webhook/config/sidecarconfig.yaml", "File containing the mutation configuration.")
flag.Parse()

sidecarConfig, err := loadConfig(parameters.sidecarCfgFile)
if err != nil {
glog.Errorf("Filed to load configuration: %v", err)
glog.Errorf("Failed to load configuration: %v", err)
}

pair, err := tls.LoadX509KeyPair(parameters.certFile, parameters.keyFile)
if err != nil {
glog.Errorf("Filed to load key pair: %v", err)
glog.Errorf("Failed to load key pair: %v", err)
}
whsvr := &WebhookServer {
sidecarConfig: sidecarConfig,
server: &http.Server {
Addr: fmt.Sprintf(":%v", parameters.port),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{pair}},

whsvr := &WebhookServer{
sidecarConfig: sidecarConfig,
server: &http.Server{
Addr: fmt.Sprintf(":%v", parameters.port),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{pair}},
},
}

// define http server and server handler
mux := http.NewServeMux()
mux.HandleFunc("/mutate", whsvr.serve)
whsvr.server.Handler = mux

// start webhook server in new rountine
go func() {
if err := whsvr.server.ListenAndServeTLS("", ""); err != nil {
glog.Errorf("Filed to listen and serve webhook server: %v", err)
glog.Errorf("Failed to listen and serve webhook server: %v", err)
}
}()

// listening OS shutdown singal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
glog.Infof("Got OS shutdown signal, shutting down wenhook server gracefully...")

glog.Infof("Got OS shutdown signal, shutting down webhook server gracefully...")
whsvr.server.Shutdown(context.Background())
}
72 changes: 36 additions & 36 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
defaulter = runtime.ObjectDefaulter(runtimeScheme)
)

var ignoredNamespaces = []string {
var ignoredNamespaces = []string{
metav1.NamespaceSystem,
metav1.NamespacePublic,
}
Expand All @@ -39,21 +39,21 @@ const (
)

type WebhookServer struct {
sidecarConfig *Config
server *http.Server
sidecarConfig *Config
server *http.Server
}

// Webhook Server parameters
type WhSvrParameters struct {
port int // webhook server port
certFile string // path to the x509 certificate for https
keyFile string // path to the x509 private key matching `CertFile`
sidecarCfgFile string // path to sidecar injector configuration file
port int // webhook server port
certFile string // path to the x509 certificate for https
keyFile string // path to the x509 private key matching `CertFile`
sidecarCfgFile string // path to sidecar injector configuration file
}

type Config struct {
Containers []corev1.Container `yaml:"containers"`
Volumes []corev1.Volume `yaml:"volumes"`
Containers []corev1.Container `yaml:"containers"`
Volumes []corev1.Volume `yaml:"volumes"`
}

type patchOperation struct {
Expand All @@ -72,10 +72,10 @@ func init() {

// (https://github.com/kubernetes/kubernetes/issues/57982)
func applyDefaultsWorkaround(containers []corev1.Container, volumes []corev1.Volume) {
defaulter.Default(&corev1.Pod {
Spec: corev1.PodSpec {
Containers: containers,
Volumes: volumes,
defaulter.Default(&corev1.Pod{
Spec: corev1.PodSpec{
Containers: containers,
Volumes: volumes,
},
})
}
Expand All @@ -86,12 +86,12 @@ func loadConfig(configFile string) (*Config, error) {
return nil, err
}
glog.Infof("New configuration: sha256sum %x", sha256.Sum256(data))

var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}

return &cfg, nil
}

Expand All @@ -100,7 +100,7 @@ func mutationRequired(ignoredList []string, metadata *metav1.ObjectMeta) bool {
// skip special kubernete system namespaces
for _, namespace := range ignoredList {
if metadata.Namespace == namespace {
glog.Infof("Skip mutation for %v for it' in special namespace:%v", metadata.Name, metadata.Namespace)
glog.Infof("Skip mutation for %v for it's in special namespace:%v", metadata.Name, metadata.Namespace)
return false
}
}
Expand All @@ -111,11 +111,11 @@ func mutationRequired(ignoredList []string, metadata *metav1.ObjectMeta) bool {
}

status := annotations[admissionWebhookAnnotationStatusKey]

// determine whether to perform mutation based on annotation for the target resource
var required bool
if strings.ToLower(status) == "injected" {
required = false;
required = false
} else {
switch strings.ToLower(annotations[admissionWebhookAnnotationInjectKey]) {
default:
Expand All @@ -124,7 +124,7 @@ func mutationRequired(ignoredList []string, metadata *metav1.ObjectMeta) bool {
required = true
}
}

glog.Infof("Mutation policy for %v/%v: status: %q required:%v", metadata.Namespace, metadata.Name, status, required)
return required
}
Expand All @@ -141,7 +141,7 @@ func addContainer(target, added []corev1.Container, basePath string) (patch []pa
} else {
path = path + "/-"
}
patch = append(patch, patchOperation {
patch = append(patch, patchOperation{
Op: "add",
Path: path,
Value: value,
Expand All @@ -162,7 +162,7 @@ func addVolume(target, added []corev1.Volume, basePath string) (patch []patchOpe
} else {
path = path + "/-"
}
patch = append(patch, patchOperation {
patch = append(patch, patchOperation{
Op: "add",
Path: path,
Value: value,
Expand All @@ -175,15 +175,15 @@ func updateAnnotation(target map[string]string, added map[string]string) (patch
for key, value := range added {
if target == nil || target[key] == "" {
target = map[string]string{}
patch = append(patch, patchOperation {
patch = append(patch, patchOperation{
Op: "add",
Path: "/metadata/annotations",
Value: map[string]string{
key: value,
},
})
} else {
patch = append(patch, patchOperation {
patch = append(patch, patchOperation{
Op: "replace",
Path: "/metadata/annotations/" + key,
Value: value,
Expand All @@ -196,7 +196,7 @@ func updateAnnotation(target map[string]string, added map[string]string) (patch
// create mutation patch for resoures
func createPatch(pod *corev1.Pod, sidecarConfig *Config, annotations map[string]string) ([]byte, error) {
var patch []patchOperation

patch = append(patch, addContainer(pod.Spec.Containers, sidecarConfig.Containers, "/spec/containers")...)
patch = append(patch, addVolume(pod.Spec.Volumes, sidecarConfig.Volumes, "/spec/volumes")...)
patch = append(patch, updateAnnotation(pod.Annotations, annotations)...)
Expand All @@ -210,38 +210,38 @@ func (whsvr *WebhookServer) mutate(ar *v1beta1.AdmissionReview) *v1beta1.Admissi
var pod corev1.Pod
if err := json.Unmarshal(req.Object.Raw, &pod); err != nil {
glog.Errorf("Could not unmarshal raw object: %v", err)
return &v1beta1.AdmissionResponse {
Result: &metav1.Status {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}

glog.Infof("AdmissionReview for Kind=%v, Namespace=%v Name=%v (%v) UID=%v patchOperation=%v UserInfo=%v",
req.Kind, req.Namespace, req.Name, pod.Name, req.UID, req.Operation, req.UserInfo)

// determine whether to perform mutation
if !mutationRequired(ignoredNamespaces, &pod.ObjectMeta) {
glog.Infof("Skipping mutation for %s/%s due to policy check", pod.Namespace, pod.Name)
return &v1beta1.AdmissionResponse {
Allowed: true,
return &v1beta1.AdmissionResponse{
Allowed: true,
}
}

// Workaround: https://github.com/kubernetes/kubernetes/issues/57982
applyDefaultsWorkaround(whsvr.sidecarConfig.Containers, whsvr.sidecarConfig.Volumes)
annotations := map[string]string{admissionWebhookAnnotationStatusKey: "injected"}
patchBytes, err := createPatch(&pod, whsvr.sidecarConfig, annotations)
if err != nil {
return &v1beta1.AdmissionResponse {
Result: &metav1.Status {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
}

glog.Infof("AdmissionResponse: patch=%v\n", string(patchBytes))
return &v1beta1.AdmissionResponse {
return &v1beta1.AdmissionResponse{
Allowed: true,
Patch: patchBytes,
PatchType: func() *v1beta1.PatchType {
Expand Down Expand Up @@ -277,8 +277,8 @@ func (whsvr *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
ar := v1beta1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
glog.Errorf("Can't decode body: %v", err)
admissionResponse = &v1beta1.AdmissionResponse {
Result: &metav1.Status {
admissionResponse = &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
},
}
Expand Down

0 comments on commit 1c160e8

Please sign in to comment.