-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the TfJob to use K8s libraries (#215)
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
Showing
563 changed files
with
121,254 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.