Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add support for envbinding with namespace selector #2432

Merged
merged 4 commits into from Oct 11, 2021

Conversation

Somefive
Copy link
Collaborator

@Somefive Somefive commented Oct 9, 2021

Description of your changes

Previous EnvBinding (cluster-gateway mode) only support ClusterSelector (only cluster name is supported). Other legacy modes (like single-cluster & ocm) breaks as workflow is reworked. In this PR, we support NamespaceSelector (only namespace name currently) in EnvBinding. Now we can simultaneously set ClusterSelector and NamespaceSelector in one environment or use them separately. For example, we can deploy application in prod namespace in sub worker cluster. Example:

apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: example-app
  namespace: default
spec:
  components:
    - name: hello-world-server
      type: webservice
      properties:
        image: crccheck/hello-world
        port: 8000
      traits:
        - type: scaler
          properties:
            replicas: 1
    - name: data-worker
      type: worker
      properties:
        image: busybox
        cmd:
          - sleep
          - '1000000'
  policies:
    - name: example-multi-env-policy
      type: env-binding
      properties:
        envs:
          - name: test
            placement: # selecting the namespace to deploy to
              namespaceSelector:
                name: test
            selector:
              components:
                - data-worker

          - name: staging
            placement: # selecting the cluster to deploy to
              clusterSelector:
                name: cluster-worker

          - name: prod
            placement: # selecting both namespace and cluster to deploy to
              clusterSelector:
                name: cluster-worker
              namespaceSelector:
                name: prod
            patch: # overlay patch on above components
              components:
                - name: hello-world-server
                  type: webservice
                  traits:
                    - type: scaler
                      properties:
                        replicas: 3

  workflow:
    steps:
      # deploy to test env
      - name: deploy-test
        type: deploy2env
        properties:
          policy: example-multi-env-policy
          env: test

      # deploy to staging env
      - name: deploy-staging
        type: deploy2env
        properties:
          policy: example-multi-env-policy
          env: staging

      # deploy to prod env
      - name: deploy-prod
        type: deploy2env
        properties:
          policy: example-multi-env-policy
          env: prod

I have:

  • Read and followed KubeVela's contribution process.
  • Related Docs updated properly. In a new feature or configuration option, an update to the documentation is necessary.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

Future work from this PR:

  1. Enable LabelSelector to select cluster/namespace. This feature might need be accompanied by VelaCLI support so that user can manage namespaces and clusters conveniently.
  2. Enable support for multi-region deployment as described in [Feature] Multi-datacenters Deployment #2385. This can be decomposed into two parts.
    a. Support hierarchical grouping for deploy locations.
    b. Support workload orchestration across namespace/cluster/region.

How has this code been tested

Special notes for your reviewer

@Somefive
Copy link
Collaborator Author

Somefive commented Oct 9, 2021

The injection of namespace in #ApplyComponent is not very elegant. It is now closely coupled with multi-cluster and ApplyComponent. Any recommendation for the code architecture? @leejanee @hongchaodeng

@codecov
Copy link

codecov bot commented Oct 9, 2021

Codecov Report

Merging #2432 (0011c45) into master (bcd7f3e) will increase coverage by 0.07%.
The diff coverage is 71.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2432      +/-   ##
==========================================
+ Coverage   61.13%   61.20%   +0.07%     
==========================================
  Files         160      160              
  Lines       16997    17014      +17     
==========================================
+ Hits        10391    10414      +23     
+ Misses       5490     5484       -6     
  Partials     1116     1116              
Flag Coverage Δ
e2e-multicluster-test 19.88% <71.87%> (+0.05%) ⬆️
e2e-rollout-tests 31.27% <15.62%> (+0.01%) ⬆️
e2etests 37.18% <15.62%> (-0.03%) ⬇️
unittests 55.46% <15.62%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
....dev/v1alpha1/envbinding/cluster_gateway_engine.go 73.33% <66.66%> (+14.50%) ⬆️
...ler/core.oam.dev/v1alpha2/application/generator.go 79.24% <75.00%> (-0.37%) ⬇️
pkg/workflow/providers/oam/apply.go 77.27% <100.00%> (+1.66%) ⬆️
...es/policydefinition/policydefinition_controller.go 60.25% <0.00%> (-2.57%) ⬇️
pkg/controller/utils/capability.go 79.85% <0.00%> (-1.47%) ⬇️
pkg/cue/definition/template.go 59.05% <0.00%> (-0.87%) ⬇️
pkg/cue/model/value/value.go 84.75% <0.00%> (+0.74%) ⬆️
...pis/core.oam.dev/v1alpha1/zz_generated.deepcopy.go 23.84% <0.00%> (+1.77%) ⬆️
...m.dev/v1alpha1/envbinding/envbinding_controller.go 72.89% <0.00%> (+3.61%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcd7f3e...0011c45. Read the comment docs.

Copy link
Member

@hongchaodeng hongchaodeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm after nit

@@ -32,6 +32,11 @@ import (
"github.com/oam-dev/kubevela/pkg/multicluster"
)

const (
// OverrideNamespaceLabelKey identifies the override namespace for patched Application
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

override namespace 是用来干什么的?解释一下?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在原始不设置 namespace 的情况下,EnvBinding 的每个 Env 下的 Application 里的 Component 可以跨 Namespace 放置资源。这里的 Override Namespace 的意思是用户可以在 Env 里面指定 Namespace 让对应的资源全都放置在 Env 指定的 Namespace 下。

func (engine *ClusterGatewayEngine) prepare(ctx context.Context, configs []v1alpha1.EnvConfig) error {
engine.clusterDecisions = make(map[string]v1alpha1.ClusterDecision)
clusterNameToConfig := make(map[string]string)
locationToConfig := make(map[string]string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locationToConfig 是做什么的?还有下面这段 forloop 代码太复杂了,需要重构下成更小的函数,用函数名来介绍下做什么的。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是做一个简单的重复检查,如果有两个 Env 它们指向了同一个 Cluster 的同一个 Namespace,就会报错。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这个 forloop 里加了一些注释来标识每段代码的作用

clusterSelector:
name: local
- name: test
placement: # selecting the namespace to deploy to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要解释一下 namespace 是指子集群的 namespace

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Member

@FogDong FogDong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/example 里面加个例子?

@github-actions
Copy link

Successfully created backport PR #2440 for release-1.1.

@Somefive Somefive deleted the multicluster_ns branch November 24, 2021 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants