|
| 1 | +// Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package logging |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/aws/aws-node-termination-handler/pkg/monitor" |
| 20 | + "github.com/rs/zerolog/log" |
| 21 | +) |
| 22 | + |
| 23 | +type versionedMsgsV1 struct{} |
| 24 | + |
| 25 | +func (versionedMsgsV1) MonitoringStarted(monitorKind string) { |
| 26 | + log.Info().Str("event_type", monitorKind).Msg("Started monitoring for events") |
| 27 | +} |
| 28 | + |
| 29 | +func (versionedMsgsV1) ProblemMonitoringForEvents(monitorKind string, err error) { |
| 30 | + log.Warn().Str("event_type", monitorKind).Err(err).Msg("There was a problem monitoring for events") |
| 31 | +} |
| 32 | + |
| 33 | +func (versionedMsgsV1) RequestingInstanceDrain(event *monitor.InterruptionEvent) { |
| 34 | + log.Info(). |
| 35 | + Str("event-id", event.EventID). |
| 36 | + Str("kind", event.Kind). |
| 37 | + Str("node-name", event.NodeName). |
| 38 | + Str("instance-id", event.InstanceID). |
| 39 | + Str("provider-id", event.ProviderID). |
| 40 | + Msg("Requesting instance drain") |
| 41 | +} |
| 42 | + |
| 43 | +func (versionedMsgsV1) SendingInterruptionEventToChannel(_ string) { |
| 44 | + log.Debug().Msg("Sending SQS_TERMINATE interruption event to the interruption channel") |
| 45 | +} |
| 46 | + |
| 47 | +type versionedMsgsV2 struct{} |
| 48 | + |
| 49 | +func (versionedMsgsV2) MonitoringStarted(monitorKind string) { |
| 50 | + log.Info().Str("monitor_type", monitorKind).Msg("Started monitoring for events") |
| 51 | +} |
| 52 | + |
| 53 | +func (versionedMsgsV2) ProblemMonitoringForEvents(monitorKind string, err error) { |
| 54 | + log.Warn().Str("monitor_type", monitorKind).Err(err).Msg("There was a problem monitoring for events") |
| 55 | +} |
| 56 | + |
| 57 | +func (versionedMsgsV2) RequestingInstanceDrain(event *monitor.InterruptionEvent) { |
| 58 | + log.Info(). |
| 59 | + Str("event-id", event.EventID). |
| 60 | + Str("kind", event.Kind). |
| 61 | + Str("monitor", event.Monitor). |
| 62 | + Str("node-name", event.NodeName). |
| 63 | + Str("instance-id", event.InstanceID). |
| 64 | + Str("provider-id", event.ProviderID). |
| 65 | + Msg("Requesting instance drain") |
| 66 | +} |
| 67 | + |
| 68 | +func (versionedMsgsV2) SendingInterruptionEventToChannel(eventKind string) { |
| 69 | + log.Debug().Msgf("Sending %s interruption event to the interruption channel", eventKind) |
| 70 | +} |
| 71 | + |
| 72 | +var VersionedMsgs interface { |
| 73 | + MonitoringStarted(monitorKind string) |
| 74 | + ProblemMonitoringForEvents(monitorKind string, err error) |
| 75 | + RequestingInstanceDrain(event *monitor.InterruptionEvent) |
| 76 | + SendingInterruptionEventToChannel(eventKind string) |
| 77 | +} = versionedMsgsV1{} |
| 78 | + |
| 79 | +func SetFormatVersion(version int) error { |
| 80 | + switch version { |
| 81 | + case 1: |
| 82 | + VersionedMsgs = versionedMsgsV1{} |
| 83 | + return nil |
| 84 | + case 2: |
| 85 | + VersionedMsgs = versionedMsgsV2{} |
| 86 | + return nil |
| 87 | + default: |
| 88 | + VersionedMsgs = versionedMsgsV1{} |
| 89 | + return fmt.Errorf("Unrecognized log format version: %d, using version 1", version) |
| 90 | + } |
| 91 | +} |
0 commit comments