Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Adds --distribution flag, refactor k8s generation
Browse files Browse the repository at this point in the history
This commit adds the --distribution flag as well as refactors the
generate.go file to one single file, kubernetes.go.

This allows for all future changes to simply be made within
kubernetes.go rather than two separate files with similar code
(generate.go + kubernetes.go)
  • Loading branch information
cdrage committed Sep 12, 2017
1 parent cfee15f commit ccae982
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 126 deletions.
2 changes: 1 addition & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var applyCmd = &cobra.Command{
kubectlCommand = append(kubectlCommand, "--namespace", Namespace)
}

if err := pkgcmd.ExecuteKubectl(InputFiles, kubectlCommand...); err != nil {
if err := pkgcmd.CreateKubernetesArtifacts(InputFiles, false, kubectlCommand...); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var createCmd = &cobra.Command{
kubectlCommand = append(kubectlCommand, "--namespace", Namespace)
}

if err := pkgcmd.ExecuteKubectl(InputFiles, kubectlCommand...); err != nil {
if err := pkgcmd.CreateKubernetesArtifacts(InputFiles, false, kubectlCommand...); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var deleteCmd = &cobra.Command{
kubectlCommand = append(kubectlCommand, "--namespace", Namespace)
}

if err := pkgcmd.ExecuteKubectl(InputFiles, kubectlCommand...); err != nil {
if err := pkgcmd.CreateKubernetesArtifacts(InputFiles, false, kubectlCommand...); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var generateCmd = &cobra.Command{
fmt.Println(err)
os.Exit(-1)
}
if err := pkgcmd.Generate(InputFiles); err != nil {
if err := pkgcmd.CreateKubernetesArtifacts(InputFiles, true, ""); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ package cmd
import (
"fmt"
"os"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
)

// Global variables
var (
GlobalVerbose bool
GlobalVerbose bool
GlobalProvider string
)

// RootCmd represents the base command when called without any subcommands
Expand All @@ -40,6 +42,12 @@ var RootCmd = &cobra.Command{
log.SetLevel(log.DebugLevel)
}

// Error out of the user has not chosen Kubernetes or OpenShift
provider := strings.ToLower(GlobalProvider)
if provider != "kubernetes" && provider != "openshift" {
log.Fatalf("%s is an unsupported provider. Supported providers are: 'kubernetes', 'openshift'.", GlobalProvider)
}

},
}

Expand All @@ -53,4 +61,5 @@ func Execute() {
// Initialize all flags
func init() {
RootCmd.PersistentFlags().BoolVarP(&GlobalVerbose, "verbose", "v", false, "verbose output")
RootCmd.PersistentFlags().StringVar(&GlobalProvider, "provider", "kubernetes", "Specify a provider. Kubernetes or OpenShift.")
}
101 changes: 0 additions & 101 deletions pkg/cmd/generate.go

This file was deleted.

90 changes: 70 additions & 20 deletions pkg/cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

"github.com/kedgeproject/kedge/pkg/spec"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
)

func ExecuteKubectl(paths []string, args ...string) error {
// Generate Kubernetes Artifacts and either writes to file
// or uses kubectl to deploy.
func CreateKubernetesArtifacts(paths []string, generate bool, args ...string) error {

files, err := GetAllYAMLFiles(paths)
if err != nil {
Expand All @@ -46,38 +51,61 @@ func ExecuteKubectl(paths []string, args ...string) error {
return errors.Wrap(err, "unable to perform controller operations")
}

for _, o := range ros {
data, err := yaml.Marshal(o)
for _, runtimeObject := range ros {

// Unmarshal said object
data, err := yaml.Marshal(runtimeObject)
if err != nil {
return errors.Wrap(err, "failed to marshal object")
}
// We need to add "-f -" at the end of the command passed to us to
// pass the generated files.
// e.g. If the command and arguments are "apply --namespace staging", then the
// final command becomes "kubectl apply --namespace staging -f -"
arguments := append(args, "-f", "-")
err = runKubectl(arguments, data)
if err != nil {
return errors.Wrap(err, "kubectl error")

// Write to file if generate = true
if generate {
err = writeObject(data)
if err != nil {
return errors.Wrap(err, "failed to write object")
}
} else {
// We need to add "-f -" at the end of the command passed to us to
// pass the generated files.
// e.g. If the command and arguments are "apply --namespace staging", then the
// final command becomes "kubectl apply --namespace staging -f -"
arguments := append(args, "-f", "-")
err = runKubectl(arguments, data)
if err != nil {
return errors.Wrap(err, "kubectl error")
}
}

}

for _, file := range extraResources {
// change the file name to absolute file name
file = findAbsPath(input.fileName, file)

// We need to add "-f absolute-filename" at the end of the command passed to us to
// pass the generated files.
// e.g. If the command and arguments are "apply --namespace staging", then the
// final command becomes "kubectl apply --namespace staging -f absolute-filename"
arguments := append(args, "-f", file)
err = runKubectl(arguments, nil)
if err != nil {
return errors.Wrap(err, "kubectl error")
if generate {
data, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrap(err, "file reading failed")
}
err = writeObject(data)
if err != nil {
return errors.Wrap(err, "failed to write object")
}
} else {

// We need to add "-f absolute-filename" at the end of the command passed to us to
// pass the generated files.
// e.g. If the command and arguments are "apply --namespace staging", then the
// final command becomes "kubectl apply --namespace staging -f absolute-filename"
arguments := append(args, "-f", file)
err = runKubectl(arguments, nil)
if err != nil {
return errors.Wrap(err, "kubectl error")
}
}
}
}

return nil
}

Expand Down Expand Up @@ -105,3 +133,25 @@ func runKubectl(args []string, data []byte) error {
fmt.Printf("%s", string(out))
return nil
}

func writeObject(data []byte) error {
_, err := fmt.Fprintln(os.Stdout, "---")
if err != nil {
return errors.Wrap(err, "could not print to STDOUT")
}

_, err = os.Stdout.Write(data)
return errors.Wrap(err, "could not write to STDOUT")
}

func findAbsPath(baseFilePath, path string) string {
// TODO: if the baseFilePath is empty then just take the
// pwd as basefilePath, here we will force user to
// use the kedge binary from the directory that has files
// otherwise there is no way of knowing where the files will be
// this condition will happen when we add support for reading from the stdin
if filepath.IsAbs(path) {
return path
}
return filepath.Join(filepath.Dir(baseFilePath), path)
}
46 changes: 46 additions & 0 deletions pkg/cmd/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2017 The Kedge Authors 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 cmd

import (
"io/ioutil"
"os"
"testing"
)

func TestWriteObject(t *testing.T) {

err := writeObject([]byte("foobar"))
if err != nil {
t.Fatalf("Error FindAbsPath: %s", err)
}

}

func TestFindAbsPath(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "absPathTest")
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmpDir)

path := findAbsPath("", tmpDir)

if path != tmpDir {
t.Fatal("Error getting correct absolute path")
}
}

0 comments on commit ccae982

Please sign in to comment.