|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 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 shutdown |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "strconv" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + "google.golang.org/grpc/credentials/insecure" |
| 30 | + core "k8s.io/api/core/v1" |
| 31 | + apiErrors "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/client-go/kubernetes" |
| 34 | + |
| 35 | + pbSharedV1 "github.com/arangodb/kube-arangodb/integrations/shared/v1/definition" |
| 36 | + pbShutdownV1 "github.com/arangodb/kube-arangodb/integrations/shutdown/v1/definition" |
| 37 | + "github.com/arangodb/kube-arangodb/pkg/logging" |
| 38 | + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" |
| 39 | + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" |
| 40 | + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" |
| 41 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 42 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 43 | +) |
| 44 | + |
| 45 | +var logger = logging.Global().RegisterAndGetLogger("platform-pod-shutdown", logging.Info) |
| 46 | + |
| 47 | +type handler struct { |
| 48 | + kubeClient kubernetes.Interface |
| 49 | + |
| 50 | + eventRecorder event.RecorderInstance |
| 51 | + |
| 52 | + operator operator.Operator |
| 53 | +} |
| 54 | + |
| 55 | +func (h *handler) Name() string { |
| 56 | + return Kind() |
| 57 | +} |
| 58 | + |
| 59 | +func (h *handler) Handle(ctx context.Context, item operation.Item) error { |
| 60 | + pod, err := util.WithKubernetesContextTimeoutP2A2(ctx, h.kubeClient.CoreV1().Pods(item.Namespace).Get, item.Name, meta.GetOptions{}) |
| 61 | + if err != nil { |
| 62 | + if apiErrors.IsNotFound(err) { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + // If not annotated, stop execution |
| 70 | + if _, ok := pod.Annotations[constants.AnnotationShutdownManagedContainer]; !ok { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + for _, container := range pod.Status.ContainerStatuses { |
| 75 | + v, ok := pod.Annotations[fmt.Sprintf("%s/%s", constants.AnnotationShutdownCoreContainer, container.Name)] |
| 76 | + if !ok { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + switch v { |
| 81 | + case constants.AnnotationShutdownCoreContainerModeWait: |
| 82 | + if container.State.Terminated == nil { |
| 83 | + // Container is not yet stopped, skip shutdown |
| 84 | + return nil |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // All containers, which are expected to shutdown, are down |
| 90 | + |
| 91 | + for _, container := range pod.Status.ContainerStatuses { |
| 92 | + v, ok := pod.Annotations[fmt.Sprintf("%s/%s", constants.AnnotationShutdownContainer, container.Name)] |
| 93 | + if !ok { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + // We did not reach running state, nothing to do |
| 98 | + if container.State.Running == nil { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + port, ok := h.getContainerPort(pod.Spec.Containers, container.Name, v) |
| 103 | + if !ok { |
| 104 | + // We did not find port, continue |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + if port.ContainerPort == 0 { |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + if pod.Status.PodIP == "" { |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + if err := util.WithKubernetesContextTimeoutP1A1(ctx, h.invokeShutdown, fmt.Sprintf("%s:%d", pod.Status.PodIP, port.ContainerPort)); err != nil { |
| 117 | + logger.WrapObj(item).Err(err).Str("container", container.Name).Debug("Unable to send shutdown request") |
| 118 | + } |
| 119 | + |
| 120 | + logger.WrapObj(item).Str("container", container.Name).Debug("Shutdown request sent") |
| 121 | + } |
| 122 | + |
| 123 | + // Always return nil |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func (h *handler) CanBeHandled(item operation.Item) bool { |
| 128 | + return item.Group == Group() && |
| 129 | + item.Version == Version() && |
| 130 | + item.Kind == Kind() |
| 131 | +} |
| 132 | + |
| 133 | +func (h *handler) getContainerPort(containers []core.Container, container, port string) (core.ContainerPort, bool) { |
| 134 | + if v, err := strconv.Atoi(port); err == nil { |
| 135 | + return core.ContainerPort{ |
| 136 | + ContainerPort: int32(v), |
| 137 | + }, true |
| 138 | + } |
| 139 | + |
| 140 | + for _, c := range containers { |
| 141 | + if c.Name != container { |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + for _, p := range c.Ports { |
| 146 | + if p.Name == port { |
| 147 | + return p, true |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return core.ContainerPort{}, false |
| 153 | +} |
| 154 | + |
| 155 | +func (h *handler) invokeShutdown(ctx context.Context, addr string) error { |
| 156 | + conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + defer conn.Close() |
| 162 | + |
| 163 | + client := pbShutdownV1.NewShutdownV1Client(conn) |
| 164 | + |
| 165 | + if _, err := client.Shutdown(ctx, &pbSharedV1.Empty{}); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments