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

[Improvement] Use stanradized k8s object handlers #1659

Merged
merged 1 commit into from
May 7, 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
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ linters-settings:
- pkg: github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1
alias: schedulerApi
- pkg: github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1/profiles
alias: schedulerProfilesv1beta1
alias: schedulerProfiles
- pkg: github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1/container
alias: schedulerContainerApi
- pkg: github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1/container/resources
Expand Down
52 changes: 52 additions & 0 deletions pkg/util/compare/k8s/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// 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 k8s

import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type FilterFunc[T any] func(in T) T

func FilterP[T any](in *T, filters ...FilterFunc[*T]) *T {
if in == nil {
return nil
}

return Filter(in, filters...)
}

func Filter[T any](in T, filters ...FilterFunc[T]) T {
for _, f := range filters {
in = f(in)
}

return in
}

func ObjectMetaFilter(in meta.ObjectMeta) meta.ObjectMeta {
return meta.ObjectMeta{
Labels: in.Labels,
Annotations: in.Annotations,
OwnerReferences: in.OwnerReferences,
Finalizers: in.Finalizers,
}
}
57 changes: 46 additions & 11 deletions pkg/util/compare/k8s/service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-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.
Expand All @@ -21,21 +21,56 @@
package k8s

import (
"fmt"

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

"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/helpers"
)

func ChecksumService(s *core.Service) (string, error) {
return checksumServiceSpec(&s.Spec)
func CoreService(in *core.Service) *core.Service {
return FilterP(in, func(in *core.Service) *core.Service {
return &core.Service{
ObjectMeta: ObjectMetaFilter(in.ObjectMeta),
Spec: Filter(in.Spec, func(in core.ServiceSpec) core.ServiceSpec {
return core.ServiceSpec{
Type: in.Type,
Ports: in.Ports,
Selector: in.Selector,
}
}),
}
})
}

func CoreServiceChecksum(in *core.Service) (string, error) {
return util.SHA256FromJSON(CoreService(in))
}

func checksumServiceSpec(s *core.ServiceSpec) (string, error) {
parts := map[string]interface{}{
"type": s.Type,
"ports": s.Ports,
"selector": s.Selector,
// add here more fields when needed
}
return util.SHA256FromJSON(parts)
func CoreServiceImmutableRecreate(logger logging.Logger) helpers.Decision[*core.Service] {
return helpers.NewImmutableFields[*core.Service](func(a, b *core.Service, changes map[string]string) helpers.Action {
if len(changes) > 0 {
s := logger

for k, v := range changes {
s = s.Str(fmt.Sprintf("field%s", k), v)
}

s.Info("Replace of Service %s required", a.GetName())
return helpers.ActionReplace
}

return helpers.ActionOK
}).Evaluate(
helpers.SubCompare[*core.Service, core.ServiceSpec](".spec", func(in *core.Service) core.ServiceSpec {
if in == nil {
return core.ServiceSpec{}
}
return in.Spec
}, helpers.FromSimpleCompareStack(".type", func(a, b core.ServiceSpec) (string, bool) {
return "ServiceType change requires object recreation", a.Type != b.Type
})),
)
}
52 changes: 39 additions & 13 deletions pkg/util/compare/k8s/statefulset.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-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.
Expand All @@ -21,23 +21,49 @@
package k8s

import (
"fmt"

apps "k8s.io/api/apps/v1"

"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/helpers"
)

func ChecksumStatefulSet(s *apps.StatefulSet) (string, error) {
return checksumStatefulSetSpec(&s.Spec)
func AppsStatefulSet(in *apps.StatefulSet) *apps.StatefulSet {
return FilterP(in, func(in *apps.StatefulSet) *apps.StatefulSet {
return &apps.StatefulSet{
ObjectMeta: ObjectMetaFilter(in.ObjectMeta),
Spec: Filter(in.Spec, func(in apps.StatefulSetSpec) apps.StatefulSetSpec {
return apps.StatefulSetSpec{
Template: in.Template,
Replicas: in.Replicas,
MinReadySeconds: in.MinReadySeconds,
Selector: in.Selector,
ServiceName: in.ServiceName,
}
}),
}
})
}

func AppsStatefulSetChecksum(in *apps.StatefulSet) (string, error) {
return util.SHA256FromJSON(AppsStatefulSet(in))
}

func checksumStatefulSetSpec(s *apps.StatefulSetSpec) (string, error) {
parts := map[string]interface{}{
"replicas": s.Replicas,
"serviceName": s.ServiceName,
"minReadySeconds": s.MinReadySeconds,
"selector": s.Selector,
"template": s.Template,
// add here more fields when needed
}
return util.SHA256FromJSON(parts)
func AppsStatefulSetRecreate(logger logging.Logger) helpers.Decision[*apps.StatefulSet] {
return helpers.NewImmutableFields[*apps.StatefulSet](func(a, b *apps.StatefulSet, changes map[string]string) helpers.Action {
if len(changes) > 0 {
s := logger

for k, v := range changes {
s = s.Str(fmt.Sprintf("field%s", k), v)
}

s.Info("Replace of StatefulSet %s required", a.GetName())
return helpers.ActionReplace
}

return helpers.ActionOK
}).Evaluate()
}
Loading