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

Automatically create namespace with application #1809

Closed
basvandenbrink opened this issue Jun 24, 2019 · 22 comments
Closed

Automatically create namespace with application #1809

basvandenbrink opened this issue Jun 24, 2019 · 22 comments
Assignees
Labels
component:core Syncing, diffing, cluster state cache enhancement New feature or request type:usability Enhancement of an existing feature
Milestone

Comments

@basvandenbrink
Copy link

Is your feature request related to a problem? Please describe.
Currently, when an application is created using Argo CD and the namespace does not exist, the synchronization of that application fails with the error Error from server (NotFound): namespaces "<namespace>" not found. I am then forced to use kubectl to create the namespace.

Describe the solution you'd like
I would like Argo CD to automatically create non-existing namespaces when I create an application.

@basvandenbrink basvandenbrink added the enhancement New feature or request label Jun 24, 2019
@alexec
Copy link
Contributor

alexec commented Jun 24, 2019

Could you add the namespace manifest to the application?

@basvandenbrink
Copy link
Author

Thanks for the tip. Adding a namespace manifest works. However, I have to hard code the namespace name in the manifest. Is there also a way to choose the namespace using the web ui and then let Argo CD create the namespace?

@jessesuen
Copy link
Member

While I agree it would be a convenient feature, we need to be careful with auto-creation of namespaces:

  • the option needs to comply with project constraints -- i.e. if the project does not permit Namespaces to be created, then just because application has this option ticked, should not allow it to be created.

  • there are some corner cases with auto-namespace deletion (if created with an application). The obvious logic would be to delete the namespace if it was created with the app. However, the question becomes how should it behave when there are other objects in the namespace (possibly created by different apps, or created out-of-band from Argo CD). A point of reference would be to see how Helm handles this.

@basvandenbrink
Copy link
Author

True, that feature can be a little bit dangerous. For me, adding a namespace manifest with a hard-coded name is sufficient. Do you want to keep this open for the discussion about auto-creating namespaces with user-definied names?

@alexec
Copy link
Contributor

alexec commented Jun 25, 2019

Could you use Helm or Kustomize to parameterise the namespace?

@basvandenbrink
Copy link
Author

Thanks for the tip. Creating a file named namespace.yaml in the templates directory of the Helm chart with the following content works for me.

apiVersion: v1
kind: Namespace
metadata:
  name: {{ .Release.Namespace }}

Disadvantage is that I would have to modify the chart (which is not possible in most cases). But for me, the alternatives mentioned in the comments are sufficient.

@PaulVanStaden
Copy link
Contributor

So to be sure the recommendation is that namespaces should either be part of the helm chart or should be handled externally in contrast with the CI/CD tool taking care of it

@mnaser
Copy link
Contributor

mnaser commented Nov 12, 2019

Is there any chance this can be revisited with the addition of native Helm applications with ArgoCD 1.3?

@benjamin-tucker
Copy link

Creating the namespace would be cool, just pointing out it is a slippery slope. We'll want annotations on it next for KIAM.

james-callahan added a commit to james-callahan/example-gitops that referenced this issue Apr 30, 2020
james-callahan added a commit to james-callahan/example-gitops that referenced this issue Apr 30, 2020
james-callahan added a commit to james-callahan/example-gitops that referenced this issue Apr 30, 2020
james-callahan added a commit to james-callahan/example-gitops that referenced this issue Apr 30, 2020
@james-callahan
Copy link
Contributor

e.g. If I'm trying to deploy a new application; who should create the namespace for it (and when?)

At the moment, to have an project create namespace needs permissions to create arbitrary namespaces: could this be narrowed? (so that e.g. a project with permissions in namespace foo can only create/modify the namespace foo)

@basvandenbrink
Copy link
Author

Because of the recent discussions, I have reopened the issue.

james-callahan added a commit to james-callahan/example-gitops that referenced this issue May 4, 2020
@jannfis jannfis added component:core Syncing, diffing, cluster state cache type:usability Enhancement of an existing feature labels May 14, 2020
@blairham
Copy link

blairham commented Jun 20, 2020

would it be possible to use argo workflow here to create the namespace? I too would like to have the namespace created if it didn't exist and removed upon delete

Update
I was able to make this work by using the helm template + kustomize, and adding the namespace as a kustomize resource
https://blog.container-solutions.com/using-helm-and-kustomize-to-build-more-declarative-kubernetes-workloads

@k
Copy link

k commented Jun 30, 2020

Putting it in as a manifest doesn't work well when there are presync hooks in the Application, because then the Namespace has to be a PreSync hook.

@alexmt alexmt added this to the v1.7 milestone Jun 30, 2020
@mayzhang2000 mayzhang2000 self-assigned this Jul 13, 2020
@jurgenweber
Copy link

jurgenweber commented Jul 23, 2020

I just make a different release using the raw chart that has all my namespaces in it.
Example:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: operational-raw
  namespace: argocd
  labels:
    app.kubernetes.io/name: raw
    helm.sh/chart: app-of-apps-0.5.18
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/instance: asdf
spec:
  project: operational
  source:
    repoURL: https://kubernetes-charts-incubator.storage.googleapis.com/
    targetRevision: 0.2.3
    chart: raw
    helm:
      releaseName: namespaces
      values:       |
        resources:
          - apiVersion: v1
            kind: Namespace
            metadata:
              labels:
                app.kubernetes.io/managed-by: argocd
                app.kubernetes.io/name: monitoring
                app.kubernetes.io/component: namespace
              name: monitoring
          - apiVersion: v1
            kind: Namespace
            metadata:
              labels:
                app.kubernetes.io/managed-by: argocd
                app.kubernetes.io/name: operations
                app.kubernetes.io/component: namespace
              name: operations
  destination:
    server: https://kubernetes.default.svc
    namespace:
  syncPolicy:
    automated: {}

ezymode.

@amazingandyyy
Copy link

Hi, I need to create a configmap before my presync job run, seems like the same issue as here, any ideas?

@Exodus
Copy link
Contributor

Exodus commented Jun 12, 2021

For those getting here through google search, it's possible to create a namespace as part of the Application CRD:

  syncPolicy:
    syncOptions:
      - CreateNamespace=true

Check ArgoCD Application CRD

@JoKuebler
Copy link

Is there also a way to delete the namespace when removing the manifest? Pruning enabled seems to leave the namespace intact.

@ozbillwang
Copy link

@Exodus

Is the feature ready or not?

  syncPolicy:
    syncOptions:
      - CreateNamespace=true

I still get error that it can't automatically create the namespace

Error from server (NotFound): error when creating "application.yaml": namespaces "pproject-1" not found

@Exodus
Copy link
Contributor

Exodus commented Sep 27, 2022

@Exodus

Is the feature ready or not?

  syncPolicy:
    syncOptions:
      - CreateNamespace=true

I still get error that it can't automatically create the namespace

Error from server (NotFound): error when creating "application.yaml": namespaces "pproject-1" not found

Yes, that's correct. If you're having issues with it, there's an ArgoCD Slack Channel in the CNCF Slack, there you could get help with support in the #argo-cd channel. You may want to check what version, post your applications yaml definition there, there might be something wrong with the syntax. Review the application.yaml that I link to in my previos post.

@vikas027
Copy link

I still get error that it can't automatically create the namespace

Hey Bill,

Long time ;)

I just tried the same with version v2.9.3, works like a charm.

@caiocsgomes
Copy link

If you reached here, know that I was for days trying to do this work on the latest version. Then I saw this last comment about v2.9.3 and decided to try it. I can confirm it works in this version. There is something wrong in the latest version.

@fatsolko
Copy link

it works
v2.12.0+ec30a48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:core Syncing, diffing, cluster state cache enhancement New feature or request type:usability Enhancement of an existing feature
Projects
None yet
Development

No branches or pull requests