Skip to content

Commit

Permalink
Mock kubebuilder integration with 'operator-sdk alpha kubebuilder' cmd (
Browse files Browse the repository at this point in the history
  • Loading branch information
hasbro17 committed Jan 13, 2020
1 parent 16b5c11 commit 70856cf
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/operator-sdk/alpha/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package alpha

import (
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/kubebuilder"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/olm"
"github.com/spf13/cobra"
)
Expand All @@ -26,5 +27,6 @@ func NewCmd() *cobra.Command {
}

cmd.AddCommand(olm.NewCmd())
cmd.AddCommand(kubebuilder.NewCmd())
return cmd
}
71 changes: 71 additions & 0 deletions cmd/operator-sdk/alpha/kubebuilder/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2019 The Operator-SDK 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 kubebuilder

import (
"fmt"
"os/exec"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/kubebuilder/olmcatalog"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

"github.com/google/shlex"
"github.com/spf13/cobra"
)

var kbFlags string

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "kubebuilder [init/create cmds]",
Short: "Kubebuilder aligned subcommands",
Long: `
This subcommand is a placeholder to test the integration of Kubebuilder and Operator SDK
subcommands until a plugin system is available to integrate the Kubebuilder CLI.
See: https://github.com/kubernetes-sigs/kubebuilder/pull/1250
and https://github.com/operator-framework/operator-sdk/blob/master/doc/proposals/openshift-4.4/kubebuilder-integration.md
`,
RunE: runKubebuilder,
Hidden: true,

Example: `
# Initialize your project
operator-sdk alpha kubebuilder init --kb-flags="--domain example.com --license apache2 --owner \"The Kubernetes authors\""
# Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate
operator-sdk alpha kubebuilder create api --kb-flags="--group ship --version v1beta1 --kind Frigate"
`,
}

cmd.Flags().StringVar(&kbFlags, "kb-flags", "", "Extra kubebuilder flags passed to init/create commands \"--group cache --version=v1alpha1\"")

cmd.AddCommand(
olmcatalog.NewCmd(),
// newScorecardCmd(),
// newTestFrameworkCmd(),
)
return cmd
}

func runKubebuilder(cmd *cobra.Command, args []string) error {
splitArgs, err := shlex.Split(kbFlags)
if err != nil {
return fmt.Errorf("kb-flags is not parseable: %v", err)
}
args = append(args, splitArgs...)

kbCmd := exec.Command("kubebuilder", args...)
return projutil.ExecCmd(kbCmd)
}
34 changes: 34 additions & 0 deletions cmd/operator-sdk/alpha/kubebuilder/olmcatalog/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 The Operator-SDK 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 olmcatalog

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "olm-catalog <olm-catalog-command>",
Short: "Invokes a olm-catalog command",
Long: `The operator-sdk olm-catalog command invokes a command to perform
Catalog related actions.`,
Run: func(cmd *cobra.Command, args []string) {
log.Info("TODO")
},
}
// cmd.AddCommand(newGenCSVCmd())
return cmd
}
1 change: 1 addition & 0 deletions internal/util/projutil/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
func ExecCmd(cmd *exec.Cmd) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
log.Debugf("Running %#v", cmd.Args)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to exec %#v: %w", cmd.Args, err)
Expand Down

0 comments on commit 70856cf

Please sign in to comment.