Unverified Commit df07be9e authored by Shawna Monero's avatar Shawna Monero Committed by GitHub
Browse files

k8s: adding more fields to cronjob proto (#1063)

* adding some fields to cronjob proto

* fixing concurrency policy enum

* fixing concurrency policy enum

* fixed ProtoForCronJob func

* fixed test

* addressing daniel's comments

* removed wrappers as per daniels suggestion
parent 17c633ed
Showing with 1075 additions and 729 deletions
+1075 -729
......@@ -7,6 +7,7 @@ option go_package = "github.com/lyft/clutch/backend/api/k8s/v1;k8sv1";
import "google/api/annotations.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
import "validate/validate.proto";
import "api/v1/annotations.proto";
......@@ -651,6 +652,20 @@ message CronJob {
map<string, string> labels = 5;
map<string, string> annotations = 6;
bool suspend = 7;
int32 num_active_jobs = 8;
enum ConcurrencyPolicy {
UNSPECIFIED = 0;
UNKNOWN = 1;
ALLOW = 2;
FORBID = 3;
REPLACE = 4;
}
ConcurrencyPolicy concurrency_policy = 9;
google.protobuf.Int64Value starting_deadline_seconds = 10;
}
message DescribeCronJobRequest {
......
This diff is collapsed.
......@@ -3441,6 +3441,22 @@ func (m *CronJob) Validate() error {
// no validation rules for Annotations
// no validation rules for Suspend
// no validation rules for NumActiveJobs
// no validation rules for ConcurrencyPolicy
if v, ok := interface{}(m.GetStartingDeadlineSeconds()).(interface{ Validate() error }); ok {
if err := v.Validate(); err != nil {
return CronJobValidationError{
field: "StartingDeadlineSeconds",
reason: "embedded message failed validation",
cause: err,
}
}
}
return nil
}
......
......@@ -3,7 +3,9 @@ package k8s
import (
"context"
"fmt"
"strings"
"github.com/golang/protobuf/ptypes/wrappers"
v1beta1 "k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
......@@ -67,7 +69,8 @@ func ProtoForCronJob(cluster string, k8scronJob *v1beta1.CronJob) *k8sapiv1.Cron
if clusterName == "" {
clusterName = cluster
}
return &k8sapiv1.CronJob{
// Required fields
ret := &k8sapiv1.CronJob{
Cluster: clusterName,
Namespace: k8scronJob.Namespace,
Name: k8scronJob.Name,
......@@ -75,4 +78,20 @@ func ProtoForCronJob(cluster string, k8scronJob *v1beta1.CronJob) *k8sapiv1.Cron
Labels: k8scronJob.Labels,
Annotations: k8scronJob.Annotations,
}
// Update optional fields
if k8scronJob.Spec.Suspend != nil {
ret.Suspend = *k8scronJob.Spec.Suspend
}
if k8scronJob.Spec.ConcurrencyPolicy != "" {
ret.ConcurrencyPolicy = k8sapiv1.CronJob_ConcurrencyPolicy(
k8sapiv1.CronJob_ConcurrencyPolicy_value[strings.ToUpper(string(k8scronJob.Spec.ConcurrencyPolicy))])
}
if k8scronJob.Status.Active != nil {
ret.NumActiveJobs = int32(len(k8scronJob.Status.Active))
}
if k8scronJob.Spec.StartingDeadlineSeconds != nil {
ret.StartingDeadlineSeconds = &wrappers.Int64Value{Value: *k8scronJob.Spec.StartingDeadlineSeconds}
}
return ret
}
......@@ -2,10 +2,12 @@ package k8s
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
v1beta1 "k8s.io/api/batch/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
......@@ -91,7 +93,7 @@ func TestProtoForCron(t *testing.T) {
},
},
{
id: "custername is not set",
id: "clustername is not set",
inputClusterName: "staging",
expectedClusterName: "staging",
expectedName: "test2",
......@@ -100,6 +102,15 @@ func TestProtoForCron(t *testing.T) {
ClusterName: "",
Name: "test2",
},
Spec: v1beta1.CronJobSpec{
ConcurrencyPolicy: v1beta1.AllowConcurrent,
Schedule: "5 4 * * *",
Suspend: &[]bool{true}[0],
StartingDeadlineSeconds: &[]int64{69}[0],
},
Status: v1beta1.CronJobStatus{
Active: []v1.ObjectReference{{}, {}},
},
},
},
}
......@@ -112,6 +123,20 @@ func TestProtoForCron(t *testing.T) {
cron := ProtoForCronJob(tt.inputClusterName, tt.cron)
assert.Equal(t, tt.expectedClusterName, cron.Cluster)
assert.Equal(t, tt.expectedName, cron.Name)
assert.Equal(t, tt.cron.Spec.Schedule, cron.Schedule)
if tt.cron.Spec.ConcurrencyPolicy != "" {
assert.Equal(t, strings.ToUpper(string(tt.cron.Spec.ConcurrencyPolicy)), cron.ConcurrencyPolicy.String())
}
if tt.cron.Spec.Suspend != nil {
assert.Equal(t, *tt.cron.Spec.Suspend, cron.Suspend)
}
if tt.cron.Spec.StartingDeadlineSeconds != nil {
assert.Equal(t, *tt.cron.Spec.StartingDeadlineSeconds, cron.StartingDeadlineSeconds.Value)
}
if tt.cron.Status.Active != nil {
assert.Equal(t, int32(len(tt.cron.Status.Active)), cron.NumActiveJobs)
}
})
}
}
......@@ -12427,6 +12427,18 @@ export namespace clutch {
 
/** CronJob annotations */
annotations?: ({ [k: string]: string }|null);
/** CronJob suspend */
suspend?: (boolean|null);
/** CronJob numActiveJobs */
numActiveJobs?: (number|null);
/** CronJob concurrencyPolicy */
concurrencyPolicy?: (clutch.k8s.v1.CronJob.ConcurrencyPolicy|null);
/** CronJob startingDeadlineSeconds */
startingDeadlineSeconds?: (google.protobuf.IInt64Value|null);
}
 
/** Represents a CronJob. */
......@@ -12456,6 +12468,18 @@ export namespace clutch {
/** CronJob annotations. */
public annotations: { [k: string]: string };
 
/** CronJob suspend. */
public suspend: boolean;
/** CronJob numActiveJobs. */
public numActiveJobs: number;
/** CronJob concurrencyPolicy. */
public concurrencyPolicy: clutch.k8s.v1.CronJob.ConcurrencyPolicy;
/** CronJob startingDeadlineSeconds. */
public startingDeadlineSeconds?: (google.protobuf.IInt64Value|null);
/**
* Verifies a CronJob message.
* @param message Plain object to verify
......@@ -12485,6 +12509,18 @@ export namespace clutch {
public toJSON(): { [k: string]: any };
}
 
namespace CronJob {
/** ConcurrencyPolicy enum. */
enum ConcurrencyPolicy {
UNSPECIFIED = 0,
UNKNOWN = 1,
ALLOW = 2,
FORBID = 3,
REPLACE = 4
}
}
/** Properties of a DescribeCronJobRequest. */
interface IDescribeCronJobRequest {
 
......
......@@ -29329,6 +29329,10 @@ export const clutch = $root.clutch = (() => {
* @property {string|null} [schedule] CronJob schedule
* @property {Object.<string,string>|null} [labels] CronJob labels
* @property {Object.<string,string>|null} [annotations] CronJob annotations
* @property {boolean|null} [suspend] CronJob suspend
* @property {number|null} [numActiveJobs] CronJob numActiveJobs
* @property {clutch.k8s.v1.CronJob.ConcurrencyPolicy|null} [concurrencyPolicy] CronJob concurrencyPolicy
* @property {google.protobuf.IInt64Value|null} [startingDeadlineSeconds] CronJob startingDeadlineSeconds
*/
 
/**
......@@ -29396,6 +29400,38 @@ export const clutch = $root.clutch = (() => {
*/
CronJob.prototype.annotations = $util.emptyObject;
 
/**
* CronJob suspend.
* @member {boolean} suspend
* @memberof clutch.k8s.v1.CronJob
* @instance
*/
CronJob.prototype.suspend = false;
/**
* CronJob numActiveJobs.
* @member {number} numActiveJobs
* @memberof clutch.k8s.v1.CronJob
* @instance
*/
CronJob.prototype.numActiveJobs = 0;
/**
* CronJob concurrencyPolicy.
* @member {clutch.k8s.v1.CronJob.ConcurrencyPolicy} concurrencyPolicy
* @memberof clutch.k8s.v1.CronJob
* @instance
*/
CronJob.prototype.concurrencyPolicy = 0;
/**
* CronJob startingDeadlineSeconds.
* @member {google.protobuf.IInt64Value|null|undefined} startingDeadlineSeconds
* @memberof clutch.k8s.v1.CronJob
* @instance
*/
CronJob.prototype.startingDeadlineSeconds = null;
/**
* Verifies a CronJob message.
* @function verify
......@@ -29435,6 +29471,28 @@ export const clutch = $root.clutch = (() => {
if (!$util.isString(message.annotations[key[i]]))
return "annotations: string{k:string} expected";
}
if (message.suspend != null && message.hasOwnProperty("suspend"))
if (typeof message.suspend !== "boolean")
return "suspend: boolean expected";
if (message.numActiveJobs != null && message.hasOwnProperty("numActiveJobs"))
if (!$util.isInteger(message.numActiveJobs))
return "numActiveJobs: integer expected";
if (message.concurrencyPolicy != null && message.hasOwnProperty("concurrencyPolicy"))
switch (message.concurrencyPolicy) {
default:
return "concurrencyPolicy: enum value expected";
case 0:
case 1:
case 2:
case 3:
case 4:
break;
}
if (message.startingDeadlineSeconds != null && message.hasOwnProperty("startingDeadlineSeconds")) {
let error = $root.google.protobuf.Int64Value.verify(message.startingDeadlineSeconds);
if (error)
return "startingDeadlineSeconds." + error;
}
return null;
};
 
......@@ -29472,6 +29530,37 @@ export const clutch = $root.clutch = (() => {
for (let keys = Object.keys(object.annotations), i = 0; i < keys.length; ++i)
message.annotations[keys[i]] = String(object.annotations[keys[i]]);
}
if (object.suspend != null)
message.suspend = Boolean(object.suspend);
if (object.numActiveJobs != null)
message.numActiveJobs = object.numActiveJobs | 0;
switch (object.concurrencyPolicy) {
case "UNSPECIFIED":
case 0:
message.concurrencyPolicy = 0;
break;
case "UNKNOWN":
case 1:
message.concurrencyPolicy = 1;
break;
case "ALLOW":
case 2:
message.concurrencyPolicy = 2;
break;
case "FORBID":
case 3:
message.concurrencyPolicy = 3;
break;
case "REPLACE":
case 4:
message.concurrencyPolicy = 4;
break;
}
if (object.startingDeadlineSeconds != null) {
if (typeof object.startingDeadlineSeconds !== "object")
throw TypeError(".clutch.k8s.v1.CronJob.startingDeadlineSeconds: object expected");
message.startingDeadlineSeconds = $root.google.protobuf.Int64Value.fromObject(object.startingDeadlineSeconds);
}
return message;
};
 
......@@ -29497,6 +29586,10 @@ export const clutch = $root.clutch = (() => {
object.namespace = "";
object.name = "";
object.schedule = "";
object.suspend = false;
object.numActiveJobs = 0;
object.concurrencyPolicy = options.enums === String ? "UNSPECIFIED" : 0;
object.startingDeadlineSeconds = null;
}
if (message.cluster != null && message.hasOwnProperty("cluster"))
object.cluster = message.cluster;
......@@ -29517,6 +29610,14 @@ export const clutch = $root.clutch = (() => {
for (let j = 0; j < keys2.length; ++j)
object.annotations[keys2[j]] = message.annotations[keys2[j]];
}
if (message.suspend != null && message.hasOwnProperty("suspend"))
object.suspend = message.suspend;
if (message.numActiveJobs != null && message.hasOwnProperty("numActiveJobs"))
object.numActiveJobs = message.numActiveJobs;
if (message.concurrencyPolicy != null && message.hasOwnProperty("concurrencyPolicy"))
object.concurrencyPolicy = options.enums === String ? $root.clutch.k8s.v1.CronJob.ConcurrencyPolicy[message.concurrencyPolicy] : message.concurrencyPolicy;
if (message.startingDeadlineSeconds != null && message.hasOwnProperty("startingDeadlineSeconds"))
object.startingDeadlineSeconds = $root.google.protobuf.Int64Value.toObject(message.startingDeadlineSeconds, options);
return object;
};
 
......@@ -29531,6 +29632,26 @@ export const clutch = $root.clutch = (() => {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};
 
/**
* ConcurrencyPolicy enum.
* @name clutch.k8s.v1.CronJob.ConcurrencyPolicy
* @enum {number}
* @property {number} UNSPECIFIED=0 UNSPECIFIED value
* @property {number} UNKNOWN=1 UNKNOWN value
* @property {number} ALLOW=2 ALLOW value
* @property {number} FORBID=3 FORBID value
* @property {number} REPLACE=4 REPLACE value
*/
CronJob.ConcurrencyPolicy = (function() {
const valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "UNSPECIFIED"] = 0;
values[valuesById[1] = "UNKNOWN"] = 1;
values[valuesById[2] = "ALLOW"] = 2;
values[valuesById[3] = "FORBID"] = 3;
values[valuesById[4] = "REPLACE"] = 4;
return values;
})();
return CronJob;
})();
 
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment