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

controller: Add defaulter #483

Merged
merged 7 commits into from
Mar 21, 2018
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/simple_tf_job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: "kubeflow.org/v1alpha2"
kind: "TFJob"
metadata:
name: "simple-job"
spec:
tfReplicaSpecs:
Worker:
template:
spec:
containers:
- name: worker-busybox
image: busybox
command: ["sleep", "30000"]
6 changes: 5 additions & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 ./vendor/k8s.io/code-ge
# --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" \
${CODEGEN_PKG}/generate-groups.sh "all" \
github.com/kubeflow/tf-operator/pkg/client github.com/kubeflow/tf-operator/pkg/apis \
tensorflow:v1alpha2 \
--go-header-file ${SCRIPT_ROOT}/hack/boilerplate/boilerplate.go.txt

# Notice: The code in code-generator does not generate defaulter by default.
echo "Generating defaulters"
${GOPATH}/bin/defaulter-gen --input-dirs github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2 -O zz_generated.defaults "$@"
3 changes: 3 additions & 0 deletions pkg/apis/tensorflow/v1alpha2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ package v1alpha2
const (
// EnvKubeflowNamespace is ENV for kubeflow namespace specified by user.
EnvKubeflowNamespace = "KUBEFLOW_NAMESPACE"

defaultPortName = "tfjob-port"
defaultPort = 2222
)
55 changes: 55 additions & 0 deletions pkg/apis/tensorflow/v1alpha2/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2018 The Kubeflow 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.

package v1alpha2

import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// Int32 is a helper routine that allocates a new int32 value
// to store v and returns a pointer to it.
func Int32(v int32) *int32 {
return &v
}

func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}

func setDefaultPort(spec *v1.PodSpec) {
for i := range spec.Containers {
if len(spec.Containers[i].Ports) == 0 {
spec.Containers[i].Ports = append(spec.Containers[i].Ports, v1.ContainerPort{
Name: defaultPortName,
ContainerPort: defaultPort,
})
}
}
}

func setDefaultReplicas(spec *TFReplicaSpec) {
if spec.Replicas == nil {
spec.Replicas = Int32(1)
}
}

// SetDefaults_TFJob sets any unspecified values to defaults.
func SetDefaults_TFJob(tfjob *TFJob) {
for _, spec := range tfjob.Spec.TFReplicaSpecs {
setDefaultReplicas(spec)
setDefaultPort(&spec.Template.Spec)
}
}
118 changes: 118 additions & 0 deletions pkg/apis/tensorflow/v1alpha2/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2018 The Kubeflow 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.

package v1alpha2

import (
"reflect"
"testing"

"k8s.io/api/core/v1"
)

const (
testImage = "test-image:latest"
)

func expectedTFJob() *TFJob {
return &TFJob{
Spec: TFJobSpec{
TFReplicaSpecs: map[TFReplicaType]*TFReplicaSpec{
TFReplicaTypeWorker: &TFReplicaSpec{
Replicas: Int32(1),
RestartPolicy: RestartPolicyAlways,
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: testImage,
Ports: []v1.ContainerPort{
v1.ContainerPort{
Name: defaultPortName,
ContainerPort: defaultPort,
},
},
},
},
},
},
},
},
},
}
}

func TestSetDefaultTFJob(t *testing.T) {
testCases := map[string]struct {
original *TFJob
expected *TFJob
}{
"set replicas": {
original: &TFJob{
Spec: TFJobSpec{
TFReplicaSpecs: map[TFReplicaType]*TFReplicaSpec{
TFReplicaTypeWorker: &TFReplicaSpec{
RestartPolicy: RestartPolicyAlways,
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: testImage,
Ports: []v1.ContainerPort{
v1.ContainerPort{
Name: defaultPortName,
ContainerPort: defaultPort,
},
},
},
},
},
},
},
},
},
},
expected: expectedTFJob(),
},
"set default port": {
original: &TFJob{
Spec: TFJobSpec{
TFReplicaSpecs: map[TFReplicaType]*TFReplicaSpec{
TFReplicaTypeWorker: &TFReplicaSpec{
Replicas: Int32(1),
RestartPolicy: RestartPolicyAlways,
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: testImage,
},
},
},
},
},
},
},
},
expected: expectedTFJob(),
},
}

for name, tc := range testCases {
SetDefaults_TFJob(tc.original)
if !reflect.DeepEqual(tc.original, tc.expected) {
t.Errorf("%s: Want\n%v; Got\n %v", name, Pformat(tc.expected), Pformat(tc.original))
}
}
}
1 change: 1 addition & 0 deletions pkg/apis/tensorflow/v1alpha2/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func init() {
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder.Register(addKnownTypes)
localSchemeBuilder.Register(addDefaultingFuncs)
}

// Resource takes an unqualified resource and returns a Group-qualified GroupResource.
Expand Down
35 changes: 35 additions & 0 deletions pkg/apis/tensorflow/v1alpha2/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 The Kubeflow 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.

package v1alpha2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add copyright.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


import (
"encoding/json"
"fmt"

log "github.com/sirupsen/logrus"
)

// Pformat returns a pretty format output of any value that can be marshalled to JSON.
func Pformat(value interface{}) string {
if s, ok := value.(string); ok {
return s
}
valueJSON, err := json.MarshalIndent(value, "", " ")
if err != nil {
log.Warningf("Couldn't pretty format %v, error: %v", value, err)
return fmt.Sprintf("%v", value)
}
return string(valueJSON)
}
45 changes: 45 additions & 0 deletions pkg/apis/tensorflow/v1alpha2/zz_generated.defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build !ignore_autogenerated

/*
Copyright 2018 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 file was autogenerated by defaulter-gen. Do not edit it manually!

package v1alpha2

import (
runtime "k8s.io/apimachinery/pkg/runtime"
)

// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&TFJob{}, func(obj interface{}) { SetObjectDefaults_TFJob(obj.(*TFJob)) })
scheme.AddTypeDefaultingFunc(&TFJobList{}, func(obj interface{}) { SetObjectDefaults_TFJobList(obj.(*TFJobList)) })
return nil
}

func SetObjectDefaults_TFJob(in *TFJob) {
SetDefaults_TFJob(in)
}

func SetObjectDefaults_TFJobList(in *TFJobList) {
for i := range in.Items {
a := &in.Items[i]
SetObjectDefaults_TFJob(a)
}
}
11 changes: 9 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

log "github.com/sirupsen/logrus"

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -199,7 +198,7 @@ func NewTFJobController(

// Set up an event handler for when tfjob resources change.
tfJobInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: tc.enqueueTFJob,
AddFunc: tc.addTFJob,
UpdateFunc: tc.updateTFJob,
// This will enter the sync loop and no-op,
// because the tfjob has been deleted from the store.
Expand Down Expand Up @@ -429,6 +428,14 @@ func genLabels(tfjobKey string) map[string]string {
}
}

// When a pod is added, set the defaults and enqueue the current tfjob.
func (tc *TFJobController) addTFJob(obj interface{}) {
tfjob := obj.(*tfv1alpha2.TFJob)
log.Infof("Adding tfjob: %s", tfjob.Name)
scheme.Scheme.Default(tfjob)
tc.enqueueTFJob(obj)
}

// When a pod is updated, enqueue the current tfjob.
func (tc *TFJobController) updateTFJob(old, cur interface{}) {
oldTFJob := old.(*tfv1alpha2.TFJob)
Expand Down