Skip to content
Snippets Groups Projects
Commit 83799252 authored by Alex Clemmer's avatar Alex Clemmer
Browse files

Implement `init` subcommand

This commit will implement the first version of the `init` subcommand.
This subcommand initializes a ksonnet application, including generating
the default directory tree and generating the appropriate ksonnet-lib.
parent 4664eaa6
No related branches found
No related tags found
No related merge requests found
// Copyright 2017 The kubecfg authors
//
//
// 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 cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/ksonnet/kubecfg/metadata"
"github.com/ksonnet/kubecfg/pkg/kubecfg"
"github.com/spf13/cobra"
)
const (
flagAPISpec = "api-spec"
)
func init() {
RootCmd.AddCommand(initCmd)
// TODO: We need to make this default to checking the `kubeconfig` file.
initCmd.PersistentFlags().String(flagAPISpec, "version:v1.7.0", "Manually specify API version from OpenAPI schema, cluster, or Kubernetes version")
}
var initCmd = &cobra.Command{
Use: "init <app-name>",
Short: "Initialize a ksonnet project",
RunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
if len(args) != 1 {
return fmt.Errorf("'init' takes a single argument that names the application we're initializing")
}
appName := args[0]
appDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}
appRoot := metadata.AbsPath(path.Join(appDir, appName))
specFlag, err := flags.GetString(flagAPISpec)
if err != nil {
return err
}
c, err := kubecfg.NewInitCmd(appRoot, specFlag)
if err != nil {
return err
}
return c.Run()
},
Long: `Initialize a ksonnet project in a new directory, 'app-name'. This process
consists of two steps:
1. Generating ksonnet-lib. Users can set flags to generate the library based on
a variety of data, including server configuration and an OpenAPI
specification of a Kubernetes build. By default, this is generated from the
capabilities of the cluster specified in the cluster of the current context
specified in $KUBECONFIG.
2. Generating the following tree in the current directory.
app-name/
.gitignore Default .gitignore; can customize VCS
.ksonnet/ Metadata for ksonnet
envs/ Env specs (defaults: dev, test, prod)
params.yaml Specifies the schema of the environments
dev.yaml
test.yaml
prod.yaml
us-east.yaml [Example of user-generated env]
components/ Top-level Kubernetes objects defining application
lib/ user-written .libsonnet files
vendor/ mixin libraries, prototypes
`,
Example: ` # Initialize ksonnet application, using the capabilities of live cluster
# specified in the $KUBECONFIG environment variable (specifically: the
# current context) to generate 'ksonnet-lib'.
ksonnet init app-name
# Initialize ksonnet application, using the OpenAPI specification generated
# in the Kubenetes v1.7.1 build to generate 'ksonnet-lib'.
ksonnet init app-name --api-spec=version:v1.7.1
# Initialize ksonnet application, using an OpenAPI specification file
# generated by a build of Kubernetes to generate 'ksonnet-lib'.
ksonnet init app-name --api-spec=file:swagger.json`,
}
package kubecfg
import "github.com/ksonnet/kubecfg/metadata"
type InitCmd struct {
rootPath metadata.AbsPath
spec metadata.ClusterSpec
}
func NewInitCmd(rootPath metadata.AbsPath, specFlag string) (*InitCmd, error) {
// NOTE: We're taking `rootPath` here as an absolute path (rather than a partial path we expand to an absolute path)
// to make it more testable.
spec, err := metadata.ParseClusterSpec(specFlag)
if err != nil {
return nil, err
}
return &InitCmd{rootPath: rootPath, spec: spec}, nil
}
func (c *InitCmd) Run() error {
_, err := metadata.Init(c.rootPath, c.spec)
return err
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment