|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package reconcile |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "reflect" |
| 26 | + |
| 27 | + "github.com/pkg/errors" |
| 28 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + |
| 31 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/deployment/patch" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util/globals" |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/tolerations" |
| 35 | +) |
| 36 | + |
| 37 | +func newRuntimeContainerSyncTolerationsAction(action api.Action, actionCtx ActionContext) Action { |
| 38 | + a := &actionRuntimeContainerSyncTolerations{} |
| 39 | + |
| 40 | + a.actionImpl = newActionImplDefRef(action, actionCtx) |
| 41 | + |
| 42 | + return a |
| 43 | +} |
| 44 | + |
| 45 | +type actionRuntimeContainerSyncTolerations struct { |
| 46 | + // actionImpl implement timeout and member id functions |
| 47 | + actionImpl |
| 48 | + |
| 49 | + actionEmptyCheckProgress |
| 50 | +} |
| 51 | + |
| 52 | +// Start starts the action for changing conditions on the provided member. |
| 53 | +func (a actionRuntimeContainerSyncTolerations) Start(ctx context.Context) (bool, error) { |
| 54 | + m, ok := a.actionCtx.GetMemberStatusByID(a.action.MemberID) |
| 55 | + if !ok { |
| 56 | + a.log.Info("member is gone already") |
| 57 | + return true, nil |
| 58 | + } |
| 59 | + |
| 60 | + cache, ok := a.actionCtx.ACS().ClusterCache(m.ClusterID) |
| 61 | + if !ok { |
| 62 | + return true, errors.Errorf("Client is not ready") |
| 63 | + } |
| 64 | + |
| 65 | + memberName := m.ArangoMemberName(a.actionCtx.GetName(), a.action.Group) |
| 66 | + member, ok := cache.ArangoMember().V1().GetSimple(memberName) |
| 67 | + if !ok { |
| 68 | + return false, errors.Errorf("ArangoMember %s not found", memberName) |
| 69 | + } |
| 70 | + |
| 71 | + pod, ok := cache.Pod().V1().GetSimple(m.Pod.GetName()) |
| 72 | + if !ok { |
| 73 | + a.log.Str("podName", m.Pod.GetName()).Info("pod is not present") |
| 74 | + return true, nil |
| 75 | + } |
| 76 | + |
| 77 | + currentTolerations := pod.Spec.Tolerations |
| 78 | + |
| 79 | + expectedTolerations := member.Spec.Template.PodSpec.Spec.Tolerations |
| 80 | + |
| 81 | + calculatedTolerations := tolerations.MergeTolerationsIfNotFound(currentTolerations, expectedTolerations) |
| 82 | + |
| 83 | + if reflect.DeepEqual(currentTolerations, calculatedTolerations) { |
| 84 | + return true, nil |
| 85 | + } |
| 86 | + |
| 87 | + p, err := patch.NewPatch(patch.ItemReplace(patch.NewPath("spec", "tolerations"), calculatedTolerations)).Marshal() |
| 88 | + if err != nil { |
| 89 | + return false, errors.Wrapf(err, "Unable to create patch") |
| 90 | + } |
| 91 | + |
| 92 | + nctx, c := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx) |
| 93 | + defer c() |
| 94 | + |
| 95 | + if _, err := a.actionCtx.ACS().CurrentClusterCache().PodsModInterface().V1().Patch(nctx, pod.GetName(), types.JSONPatchType, p, meta.PatchOptions{}); err != nil { |
| 96 | + return false, errors.Wrapf(err, "Unable to apply patch") |
| 97 | + } |
| 98 | + |
| 99 | + return true, nil |
| 100 | +} |
0 commit comments