-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable istio sidecar injection in Suggestion and Training Jobs (#1050)
* Add istio sidecar inject annotation to Suggestion pod and all training jobs * Add annotation to Training Jobs from Controller * Fix comment
- Loading branch information
1 parent
e3c615d
commit f98f7c9
Showing
4 changed files
with
116 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
suggestionsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/suggestions/v1alpha3" | ||
"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts" | ||
pytorchv1 "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1" | ||
tfv1 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1" | ||
batchv1 "k8s.io/api/batch/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("util-annotations") | ||
) | ||
|
||
// SuggestionAnnotations returns the expected suggestion annotations. | ||
func SuggestionAnnotations(instance *suggestionsv1alpha3.Suggestion) map[string]string { | ||
return appendAnnotation( | ||
instance.Annotations, | ||
consts.AnnotationIstioSidecarInjectName, | ||
consts.AnnotationIstioSidecarInjectValue) | ||
} | ||
|
||
// TrainingJobAnnotations adds annotations to unstructured job. | ||
func TrainingJobAnnotations(desiredJob *unstructured.Unstructured) error { | ||
kind := desiredJob.GetKind() | ||
switch kind { | ||
case consts.JobKindJob: | ||
batchJob := &batchv1.Job{} | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(desiredJob.Object, &batchJob) | ||
if err != nil { | ||
log.Error(err, "Convert unstructured to job error") | ||
return err | ||
} | ||
batchJob.Spec.Template.ObjectMeta.Annotations = appendAnnotation( | ||
batchJob.Spec.Template.ObjectMeta.Annotations, | ||
consts.AnnotationIstioSidecarInjectName, | ||
consts.AnnotationIstioSidecarInjectValue) | ||
desiredJob.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(batchJob) | ||
if err != nil { | ||
log.Error(err, "Convert job to unstructured error") | ||
return err | ||
} | ||
return nil | ||
case consts.JobKindTF: | ||
tfJob := &tfv1.TFJob{} | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(desiredJob.Object, &tfJob) | ||
if err != nil { | ||
log.Error(err, "Convert unstructured to TFJob error") | ||
return err | ||
} | ||
for _, replicaSpec := range tfJob.Spec.TFReplicaSpecs { | ||
replicaSpec.Template.ObjectMeta.Annotations = appendAnnotation( | ||
replicaSpec.Template.ObjectMeta.Annotations, | ||
consts.AnnotationIstioSidecarInjectName, | ||
consts.AnnotationIstioSidecarInjectValue) | ||
} | ||
desiredJob.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(tfJob) | ||
if err != nil { | ||
log.Error(err, "Convert TFJob to unstructured error") | ||
return err | ||
} | ||
return nil | ||
case consts.JobKindPyTorch: | ||
pytorchJob := &pytorchv1.PyTorchJob{} | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(desiredJob.Object, &pytorchJob) | ||
if err != nil { | ||
log.Error(err, "Convert unstructured to PytorchJob error") | ||
return err | ||
} | ||
for _, replicaSpec := range pytorchJob.Spec.PyTorchReplicaSpecs { | ||
replicaSpec.Template.ObjectMeta.Annotations = appendAnnotation( | ||
replicaSpec.Template.ObjectMeta.Annotations, | ||
consts.AnnotationIstioSidecarInjectName, | ||
consts.AnnotationIstioSidecarInjectValue) | ||
} | ||
desiredJob.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(pytorchJob) | ||
if err != nil { | ||
log.Error(err, "Convert PytorchJob to unstructured error") | ||
return err | ||
} | ||
return nil | ||
default: | ||
return fmt.Errorf("Invalid Katib Training Job kind %v", kind) | ||
} | ||
|
||
} | ||
|
||
func appendAnnotation(annotations map[string]string, newAnnotationName string, newAnnotationValue string) map[string]string { | ||
res := make(map[string]string) | ||
for k, v := range annotations { | ||
res[k] = v | ||
} | ||
res[newAnnotationName] = newAnnotationValue | ||
|
||
return res | ||
} |