diff --git a/go.mod b/go.mod index 7c5017d9..ae90ee0f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 diff --git a/pkg/controller.v1/common/pod.go b/pkg/controller.v1/common/pod.go index fbf79a09..bb360f66 100644 --- a/pkg/controller.v1/common/pod.go +++ b/pkg/controller.v1/common/pod.go @@ -16,6 +16,7 @@ package common import ( "fmt" + "github.com/kubeflow/common/pkg/controller.v1/control" "reflect" "strconv" "strings" @@ -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 @@ -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) @@ -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 } diff --git a/pkg/controller.v1/common/service.go b/pkg/controller.v1/common/service.go index d2155c4e..2d49c2bd 100644 --- a/pkg/controller.v1/common/service.go +++ b/pkg/controller.v1/common/service.go @@ -15,6 +15,7 @@ package common import ( "fmt" + "github.com/kubeflow/common/pkg/controller.v1/control" "strconv" "strings" @@ -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) @@ -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) } @@ -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 } diff --git a/pkg/controller.v1/common/pod_control.go b/pkg/controller.v1/control/pod_control.go similarity index 97% rename from pkg/controller.v1/common/pod_control.go rename to pkg/controller.v1/control/pod_control.go index 74ea85dd..cb4b98bf 100644 --- a/pkg/controller.v1/common/pod_control.go +++ b/pkg/controller.v1/control/pod_control.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package common +package control import ( "fmt" @@ -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) diff --git a/pkg/controller.v1/common/pod_control_test.go b/pkg/controller.v1/control/pod_control_test.go similarity index 99% rename from pkg/controller.v1/common/pod_control_test.go rename to pkg/controller.v1/control/pod_control_test.go index ca280aa9..c6e751d8 100644 --- a/pkg/controller.v1/common/pod_control_test.go +++ b/pkg/controller.v1/control/pod_control_test.go @@ -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" diff --git a/pkg/controller.v1/common/service_control.go b/pkg/controller.v1/control/service_control.go similarity index 86% rename from pkg/controller.v1/common/service_control.go rename to pkg/controller.v1/control/service_control.go index 3441703e..757aa397 100644 --- a/pkg/controller.v1/common/service_control.go +++ b/pkg/controller.v1/control/service_control.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package common +package control import ( "fmt" @@ -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 @@ -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) @@ -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) @@ -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 -} diff --git a/pkg/controller.v1/common/service_control_test.go b/pkg/controller.v1/control/service_control_test.go similarity index 99% rename from pkg/controller.v1/common/service_control_test.go rename to pkg/controller.v1/control/service_control_test.go index be352226..2acbc773 100644 --- a/pkg/controller.v1/common/service_control_test.go +++ b/pkg/controller.v1/control/service_control_test.go @@ -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" diff --git a/pkg/controller.v1/common/service_ref_manager.go b/pkg/controller.v1/control/service_ref_manager.go similarity index 99% rename from pkg/controller.v1/common/service_ref_manager.go rename to pkg/controller.v1/control/service_ref_manager.go index f27472b2..cafc7aa2 100644 --- a/pkg/controller.v1/common/service_ref_manager.go +++ b/pkg/controller.v1/control/service_ref_manager.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package common +package control import ( "fmt" diff --git a/pkg/controller.v1/common/service_ref_manager_test.go b/pkg/controller.v1/control/service_ref_manager_test.go similarity index 99% rename from pkg/controller.v1/common/service_ref_manager_test.go rename to pkg/controller.v1/control/service_ref_manager_test.go index 960c5620..8256ba05 100644 --- a/pkg/controller.v1/common/service_ref_manager_test.go +++ b/pkg/controller.v1/control/service_ref_manager_test.go @@ -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" diff --git a/pkg/controller.v1/control/utils.go b/pkg/controller.v1/control/utils.go new file mode 100644 index 00000000..11949877 --- /dev/null +++ b/pkg/controller.v1/control/utils.go @@ -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 +}