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] ArangoProfile Selectors #1627

Merged
merged 1 commit into from
Mar 22, 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
Expand Up @@ -9,6 +9,7 @@
- (Feature) Discover Namespace in DebugPackage from K8S
- (Feature) Expose Force CRD Install option
- (Maintenance) Move Container utils functions
- (Feature) ArangoProfile Selectors

## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
- (Feature) Extract Scheduler API
Expand Down
8 changes: 8 additions & 0 deletions docs/api/ArangoMLExtension.V1Alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ Default Value: `false`

***

### .spec.deployment.imagePullSecrets

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

ImagePullSecrets define Secrets used to pull Image from registry

***

### .spec.deployment.labels

Type: `object` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/pod/resources/metadata.go#L39)</sup>
Expand Down
16 changes: 16 additions & 0 deletions docs/api/ArangoProfile.V1Alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ title: ArangoProfile V1Alpha1

## Spec

### .spec.selectors.label

Type: `meta.LabelSelector` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/profile_selectors.go#L32)</sup>

Label keeps information about label selector

***

### .spec.template.container.all.env

Type: `core.EnvVar` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/container/resources/environments.go#L36)</sup>
Expand Down Expand Up @@ -281,6 +289,14 @@ Default Value: `false`

***

### .spec.template.pod.imagePullSecrets

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

ImagePullSecrets define Secrets used to pull Image from registry

***

### .spec.template.pod.labels

Type: `object` <sup>[\[ref\]](https://github.com/arangodb/kube-arangodb/blob/1.2.39/pkg/apis/scheduler/v1alpha1/pod/resources/metadata.go#L39)</sup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func (r *Resources) Apply(_ *core.PodTemplateSpec, template *core.Container) err
return nil
}

template.Resources = r.GetResources()
res := r.GetResources()

template.Resources.Limits = kresources.UpscaleContainerResourceList(template.Resources.Limits, res.Limits)
template.Resources.Requests = kresources.UpscaleContainerResourceList(template.Resources.Requests, res.Requests)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (v *VolumeMounts) Apply(_ *core.PodTemplateSpec, container *core.Container)

obj := v.DeepCopy()

container.VolumeMounts = obj.VolumeMounts
container.VolumeMounts = kresources.MergeVolumeMounts(container.VolumeMounts, obj.VolumeMounts...)

return nil
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/scheduler/v1alpha1/pod/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Pod struct {
// Metadata keeps the metadata settings for Pod
*schedulerPodResourcesApi.Metadata `json:",inline"`

// Image keeps the image information
*schedulerPodResourcesApi.Image `json:",inline"`

// Scheduling keeps the scheduling information
*schedulerPodResourcesApi.Scheduling `json:",inline"`

Expand Down Expand Up @@ -65,6 +68,7 @@ func (a *Pod) With(other *Pod) *Pod {

return &Pod{
Scheduling: a.Scheduling.With(other.Scheduling),
Image: a.Image.With(other.Image),
Namespace: a.Namespace.With(other.Namespace),
Security: a.Security.With(other.Security),
Volumes: a.Volumes.With(other.Volumes),
Expand All @@ -80,6 +84,7 @@ func (a *Pod) Apply(template *core.PodTemplateSpec) error {

return shared.WithErrors(
a.Scheduling.Apply(template),
a.Image.Apply(template),
a.Namespace.Apply(template),
a.Security.Apply(template),
a.Volumes.Apply(template),
Expand All @@ -96,6 +101,14 @@ func (a *Pod) GetSecurity() *schedulerPodResourcesApi.Security {
return a.Security
}

func (a *Pod) GetImage() *schedulerPodResourcesApi.Image {
if a == nil {
return nil
}

return a.Image
}

func (a *Pod) GetScheduling() *schedulerPodResourcesApi.Scheduling {
if a == nil {
return nil
Expand Down Expand Up @@ -142,6 +155,7 @@ func (a *Pod) Validate() error {
}
return shared.WithErrors(
a.Scheduling.Validate(),
a.Image.Validate(),
a.Namespace.Validate(),
a.Security.Validate(),
a.Volumes.Validate(),
Expand Down
87 changes: 87 additions & 0 deletions pkg/apis/scheduler/v1alpha1/pod/resources/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// 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"
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
)

type ImagePullSecrets []string

var _ interfaces.Pod[Image] = &Image{}

type Image struct {
// ImagePullSecrets define Secrets used to pull Image from registry
ImagePullSecrets ImagePullSecrets `json:"imagePullSecrets,omitempty"`
}

func (i *Image) Apply(pod *core.PodTemplateSpec) error {
if i == nil {
return nil
}

for _, secret := range i.ImagePullSecrets {
if hasImagePullSecret(pod.Spec.ImagePullSecrets, secret) {
continue
}

pod.Spec.ImagePullSecrets = append(pod.Spec.ImagePullSecrets, core.LocalObjectReference{
Name: secret,
})
}

return nil
}

func (i *Image) With(other *Image) *Image {
if i == nil && other == nil {
return nil
}

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

return other.DeepCopy()
}

func (i *Image) Validate() error {
if i == nil {
return nil
}

return shared.WithErrors(
shared.PrefixResourceErrors("pullSecrets", shared.ValidateList(i.ImagePullSecrets, shared.ValidateResourceName)),
)
}

func hasImagePullSecret(secrets []core.LocalObjectReference, secret string) bool {
for _, sec := range secrets {
if sec.Name == secret {
return true
}
}

return false
}
88 changes: 88 additions & 0 deletions pkg/apis/scheduler/v1alpha1/pod/resources/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// 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 (
"testing"

"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
)

func applyImage(t *testing.T, template *core.PodTemplateSpec, ns ...*Image) func(in func(t *testing.T, pod *core.PodTemplateSpec)) {
var i *Image

for _, n := range ns {
require.NoError(t, n.Validate())

i = i.With(n)

require.NoError(t, i.Validate())
}

template = template.DeepCopy()

if template == nil {
template = &core.PodTemplateSpec{}
}

require.NoError(t, i.Apply(template))

return func(in func(t *testing.T, spec *core.PodTemplateSpec)) {
t.Run("Validate", func(t *testing.T) {
in(t, template)
})
}
}

func Test_Image(t *testing.T) {
t.Run("With Nil", func(t *testing.T) {
applyImage(t, nil, nil)(func(t *testing.T, pod *core.PodTemplateSpec) {
require.Len(t, pod.Spec.ImagePullSecrets, 0)
})
})
t.Run("With Empty", func(t *testing.T) {
applyImage(t, &core.PodTemplateSpec{})(func(t *testing.T, pod *core.PodTemplateSpec) {
require.Len(t, pod.Spec.ImagePullSecrets, 0)
})
})
t.Run("With PS", func(t *testing.T) {
applyImage(t, &core.PodTemplateSpec{}, &Image{
ImagePullSecrets: []string{
"secret",
},
})(func(t *testing.T, pod *core.PodTemplateSpec) {
require.Len(t, pod.Spec.ImagePullSecrets, 1)
require.Equal(t, "secret", pod.Spec.ImagePullSecrets[0].Name)
})
})
t.Run("With PS2", func(t *testing.T) {
applyImage(t, &core.PodTemplateSpec{}, &Image{
ImagePullSecrets: []string{
"secret",
"secret",
},
})(func(t *testing.T, pod *core.PodTemplateSpec) {
require.Len(t, pod.Spec.ImagePullSecrets, 1)
require.Equal(t, "secret", pod.Spec.ImagePullSecrets[0].Name)
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (s *ServiceAccount) Apply(template *core.PodTemplateSpec) error {
c := s.DeepCopy()

template.Spec.ServiceAccountName = c.ServiceAccountName
template.Spec.AutomountServiceAccountToken = c.AutomountServiceAccountToken
if c.AutomountServiceAccountToken != nil {
template.Spec.AutomountServiceAccountToken = c.AutomountServiceAccountToken
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/scheduler/v1alpha1/pod/resources/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (v *Volumes) Apply(template *core.PodTemplateSpec) error {

obj := v.DeepCopy()

template.Spec.Volumes = obj.Volumes
template.Spec.Volumes = kresources.MergeVolumes(template.Spec.Volumes, obj.Volumes...)

return nil
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/scheduler/v1alpha1/pod/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading