BE: Extend NetworkPolicy GraphQL API with `Environments` field
Why are we doing this work
- Policies can affect many different environments and users want to be able to quickly see which environment is affected by which policy
Non-functional requirements
-
Documentation - GraphQL documentation is updated, -
Testing
Implementation plan
-
backend Extend ee/app/services/network_policies/resources_service.rb
methods:-
extract_info_for_kubeclient
to additionally return environment_ids related to given Kubernetes namespace:
kubernetes_namespaces .order(updated_at: :desc) .preload(:platform_kubernetes) .group_by(&:namespace) .map { |namespace, kubernetes_namespaces| [kubernetes_namespaces.first.platform_kubernetes, [namespace, kubernetes_namespaces.map(&:environment_id)]] }
-
extend_per_environment
to accept and sendenvironment_ids
to policyfrom_resource
method to store IDs in policy:
def execute_per_environment(platform, namespace, environment_ids) policies = platform.kubeclient .get_network_policies(namespace: namespace) .map { |resource| Gitlab::Kubernetes::NetworkPolicy.from_resource(resource, environment_ids) } policies += platform.kubeclient .get_cilium_network_policies(namespace: namespace) .map { |resource| Gitlab::Kubernetes::CiliumNetworkPolicy.from_resource(resource, environment_ids) } [policies, nil] rescue Kubeclient::HttpError => e [policies, e] end
-
execute
to readenvironment_ids
fromextract_info_for_kubeclient
response and to send it toexecute_per_environment
method,
def execute return no_platform_response unless has_deployment_platform? @kubeclient_info policies = [] errors = [] @kubeclient_info.each do |platform, (namespace, environment_ids)| policies_per_environment, error_per_environment = execute_per_environment(platform, namespace, environment_ids) policies += policies_per_environment if policies_per_environment errors << error_per_environment if error_per_environment end errors.empty? ? ServiceResponse.success(payload: policies) : kubernetes_error_response(errors.join, policies) end
-
-
backend extend from_resource
method to accept optional argumentenvironment_ids
inGitlab::Kubernetes::NetworkPolicy
andGitlab::Kubernetes::CiliumNetworkPolicy
, -
backend return environment_ids
inas_json
method inGitlab::Kubernetes::NetworkPolicyCommon
so they will be returned in the response, -
backend extend Types::NetworkPolicyType
with new fieldenvironments
:Types::EnvironmentType.connection_type, null: true
and add useResolvers::EnvironmentsResolver
resolver to fetchEnvironment
from database with IDs given in policyenvironment_ids
, -
backend extend Environments::EnvironmentsFinder
to be able to filter environments based on value ofenvironment_ids
Edited by Sashi Kumar Kumaresan