Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add Core fields to the Scheduler Container Spec #1616

Merged
merged 1 commit into from
Mar 12, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
- (Feature) Add Core fields to the Scheduler Container Spec

## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
- (Feature) Extract Scheduler API
Expand Down
440 changes: 440 additions & 0 deletions docs/api/ArangoMLExtension.V1Alpha1.md

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions docs/api/ArangoMLStorage.V1Alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ Default Value: `/`

***

### .spec.mode.sidecar.args

Type: `array` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/container/resources/core.go#L50)</sup>

Arguments to the entrypoint.
The container image's CMD is used if this is not provided.
Variable references $(VAR_NAME) are expanded using the container's environment. If a variable
cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced
to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will
produce the string literal "$(VAR_NAME)". Escaped references will never be expanded, regardless
of whether the variable exists or not. Cannot be updated.

Links:
* [Kubernetes Docs](https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell)

***

### .spec.mode.sidecar.command

Type: `array` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/container/resources/core.go#L40)</sup>

Entrypoint array. Not executed within a shell.
The container image's ENTRYPOINT is used if this is not provided.
Variable references $(VAR_NAME) are expanded using the container's environment. If a variable
cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced
to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will
produce the string literal "$(VAR_NAME)". Escaped references will never be expanded, regardless
of whether the variable exists or not. Cannot be updated.

Links:
* [Kubernetes Docs](https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell)

***

### .spec.mode.sidecar.controllerListenPort

Type: `integer` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/ml/v1alpha1/storage_spec_mode_sidecar.go#L36)</sup>
Expand Down Expand Up @@ -258,6 +292,16 @@ Type: `[]core.VolumeMount` <sup>[\[ref\]](https://github.com/arangodb/kube-arang

VolumeMounts keeps list of pod volumes to mount into the container's filesystem.

***

### .spec.mode.sidecar.workingDir

Type: `string` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/container/resources/core.go#L55)</sup>

Container's working directory.
If not specified, the container runtime's default will be used, which
might be configured in the container image.

## Status

### .status.conditions
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/scheduler/v1alpha1/container/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (c Containers) Validate() error {
var _ interfaces.Container[Container] = &Container{}

type Container struct {
// Core keeps the core settings for Container
*schedulerContainerResourcesApi.Core `json:",inline"`

// Security keeps the security settings for Container
*schedulerContainerResourcesApi.Security `json:",inline"`

Expand Down Expand Up @@ -135,6 +138,7 @@ func (c *Container) Apply(template *core.PodTemplateSpec, container *core.Contai
}

return shared.WithErrors(
c.Core.Apply(template, container),
c.Security.Apply(template, container),
c.Environments.Apply(template, container),
c.Image.Apply(template, container),
Expand All @@ -146,6 +150,14 @@ func (c *Container) Apply(template *core.PodTemplateSpec, container *core.Contai
)
}

func (c *Container) GetCore() *schedulerContainerResourcesApi.Core {
if c == nil || c.Core == nil {
return nil
}

return c.Core
}

func (c *Container) GetImage() *schedulerContainerResourcesApi.Image {
if c == nil || c.Image == nil {
return nil
Expand Down Expand Up @@ -224,6 +236,7 @@ func (c *Container) With(other *Container) *Container {
}

return &Container{
Core: c.Core.With(other.Core),
Security: c.Security.With(other.Security),
Environments: c.Environments.With(other.Environments),
Image: c.Image.With(other.Image),
Expand All @@ -241,6 +254,7 @@ func (c *Container) Validate() error {
}

return shared.WithErrors(
shared.PrefixResourceErrors("core", c.Core.Validate()),
shared.PrefixResourceErrors("containerSecurity", c.Security.Validate()),
shared.PrefixResourceErrors("containerEnvironments", c.Environments.Validate()),
shared.PrefixResourceErrors("containerResources", c.Image.Validate()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func Test_Container(t *testing.T) {
require.Nil(t, spec.Image)
require.Nil(t, spec.Security)
require.Nil(t, spec.Environments)
require.Nil(t, spec.VolumeMounts)
require.Nil(t, spec.Core)

require.Len(t, container.Env, 0)
})
Expand All @@ -100,12 +102,17 @@ func Test_Container(t *testing.T) {
require.Nil(t, spec.Image)
require.Nil(t, spec.Security)
require.Nil(t, spec.Environments)
require.Nil(t, spec.VolumeMounts)
require.Nil(t, spec.Core)

require.Len(t, container.Env, 0)
})
})
t.Run("With fields", func(t *testing.T) {
applyContainer(t, &core.PodTemplateSpec{}, &core.Container{}, &Container{
Core: &schedulerContainerResourcesApi.Core{
Args: []string{"A"},
},
Security: &schedulerContainerResourcesApi.Security{
SecurityContext: &core.SecurityContext{
RunAsUser: util.NewType[int64](50),
Expand Down Expand Up @@ -171,6 +178,12 @@ func Test_Container(t *testing.T) {
},
})(func(t *testing.T, pod *core.PodTemplateSpec, container *core.Container, spec *Container) {
// Spec
require.NotNil(t, spec.Core)
require.NotNil(t, spec.Core.Args)
require.Contains(t, spec.Core.Args, "A")
require.Empty(t, spec.Core.Command)
require.Empty(t, spec.Core.WorkingDir)

require.NotNil(t, spec.Resources)
require.NotNil(t, spec.Resources.Resources)
require.Contains(t, spec.Resources.Resources.Limits, core.ResourceCPU)
Expand Down Expand Up @@ -225,6 +238,9 @@ func Test_Container_YAML(t *testing.T) {
securityContext:
runAsUser: 50

args:
- A

env:
- name: key1
value: value1
Expand Down Expand Up @@ -264,6 +280,12 @@ securityContext:
runAsUser: 10
`)(func(t *testing.T, pod *core.PodTemplateSpec, container *core.Container, spec *Container) {
// Spec
require.NotNil(t, spec.Core)
require.NotNil(t, spec.Core.Args)
require.Contains(t, spec.Core.Args, "A")
require.Empty(t, spec.Core.Command)
require.Empty(t, spec.Core.WorkingDir)

require.NotNil(t, spec.Resources)
require.NotNil(t, spec.Resources.Resources)
require.Contains(t, spec.Resources.Resources.Limits, core.ResourceCPU)
Expand Down
42 changes: 28 additions & 14 deletions pkg/apis/scheduler/v1alpha1/container/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ var _ interfaces.Pod[Generic] = &Generic{}
type Generic struct {
// Environments keeps the environment variables for Container
*schedulerContainerResourcesApi.Environments `json:",inline"`

// VolumeMounts define volume mounts assigned to the Container
*schedulerContainerResourcesApi.VolumeMounts `json:",inline"`
}

func (c *Generic) Apply(template *core.PodTemplateSpec) error {
if c == nil {
func (g *Generic) Apply(template *core.PodTemplateSpec) error {
if g == nil {
return nil
}

for id := range template.Spec.Containers {
if err := shared.WithErrors(
c.Environments.Apply(template, &template.Spec.Containers[id]),
g.Environments.Apply(template, &template.Spec.Containers[id]),
g.VolumeMounts.Apply(template, &template.Spec.Containers[id]),
); err != nil {
return err
}
Expand All @@ -51,38 +55,48 @@ func (c *Generic) Apply(template *core.PodTemplateSpec) error {
return nil
}

func (c *Generic) GetEnvironments() *schedulerContainerResourcesApi.Environments {
if c == nil || c.Environments == nil {
func (g *Generic) GetEnvironments() *schedulerContainerResourcesApi.Environments {
if g == nil || g.Environments == nil {
return nil
}

return g.Environments
}

func (g *Generic) GetVolumeMounts() *schedulerContainerResourcesApi.VolumeMounts {
if g == nil || g.VolumeMounts == nil {
return nil
}

return c.Environments
return g.VolumeMounts
}

func (c *Generic) With(other *Generic) *Generic {
if c == nil && other == nil {
func (g *Generic) With(other *Generic) *Generic {
if g == nil && other == nil {
return nil
}

if c == nil {
if g == nil {
return other.DeepCopy()
}

if other == nil {
return c.DeepCopy()
return g.DeepCopy()
}

return &Generic{
Environments: c.Environments.With(other.Environments),
Environments: g.Environments.With(other.Environments),
VolumeMounts: g.VolumeMounts.With(other.VolumeMounts),
}
}

func (c *Generic) Validate() error {
if c == nil {
func (g *Generic) Validate() error {
if g == nil {
return nil
}

return shared.WithErrors(
shared.PrefixResourceErrors("containerEnvironments", c.Environments.Validate()),
shared.PrefixResourceErrors("containerEnvironments", g.Environments.Validate()),
shared.PrefixResourceErrors("volumeMounts", g.VolumeMounts.Validate()),
)
}
14 changes: 14 additions & 0 deletions pkg/apis/scheduler/v1alpha1/container/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ func Test_Generic(t *testing.T) {
t.Run("Nil", func(t *testing.T) {
applyGeneric(t, nil, nil)(func(t *testing.T, pod *core.PodTemplateSpec, spec *Generic) {
require.Nil(t, spec.Environments)
require.Nil(t, spec.VolumeMounts)
})
})
t.Run("Empty template", func(t *testing.T) {
applyGeneric(t, &core.PodTemplateSpec{})(func(t *testing.T, pod *core.PodTemplateSpec, spec *Generic) {
require.Nil(t, spec.Environments)
require.Nil(t, spec.VolumeMounts)
})
})
t.Run("With fields", func(t *testing.T) {
Expand All @@ -99,7 +101,19 @@ func Test_Generic(t *testing.T) {
},
},
},
VolumeMounts: &schedulerContainerResourcesApi.VolumeMounts{
VolumeMounts: []core.VolumeMount{
{
Name: "TEST",
MountPath: "/data",
},
},
},
})(func(t *testing.T, pod *core.PodTemplateSpec, spec *Generic) {
require.NotNil(t, spec.VolumeMounts)
require.Len(t, spec.VolumeMounts.VolumeMounts, 1)
require.EqualValues(t, "TEST", spec.VolumeMounts.VolumeMounts[0].Name)

// Spec
require.NotNil(t, spec.Environments)
require.Len(t, spec.Environments.Env, 1)
Expand Down
86 changes: 86 additions & 0 deletions pkg/apis/scheduler/v1alpha1/container/resources/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package resources

import (
core "k8s.io/api/core/v1"

"github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1alpha1/interfaces"
)

var _ interfaces.Container[Core] = &Core{}

type Core struct {
// Entrypoint array. Not executed within a shell.
// The container image's ENTRYPOINT is used if this is not provided.
// Variable references $(VAR_NAME) are expanded using the container's environment. If a variable
// cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced
// to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will
// produce the string literal "$(VAR_NAME)". Escaped references will never be expanded, regardless
// of whether the variable exists or not. Cannot be updated.
// +doc/link: Kubernetes Docs|https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
Command []string `json:"command,omitempty"`

// Arguments to the entrypoint.
// The container image's CMD is used if this is not provided.
// Variable references $(VAR_NAME) are expanded using the container's environment. If a variable
// cannot be resolved, the reference in the input string will be unchanged. Double $$ are reduced
// to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will
// produce the string literal "$(VAR_NAME)". Escaped references will never be expanded, regardless
// of whether the variable exists or not. Cannot be updated.
// +doc/link: Kubernetes Docs|https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
Args []string `json:"args,omitempty"`

// Container's working directory.
// If not specified, the container runtime's default will be used, which
// might be configured in the container image.
WorkingDir string `json:"workingDir,omitempty"`
}

func (c *Core) Apply(_ *core.PodTemplateSpec, container *core.Container) error {
if c == nil {
return nil
}

d := c.DeepCopy()

container.Args = d.Args
container.Command = d.Command
container.WorkingDir = d.WorkingDir

return nil
}

func (c *Core) With(other *Core) *Core {
if c == nil && other == nil {
return nil
}

if other == nil {
return c.DeepCopy()
}

return other.DeepCopy()
}

func (c *Core) Validate() error {
return nil
}
Loading