Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Unify the label keys (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jian-he authored and k8s-ci-robot committed Apr 26, 2019
1 parent 48fede2 commit ba975e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 53 deletions.
21 changes: 3 additions & 18 deletions job_controller/job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,9 @@ type ControllerInterface interface {
// Returns the GroupVersion of the API
GetAPIGroupVersion() schema.GroupVersion

// Returns the Group Name(key) in the labels of the job
GetGroupNameLabelKey() string

// Returns the Job Name(key) in the labels of the job
GetJobNameLabelKey() string

// Returns the Group Name(value) in the labels of the job
GetGroupNameLabelValue() string

// Returns the Replica Type(key) in the labels of the job
GetReplicaTypeLabelKey() string

// Returns the Replica Index(value) in the labels of the job
GetReplicaIndexLabelKey() string

// Returns the Job Role(key) in the labels of the job
GetJobRoleKey() string

// Returns the Job from Informer Cache
GetJobFromInformerCache(namespace, name string) (metav1.Object, error)

Expand Down Expand Up @@ -235,8 +220,8 @@ func (jc *JobController) GenOwnerReference(obj metav1.Object) *metav1.OwnerRefer
}

func (jc *JobController) GenLabels(jobName string) map[string]string {
labelGroupName := jc.Controller.GetGroupNameLabelKey()
labelJobName := jc.Controller.GetJobNameLabelKey()
labelGroupName := LabelGroupName
labelJobName := LabelJobName
groupName := jc.Controller.GetGroupNameLabelValue()
return map[string]string{
labelGroupName: groupName,
Expand Down Expand Up @@ -271,7 +256,7 @@ func (jc *JobController) SyncPodGroup(job metav1.Object, minAvailableReplicas in

// SyncPdb will create a PDB for gang scheduling by kube-batch.
func (jc *JobController) SyncPdb(job metav1.Object, minAvailableReplicas int32) (*v1beta1.PodDisruptionBudget, error) {
labelJobName := jc.Controller.GetJobNameLabelKey()
labelJobName := LabelJobName

// Check the pdb exist or not
pdb, err := jc.KubeClientSet.PolicyV1beta1().PodDisruptionBudgets(job.GetNamespace()).Get(job.GetName(), metav1.GetOptions{})
Expand Down
20 changes: 10 additions & 10 deletions job_controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (jc *JobController) AddPod(obj interface{}) {
logger := commonutil.LoggerForPod(pod, jc.Controller.GetAPIGroupVersionKind().Kind)

if job == nil {
if pod.Labels[jc.Controller.GetGroupNameLabelKey()] == jc.Controller.GetGroupNameLabelValue() {
if pod.Labels[LabelGroupName] == jc.Controller.GetGroupNameLabelValue() {
logger.Info("This pod's job does not exist")
}
return
Expand All @@ -77,12 +77,12 @@ func (jc *JobController) AddPod(obj interface{}) {
return
}

if _, ok := pod.Labels[jc.Controller.GetReplicaTypeLabelKey()]; !ok {
if _, ok := pod.Labels[ReplicaTypeLabel]; !ok {
logger.Infof("This pod maybe not created by %v", jc.Controller.ControllerName())
return
}

rtype := pod.Labels[jc.Controller.GetReplicaTypeLabelKey()]
rtype := pod.Labels[ReplicaTypeLabel]
expectationPodsKey := GenExpectationPodsKey(jobKey, rtype)

jc.Expectations.CreationObserved(expectationPodsKey)
Expand Down Expand Up @@ -178,12 +178,12 @@ func (jc *JobController) DeletePod(obj interface{}) {
return
}

if _, ok := pod.Labels[jc.Controller.GetReplicaTypeLabelKey()]; !ok {
if _, ok := pod.Labels[ReplicaTypeLabel]; !ok {
logger.Infof("This pod maybe not created by %v", jc.Controller.ControllerName())
return
}

rtype := pod.Labels[jc.Controller.GetReplicaTypeLabelKey()]
rtype := pod.Labels[ReplicaTypeLabel]
expectationPodsKey := GenExpectationPodsKey(jobKey, rtype)

jc.Expectations.DeletionObserved(expectationPodsKey)
Expand Down Expand Up @@ -235,7 +235,7 @@ func (jc *JobController) FilterPodsForReplicaType(pods []*v1.Pod, replicaType st
MatchLabels: make(map[string]string),
}

replicaSelector.MatchLabels[jc.Controller.GetReplicaTypeLabelKey()] = replicaType
replicaSelector.MatchLabels[ReplicaTypeLabel] = replicaType

for _, pod := range pods {
selector, err := metav1.LabelSelectorAsSelector(replicaSelector)
Expand All @@ -254,11 +254,11 @@ func (jc *JobController) FilterPodsForReplicaType(pods []*v1.Pod, replicaType st
func (jc *JobController) GetPodSlices(pods []*v1.Pod, replicas int, logger *log.Entry) [][]*v1.Pod {
podSlices := make([][]*v1.Pod, replicas)
for _, pod := range pods {
if _, ok := pod.Labels[jc.Controller.GetReplicaIndexLabelKey()]; !ok {
if _, ok := pod.Labels[ReplicaIndexLabel]; !ok {
logger.Warning("The pod do not have the index label.")
continue
}
index, err := strconv.Atoi(pod.Labels[jc.Controller.GetReplicaIndexLabelKey()])
index, err := strconv.Atoi(pod.Labels[ReplicaIndexLabel])
if err != nil {
logger.Warningf("Error when strconv.Atoi: %v", err)
continue
Expand Down Expand Up @@ -375,8 +375,8 @@ func (jc *JobController) createNewPod(job interface{}, rt, index string, spec *c

// Set type and index for the worker.
labels := jc.GenLabels(metaObject.GetName())
labels[jc.Controller.GetReplicaTypeLabelKey()] = rt
labels[jc.Controller.GetReplicaIndexLabelKey()] = index
labels[ReplicaTypeLabel] = rt
labels[ReplicaIndexLabel] = index

if masterRole {
labels[LabelJobRole] = "master"
Expand Down
10 changes: 5 additions & 5 deletions job_controller/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func (jc *JobController) AddService(obj interface{}) {
return
}

if _, ok := service.Labels[jc.Controller.GetReplicaTypeLabelKey()]; !ok {
if _, ok := service.Labels[ReplicaTypeLabel]; !ok {
log.Infof("This service maybe not created by %v", jc.Controller.ControllerName())
return
}

rtype := service.Labels[jc.Controller.GetReplicaTypeLabelKey()]
rtype := service.Labels[ReplicaTypeLabel]
expectationServicesKey := GenExpectationServicesKey(jobKey, rtype)

jc.Expectations.CreationObserved(expectationServicesKey)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (jc *JobController) FilterServicesForReplicaType(services []*v1.Service, re
MatchLabels: make(map[string]string),
}

replicaSelector.MatchLabels[jc.Controller.GetReplicaTypeLabelKey()] = replicaType
replicaSelector.MatchLabels[ReplicaTypeLabel] = replicaType

for _, service := range services {
selector, err := metav1.LabelSelectorAsSelector(replicaSelector)
Expand All @@ -140,11 +140,11 @@ func (jc *JobController) FilterServicesForReplicaType(services []*v1.Service, re
func (jc *JobController) GetServiceSlices(services []*v1.Service, replicas int, logger *log.Entry) [][]*v1.Service {
serviceSlices := make([][]*v1.Service, replicas)
for _, service := range services {
if _, ok := service.Labels[jc.Controller.GetReplicaIndexLabelKey()]; !ok {
if _, ok := service.Labels[ReplicaIndexLabel]; !ok {
logger.Warning("The service do not have the index label.")
continue
}
index, err := strconv.Atoi(service.Labels[jc.Controller.GetReplicaIndexLabelKey()])
index, err := strconv.Atoi(service.Labels[ReplicaIndexLabel])
if err != nil {
logger.Warningf("Error when strconv.Atoi: %v", err)
continue
Expand Down
20 changes: 0 additions & 20 deletions job_controller/test_job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,10 @@ func (TestJobController) GetAPIGroupVersion() schema.GroupVersion {
return testv1.SchemeGroupVersion
}

func (TestJobController) GetGroupNameLabelKey() string {
return LabelGroupName
}

func (TestJobController) GetJobNameLabelKey() string {
return LabelJobName
}

func (TestJobController) GetGroupNameLabelValue() string {
return testv1.GroupName
}

func (TestJobController) GetReplicaTypeLabelKey() string {
return ReplicaTypeLabel
}

func (TestJobController) GetReplicaIndexLabelKey() string {
return ReplicaIndexLabel
}

func (TestJobController) GetJobRoleKey() string {
return LabelJobRole
}

func (t *TestJobController) GetJobFromInformerCache(namespace, name string) (v1.Object, error) {
return t.job, nil
}
Expand Down

0 comments on commit ba975e8

Please sign in to comment.