Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the TfJob to use K8s libraries #215

Merged
merged 5 commits into from
Dec 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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