Skip to content

Commit

Permalink
Merge pull request #1580 from cliveseldon/1561_compentspec_labels
Browse files Browse the repository at this point in the history
Ensure all labels and annotations added to created deployments
  • Loading branch information
ukclivecox authored Mar 26, 2020
2 parents 10662af + 72a4b49 commit 24e0f57
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 5 deletions.
31 changes: 31 additions & 0 deletions notebooks/resources/model_with_labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: machinelearning.seldon.io/v1
kind: SeldonDeployment
metadata:
name: seldon-model
spec:
name: test-deployment
annotations:
ann1: value1
predictors:
- componentSpecs:
- spec:
containers:
- image: seldonio/mock_classifier_rest:1.3
name: classifier
metadata:
annotations:
ann3: value3
labels:
label2: value2
graph:
children: []
endpoint:
type: REST
name: classifier
type: MODEL
name: example
replicas: 1
labels:
label1: value1
annotations:
ann2: value2
29 changes: 24 additions & 5 deletions operator/controllers/seldondeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,10 @@ func createContainerService(deploy *appsv1.Deployment, p machinelearningv1.Predi
func createDeploymentWithoutEngine(depName string, seldonId string, seldonPodSpec *machinelearningv1.SeldonPodSpec, p *machinelearningv1.PredictorSpec, mlDep *machinelearningv1.SeldonDeployment) *appsv1.Deployment {
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: depName,
Namespace: getNamespace(mlDep),
Labels: map[string]string{machinelearningv1.Label_seldon_id: seldonId, "app": depName, "fluentd": "true"},
Name: depName,
Namespace: getNamespace(mlDep),
Labels: map[string]string{machinelearningv1.Label_seldon_id: seldonId, "app": depName, "fluentd": "true"},
Annotations: map[string]string{},
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
Expand All @@ -757,7 +758,7 @@ func createDeploymentWithoutEngine(depName string, seldonId string, seldonPodSpe
machinelearningv1.Label_app: depName,
machinelearningv1.Label_fluentd: "true",
},
Annotations: mlDep.Spec.Annotations,
Annotations: map[string]string{},
},
},
Strategy: appsv1.DeploymentStrategy{RollingUpdate: &appsv1.RollingUpdateDeployment{MaxUnavailable: &intstr.IntOrString{StrVal: "10%"}}},
Expand All @@ -775,10 +776,21 @@ func createDeploymentWithoutEngine(depName string, seldonId string, seldonPodSpe
deploy.Spec.Template.ObjectMeta.Labels[machinelearningv1.Label_shadow] = "true"
}

//Add annotations from top level
for k, v := range mlDep.Spec.Annotations {
deploy.Annotations[k] = v
deploy.Spec.Template.ObjectMeta.Annotations[k] = v
}
// Add annottaions from predictor
for k, v := range p.Annotations {
deploy.Annotations[k] = v
deploy.Spec.Template.ObjectMeta.Annotations[k] = v
}
if seldonPodSpec != nil {
deploy.Spec.Template.Spec = seldonPodSpec.Spec
// add more annotations
// add more annotations from metadata
for k, v := range seldonPodSpec.Metadata.Annotations {
deploy.Annotations[k] = v
deploy.Spec.Template.ObjectMeta.Annotations[k] = v
}
}
Expand All @@ -788,6 +800,13 @@ func createDeploymentWithoutEngine(depName string, seldonId string, seldonPodSpe
deploy.ObjectMeta.Labels[k] = v
deploy.Spec.Template.ObjectMeta.Labels[k] = v
}
// add labels from podSpec metadata
if seldonPodSpec != nil {
for k, v := range seldonPodSpec.Metadata.Labels {
deploy.ObjectMeta.Labels[k] = v
deploy.Spec.Template.ObjectMeta.Labels[k] = v
}
}

//Add some default to help with diffs in controller
if deploy.Spec.Template.Spec.RestartPolicy == "" {
Expand Down
103 changes: 103 additions & 0 deletions operator/controllers/seldondeployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"path/filepath"
"testing"
"time"
)

Expand Down Expand Up @@ -530,3 +531,105 @@ var _ = Describe("Create a Seldon Deployment with hpa", func() {
})

})

// --- Non Ginkgo Tests

func TestCreateDeploymentWithLabelsAndAnnotations(t *testing.T) {
g := NewGomegaWithT(t)
depName := "dep"
labelKey1 := "key1"
labelValue1 := "value1"
labelKey2 := "key2"
labelValue2 := "value2"
annotationKey1 := "key1"
annotationValue1 := "value1"
annotationKey2 := "key2"
annotationValue2 := "value2"
annotationKey3 := "key3"
annotationValue3 := "value3"
modelType := machinelearningv1.MODEL
instance := &machinelearningv1.SeldonDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: depName,
Namespace: "default",
},
Spec: machinelearningv1.SeldonDeploymentSpec{
Name: "mydep",
Annotations: map[string]string{annotationKey1: annotationValue1},
Predictors: []machinelearningv1.PredictorSpec{
{
Name: "p1",
Labels: map[string]string{labelKey1: labelValue1},
Annotations: map[string]string{annotationKey2: annotationValue2},
ComponentSpecs: []*machinelearningv1.SeldonPodSpec{
{
Metadata: metav1.ObjectMeta{
Labels: map[string]string{labelKey2: labelValue2},
Annotations: map[string]string{annotationKey3: annotationValue3},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Image: "seldonio/mock_classifier:1.0",
Name: "classifier",
},
},
},
},
},
Graph: &machinelearningv1.PredictiveUnit{
Name: "classifier",
Type: &modelType,
},
},
},
},
}

dep := createDeploymentWithoutEngine(depName, "a", instance.Spec.Predictors[0].ComponentSpecs[0], &instance.Spec.Predictors[0], instance)
g.Expect(dep.Labels[labelKey1]).To(Equal(labelValue1))
g.Expect(dep.Labels[labelKey2]).To(Equal(labelValue2))
g.Expect(dep.Spec.Template.ObjectMeta.Labels[labelKey1]).To(Equal(labelValue1))
g.Expect(dep.Spec.Template.ObjectMeta.Labels[labelKey2]).To(Equal(labelValue2))
g.Expect(dep.Annotations[annotationKey1]).To(Equal(annotationValue1))
g.Expect(dep.Annotations[annotationKey2]).To(Equal(annotationValue2))
g.Expect(dep.Annotations[annotationKey3]).To(Equal(annotationValue3))
}

func TestCreateDeploymentWithNoLabelsAndAnnotations(t *testing.T) {
depName := "dep"
modelType := machinelearningv1.MODEL
instance := &machinelearningv1.SeldonDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: depName,
Namespace: "default",
},
Spec: machinelearningv1.SeldonDeploymentSpec{
Name: "mydep",
Predictors: []machinelearningv1.PredictorSpec{
{
Name: "p1",
ComponentSpecs: []*machinelearningv1.SeldonPodSpec{
{
Metadata: metav1.ObjectMeta{},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Image: "seldonio/mock_classifier:1.0",
Name: "classifier",
},
},
},
},
},
Graph: &machinelearningv1.PredictiveUnit{
Name: "classifier",
Type: &modelType,
},
},
},
},
}

_ = createDeploymentWithoutEngine(depName, "a", instance.Spec.Predictors[0].ComponentSpecs[0], &instance.Spec.Predictors[0], instance)
}

0 comments on commit 24e0f57

Please sign in to comment.