|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2020 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 | +// Author Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package reconcile |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "sort" |
| 28 | + |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 30 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + |
| 32 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/deployment/pod" |
| 34 | + "github.com/rs/zerolog" |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + registerAction(api.ActionTypeEncryptionKeyStatusUpdate, newEncryptionKeyStatusUpdate) |
| 39 | +} |
| 40 | + |
| 41 | +func newEncryptionKeyStatusUpdate(log zerolog.Logger, action api.Action, actionCtx ActionContext) Action { |
| 42 | + a := &encryptionKeyStatusUpdateAction{} |
| 43 | + |
| 44 | + a.actionImpl = newActionImplDefRef(log, action, actionCtx, defaultTimeout) |
| 45 | + |
| 46 | + return a |
| 47 | +} |
| 48 | + |
| 49 | +type encryptionKeyStatusUpdateAction struct { |
| 50 | + actionImpl |
| 51 | + |
| 52 | + actionEmptyCheckProgress |
| 53 | +} |
| 54 | + |
| 55 | +func (a *encryptionKeyStatusUpdateAction) Start(ctx context.Context) (bool, error) { |
| 56 | + if err := ensureEncryptionSupport(a.actionCtx); err != nil { |
| 57 | + a.log.Error().Err(err).Msgf("Action not supported") |
| 58 | + return true, nil |
| 59 | + } |
| 60 | + |
| 61 | + f, err := a.actionCtx.SecretsInterface().Get(pod.GetKeyfolderSecretName(a.actionCtx.GetAPIObject().GetName()), meta.GetOptions{}) |
| 62 | + if err != nil { |
| 63 | + a.log.Error().Err(err).Msgf("Unable to get folder info") |
| 64 | + return true, nil |
| 65 | + } |
| 66 | + |
| 67 | + keys := make([]string, 0, len(f.Data)) |
| 68 | + |
| 69 | + for key := range f.Data { |
| 70 | + keys = append(keys, key) |
| 71 | + } |
| 72 | + |
| 73 | + sort.Strings(keys) |
| 74 | + |
| 75 | + keyHashes := util.PrefixStringArray(keys, "sha256:") |
| 76 | + |
| 77 | + if err = a.actionCtx.WithStatusUpdate(func(s *api.DeploymentStatus) bool { |
| 78 | + if len(keyHashes) == 0 { |
| 79 | + if s.CurrentEncryptionKeyHashes != nil { |
| 80 | + s.CurrentEncryptionKeyHashes = nil |
| 81 | + return true |
| 82 | + } |
| 83 | + |
| 84 | + return false |
| 85 | + } |
| 86 | + |
| 87 | + if !util.CompareStringArray(keyHashes, s.CurrentEncryptionKeyHashes) { |
| 88 | + s.CurrentEncryptionKeyHashes = keyHashes |
| 89 | + return true |
| 90 | + } |
| 91 | + return false |
| 92 | + }); err != nil { |
| 93 | + return false, err |
| 94 | + } |
| 95 | + |
| 96 | + return true, nil |
| 97 | +} |
0 commit comments