Skip to content

Commit

Permalink
Fix some typos in code and document
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLike committed Mar 15, 2019
1 parent 4c94adf commit cc8925c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 117 deletions.
144 changes: 42 additions & 102 deletions cmd/admission/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("", "")
}
4 changes: 2 additions & 2 deletions cmd/controllers/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions pkg/apis/batch/v1alpha1/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ const (
JobNameKey = "volcano.sh/job-name"
JobNamespaceKey = "volcano.sh/job-namespace"
DefaultTaskSpec = "default"
JobVersion = "volcano.sh/job-version"
)
2 changes: 1 addition & 1 deletion pkg/apis/batch/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/apis/bus/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions pkg/cli/job/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package job

import (
"fmt"
"io"
"os"
"volcano.sh/volcano/pkg/apis/batch/v1alpha1"

"github.com/spf13/cobra"

Expand All @@ -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) {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/cli/job/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/clientset/versioned/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/clientset/versioned/fake/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/clientset/versioned/typed/batch/v1alpha1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/client/clientset/versioned/typed/bus/v1alpha1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc8925c

Please sign in to comment.