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

Move Pods/Services control interface to separate folder #72

Merged
merged 2 commits into from
Apr 15, 2020
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
k8s.io/client-go v0.16.9-beta.0
k8s.io/code-generator v0.15.10
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30
k8s.io/kubernetes v1.18.0
k8s.io/kubernetes v1.16.2
volcano.sh/volcano v0.4.0
)

Expand All @@ -39,7 +39,7 @@ replace (
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.15.10
k8s.io/kube-proxy => k8s.io/kube-proxy v0.15.10
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.15.10
k8s.io/kubectl => k8s.io/kubectl v0.15.11-beta.0
k8s.io/kubectl => k8s.io/kubectl v0.15.10
k8s.io/kubelet => k8s.io/kubelet v0.15.10
k8s.io/kubernetes => k8s.io/kubernetes v1.15.10
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.15.10
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller.v1/common/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package common

import (
"fmt"
"github.com/kubeflow/common/pkg/controller.v1/control"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -437,14 +438,14 @@ func (jc *JobController) createNewPod(job interface{}, rt, index string, spec *a

func (jc *JobController) createPodWithControllerRef(namespace string, template *v1.PodTemplateSpec,
controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
if err := validateControllerRef(controllerRef); err != nil {
if err := control.ValidateControllerRef(controllerRef); err != nil {
return err
}
return jc.createPod("", namespace, template, controllerObject, controllerRef)
}

func (jc *JobController) createPod(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
pod, err := GetPodFromTemplate(template, object, controllerRef)
pod, err := control.GetPodFromTemplate(template, object, controllerRef)
pod.Namespace = namespace
if err != nil {
return err
Expand All @@ -456,7 +457,7 @@ func (jc *JobController) createPod(nodeName, namespace string, template *v1.PodT
return fmt.Errorf("unable to create pods, no labels")
}
if err := jc.Controller.CreatePod(object, pod); err != nil {
jc.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreatePodReason, "Error creating: %v", err)
jc.Recorder.Eventf(object, v1.EventTypeWarning, control.FailedCreatePodReason, "Error creating: %v", err)
return err
} else {
logger := commonutil.LoggerForPod(pod, jc.Controller.GetAPIGroupVersionKind().Kind)
Expand All @@ -466,7 +467,7 @@ func (jc *JobController) createPod(nodeName, namespace string, template *v1.PodT
return nil
}
logger.Infof("Controller %v created pod %v", accessor.GetName(), pod.Name)
jc.Recorder.Eventf(object, v1.EventTypeNormal, SuccessfulCreatePodReason, "Created pod: %v", pod.Name)
jc.Recorder.Eventf(object, v1.EventTypeNormal, control.SuccessfulCreatePodReason, "Created pod: %v", pod.Name)
}
return nil
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller.v1/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package common

import (
"fmt"
"github.com/kubeflow/common/pkg/controller.v1/control"
"strconv"
"strings"

Expand Down Expand Up @@ -283,7 +284,7 @@ func (jc *JobController) CreateNewService(job metav1.Object, rtype apiv1.Replica
}

func (jc *JobController) CreateServicesWithControllerRef(namespace string, service *v1.Service, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
if err := validateControllerRef(controllerRef); err != nil {
if err := control.ValidateControllerRef(controllerRef); err != nil {
return err
}
return jc.createServices(namespace, service, controllerObject, controllerRef)
Expand All @@ -293,16 +294,16 @@ func (jc *JobController) createServices(namespace string, service *v1.Service, o
if labels.Set(service.Labels).AsSelectorPreValidated().Empty() {
return fmt.Errorf("unable to create Services, no labels")
}
serviceWithOwner, err := getServiceFromTemplate(service, object, controllerRef)
serviceWithOwner, err := control.GetServiceFromTemplate(service, object, controllerRef)
serviceWithOwner.Namespace = namespace
if err != nil {
jc.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreateServiceReason, "Error creating: %v", err)
jc.Recorder.Eventf(object, v1.EventTypeWarning, control.FailedCreateServiceReason, "Error creating: %v", err)
return fmt.Errorf("unable to create services: %v", err)
}

err = jc.Controller.CreateService(object, serviceWithOwner)
if err != nil {
jc.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreateServiceReason, "Error creating: %v", err)
jc.Recorder.Eventf(object, v1.EventTypeWarning, control.FailedCreateServiceReason, "Error creating: %v", err)
return fmt.Errorf("unable to create services: %v", err)
}

Expand All @@ -312,7 +313,7 @@ func (jc *JobController) createServices(namespace string, service *v1.Service, o
return nil
}
log.Infof("Controller %v created service %v", accessor.GetName(), serviceWithOwner.Name)
jc.Recorder.Eventf(object, v1.EventTypeNormal, SuccessfulCreateServiceReason, "Created service: %v", serviceWithOwner.Name)
jc.Recorder.Eventf(object, v1.EventTypeNormal, control.SuccessfulCreateServiceReason, "Created service: %v", serviceWithOwner.Name)

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package control

import (
"fmt"
Expand Down Expand Up @@ -79,14 +79,14 @@ func (r RealPodControl) CreatePods(namespace string, template *v1.PodTemplateSpe
}

func (r RealPodControl) CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
if err := validateControllerRef(controllerRef); err != nil {
if err := ValidateControllerRef(controllerRef); err != nil {
return err
}
return r.createPods("", namespace, template, controllerObject, controllerRef)
}

func (r RealPodControl) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
if err := validateControllerRef(controllerRef); err != nil {
if err := ValidateControllerRef(controllerRef); err != nil {
return err
}
return r.createPods(nodeName, namespace, template, object, controllerRef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package control

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package control

import (
"fmt"
Expand Down Expand Up @@ -49,25 +49,6 @@ type ServiceControlInterface interface {
DeleteService(namespace, serviceID string, object runtime.Object) error
}

func validateControllerRef(controllerRef *metav1.OwnerReference) error {
if controllerRef == nil {
return fmt.Errorf("controllerRef is nil")
}
if len(controllerRef.APIVersion) == 0 {
return fmt.Errorf("controllerRef has empty APIVersion")
}
if len(controllerRef.Kind) == 0 {
return fmt.Errorf("controllerRef has empty Kind")
}
if controllerRef.Controller == nil || !*controllerRef.Controller {
return fmt.Errorf("controllerRef.Controller is not set to true")
}
if controllerRef.BlockOwnerDeletion == nil || !*controllerRef.BlockOwnerDeletion {
return fmt.Errorf("controllerRef.BlockOwnerDeletion is not set")
}
return nil
}

// RealServiceControl is the default implementation of ServiceControlInterface.
type RealServiceControl struct {
KubeClient clientset.Interface
Expand All @@ -84,7 +65,7 @@ func (r RealServiceControl) CreateServices(namespace string, service *v1.Service
}

func (r RealServiceControl) CreateServicesWithControllerRef(namespace string, service *v1.Service, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
if err := validateControllerRef(controllerRef); err != nil {
if err := ValidateControllerRef(controllerRef); err != nil {
return err
}
return r.createServices(namespace, service, controllerObject, controllerRef)
Expand All @@ -94,7 +75,7 @@ func (r RealServiceControl) createServices(namespace string, service *v1.Service
if labels.Set(service.Labels).AsSelectorPreValidated().Empty() {
return fmt.Errorf("unable to create Services, no labels")
}
serviceWithOwner, err := getServiceFromTemplate(service, object, controllerRef)
serviceWithOwner, err := GetServiceFromTemplate(service, object, controllerRef)
if err != nil {
r.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreateServiceReason, "Error creating: %v", err)
return fmt.Errorf("unable to create services: %v", err)
Expand Down Expand Up @@ -205,11 +186,3 @@ func (f *FakeServiceControl) Clear() {
f.CreateLimit = 0
f.CreateCallCount = 0
}

func getServiceFromTemplate(template *v1.Service, parentObject runtime.Object, controllerRef *metav1.OwnerReference) (*v1.Service, error) {
service := template.DeepCopy()
if controllerRef != nil {
service.OwnerReferences = append(service.OwnerReferences, *controllerRef)
}
return service, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package control

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package control

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package control

import (
testutilv1 "github.com/kubeflow/common/test_job/test_util/v1"
Expand Down
35 changes: 35 additions & 0 deletions pkg/controller.v1/control/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package control

import (
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func ValidateControllerRef(controllerRef *metav1.OwnerReference) error {
if controllerRef == nil {
return fmt.Errorf("controllerRef is nil")
}
if len(controllerRef.APIVersion) == 0 {
return fmt.Errorf("controllerRef has empty APIVersion")
}
if len(controllerRef.Kind) == 0 {
return fmt.Errorf("controllerRef has empty Kind")
}
if controllerRef.Controller == nil || !*controllerRef.Controller {
return fmt.Errorf("controllerRef.Controller is not set to true")
}
if controllerRef.BlockOwnerDeletion == nil || !*controllerRef.BlockOwnerDeletion {
return fmt.Errorf("controllerRef.BlockOwnerDeletion is not set")
}
return nil
}

func GetServiceFromTemplate(template *v1.Service, parentObject runtime.Object, controllerRef *metav1.OwnerReference) (*v1.Service, error) {
service := template.DeepCopy()
if controllerRef != nil {
service.OwnerReferences = append(service.OwnerReferences, *controllerRef)
}
return service, nil
}