Skip to content

Commit

Permalink
sync-notebook needs to look for a Deployment kind that is version ind…
Browse files Browse the repository at this point in the history
…ependent (kubeflow#2194)

Signed-off-by: Abhilash Pallerlamudi <[email protected]>
  • Loading branch information
stpabhi authored and k8s-ci-robot committed Jan 4, 2019
1 parent fda860f commit 47162b3
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 1 deletion.
74 changes: 74 additions & 0 deletions kubeflow/common/util.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,80 @@
return:: aux(a, b, 0, 0, []) tailstrict,
}.return,

groupByResource(resources):: {
local getKey(resource) = {
return::
resource.kind,
}.return,
local getValue(resource) = {
return::
{ [resource.metadata.name]+: resource },
}.return,
return:: util.foldl(getKey, getValue, resources),
}.return,

comparator(a, b):: {
return::
if a.metadata.name == b.metadata.name then
0
else
if a.metadata.name < b.metadata.name then
-1
else
1,
}.return,

validateResource(resource):: {
return::
if std.type(resource) == "object" &&
std.objectHas(resource, "kind") &&
std.objectHas(resource, "apiVersion") &&
std.objectHas(resource, "metadata") &&
std.objectHas(resource.metadata, "name") then
true
else
false,
}.return,

extractGroups(obj)::
if std.type(obj) == "object" then
[obj[key] for key in std.objectFields(obj)]
else
[],

extractResources(group)::
if std.type(group) == "object" then
[group[key] for key in std.objectFields(group)]
else
[],

curryResources(resources, exists):: {
local existingResource(resource) = {
local resourceExists(kind, name) = {
return::
if std.objectHas(resources, kind) &&
std.objectHas(resources[kind], name) then
true
else
false,
}.return,
return::
if util.validateResource(resource) then
resourceExists(resource.kind, resource.metadata.name)
else
false,
}.return,
local missingResource(resource) = {
return::
existingResource(resource) == false,
}.return,
return::
if exists == true then
existingResource
else
missingResource,
}.return,

// Produce a list of manifests. obj must be an array
list(obj):: k.core.v1.list.new(obj,),
}
1 change: 1 addition & 0 deletions kubeflow/jupyter/notebooks.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
data: {
"sync-notebook.jsonnet": (importstr "sync-notebook.jsonnet"),
"util.libsonnet": (importstr "kubeflow/jupyter/util.libsonnet"),
},
},
notebooksConfigMap:: notebooksConfigMap,
Expand Down
8 changes: 7 additions & 1 deletion kubeflow/jupyter/sync-notebook.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// - Service
// - Pod
function(request) {
local util = import "util.libsonnet",
local sharedNamespace = request.controller.metadata.annotations.namespace,
local templateSpec = request.parent.spec.template.spec,
local podTemplateSpec = {
Expand Down Expand Up @@ -115,7 +116,12 @@ function(request) {
},
},
],
children: children,
local validatedChildren = util.sort(std.filter(util.validateResource, children), util.comparator),
local requestedChildren = std.flattenArrays(std.map(util.extractResources, util.extractGroups(request.children))),
local groupedRequestedChildren = util.groupByResource(requestedChildren),
local missingChildren = util.sort(std.filter(util.curryResources(groupedRequestedChildren, false), validatedChildren), util.comparator),
local desired = requestedChildren + missingChildren,
children: desired,
status: {
phase: "Active",
conditions: [{
Expand Down
1 change: 1 addition & 0 deletions kubeflow/jupyter/tests/notebooks_test.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ std.assertEqual(
apiVersion: "v1",
data: {
"sync-notebook.jsonnet": (importstr "../sync-notebook.jsonnet"),
"util.libsonnet": (importstr "kubeflow/jupyter/util.libsonnet"),
},
kind: "ConfigMap",
metadata: {
Expand Down
179 changes: 179 additions & 0 deletions kubeflow/jupyter/util.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Some useful routines.
// Duplicating kubeflow/common/util.libsonnet for unit tests to work.
// For lambda metacontroller it's available via ConfigMap.
{
local k = import "k.libsonnet",
local util = self,

// Convert a string to lower case.
lower:: function(x) {
local cp(c) = std.codepoint(c),
local lowerLetter(c) = if cp(c) >= 65 && cp(c) < 91 then
std.char(cp(c) + 32)
else c,
result:: std.join("", std.map(lowerLetter, std.stringChars(x))),
}.result,

// Convert non-boolean types like string,number to a boolean.
// This is primarily intended for dealing with parameters that should be booleans.
toBool:: function(x) {
result::
if std.type(x) == "boolean" then
x
else if std.type(x) == "string" then
std.asciiUpper(x) == "TRUE"
else if std.type(x) == "number" then
x != 0
else
false,
}.result,

// Convert a comma-delimited string to an Array
toArray:: function(str) {
local trim(str) = {
rest::
if std.startsWith(str, " ") then
std.substr(str, 1, std.length(str) - 1)
else
str,
}.rest,
result::
if std.type(str) == "string" && str != "null" && std.length(str) > 0 then
std.map(trim, std.split(str, ","))
else [],
}.result,

foldl:: function(key, value, objs) {
local aux(arr, i, running) =
if i >= std.length(arr) then
running
else
aux(arr, i + 1, running { [key(arr[i])]+: value(arr[i]) }) tailstrict,
return:: aux(objs, 0, {},),
}.return,

sort:: function(arr, compare=function(a, b) {
return::
if a == b then
0
else if a < b then
-1
else
1,
}.return) {
local l = std.length(arr),
local f = {
local pivot = arr[0],
local rest = std.makeArray(l - 1, function(i) arr[i + 1]),
local left = std.filter(function(x) compare(x, pivot) <= 0, rest),
local right = std.filter(function(x) compare(x, pivot) > 0, rest),
return:: util.sort(left, compare) + [pivot] + util.sort(right, compare),
}.return,
return::
if std.length(arr) == 0 then
[]
else
f,
}.return,

setDiff:: function(a, b, compare=function(a, b) {
return::
if a == b then
0
else if a < b then
-1
else
1,
}.return) {
local aux(a, b, i, j, acc) =
if i >= std.length(a) then
acc
else if j >= std.length(b) then
aux(a, b, i + 1, j, acc + [a[i]]) tailstrict
else
if compare(a[i], b[j]) == 0 then
aux(a, b, i + 1, j + 1, acc) tailstrict
else if compare(a[i], b[j]) == -1 then
aux(a, b, i + 1, j, acc + [a[i]]) tailstrict
else
aux(a, b, i, j + 1, acc) tailstrict,
return:: aux(a, b, 0, 0, []) tailstrict,
}.return,

groupByResource(resources):: {
local getKey(resource) = {
return::
resource.kind,
}.return,
local getValue(resource) = {
return::
{ [resource.metadata.name]+: resource },
}.return,
return:: util.foldl(getKey, getValue, resources),
}.return,

comparator(a, b):: {
return::
if a.metadata.name == b.metadata.name then
0
else
if a.metadata.name < b.metadata.name then
-1
else
1,
}.return,

validateResource(resource):: {
return::
if std.type(resource) == "object" &&
std.objectHas(resource, "kind") &&
std.objectHas(resource, "apiVersion") &&
std.objectHas(resource, "metadata") &&
std.objectHas(resource.metadata, "name") then
true
else
false,
}.return,

extractGroups(obj)::
if std.type(obj) == "object" then
[obj[key] for key in std.objectFields(obj)]
else
[],

extractResources(group)::
if std.type(group) == "object" then
[group[key] for key in std.objectFields(group)]
else
[],

curryResources(resources, exists):: {
local existingResource(resource) = {
local resourceExists(kind, name) = {
return::
if std.objectHas(resources, kind) &&
std.objectHas(resources[kind], name) then
true
else
false,
}.return,
return::
if util.validateResource(resource) then
resourceExists(resource.kind, resource.metadata.name)
else
false,
}.return,
local missingResource(resource) = {
return::
existingResource(resource) == false,
}.return,
return::
if exists == true then
existingResource
else
missingResource,
}.return,

// Produce a list of manifests. obj must be an array
list(obj):: k.core.v1.list.new(obj,),
}

0 comments on commit 47162b3

Please sign in to comment.