Welcome to mirror list, hosted at ThFree Co, Russian Federation.

network_policy_common.rb « kubernetes « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b6e46d21eff0eac7d18747798f7220bcd042504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module Gitlab
  module Kubernetes
    module NetworkPolicyCommon
      DISABLED_BY_LABEL = :'network-policy.gitlab.com/disabled_by'

      def as_json(opts = nil)
        {
          name: name,
          namespace: namespace,
          creation_timestamp: creation_timestamp,
          manifest: manifest,
          is_autodevops: autodevops?,
          is_enabled: enabled?
        }
      end

      def autodevops?
        return false unless labels

        !labels[:chart].nil? && labels[:chart].start_with?('auto-deploy-app-')
      end

      # selector selects pods that should be targeted by this
      # policy. It can represent podSelector, nodeSelector or
      # endpointSelector  We can narrow selection by requiring
      # this policy to match our custom labels. Since DISABLED_BY
      # label will not be on any pod a policy will be effectively disabled.
      def enabled?
        return true unless selector&.key?(:matchLabels)

        !selector[:matchLabels]&.key?(DISABLED_BY_LABEL)
      end

      def enable
        return if enabled?

        selector[:matchLabels].delete(DISABLED_BY_LABEL)
      end

      def disable
        selector[:matchLabels] ||= {}
        selector[:matchLabels].merge!(DISABLED_BY_LABEL => 'gitlab')
      end

      private

      def metadata
        meta = { name: name, namespace: namespace }
        meta[:labels] = labels if labels
        meta[:resourceVersion] = resource_version if defined?(resource_version)
        meta
      end

      def spec
        raise NotImplementedError
      end

      def manifest
        YAML.dump({ metadata: metadata, spec: spec }.deep_stringify_keys)
      end
    end
  end
end