From cc8925c57bd8ac72e96984825803f252b9b57151 Mon Sep 17 00:00:00 2001 From: TommyLike Date: Fri, 15 Mar 2019 11:23:21 +0800 Subject: [PATCH] Fix some typos in code and document --- cmd/admission/main.go | 144 +++++------------- cmd/controllers/app/server.go | 4 +- pkg/admission/{admin_job.go => admit_job.go} | 0 pkg/apis/batch/v1alpha1/labels.go | 1 + .../batch/v1alpha1/zz_generated.deepcopy.go | 2 +- .../bus/v1alpha1/zz_generated.deepcopy.go | 2 +- pkg/cli/job/list.go | 35 ++++- pkg/cli/job/run.go | 2 +- pkg/client/clientset/versioned/doc.go | 2 +- pkg/client/clientset/versioned/fake/doc.go | 2 +- .../versioned/typed/batch/v1alpha1/doc.go | 2 +- .../versioned/typed/bus/v1alpha1/doc.go | 2 +- 12 files changed, 81 insertions(+), 117 deletions(-) rename pkg/admission/{admin_job.go => admit_job.go} (100%) diff --git a/cmd/admission/main.go b/cmd/admission/main.go index 10bccf5472..458edb960a 100644 --- a/cmd/admission/main.go +++ b/cmd/admission/main.go @@ -13,122 +13,62 @@ 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 admission +package main import ( - "encoding/json" + "flag" "fmt" - "math/rand" - "time" - - "github.com/golang/glog" - - "k8s.io/api/admission/v1beta1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1" + "io/ioutil" + "net/http" + "os" + "strconv" + + "volcano.sh/volcano/cmd/admission/app" + appConf "volcano.sh/volcano/cmd/admission/app/configure" + admissioncontroller "volcano.sh/volcano/pkg/admission" ) -type patchOperation struct { - Op string `json:"op"` - Path string `json:"path"` - Value interface{} `json:"value,omitempty"` +func serveJobs(w http.ResponseWriter, r *http.Request) { + app.Serve(w, r, admissioncontroller.AdmitJobs) } -// mutate job. -func MutateJobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse { - glog.V(3).Infof("mutating jobs") - - job, err := DecodeJob(ar.Request.Object, ar.Request.Resource) - if err != nil { - return ToAdmissionResponse(err) - } - - reviewResponse := v1beta1.AdmissionResponse{} - reviewResponse.Allowed = true - - var patchBytes []byte - switch ar.Request.Operation { - case v1beta1.Create: - patchBytes, err = createPatch(job) - break - default: - err = fmt.Errorf("expect operation to be 'CREATE' ") - return ToAdmissionResponse(err) - } - - if err != nil { - reviewResponse.Result = &metav1.Status{Message: err.Error()} - return &reviewResponse - } - glog.V(3).Infof("AdmissionResponse: patch=%v\n", string(patchBytes)) - reviewResponse.Patch = patchBytes - pt := v1beta1.PatchTypeJSONPatch - reviewResponse.PatchType = &pt - - return &reviewResponse +func serveMutateJobs(w http.ResponseWriter, r *http.Request) { + app.Serve(w, r, admissioncontroller.MutateJobs) } -func createPatch(job v1alpha1.Job) ([]byte, error) { - var patch []patchOperation - patch = append(patch, mutateJobVersion(job.Status, "/status")...) - patch = append(patch, mutateSpec(job.Spec.Tasks, "/spec/tasks")...) - patch = append(patch, mutateMetadata(job.ObjectMeta, "/metadata")...) +func main() { + config := appConf.NewConfig() + config.AddFlags() + flag.Parse() - return json.Marshal(patch) -} + http.HandleFunc(admissioncontroller.AdmitJobPath, serveJobs) + http.HandleFunc(admissioncontroller.MutateJobPath, serveMutateJobs) -func mutateSpec(tasks []v1alpha1.TaskSpec, basePath string) (patch []patchOperation) { - for index := range tasks { - // add default task name - taskName := tasks[index].Name - if len(taskName) == 0 { - tasks[index].Name = v1alpha1.DefaultTaskSpec - } + if err := config.CheckPortOrDie(); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) } - patch = append(patch, patchOperation{ - Op: "replace", - Path: basePath, - Value: tasks, - }) + addr := ":" + strconv.Itoa(config.Port) - return patch -} - -func mutateJobVersion(jobStatus v1alpha1.JobStatus, basePath string) (patch []patchOperation) { - jobStatus.Version = 1 - patch = append(patch, patchOperation{ - Op: "replace", - Path: basePath, - Value: jobStatus, - }) - return patch -} + clientset := app.GetClient(config) -func mutateMetadata(metadata metav1.ObjectMeta, basePath string) (patch []patchOperation) { - if len(metadata.Annotations) == 0 { - metadata.Annotations = make(map[string]string) + caCertPem, err := ioutil.ReadFile(config.CaCertFile) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + } else { + // patch caBundle in webhook + if err = appConf.PatchMutateWebhookConfig(clientset.AdmissionregistrationV1beta1().MutatingWebhookConfigurations(), + config.MutateWebhookConfigName, config.MutateWebhookName, caCertPem); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + } + if err = appConf.PatchValidateWebhookConfig(clientset.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations(), + config.ValidateWebhookConfigName, config.ValidateWebhookName, caCertPem); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + } } - randomStr := genRandomStr(5) - metadata.Annotations[PVCInputName] = fmt.Sprintf("%s-input-%s", metadata.Name, randomStr) - metadata.Annotations[PVCOutputName] = fmt.Sprintf("%s-output-%s", metadata.Name, randomStr) - patch = append(patch, patchOperation{ - Op: "replace", - Path: basePath, - Value: metadata, - }) - - return patch -} - -func genRandomStr(l int) string { - str := "0123456789abcdefghijklmnopqrstuvwxyz" - bytes := []byte(str) - result := []byte{} - r := rand.New(rand.NewSource(time.Now().UnixNano())) - for i := 0; i < l; i++ { - result = append(result, bytes[r.Intn(len(bytes))]) + server := &http.Server{ + Addr: addr, + TLSConfig: app.ConfigTLS(config, clientset), } - return string(result) + server.ListenAndServeTLS("", "") } diff --git a/cmd/controllers/app/server.go b/cmd/controllers/app/server.go index eb0583eda9..c7566b7bce 100644 --- a/cmd/controllers/app/server.go +++ b/cmd/controllers/app/server.go @@ -60,10 +60,10 @@ func Run(opt *options.ServerOption) error { return err } - queuejobctrl := job.NewJobController(config) + JobController := job.NewJobController(config) run := func(ctx context.Context) { - queuejobctrl.Run(ctx.Done()) + JobController.Run(ctx.Done()) <-ctx.Done() } diff --git a/pkg/admission/admin_job.go b/pkg/admission/admit_job.go similarity index 100% rename from pkg/admission/admin_job.go rename to pkg/admission/admit_job.go diff --git a/pkg/apis/batch/v1alpha1/labels.go b/pkg/apis/batch/v1alpha1/labels.go index c08b32f0d0..99e016f11e 100644 --- a/pkg/apis/batch/v1alpha1/labels.go +++ b/pkg/apis/batch/v1alpha1/labels.go @@ -21,4 +21,5 @@ const ( JobNameKey = "volcano.sh/job-name" JobNamespaceKey = "volcano.sh/job-namespace" DefaultTaskSpec = "default" + JobVersion = "volcano.sh/job-version" ) diff --git a/pkg/apis/batch/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/batch/v1alpha1/zz_generated.deepcopy.go index 7e38caca05..e00c862459 100644 --- a/pkg/apis/batch/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/batch/v1alpha1/zz_generated.deepcopy.go @@ -1,7 +1,7 @@ // +build !ignore_autogenerated /* -Copyright 2019 The Vulcan Authors. +Copyright 2019 The Volcano Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pkg/apis/bus/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/bus/v1alpha1/zz_generated.deepcopy.go index ff8d1aa7d1..db373646fe 100644 --- a/pkg/apis/bus/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/bus/v1alpha1/zz_generated.deepcopy.go @@ -1,7 +1,7 @@ // +build !ignore_autogenerated /* -Copyright 2019 The Vulcan Authors. +Copyright 2019 The Volcano Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pkg/cli/job/list.go b/pkg/cli/job/list.go index 956b5a78cc..98f687a892 100644 --- a/pkg/cli/job/list.go +++ b/pkg/cli/job/list.go @@ -17,6 +17,9 @@ package job import ( "fmt" + "io" + "os" + "volcano.sh/volcano/pkg/apis/batch/v1alpha1" "github.com/spf13/cobra" @@ -31,6 +34,18 @@ type listFlags struct { Namespace string } +const ( + Name string = "Name" + Creation string = "Creation" + Phase string = "Phase" + Replicas string = "Replicas" + Min string = "Min" + Pending string = "Pending" + Running string = "Running" + Succeeded string = "Succeeded" + Failed string = "Failed" +) + var listJobFlags = &listFlags{} func InitListFlags(cmd *cobra.Command) { @@ -55,19 +70,27 @@ func ListJobs() error { fmt.Printf("No resources found\n") return nil } + PrintJobs(jobs, os.Stdout) + + return nil +} - fmt.Printf("%-25s%-25s%-12s%-12s%-6s%-10s%-10s%-12s%-10s\n", - "Name", "Creation", "Phase", "Replicas", "Min", "Pending", "Running", "Succeeded", "Failed") +func PrintJobs(jobs *v1alpha1.JobList, writer io.Writer) { + _, err := fmt.Fprintf(writer, "%-25s%-25s%-12s%-12s%-6s%-10s%-10s%-12s%-10s\n", + Name, Creation, Phase, Replicas, Min, Pending, Running, Succeeded, Failed) + if err != nil { + fmt.Printf("Failed to print list command result: %s.\n", err) + } for _, job := range jobs.Items { replicas := int32(0) for _, ts := range job.Spec.Tasks { replicas += ts.Replicas } - - fmt.Printf("%-25s%-25s%-12s%-12d%-6d%-10d%-10d%-12d%-10d\n", + _, err = fmt.Fprintf(writer, "%-25s%-25s%-12s%-12d%-6d%-10d%-10d%-12d%-10d\n", job.Name, job.CreationTimestamp.Format("2006-01-02 15:04:05"), job.Status.State.Phase, replicas, job.Status.MinAvailable, job.Status.Pending, job.Status.Running, job.Status.Succeeded, job.Status.Failed) + if err != nil { + fmt.Printf("Failed to print list command result: %s.\n", err) + } } - - return nil } diff --git a/pkg/cli/job/run.go b/pkg/cli/job/run.go index 5635b23b66..0dca1b01b0 100644 --- a/pkg/cli/job/run.go +++ b/pkg/cli/job/run.go @@ -50,7 +50,7 @@ func InitRunFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&launchJobFlags.Requests, "requests", "", "cpu=1000m,memory=100Mi", "the resource request of the task") } -var jobName = "job.volcano.volcano.sh" +var jobName = "job.volcano.sh" func RunJob() error { config, err := buildConfig(launchJobFlags.Master, launchJobFlags.Kubeconfig) diff --git a/pkg/client/clientset/versioned/doc.go b/pkg/client/clientset/versioned/doc.go index 41721ca52d..e5d065e505 100644 --- a/pkg/client/clientset/versioned/doc.go +++ b/pkg/client/clientset/versioned/doc.go @@ -1,5 +1,5 @@ /* -Copyright The Kubernetes Authors. +Copyright 2019 The Volcano Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pkg/client/clientset/versioned/fake/doc.go b/pkg/client/clientset/versioned/fake/doc.go index 9b99e71670..715c3e1a65 100644 --- a/pkg/client/clientset/versioned/fake/doc.go +++ b/pkg/client/clientset/versioned/fake/doc.go @@ -1,5 +1,5 @@ /* -Copyright The Kubernetes Authors. +Copyright 2019 The Volcano Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pkg/client/clientset/versioned/typed/batch/v1alpha1/doc.go b/pkg/client/clientset/versioned/typed/batch/v1alpha1/doc.go index df51baa4d4..5b5f708b3a 100644 --- a/pkg/client/clientset/versioned/typed/batch/v1alpha1/doc.go +++ b/pkg/client/clientset/versioned/typed/batch/v1alpha1/doc.go @@ -1,5 +1,5 @@ /* -Copyright The Kubernetes Authors. +Copyright 2019 The Volcano Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pkg/client/clientset/versioned/typed/bus/v1alpha1/doc.go b/pkg/client/clientset/versioned/typed/bus/v1alpha1/doc.go index df51baa4d4..5b5f708b3a 100644 --- a/pkg/client/clientset/versioned/typed/bus/v1alpha1/doc.go +++ b/pkg/client/clientset/versioned/typed/bus/v1alpha1/doc.go @@ -1,5 +1,5 @@ /* -Copyright The Kubernetes Authors. +Copyright 2019 The Volcano Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.