forked from jaegertracing/jaeger-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command to generate k8s manifests (jaegertracing#1046)
* Add CLI command jaeger-operator generate to generate manifests Defaults to stdin/stdout Examples: jaeger-operator generate --jaeger-all-in-one-image localimage --cr ./deploy/examples/all-in-one-with-options.yaml | kubectl apply -f - cat jaeger.yaml | podman run jaeger-operator generate > manifest.yaml Fixes jaegertracing#375 Signed-off-by: Carl Henrik Lunde <[email protected]> * Add missing TypeMeta for corev1.ServiceAccount Signed-off-by: Carl Henrik Lunde <[email protected]> * Add e2e tests for `jaeger-operator generate` Signed-off-by: Carl Henrik Lunde <[email protected]> * Avoid command line flag duplication Signed-off-by: Carl Henrik Lunde <[email protected]> * Fixup - allow EOF on stdin Signed-off-by: Carl Henrik Lunde <[email protected]> * README: Explain generate subcommand Signed-off-by: Carl Henrik Lunde <[email protected]> * Generate command description: update Signed-off-by: Carl Henrik Lunde <[email protected]> * Strategy: Test that All includes all types Signed-off-by: Carl Henrik Lunde <[email protected]> * Remove solved TODOs Signed-off-by: Carl Henrik Lunde <[email protected]> * Use AllInOneSmokeTest for e2e CLI generate Signed-off-by: Carl Henrik Lunde <[email protected]> * Fix tag name in README Signed-off-by: Carl Henrik Lunde <[email protected]> * Update docs Signed-off-by: Carl Henrik Lunde <[email protected]> * Use require.No* for better error message in e2e test Signed-off-by: Carl Henrik Lunde <[email protected]> * Update docs, mark as experimental Signed-off-by: Carl Henrik Lunde <[email protected]> * Minor refactor/cleanup Signed-off-by: Carl Henrik Lunde <[email protected]> Co-authored-by: Carl Henrik Lunde <[email protected]>
- Loading branch information
Showing
11 changed files
with
390 additions
and
22 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
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,111 @@ | ||
package generate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
|
||
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1" | ||
"github.com/jaegertracing/jaeger-operator/pkg/cmd/start" | ||
"github.com/jaegertracing/jaeger-operator/pkg/strategy" | ||
) | ||
|
||
// NewGenerateCommand starts the Jaeger Operator | ||
func NewGenerateCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate", | ||
Short: "(experimental) Generate YAML manifests from Jaeger CRD", | ||
Long: `Generate YAML manifests from Jaeger CRD. | ||
Defaults to reading Jaeger CRD from standard input and writing the manifest file to standard output, override with --cr <filename> and --output <filename>.`, | ||
RunE: generate, | ||
} | ||
|
||
cmd.Flags().String("cr", "/dev/stdin", "Input Jaeger CRD") | ||
viper.BindPFlag("cr", cmd.Flags().Lookup("cr")) | ||
|
||
cmd.Flags().String("output", "/dev/stdout", "Where to print the generated YAML documents") | ||
viper.BindPFlag("output", cmd.Flags().Lookup("output")) | ||
|
||
start.AddFlags(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func createSpecFromYAML(filename string) (*v1.Jaeger, error) { | ||
// #nosec G304: Potential file inclusion via variable | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
var spec v1.Jaeger | ||
decoder := yaml.NewYAMLOrJSONDecoder(f, 8192) | ||
if err := decoder.Decode(&spec); err != nil && err != io.EOF { | ||
return nil, err | ||
} | ||
|
||
return &spec, nil | ||
} | ||
|
||
func generate(_ *cobra.Command, _ []string) error { | ||
level, err := log.ParseLevel(viper.GetString("log-level")) | ||
if err != nil { | ||
log.SetLevel(log.InfoLevel) | ||
} else { | ||
log.SetLevel(level) | ||
} | ||
|
||
input := viper.GetString("cr") | ||
if input == "/dev/stdin" { | ||
// Reading from stdin by default is neat when running as a | ||
// container instead of a binary, but is confusing when no | ||
// input is sent and the program just hangs | ||
log.Info("Reading Jaeger CRD from standard input (use --cr <filename> to override)") | ||
} | ||
|
||
spec, err := createSpecFromYAML(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := strategy.For(context.Background(), spec) | ||
|
||
outputName := viper.GetString("output") | ||
out, err := os.OpenFile(outputName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer out.Close() | ||
|
||
encoder := k8s_json.NewYAMLSerializer(k8s_json.DefaultMetaFactory, nil, nil) | ||
for _, obj := range s.All() { | ||
// OwnerReferences normally references the CR, but it is not a | ||
// resource in the cluster so we must remove it | ||
|
||
type f interface { | ||
SetOwnerReferences(references []metav1.OwnerReference) | ||
} | ||
|
||
meta := obj.(f) | ||
meta.SetOwnerReferences(nil) | ||
|
||
fmt.Fprintln(out, "---") | ||
if err := encoder.Encode(obj, out); err != nil { | ||
log.Fatal(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
Oops, something went wrong.