1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

[Feature] [ML] Use Scheduler API (#1620)

This commit is contained in:
Adam Janikowski 2024-03-18 08:49:35 +01:00 committed by GitHub
parent 2eb83f3cbb
commit a0f255009d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 70 additions and 213 deletions

View file

@ -4,6 +4,7 @@
- (Feature) Add Core fields to the Scheduler Container Spec
- (Feature) Add Metadata fields to the Scheduler Pod Spec
- (Feature) Extend Backup Details in DebugPackage
- (Feature) (ML) Use Scheduler API
## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
- (Feature) Extract Scheduler API

View file

@ -23,6 +23,7 @@ package v1alpha1
import (
schedulerPodApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1alpha1/pod"
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/util"
)
type ProfileTemplate struct {
@ -33,6 +34,50 @@ type ProfileTemplate struct {
Container *ProfileContainerTemplate `json:"container,omitempty"`
}
func (p *ProfileTemplate) GetPod() *schedulerPodApi.Pod {
if p == nil || p.Pod == nil {
return nil
}
return p.Pod
}
func (p *ProfileTemplate) GetContainer() *ProfileContainerTemplate {
if p == nil || p.Container == nil {
return nil
}
return p.Container
}
func (p *ProfileTemplate) GetPriority() int {
if p == nil || p.Priority == nil {
return 0
}
return *p.Priority
}
func (p *ProfileTemplate) With(other *ProfileTemplate) *ProfileTemplate {
if p == nil && other == nil {
return nil
}
if p == nil {
return other.DeepCopy()
}
if other == nil {
return p.DeepCopy()
}
return &ProfileTemplate{
Priority: util.NewType(max(p.GetPriority(), other.GetPriority())),
Pod: p.Pod.With(other.Pod),
Container: p.Container.With(other.Container),
}
}
func (p *ProfileTemplate) Validate() error {
if p == nil {
return nil

View file

@ -25,7 +25,6 @@ import (
core "k8s.io/api/core/v1"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)
@ -33,7 +32,7 @@ type ProfileTemplates []*ProfileTemplate
func (p ProfileTemplates) Sort() ProfileTemplates {
sort.Slice(p, func(i, j int) bool {
if a, b := util.WithDefault(p[i].Priority), util.WithDefault(p[j].Priority); a != b {
if a, b := p[i].GetPriority(), p[j].GetPriority(); a != b {
return a < b
}
@ -43,26 +42,32 @@ func (p ProfileTemplates) Sort() ProfileTemplates {
return p
}
func (p ProfileTemplates) Merge() *ProfileTemplate {
var z *ProfileTemplate
for _, q := range p {
z = z.With(q)
}
return z
}
func (p ProfileTemplates) RenderOnTemplate(pod *core.PodTemplateSpec) error {
t := p.Merge()
// Apply Pod Spec
for id := range p {
if err := p[id].Pod.Apply(pod); err != nil {
return errors.Wrapf(err, "Error while rendering Pod for %d", id)
}
if err := t.GetPod().Apply(pod); err != nil {
return errors.Wrapf(err, "Error while rendering Pod")
}
// Apply Generic Containers Spec
for id := range p {
if err := p[id].Container.ApplyGeneric(pod); err != nil {
return errors.Wrapf(err, "Error while rendering Pod for %d", id)
}
if err := t.GetContainer().ApplyGeneric(pod); err != nil {
return errors.Wrapf(err, "Error while rendering Pod")
}
// Apply Containers Spec
for id := range p {
if err := p[id].Container.ApplyContainers(pod); err != nil {
return errors.Wrapf(err, "Error while rendering Pod for %d", id)
}
if err := t.GetContainer().ApplyContainers(pod); err != nil {
return errors.Wrapf(err, "Error while rendering Pod")
}
return nil

View file

@ -1,84 +0,0 @@
//
// 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 ml
import (
"fmt"
"strings"
core "k8s.io/api/core/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
mlApi "github.com/arangodb/kube-arangodb/pkg/apis/ml/v1alpha1"
sharedApi "github.com/arangodb/kube-arangodb/pkg/apis/shared/v1"
)
func GetJWTAuthFileTokenPath(prefix string) string {
base := "/etc/arangodb/jwt"
if prefix == "" {
return base
}
return fmt.Sprintf("%s-%s", base, prefix)
}
func AddJWTAuthFileToContainers(ext *mlApi.ArangoMLExtension, deployment *api.ArangoDeployment, spec *core.PodTemplateSpec, containers ...*core.Container) {
authSpec := deployment.GetAcceptedSpec().Authentication
if !authSpec.IsAuthenticated() {
return
}
if ext.GetStatus().ArangoDB == nil {
// not ready yet, skip for now
return
}
mountJWTTokenSecret("", ext.GetStatus().ArangoDB.JWTTokenSecret, spec, containers...)
mountJWTTokenSecret("METADATA", ext.GetStatus().MetadataService.JWTTokenSecret, spec, containers...)
}
// mountJWTTokenSecret is assuming that prefix contains only alphanumeric symbols and/or '-'
func mountJWTTokenSecret(prefix string, secret *sharedApi.Object, spec *core.PodTemplateSpec, containers ...*core.Container) {
if secret.IsEmpty() {
return
}
mountName := "deployment-auth-jwt"
if prefix != "" {
mountName = fmt.Sprintf("%s-%s", mountName, strings.ToLower(prefix))
}
spec.Spec.Volumes = append(spec.Spec.Volumes, core.Volume{
Name: mountName,
VolumeSource: core.VolumeSource{
Secret: &core.SecretVolumeSource{
SecretName: secret.GetName(),
},
},
})
for _, container := range containers {
container.VolumeMounts = append(container.VolumeMounts, core.VolumeMount{
Name: mountName,
ReadOnly: true,
MountPath: GetJWTAuthFileTokenPath(prefix),
})
}
}

View file

@ -1,51 +0,0 @@
//
// 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 ml
import (
core "k8s.io/api/core/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
)
func AddTLSToContainers(deployment *api.ArangoDeployment, spec *core.PodTemplateSpec, containers ...*core.Container) {
if !deployment.GetAcceptedSpec().TLS.IsSecure() {
return
}
spec.Spec.Volumes = append(spec.Spec.Volumes, core.Volume{
Name: "deployment-ca",
VolumeSource: core.VolumeSource{
Secret: &core.SecretVolumeSource{
SecretName: resources.GetCASecretName(deployment),
},
},
})
for _, container := range containers {
container.VolumeMounts = append(container.VolumeMounts, core.VolumeMount{
Name: "deployment-ca",
ReadOnly: true,
MountPath: "/etc/arangodb/tls",
})
}
}

View file

@ -1,55 +0,0 @@
//
// 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 ml
import (
core "k8s.io/api/core/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/deployment/pod"
)
func AddJWTFolderToPod(deployment *api.ArangoDeployment, spec *core.PodTemplateSpec, integrationContainer *core.Container) {
if deployment.GetAcceptedSpec().Authentication.IsAuthenticated() {
spec.Spec.Volumes = append(spec.Spec.Volumes, core.Volume{
Name: shared.ClusterJWTSecretVolumeName,
VolumeSource: core.VolumeSource{
Secret: &core.SecretVolumeSource{
SecretName: pod.JWTSecretFolder(deployment.GetName()),
},
},
})
} else {
spec.Spec.Volumes = append(spec.Spec.Volumes, core.Volume{
Name: shared.ClusterJWTSecretVolumeName,
VolumeSource: core.VolumeSource{
EmptyDir: &core.EmptyDirVolumeSource{},
},
})
}
integrationContainer.VolumeMounts = append(integrationContainer.VolumeMounts, core.VolumeMount{
Name: shared.ClusterJWTSecretVolumeName,
ReadOnly: true,
MountPath: shared.ClusterJWTSecretVolumeMountDir,
})
}

View file

@ -23,6 +23,7 @@ package operator
import (
"context"
"fmt"
"runtime/debug"
"github.com/arangodb/kube-arangodb/pkg/operatorV2/operation"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
@ -52,6 +53,10 @@ func (o *operator) processNextItem() bool {
e.Interface("err", obj)
}
if v := debug.Stack(); len(v) != 0 {
e = e.Str("stack", string(v))
}
e.Error("Recovered from panic")
}
}()

View file

@ -383,12 +383,3 @@ func EnsureServiceAccount(ctx context.Context, client kubernetes.Interface, owne
return false, nil
}
func AppendServiceAccount(obj *sharedApi.ServiceAccount, spec *core.PodTemplateSpec) {
if obj == nil || obj.Object == nil || spec == nil {
return
}
spec.Spec.ServiceAccountName = obj.Object.GetName()
spec.Spec.AutomountServiceAccountToken = util.NewType(true)
}