|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package api |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + |
| 22 | + "github.com/golang/glog" |
| 23 | + "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1" |
| 24 | + "github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha2" |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | +) |
| 28 | + |
| 29 | +//PodGroupConditionType is of string type which represents podGroup Condition |
| 30 | +type PodGroupConditionType string |
| 31 | + |
| 32 | +const ( |
| 33 | + //PodGroupUnschedulableType represents unschedulable podGroup condition |
| 34 | + PodGroupUnschedulableType PodGroupConditionType = "Unschedulable" |
| 35 | +) |
| 36 | + |
| 37 | +// PodGroupPhase is the phase of a pod group at the current time. |
| 38 | +type PodGroupPhase string |
| 39 | + |
| 40 | +// These are the valid phase of podGroups. |
| 41 | +const ( |
| 42 | + //PodGroupVersionV1Alpha1 represents PodGroupVersion of V1Alpha1 |
| 43 | + PodGroupVersionV1Alpha1 string = "v1alpha1" |
| 44 | + |
| 45 | + //PodGroupVersionV1Alpha2 represents PodGroupVersion of V1Alpha2 |
| 46 | + PodGroupVersionV1Alpha2 string = "v1alpha2" |
| 47 | + // PodPending means the pod group has been accepted by the system, but scheduler can not allocate |
| 48 | + // enough resources to it. |
| 49 | + PodGroupPending PodGroupPhase = "Pending" |
| 50 | + |
| 51 | + // PodRunning means `spec.minMember` pods of PodGroups has been in running phase. |
| 52 | + PodGroupRunning PodGroupPhase = "Running" |
| 53 | + |
| 54 | + // PodGroupUnknown means part of `spec.minMember` pods are running but the other part can not |
| 55 | + // be scheduled, e.g. not enough resource; scheduler will wait for related controller to recover it. |
| 56 | + PodGroupUnknown PodGroupPhase = "Unknown" |
| 57 | +) |
| 58 | + |
| 59 | +// PodGroupCondition contains details for the current state of this pod group. |
| 60 | +type PodGroupCondition struct { |
| 61 | + // Type is the type of the condition |
| 62 | + Type PodGroupConditionType `json:"type,omitempty" protobuf:"bytes,1,opt,name=type"` |
| 63 | + |
| 64 | + // Status is the status of the condition. |
| 65 | + Status v1.ConditionStatus `json:"status,omitempty" protobuf:"bytes,2,opt,name=status"` |
| 66 | + |
| 67 | + // The ID of condition transition. |
| 68 | + TransitionID string `json:"transitionID,omitempty" protobuf:"bytes,3,opt,name=transitionID"` |
| 69 | + |
| 70 | + // Last time the phase transitioned from another to current phase. |
| 71 | + // +optional |
| 72 | + LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty" protobuf:"bytes,4,opt,name=lastTransitionTime"` |
| 73 | + |
| 74 | + // Unique, one-word, CamelCase reason for the phase's last transition. |
| 75 | + // +optional |
| 76 | + Reason string `json:"reason,omitempty" protobuf:"bytes,5,opt,name=reason"` |
| 77 | + |
| 78 | + // Human-readable message indicating details about last transition. |
| 79 | + // +optional |
| 80 | + Message string `json:"message,omitempty" protobuf:"bytes,6,opt,name=message"` |
| 81 | +} |
| 82 | + |
| 83 | +// PodGroup is a collection of Pod; used for batch workload. |
| 84 | +type PodGroup struct { |
| 85 | + metav1.TypeMeta `json:",inline"` |
| 86 | + // Standard object's metadata. |
| 87 | + // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata |
| 88 | + // +optional |
| 89 | + metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` |
| 90 | + |
| 91 | + // Specification of the desired behavior of the pod group. |
| 92 | + // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status |
| 93 | + // +optional |
| 94 | + Spec PodGroupSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"` |
| 95 | + |
| 96 | + // Status represents the current information about a pod group. |
| 97 | + // This data may not be up to date. |
| 98 | + // +optional |
| 99 | + Status PodGroupStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"` |
| 100 | + |
| 101 | + //Version represents the version of PodGroup |
| 102 | + Version string |
| 103 | +} |
| 104 | + |
| 105 | +// PodGroupSpec represents the template of a pod group. |
| 106 | +type PodGroupSpec struct { |
| 107 | + // MinMember defines the minimal number of members/tasks to run the pod group; |
| 108 | + // if there's not enough resources to start all tasks, the scheduler |
| 109 | + // will not start anyone. |
| 110 | + MinMember int32 `json:"minMember,omitempty" protobuf:"bytes,1,opt,name=minMember"` |
| 111 | + |
| 112 | + // Queue defines the queue to allocate resource for PodGroup; if queue does not exist, |
| 113 | + // the PodGroup will not be scheduled. |
| 114 | + Queue string `json:"queue,omitempty" protobuf:"bytes,2,opt,name=queue"` |
| 115 | + |
| 116 | + // If specified, indicates the PodGroup's priority. "system-node-critical" and |
| 117 | + // "system-cluster-critical" are two special keywords which indicate the |
| 118 | + // highest priorities with the former being the highest priority. Any other |
| 119 | + // name must be defined by creating a PriorityClass object with that name. |
| 120 | + // If not specified, the PodGroup priority will be default or zero if there is no |
| 121 | + // default. |
| 122 | + // +optional |
| 123 | + PriorityClassName string `json:"priorityClassName,omitempty" protobuf:"bytes,3,opt,name=priorityClassName"` |
| 124 | +} |
| 125 | + |
| 126 | +// PodGroupStatus represents the current state of a pod group. |
| 127 | +type PodGroupStatus struct { |
| 128 | + // Current phase of PodGroup. |
| 129 | + Phase PodGroupPhase `json:"phase,omitempty" protobuf:"bytes,1,opt,name=phase"` |
| 130 | + |
| 131 | + // The conditions of PodGroup. |
| 132 | + // +optional |
| 133 | + Conditions []PodGroupCondition `json:"conditions,omitempty" protobuf:"bytes,2,opt,name=conditions"` |
| 134 | + |
| 135 | + // The number of actively running pods. |
| 136 | + // +optional |
| 137 | + Running int32 `json:"running,omitempty" protobuf:"bytes,3,opt,name=running"` |
| 138 | + |
| 139 | + // The number of pods which reached phase Succeeded. |
| 140 | + // +optional |
| 141 | + Succeeded int32 `json:"succeeded,omitempty" protobuf:"bytes,4,opt,name=succeeded"` |
| 142 | + |
| 143 | + // The number of pods which reached phase Failed. |
| 144 | + // +optional |
| 145 | + Failed int32 `json:"failed,omitempty" protobuf:"bytes,5,opt,name=failed"` |
| 146 | +} |
| 147 | + |
| 148 | +//ConvertPodGroupInfoToV1Alpha converts api.PodGroup type to v1alpha1.PodGroup |
| 149 | +func ConvertPodGroupInfoToV1Alpha(pg *PodGroup) (*v1alpha1.PodGroup, error) { |
| 150 | + marshalled, err := json.Marshal(*pg) |
| 151 | + if err != nil { |
| 152 | + glog.Errorf("Failed to Marshal podgroup %s with error: %v", pg.Name, err) |
| 153 | + } |
| 154 | + |
| 155 | + convertedPg := &v1alpha1.PodGroup{} |
| 156 | + err = json.Unmarshal(marshalled, convertedPg) |
| 157 | + if err != nil { |
| 158 | + glog.Errorf("Failed to Unmarshal Data into v1alpha1.PodGroup type with error: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + return convertedPg, nil |
| 162 | +} |
| 163 | + |
| 164 | +//ConvertV1Alpha1ToPodGroupInfo converts v1alpha1.PodGroup to api.PodGroup type |
| 165 | +func ConvertV1Alpha1ToPodGroupInfo(pg *v1alpha1.PodGroup) (*PodGroup, error) { |
| 166 | + marshalled, err := json.Marshal(*pg) |
| 167 | + if err != nil { |
| 168 | + glog.Errorf("Failed to Marshal podgroup %s with error: %v", pg.Name, err) |
| 169 | + } |
| 170 | + |
| 171 | + convertedPg := &PodGroup{} |
| 172 | + err = json.Unmarshal(marshalled, convertedPg) |
| 173 | + if err != nil { |
| 174 | + glog.Errorf("Failed to Unmarshal Data into api.PodGroup type with error: %v", err) |
| 175 | + } |
| 176 | + convertedPg.Version = PodGroupVersionV1Alpha1 |
| 177 | + |
| 178 | + return convertedPg, nil |
| 179 | +} |
| 180 | + |
| 181 | +//ConvertPodGroupInfoToV2Alpha converts api.PodGroup type to v1alpha2.PodGroup |
| 182 | +func ConvertPodGroupInfoToV2Alpha(pg *PodGroup) (*v1alpha2.PodGroup, error) { |
| 183 | + marshalled, err := json.Marshal(*pg) |
| 184 | + if err != nil { |
| 185 | + glog.Errorf("Failed to Marshal podgroup %s with error: %v", pg.Name, err) |
| 186 | + } |
| 187 | + |
| 188 | + convertedPg := &v1alpha2.PodGroup{} |
| 189 | + err = json.Unmarshal(marshalled, convertedPg) |
| 190 | + if err != nil { |
| 191 | + glog.Errorf("Failed to Unmarshal Data into v1alpha2.PodGroup type with error: %v", err) |
| 192 | + } |
| 193 | + |
| 194 | + return convertedPg, nil |
| 195 | +} |
| 196 | + |
| 197 | +//ConvertV1Alpha2ToPodGroupInfo converts v1alpha2.PodGroup to api.PodGroup type |
| 198 | +func ConvertV1Alpha2ToPodGroupInfo(pg *v1alpha2.PodGroup) (*PodGroup, error) { |
| 199 | + marshalled, err := json.Marshal(*pg) |
| 200 | + if err != nil { |
| 201 | + glog.Errorf("Failed to Marshal podgroup %s with error: %v", pg.Name, err) |
| 202 | + } |
| 203 | + |
| 204 | + convertedPg := &PodGroup{} |
| 205 | + err = json.Unmarshal(marshalled, convertedPg) |
| 206 | + if err != nil { |
| 207 | + glog.Errorf("Failed to Unmarshal Data into api.PodGroup type with error: %v", err) |
| 208 | + } |
| 209 | + convertedPg.Version = PodGroupVersionV1Alpha2 |
| 210 | + |
| 211 | + return convertedPg, nil |
| 212 | +} |
0 commit comments