-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from NVIDIA/dryrun
Add Dryrun sub cmd
- Loading branch information
Showing
7 changed files
with
250 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* 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" | ||
|
||
"github.com/NVIDIA/holodeck/api/holodeck/v1alpha1" | ||
"github.com/NVIDIA/holodeck/pkg/jyaml" | ||
"github.com/NVIDIA/holodeck/pkg/provider/aws" | ||
"github.com/NVIDIA/holodeck/pkg/provisioner" | ||
|
||
"github.com/sirupsen/logrus" | ||
cli "github.com/urfave/cli/v2" | ||
) | ||
|
||
type options struct { | ||
envFile string | ||
|
||
cfg v1alpha1.Environment | ||
} | ||
|
||
type command struct { | ||
logger *logrus.Logger | ||
} | ||
|
||
// NewCommand constructs a dryrun command with the specified logger | ||
func NewCommand(logger *logrus.Logger) *cli.Command { | ||
c := command{ | ||
logger: logger, | ||
} | ||
return c.build() | ||
} | ||
|
||
func (m command) build() *cli.Command { | ||
opts := options{} | ||
|
||
// Create the 'dryrun' command | ||
dryrun := cli.Command{ | ||
Name: "dryrun", | ||
Usage: "dryrun a test environment based on config file", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "envFile", | ||
Aliases: []string{"f"}, | ||
Usage: "Path to the Environment file", | ||
Destination: &opts.envFile, | ||
}, | ||
}, | ||
Before: func(c *cli.Context) error { | ||
// Read the config file | ||
var err error | ||
opts.cfg, err = jyaml.UnmarshalFromFile[v1alpha1.Environment](opts.envFile) | ||
if err != nil { | ||
fmt.Printf("failed to read config file: %v\n", err) | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
Action: func(c *cli.Context) error { | ||
return m.run(c, &opts) | ||
}, | ||
} | ||
|
||
return &dryrun | ||
} | ||
|
||
func (m command) run(c *cli.Context, opts *options) error { | ||
// Check Provider | ||
switch opts.cfg.Spec.Provider { | ||
case v1alpha1.ProviderAWS: | ||
err := validateAWS(opts) | ||
if err != nil { | ||
return err | ||
} | ||
case v1alpha1.ProviderSSH: | ||
// Creating a new provisioner will validate the private key and hostUrl | ||
_, err := provisioner.New(opts.cfg.Spec.Auth.PrivateKey, opts.cfg.Spec.Instance.HostUrl) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("unknown provider %s", opts.cfg.Spec.Provider) | ||
} | ||
|
||
// Check Provisioner | ||
err := provisioner.Dryrun(opts.cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Dryrun succeeded\n") | ||
|
||
return nil | ||
} | ||
|
||
func validateAWS(opts *options) error { | ||
client, err := aws.New(opts.cfg, opts.envFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = client.DryRun(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* 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 provisioner | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/NVIDIA/holodeck/api/holodeck/v1alpha1" | ||
) | ||
|
||
func Dryrun(env v1alpha1.Environment) error { | ||
// Resolve dependencies from top to bottom | ||
fmt.Printf("Resolving dependencies...\n") | ||
// kubernetes -> container runtime -> node | ||
if env.Spec.Kubernetes.Install { | ||
if !env.Spec.ContainerRuntime.Install { | ||
return fmt.Errorf("cannot install Kubernetes without a container runtime") | ||
} | ||
// check if env.Spec.Kubernetes.KubernetesVersion is in the format of vX.Y.Z | ||
if !strings.HasPrefix(env.Spec.Kubernetes.KubernetesVersion, "v") { | ||
return fmt.Errorf("Kubernetes version %s is not in the format of vX.Y.Z", env.Spec.Kubernetes.KubernetesVersion) | ||
} | ||
} | ||
|
||
if env.Spec.ContainerRuntime.Install && (env.Spec.ContainerRuntime.Name != v1alpha1.ContainerRuntimeContainerd && | ||
env.Spec.ContainerRuntime.Name != v1alpha1.ContainerRuntimeCrio && | ||
env.Spec.ContainerRuntime.Name != v1alpha1.ContainerRuntimeDocker) { | ||
return fmt.Errorf("container runtime %s not supported", env.Spec.ContainerRuntime.Name) | ||
} | ||
|
||
if env.Spec.NVContainerToolKit.Install && !env.Spec.ContainerRuntime.Install { | ||
return fmt.Errorf("cannot install NVContainer Toolkit without a container runtime") | ||
} | ||
if env.Spec.NVContainerToolKit.Install && !env.Spec.NVDriver.Install { | ||
return fmt.Errorf("cannot install NVContainer Toolkit without the NVIDIA driver") | ||
} | ||
|
||
return nil | ||
} |