Skip to content

Commit

Permalink
Refactor the TfJob to use K8s libraries (#215)
Browse files Browse the repository at this point in the history
fix #206

1.refactor the spec package to the kubernetes standard API, all detention of our CRD is in pkg pkg/apis/tensorflow and also the validation、 set Default for the CRD and some help func.

2.for use K8s code gen, wo import the project https://github.com/kubernetes/code-generator, and some shell script in the folder hack. You can see how to use it in https://blog.openshift.com/kubernetes-deep-dive-code-generation-customresources/

3.All the file in pkg/client are all auto generated and we should not modify it manually.

4.I do not plan to change the logical to use the sharedInformer and clientset. This PR is big enough, I think I can do it in a follow PR.

5.SharedInformer is a helpful tool with cache that we can use it to list and watch our resources. More details you can see http://borismattijssen.github.io/articles/kubernetes-informers-controllers-reflectors-stores
  • Loading branch information
wackxu authored and jlewi committed Dec 29, 2017
1 parent 430cf17 commit ed7cae7
Show file tree
Hide file tree
Showing 563 changed files with 121,254 additions and 2 deletions.
14 changes: 12 additions & 2 deletions glide.lock

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

5 changes: 5 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ import:
version: 019ae5ada31de202164b118aee88ee2d14075c31
- package: k8s.io/apiextensions-apiserver
version: kubernetes-1.8.4
- package: k8s.io/code-generator
version: kubernetes-1.8.5
- package: github.com/hashicorp/golang-lru
version: a0d98a5f288019575c6d1f4bb1573fef2d1fcdc4

34 changes: 34 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# Copyright 2017 The Kubernetes Authors.
#
# 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.

# This shell is used to auto generate some useful tools for k8s, such as lister,
# informer, deepcopy, defaulter and so on.

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}

# generate the code with:
# --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
# instead of the $GOPATH directly. For normal projects this can be dropped.
${CODEGEN_PKG}/generate-groups.sh "defaulter,deepcopy,client,informer,lister" \
github.com/tensorflow/k8s/pkg/client github.com/tensorflow/k8s/pkg/apis \
tensorflow:v1alpha1

48 changes: 48 additions & 0 deletions hack/verify-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Copyright 2017 The Kubernetes Authors.
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..

DIFFROOT="${SCRIPT_ROOT}/pkg"
TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg"
_tmp="${SCRIPT_ROOT}/_tmp"

cleanup() {
rm -rf "${_tmp}"
}
trap "cleanup" EXIT SIGINT

cleanup

mkdir -p "${TMP_DIFFROOT}"
cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}"

"${SCRIPT_ROOT}/hack/update-codegen.sh"
echo "diffing ${DIFFROOT} against freshly generated codegen"
ret=0
diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$?
cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}"
if [[ $ret -eq 0 ]]
then
echo "${DIFFROOT} up to date."
else
echo "${DIFFROOT} is out of date. Please run hack/update-codegen.sh"
exit 1
fi
95 changes: 95 additions & 0 deletions pkg/apis/tensorflow/helper/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package helper

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tfv1 "github.com/tensorflow/k8s/pkg/apis/tensorflow/v1alpha1"
"fmt"
"k8s.io/api/core/v1"
"github.com/tensorflow/k8s/pkg/util"
)

// AsOwner make OwnerReference according to the parameter
func AsOwner(tfJob *tfv1.TfJob) metav1.OwnerReference {
trueVar := true
// Both api.OwnerReference and metatypes.OwnerReference are combined into that.
return metav1.OwnerReference{
APIVersion: tfJob.APIVersion,
Kind: tfJob.Kind,
Name: tfJob.ObjectMeta.Name,
UID: tfJob.ObjectMeta.UID,
Controller: &trueVar,
BlockOwnerDeletion: &trueVar,
}
}

// ConfigureAcceleratorsForTfJobSpec adds any accelerator specific configuration to the pods.
func ConfigureAcceleratorsForTfJobSpec(c *tfv1.TfJobSpec, accelerators map[string]tfv1.AcceleratorConfig) error {
for _, r := range c.ReplicaSpecs {
if r.Template == nil {
return fmt.Errorf("Replica is missing Template; %v", util.Pformat(r))
}
for i, c := range r.Template.Spec.Containers {
if c.Name == string(tfv1.TENSORFLOW) {
// Identify the accelerators attached to this container.
a := map[string]tfv1.AcceleratorConfig{}

lists := []v1.ResourceList{c.Resources.Limits, c.Resources.Requests}
for _, resources := range lists {
for name, _ := range resources {

if _, ok := accelerators[string(name)]; !ok {
continue
}

// Add the expected mounts to the pods.
a[string(name)] = accelerators[string(name)]
}
}

// Add accelerator information to the pod.
for _, config := range a {
for _, v := range config.Volumes {
r.Template.Spec.Volumes = append(r.Template.Spec.Volumes,
v1.Volume{
Name: v.Name,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: v.HostPath,
},
},
})
c.VolumeMounts = append(c.VolumeMounts, v1.VolumeMount{
Name: v.Name,
MountPath: v.MountPath,
})
}

for _, envVar := range config.EnvVars {
c.Env = append(c.Env, v1.EnvVar{
Name: envVar.Name,
Value: envVar.Value,
})
}
}
r.Template.Spec.Containers[i] = c
break
}
}
}
return nil
}

// Cleanup cleans up user passed spec, e.g. defaulting, transforming fields.
// TODO: move this to admission controller
func Cleanup(c *tfv1.TfJobSpec) {
// TODO(jlewi): Add logic to cleanup user provided spec; e.g. by filling in defaults.
// We should have default container images so user doesn't have to provide these.
}

func CRDName() string {
return fmt.Sprintf("%s.%s", tfv1.CRDKindPlural, tfv1.CRDGroup)
}

func scalingReason(from, to int) string {
return fmt.Sprintf("Current cluster size: %d, desired cluster size: %d", from, to)
}
Loading

0 comments on commit ed7cae7

Please sign in to comment.