-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FABRIC-7588] Added yaml exporter code into Zookeeper Operator (#52)
* [FABRIC-7588] Added yaml exporter code into Zookeeper Operator
- Loading branch information
1 parent
85d0985
commit b823f94
Showing
5 changed files
with
308 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) 2018 Dell Inc., or its subsidiaries. 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 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"runtime" | ||
"strconv" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
|
||
sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
"github.com/pravega/zookeeper-operator/pkg/controller/zookeepercluster" | ||
"github.com/pravega/zookeeper-operator/pkg/version" | ||
"github.com/pravega/zookeeper-operator/pkg/yamlexporter" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
) | ||
|
||
var ( | ||
log = logf.Log.WithName("cmd") | ||
versionFlag bool | ||
) | ||
|
||
func init() { | ||
flag.BoolVar(&versionFlag, "version", false, "Show version and quit") | ||
} | ||
|
||
func printVersion() { | ||
log.Info(fmt.Sprintf("zookeeper-operator Version: %v", version.Version)) | ||
log.Info(fmt.Sprintf("Git SHA: %s", version.GitSHA)) | ||
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) | ||
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) | ||
log.Info(fmt.Sprintf("operator-sdk Version: %v", sdkVersion.Version)) | ||
} | ||
|
||
func main() { | ||
flags := flag.NewFlagSet("myFlagSet", flag.ExitOnError) | ||
ifilePtr := flags.String("i", "./ZookeeperCluster.yaml", "Input YAML file") | ||
odirPtr := flags.String("o", ".", "YAML output directory") | ||
|
||
_ = flags.Parse(os.Args[1:]) | ||
|
||
log.Info(fmt.Sprintf("Input YAML file -i:%s", *ifilePtr)) | ||
log.Info(fmt.Sprintf("Output YAML Directory -o:%s", *odirPtr)) | ||
|
||
// Read input YAML file -- This is the ZookeeperCluster Resource YAML file | ||
log.Info(fmt.Sprintf("Reading YAML file from the file:%s", *ifilePtr)) | ||
z, err := yamlexporter.ReadInputClusterYAMLFile(*ifilePtr) | ||
if err != nil { | ||
log.Error(err, "read input YAML file failed") | ||
os.Exit(1) | ||
} | ||
|
||
// create base output directory and sub-directories named based on the deployment phase | ||
log.Info(fmt.Sprintf("create base output dir:%s and phase based subdirs", *odirPtr)) | ||
err = yamlexporter.CreateYAMLOutputDir(*odirPtr) | ||
if err != nil { | ||
log.Error(err, "create output dir failed") | ||
os.Exit(1) | ||
} | ||
|
||
// we need to provide our own UID for ECSCluster Resource, since the rest of the resources will reference UID of ECSCluster and it must be there. | ||
rand.Seed(time.Now().UnixNano()) | ||
uid := strconv.FormatUint(rand.Uint64(), 10) | ||
log.Info(fmt.Sprintf("UID of the ECSCluster Resource:%s\n", uid)) | ||
z.UID = types.UID(uid) | ||
|
||
yamlexporter.YAMLOutputDir = *odirPtr | ||
|
||
reconcilerZookeeper := zookeepercluster.YAMLExporterReconciler(z) | ||
|
||
// Generate YAML files | ||
err = reconcilerZookeeper.GenerateYAML(z) | ||
if err != nil { | ||
log.Error(err, "YAML file generation failed") | ||
os.Exit(1) | ||
} | ||
} |
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,90 @@ | ||
/** | ||
* Copyright (c) 2019 Dell Inc., or its subsidiaries. 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 | ||
*/ | ||
|
||
package yamlexporter | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/ghodss/yaml" | ||
"github.com/pkg/errors" | ||
"github.com/pravega/zookeeper-operator/pkg/apis/zookeeper/v1beta1" | ||
) | ||
|
||
// YAMLOutputDir where the zookeeper YAML resources will get generated | ||
var YAMLOutputDir string | ||
|
||
// CreateOutputSubDir creates a subdirectories where we want create the YAML file | ||
func CreateOutputSubDir(clusterName string, compName string) (string, error) { | ||
fpath := filepath.Join(clusterName, compName) | ||
return fpath, createDirIfNotExist(fpath) | ||
} | ||
|
||
// GenerateOutputYAMLFile writes YAML output for a resource | ||
func GenerateOutputYAMLFile(subdir string, depType string, data interface{}) error { | ||
filename := filepath.Join(subdir, depType+"."+"yaml") | ||
fileFd, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = fileFd.Close() | ||
}() | ||
yamlWriter := bufio.NewWriter(fileFd) | ||
defer yamlWriter.Flush() | ||
yamlData, err := yaml.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
n, err := yamlWriter.Write(yamlData) | ||
if err != nil { | ||
return errors.Wrapf(err, "write failed total bytes written:%d", n) | ||
} | ||
return nil | ||
} | ||
|
||
func createDirIfNotExist(dir string) error { | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
err = os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// CreateYAMLOutputDir create output directory for YAML output | ||
func CreateYAMLOutputDir(maindir string) error { | ||
err := createDirIfNotExist(maindir) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ReadInputClusterYAMLFile will read input YAML file and returns Go struct for ZookeeperCluster | ||
func ReadInputClusterYAMLFile(inyamlfile string) (*v1beta1.ZookeeperCluster, error) { | ||
if _, err := os.Stat(inyamlfile); os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
var z v1beta1.ZookeeperCluster | ||
source, err := ioutil.ReadFile(inyamlfile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = yaml.Unmarshal(source, &z) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &z, err | ||
} |