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

CLOUDP-280015: Added kubernetes dry run command #3588

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ linters-settings:
alias: corev1
- pkg: k8s.io/api/rbac/v1
alias: rbacv1
- pkg: k8s.io/api/batch/v1
alias: batchv1
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
alias: apiextensionsv1
- pkg: k8s.io/apiserver/pkg/storage/names
Expand Down
87 changes: 87 additions & 0 deletions docs/command/atlas-kubernetes-dry-run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
.. _atlas-kubernetes-dry-run:

========================
atlas kubernetes dry-run
========================

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Deploy and run Atlas Kubernetes Operator in dry-run mode

This command deploys the Atlas Kubernetes operator with the DryRun mode.

TODO: ask Dan about the proper description of the dry-run mode.


Syntax
------

.. code-block::
:caption: Command Syntax

atlas kubernetes dry-run [options]

.. Code end marker, please don't delete this comment

Options
-------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - -h, --help
-
- false
- help for dry-run
* - --operatorVersion
- string
- false
- Version of Atlas Kubernetes Operator to generate resources for. This value defaults to "2.6.0".
* - --orgId
- string
- false
- Organization ID to use. This option overrides the settings in the configuration file or environment variable.
* - --targetNamespace
- string
- false
- Namespaces to use for generated kubernetes entities
* - --watch
-
- false
- Flag that indicates whether to watch the command until it completes its execution or the watch times out. To set the time that the watch times out, use the --watchTimeout option.
* - --watchNamespaces
- strings
- false
- List that contains namespaces that the operator will listen to.
* - --watchTimeout
- int
- false
- Time in seconds until a watch times out. After a watch times out, the CLI no longer watches the command. This value defaults to 120.

Inherited Options
-----------------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - -P, --profile
- string
- false
- Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings.

6 changes: 5 additions & 1 deletion docs/command/atlas-kubernetes-operator-install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Options
-
- false
- Flag that indicates whether to configure Atlas for Government as a target of the operator.
* - --configOnly
-
- false
- Flag that indicates whether to generate only the operator configuration files without installing the Operator
* - -h, --help
-
- false
Expand Down Expand Up @@ -84,7 +88,7 @@ Options
- string
- false
- Namespace where to install the operator.
* - --watchNamespace
* - --watchNamespaces
- strings
- false
- List that contains namespaces that the operator will listen to.
Expand Down
2 changes: 2 additions & 0 deletions docs/command/atlas-kubernetes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ Related Commands
----------------

* :ref:`atlas-kubernetes-config` - Manage Kubernetes configuration resources.
* :ref:`atlas-kubernetes-dry-run` - Deploy and run Atlas Kubernetes Operator in dry-run mode
* :ref:`atlas-kubernetes-operator` - Manage Atlas Kubernetes Operator.


.. toctree::
:titlesonly:

config </command/atlas-kubernetes-config>
dry-run </command/atlas-kubernetes-dry-run>
operator </command/atlas-kubernetes-operator>

114 changes: 114 additions & 0 deletions internal/cli/kubernetes/dryrun/dryrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dryrun

import (
"fmt"
"strings"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/features"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/validation"
)

var ErrUnsupportedOperatorVersionFmt = "version %q is not supported. Supported versions: %v"

const defaultTimeoutSec = 120

type Opts struct {
cli.OrgOpts
cli.OutputOpts

operatorVersion string
targetNamespace string
watchNamespaces []string
waitForJob bool
waitTimeout int64
}

func (opts *Opts) ValidateTargetNamespace() error {
if errs := validation.IsDNS1123Label(opts.targetNamespace); len(errs) != 0 {
return fmt.Errorf("%s parameter is invalid: %v", flag.OperatorTargetNamespace, errs)
}
return nil
}

func (opts *Opts) ValidateOperatorVersion() error {
if _, versionFound := features.GetResourcesForVersion(opts.operatorVersion); versionFound {
return nil
}
return fmt.Errorf(ErrUnsupportedOperatorVersionFmt, opts.operatorVersion, features.SupportedVersions())
}

func (opts *Opts) Run() error {
worker := NewWorker().
WithTargetNamespace(opts.targetNamespace).
WithWatchNamespaces(strings.Join(opts.watchNamespaces, ",")).
WithOperatorVersion(opts.operatorVersion).
WithWaitForCompletion(opts.waitForJob).
WithWaitTimeoutSec(opts.waitTimeout)
return worker.Run()
}

// Builder builds a cobra.Command for the Kubernetes dryrun installation.
func Builder() *cobra.Command {
const use = "dry-run"

opts := &Opts{}

cmd := &cobra.Command{
Use: use,
Args: require.NoArgs,
Aliases: cli.GenerateAliases(use),
Short: "Deploy and run Atlas Kubernetes Operator in dry-run mode",
Long: `This command deploys the Atlas Kubernetes operator with the DryRun mode.

TODO: ask Dan about the proper description of the dry-run mode.
`,
PreRunE: func(_ *cobra.Command, _ []string) error {
return opts.OrgOpts.PreRunE(
opts.ValidateTargetNamespace,
opts.ValidateOperatorVersion,
)
},
RunE: func(_ *cobra.Command, _ []string) error {
return opts.Run()
},
}

opts.AddOrgOptFlags(cmd)
cmd.Flags().StringVar(&opts.targetNamespace, flag.OperatorTargetNamespace, "", usage.OperatorTargetNamespace)
cmd.Flags().StringSliceVar(&opts.watchNamespaces, flag.OperatorWatchNamespaces, []string{}, usage.OperatorWatchNamespace)
cmd.Flags().StringVar(&opts.operatorVersion, flag.OperatorVersion, features.LatestOperatorMajorVersion, usage.OperatorVersion)
cmd.Flags().BoolVar(&opts.waitForJob, flag.EnableWatch, false, usage.EnableWatch)
cmd.Flags().Int64Var(&opts.waitTimeout, flag.WatchTimeout, defaultTimeoutSec, usage.WatchTimeout)
return cmd
}
Loading