-
Notifications
You must be signed in to change notification settings - Fork 440
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
feat: Refactor to make it easy to extend new kinds #865
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 @@ | ||
package v1alpha3 | ||
|
||
import ( | ||
"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
const ( | ||
// JobNameLabel represents the label key for the job name, the value is job name | ||
JobNameLabel = "job-name" | ||
// JobRoleLabel represents the label key for the job role, e.g. the value is master | ||
JobRoleLabel = "job-role" | ||
// TFJobRoleLabel is deprecated in kubeflow 0.7, but we need to be compatible. | ||
TFJobRoleLabel = "tf-job-role" | ||
// PyTorchJobRoleLabel is deprecated in kubeflow 0.7, but we need to be compatible. | ||
PyTorchJobRoleLabel = "pytorch-job-role" | ||
) | ||
|
||
// JobRoleMap is the map which is used to determin if the replica is master. | ||
// Katib will inject metrics collector into master replica. | ||
var JobRoleMap = map[string][]string{ | ||
// Job kind does not support distributed training, thus no master. | ||
consts.JobKindJob: {}, | ||
consts.JobKindTF: {JobRoleLabel, TFJobRoleLabel}, | ||
consts.JobKindPyTorch: {JobRoleLabel, PyTorchJobRoleLabel}, | ||
} | ||
|
||
// GetSupportedJobList returns the list of the supported jobs' GVK. | ||
func GetSupportedJobList() []schema.GroupVersionKind { | ||
supportedJobList := []schema.GroupVersionKind{ | ||
{ | ||
Group: consts.JobGroupJob, | ||
Version: consts.JobVersionJob, | ||
Kind: consts.JobKindJob, | ||
}, | ||
{ | ||
Group: consts.JobGroupKubeflow, | ||
Version: consts.JobVersionTF, | ||
Kind: consts.JobKindTF, | ||
}, | ||
{ | ||
Group: consts.JobGroupKubeflow, | ||
Version: consts.JobVersionPyTorch, | ||
Kind: consts.JobKindPyTorch, | ||
}, | ||
} | ||
return supportedJobList | ||
} |
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,68 @@ | ||
package job | ||
|
||
import ( | ||
"fmt" | ||
|
||
commonv1 "github.com/kubeflow/tf-operator/pkg/apis/common/v1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
) | ||
|
||
var ( | ||
log = logf.Log.WithName("provider-job") | ||
) | ||
|
||
// Job is the provider of Job kind. | ||
type Job struct{} | ||
|
||
// GetDeployedJobStatus get the deployed job status. | ||
func (j Job) GetDeployedJobStatus( | ||
deployedJob *unstructured.Unstructured) (*commonv1.JobCondition, error) { | ||
jobCondition := commonv1.JobCondition{} | ||
// Set default type to running. | ||
jobCondition.Type = commonv1.JobRunning | ||
status, ok, unerr := unstructured.NestedFieldCopy(deployedJob.Object, "status") | ||
if !ok { | ||
if unerr != nil { | ||
log.Error(unerr, "NestedFieldCopy unstructured to status error") | ||
return nil, unerr | ||
} | ||
err := fmt.Errorf("value is missing") | ||
log.Error(err, "NestedFieldCopy unstructured to status error") | ||
return nil, err | ||
} | ||
|
||
statusMap := status.(map[string]interface{}) | ||
jobStatus := batchv1.JobStatus{} | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(statusMap, &jobStatus) | ||
if err != nil { | ||
log.Error(err, "Convert unstructured to status error") | ||
return nil, err | ||
} | ||
for _, cond := range jobStatus.Conditions { | ||
if cond.Type == batchv1.JobComplete && cond.Status == corev1.ConditionTrue { | ||
jobCondition.Type = commonv1.JobSucceeded | ||
// JobConditions message not populated when succeeded for batchv1 Job | ||
break | ||
} | ||
if cond.Type == batchv1.JobFailed && cond.Status == corev1.ConditionTrue { | ||
jobCondition.Type = commonv1.JobFailed | ||
jobCondition.Message = cond.Message | ||
break | ||
} | ||
} | ||
return &jobCondition, nil | ||
} | ||
|
||
// IsTrainingContainer returns if the c is the actual training container. | ||
func (j Job) IsTrainingContainer(index int, c corev1.Container) bool { | ||
if index == 0 { | ||
// for Job worker, the first container will be taken as worker container, | ||
// katib document should note it | ||
return true | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gaocegege Just wondering why don't we keep all job related constants in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot do it since it will cause import cycle. If we defined consts in job/v1alpha3, job/v1alpha3 uses job/v1alph3/job, job/v1alph3/job uses consts defined in job/v1alpha3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work for you? @johnugeorge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that we have defined too many constants :)